mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
test: switch some unit tests to doctest (#5383)
This change moves some tests from the current unit tests that are compiled into the rippled binary to using the doctest framework.
This commit is contained in:
@@ -1,144 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/basics/RangeSet.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
namespace ripple {
|
||||
class RangeSet_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
testPrevMissing()
|
||||
{
|
||||
testcase("prevMissing");
|
||||
|
||||
// Set will include:
|
||||
// [ 0, 5]
|
||||
// [10,15]
|
||||
// [20,25]
|
||||
// etc...
|
||||
|
||||
RangeSet<std::uint32_t> set;
|
||||
for (std::uint32_t i = 0; i < 10; ++i)
|
||||
set.insert(range(10 * i, 10 * i + 5));
|
||||
|
||||
for (std::uint32_t i = 1; i < 100; ++i)
|
||||
{
|
||||
std::optional<std::uint32_t> expected;
|
||||
// no prev missing in domain for i <= 6
|
||||
if (i > 6)
|
||||
{
|
||||
std::uint32_t const oneBelowRange = (10 * (i / 10)) - 1;
|
||||
|
||||
expected = ((i % 10) > 6) ? (i - 1) : oneBelowRange;
|
||||
}
|
||||
BEAST_EXPECT(prevMissing(set, i) == expected);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testToString()
|
||||
{
|
||||
testcase("toString");
|
||||
|
||||
RangeSet<std::uint32_t> set;
|
||||
BEAST_EXPECT(to_string(set) == "empty");
|
||||
|
||||
set.insert(1);
|
||||
BEAST_EXPECT(to_string(set) == "1");
|
||||
|
||||
set.insert(range(4u, 6u));
|
||||
BEAST_EXPECT(to_string(set) == "1,4-6");
|
||||
|
||||
set.insert(2);
|
||||
BEAST_EXPECT(to_string(set) == "1-2,4-6");
|
||||
|
||||
set.erase(range(4u, 5u));
|
||||
BEAST_EXPECT(to_string(set) == "1-2,6");
|
||||
}
|
||||
|
||||
void
|
||||
testFromString()
|
||||
{
|
||||
testcase("fromString");
|
||||
|
||||
RangeSet<std::uint32_t> set;
|
||||
|
||||
BEAST_EXPECT(!from_string(set, ""));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 0);
|
||||
|
||||
BEAST_EXPECT(!from_string(set, "#"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 0);
|
||||
|
||||
BEAST_EXPECT(!from_string(set, ","));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 0);
|
||||
|
||||
BEAST_EXPECT(!from_string(set, ",-"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 0);
|
||||
|
||||
BEAST_EXPECT(!from_string(set, "1,,2"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 0);
|
||||
|
||||
BEAST_EXPECT(from_string(set, "1"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 1);
|
||||
BEAST_EXPECT(boost::icl::first(set) == 1);
|
||||
|
||||
BEAST_EXPECT(from_string(set, "1,1"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 1);
|
||||
BEAST_EXPECT(boost::icl::first(set) == 1);
|
||||
|
||||
BEAST_EXPECT(from_string(set, "1-1"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 1);
|
||||
BEAST_EXPECT(boost::icl::first(set) == 1);
|
||||
|
||||
BEAST_EXPECT(from_string(set, "1,4-6"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 4);
|
||||
BEAST_EXPECT(boost::icl::first(set) == 1);
|
||||
BEAST_EXPECT(!boost::icl::contains(set, 2));
|
||||
BEAST_EXPECT(!boost::icl::contains(set, 3));
|
||||
BEAST_EXPECT(boost::icl::contains(set, 4));
|
||||
BEAST_EXPECT(boost::icl::contains(set, 5));
|
||||
BEAST_EXPECT(boost::icl::last(set) == 6);
|
||||
|
||||
BEAST_EXPECT(from_string(set, "1-2,4-6"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 5);
|
||||
BEAST_EXPECT(boost::icl::first(set) == 1);
|
||||
BEAST_EXPECT(boost::icl::contains(set, 2));
|
||||
BEAST_EXPECT(boost::icl::contains(set, 4));
|
||||
BEAST_EXPECT(boost::icl::last(set) == 6);
|
||||
|
||||
BEAST_EXPECT(from_string(set, "1-2,6"));
|
||||
BEAST_EXPECT(boost::icl::length(set) == 3);
|
||||
BEAST_EXPECT(boost::icl::first(set) == 1);
|
||||
BEAST_EXPECT(boost::icl::contains(set, 2));
|
||||
BEAST_EXPECT(boost::icl::last(set) == 6);
|
||||
}
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testPrevMissing();
|
||||
testToString();
|
||||
testFromString();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(RangeSet, ripple_basics, ripple);
|
||||
|
||||
} // namespace ripple
|
||||
@@ -1,116 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github0.com/ripple/rippled
|
||||
Copyright (c) 2012-2016 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
struct Slice_test : beast::unit_test::suite
|
||||
{
|
||||
void
|
||||
run() override
|
||||
{
|
||||
std::uint8_t const data[] = {
|
||||
0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23, 0x71, 0x6d, 0x2a,
|
||||
0x18, 0xb4, 0x70, 0xcb, 0xf5, 0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c,
|
||||
0xf0, 0x2c, 0x15, 0xd1, 0xf9, 0x9b, 0x66, 0xd2, 0x30, 0xd3};
|
||||
|
||||
{
|
||||
testcase("Equality & Inequality");
|
||||
|
||||
Slice const s0{};
|
||||
|
||||
BEAST_EXPECT(s0.size() == 0);
|
||||
BEAST_EXPECT(s0.data() == nullptr);
|
||||
BEAST_EXPECT(s0 == s0);
|
||||
|
||||
// Test slices of equal and unequal size pointing to same data:
|
||||
for (std::size_t i = 0; i != sizeof(data); ++i)
|
||||
{
|
||||
Slice const s1{data, i};
|
||||
|
||||
BEAST_EXPECT(s1.size() == i);
|
||||
BEAST_EXPECT(s1.data() != nullptr);
|
||||
|
||||
if (i == 0)
|
||||
BEAST_EXPECT(s1 == s0);
|
||||
else
|
||||
BEAST_EXPECT(s1 != s0);
|
||||
|
||||
for (std::size_t j = 0; j != sizeof(data); ++j)
|
||||
{
|
||||
Slice const s2{data, j};
|
||||
|
||||
if (i == j)
|
||||
BEAST_EXPECT(s1 == s2);
|
||||
else
|
||||
BEAST_EXPECT(s1 != s2);
|
||||
}
|
||||
}
|
||||
|
||||
// Test slices of equal size but pointing to different data:
|
||||
std::array<std::uint8_t, sizeof(data)> a;
|
||||
std::array<std::uint8_t, sizeof(data)> b;
|
||||
|
||||
for (std::size_t i = 0; i != sizeof(data); ++i)
|
||||
a[i] = b[i] = data[i];
|
||||
|
||||
BEAST_EXPECT(makeSlice(a) == makeSlice(b));
|
||||
b[7]++;
|
||||
BEAST_EXPECT(makeSlice(a) != makeSlice(b));
|
||||
a[7]++;
|
||||
BEAST_EXPECT(makeSlice(a) == makeSlice(b));
|
||||
}
|
||||
|
||||
{
|
||||
testcase("Indexing");
|
||||
|
||||
Slice const s{data, sizeof(data)};
|
||||
|
||||
for (std::size_t i = 0; i != sizeof(data); ++i)
|
||||
BEAST_EXPECT(s[i] == data[i]);
|
||||
}
|
||||
|
||||
{
|
||||
testcase("Advancing");
|
||||
|
||||
for (std::size_t i = 0; i < sizeof(data); ++i)
|
||||
{
|
||||
for (std::size_t j = 0; i + j < sizeof(data); ++j)
|
||||
{
|
||||
Slice s(data + i, sizeof(data) - i);
|
||||
s += j;
|
||||
|
||||
BEAST_EXPECT(s.data() == data + i + j);
|
||||
BEAST_EXPECT(s.size() == sizeof(data) - i - j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(Slice, ripple_basics, ripple);
|
||||
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
@@ -1,82 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012-2018 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#include <xrpl/basics/base64.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
class base64_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
check(std::string const& in, std::string const& out)
|
||||
{
|
||||
auto const encoded = base64_encode(in);
|
||||
BEAST_EXPECT(encoded == out);
|
||||
BEAST_EXPECT(base64_decode(encoded) == in);
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
check("", "");
|
||||
check("f", "Zg==");
|
||||
check("fo", "Zm8=");
|
||||
check("foo", "Zm9v");
|
||||
check("foob", "Zm9vYg==");
|
||||
check("fooba", "Zm9vYmE=");
|
||||
check("foobar", "Zm9vYmFy");
|
||||
|
||||
check(
|
||||
"Man is distinguished, not only by his reason, but by this "
|
||||
"singular passion from "
|
||||
"other animals, which is a lust of the mind, that by a "
|
||||
"perseverance of delight "
|
||||
"in the continued and indefatigable generation of knowledge, "
|
||||
"exceeds the short "
|
||||
"vehemence of any carnal pleasure.",
|
||||
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dC"
|
||||
"BieSB0aGlz"
|
||||
"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG"
|
||||
"x1c3Qgb2Yg"
|
||||
"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aG"
|
||||
"UgY29udGlu"
|
||||
"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleG"
|
||||
"NlZWRzIHRo"
|
||||
"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=");
|
||||
|
||||
std::string const notBase64 = "not_base64!!";
|
||||
std::string const truncated = "not";
|
||||
BEAST_EXPECT(base64_decode(notBase64) == base64_decode(truncated));
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(base64, ripple_basics, ripple);
|
||||
|
||||
} // namespace ripple
|
||||
@@ -1,63 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/basics/contract.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
class contract_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
try
|
||||
{
|
||||
Throw<std::runtime_error>("Throw test");
|
||||
}
|
||||
catch (std::runtime_error const& e1)
|
||||
{
|
||||
BEAST_EXPECT(std::string(e1.what()) == "Throw test");
|
||||
|
||||
try
|
||||
{
|
||||
Rethrow();
|
||||
}
|
||||
catch (std::runtime_error const& e2)
|
||||
{
|
||||
BEAST_EXPECT(std::string(e2.what()) == "Throw test");
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
BEAST_EXPECT(false);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
BEAST_EXPECT(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(contract, basics, ripple);
|
||||
|
||||
} // namespace ripple
|
||||
@@ -1,62 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012-2016 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/basics/mulDiv.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
struct mulDiv_test : beast::unit_test::suite
|
||||
{
|
||||
void
|
||||
run() override
|
||||
{
|
||||
auto const max = std::numeric_limits<std::uint64_t>::max();
|
||||
std::uint64_t const max32 = std::numeric_limits<std::uint32_t>::max();
|
||||
|
||||
auto result = mulDiv(85, 20, 5);
|
||||
BEAST_EXPECT(result && *result == 340);
|
||||
result = mulDiv(20, 85, 5);
|
||||
BEAST_EXPECT(result && *result == 340);
|
||||
|
||||
result = mulDiv(0, max - 1, max - 3);
|
||||
BEAST_EXPECT(result && *result == 0);
|
||||
result = mulDiv(max - 1, 0, max - 3);
|
||||
BEAST_EXPECT(result && *result == 0);
|
||||
|
||||
result = mulDiv(max, 2, max / 2);
|
||||
BEAST_EXPECT(result && *result == 4);
|
||||
result = mulDiv(max, 1000, max / 1000);
|
||||
BEAST_EXPECT(result && *result == 1000000);
|
||||
result = mulDiv(max, 1000, max / 1001);
|
||||
BEAST_EXPECT(result && *result == 1001000);
|
||||
result = mulDiv(max32 + 1, max32 + 1, 5);
|
||||
BEAST_EXPECT(result && *result == 3689348814741910323);
|
||||
|
||||
// Overflow
|
||||
result = mulDiv(max - 1, max - 2, 5);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(mulDiv, ripple_basics, ripple);
|
||||
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
@@ -1,193 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github0.com/ripple/rippled
|
||||
Copyright (c) 2021 Ripple Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/basics/scope.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
struct scope_test : beast::unit_test::suite
|
||||
{
|
||||
void
|
||||
test_scope_exit()
|
||||
{
|
||||
// scope_exit always executes the functor on destruction,
|
||||
// unless release() is called
|
||||
int i = 0;
|
||||
{
|
||||
scope_exit x{[&i]() { i = 1; }};
|
||||
}
|
||||
BEAST_EXPECT(i == 1);
|
||||
{
|
||||
scope_exit x{[&i]() { i = 2; }};
|
||||
x.release();
|
||||
}
|
||||
BEAST_EXPECT(i == 1);
|
||||
{
|
||||
scope_exit x{[&i]() { i += 2; }};
|
||||
auto x2 = std::move(x);
|
||||
}
|
||||
BEAST_EXPECT(i == 3);
|
||||
{
|
||||
scope_exit x{[&i]() { i = 4; }};
|
||||
x.release();
|
||||
auto x2 = std::move(x);
|
||||
}
|
||||
BEAST_EXPECT(i == 3);
|
||||
{
|
||||
try
|
||||
{
|
||||
scope_exit x{[&i]() { i = 5; }};
|
||||
throw 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(i == 5);
|
||||
{
|
||||
try
|
||||
{
|
||||
scope_exit x{[&i]() { i = 6; }};
|
||||
x.release();
|
||||
throw 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(i == 5);
|
||||
}
|
||||
|
||||
void
|
||||
test_scope_fail()
|
||||
{
|
||||
// scope_fail executes the functor on destruction only
|
||||
// if an exception is unwinding, unless release() is called
|
||||
int i = 0;
|
||||
{
|
||||
scope_fail x{[&i]() { i = 1; }};
|
||||
}
|
||||
BEAST_EXPECT(i == 0);
|
||||
{
|
||||
scope_fail x{[&i]() { i = 2; }};
|
||||
x.release();
|
||||
}
|
||||
BEAST_EXPECT(i == 0);
|
||||
{
|
||||
scope_fail x{[&i]() { i = 3; }};
|
||||
auto x2 = std::move(x);
|
||||
}
|
||||
BEAST_EXPECT(i == 0);
|
||||
{
|
||||
scope_fail x{[&i]() { i = 4; }};
|
||||
x.release();
|
||||
auto x2 = std::move(x);
|
||||
}
|
||||
BEAST_EXPECT(i == 0);
|
||||
{
|
||||
try
|
||||
{
|
||||
scope_fail x{[&i]() { i = 5; }};
|
||||
throw 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(i == 5);
|
||||
{
|
||||
try
|
||||
{
|
||||
scope_fail x{[&i]() { i = 6; }};
|
||||
x.release();
|
||||
throw 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(i == 5);
|
||||
}
|
||||
|
||||
void
|
||||
test_scope_success()
|
||||
{
|
||||
// scope_success executes the functor on destruction only
|
||||
// if an exception is not unwinding, unless release() is called
|
||||
int i = 0;
|
||||
{
|
||||
scope_success x{[&i]() { i = 1; }};
|
||||
}
|
||||
BEAST_EXPECT(i == 1);
|
||||
{
|
||||
scope_success x{[&i]() { i = 2; }};
|
||||
x.release();
|
||||
}
|
||||
BEAST_EXPECT(i == 1);
|
||||
{
|
||||
scope_success x{[&i]() { i += 2; }};
|
||||
auto x2 = std::move(x);
|
||||
}
|
||||
BEAST_EXPECT(i == 3);
|
||||
{
|
||||
scope_success x{[&i]() { i = 4; }};
|
||||
x.release();
|
||||
auto x2 = std::move(x);
|
||||
}
|
||||
BEAST_EXPECT(i == 3);
|
||||
{
|
||||
try
|
||||
{
|
||||
scope_success x{[&i]() { i = 5; }};
|
||||
throw 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(i == 3);
|
||||
{
|
||||
try
|
||||
{
|
||||
scope_success x{[&i]() { i = 6; }};
|
||||
x.release();
|
||||
throw 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(i == 3);
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
test_scope_exit();
|
||||
test_scope_fail();
|
||||
test_scope_success();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(scope, ripple_basics, ripple);
|
||||
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
@@ -1,258 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
|
||||
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <xrpl/basics/tagged_integer.h>
|
||||
#include <xrpl/beast/unit_test.h>
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
class tagged_integer_test : public beast::unit_test::suite
|
||||
{
|
||||
private:
|
||||
struct Tag1
|
||||
{
|
||||
};
|
||||
struct Tag2
|
||||
{
|
||||
};
|
||||
|
||||
// Static checks that types are not interoperable
|
||||
|
||||
using TagUInt1 = tagged_integer<std::uint32_t, Tag1>;
|
||||
using TagUInt2 = tagged_integer<std::uint32_t, Tag2>;
|
||||
using TagUInt3 = tagged_integer<std::uint64_t, Tag1>;
|
||||
|
||||
// Check construction of tagged_integers
|
||||
static_assert(
|
||||
std::is_constructible<TagUInt1, std::uint32_t>::value,
|
||||
"TagUInt1 should be constructible using a std::uint32_t");
|
||||
|
||||
static_assert(
|
||||
!std::is_constructible<TagUInt1, std::uint64_t>::value,
|
||||
"TagUInt1 should not be constructible using a std::uint64_t");
|
||||
|
||||
static_assert(
|
||||
std::is_constructible<TagUInt3, std::uint32_t>::value,
|
||||
"TagUInt3 should be constructible using a std::uint32_t");
|
||||
|
||||
static_assert(
|
||||
std::is_constructible<TagUInt3, std::uint64_t>::value,
|
||||
"TagUInt3 should be constructible using a std::uint64_t");
|
||||
|
||||
// Check assignment of tagged_integers
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt1, std::uint32_t>::value,
|
||||
"TagUInt1 should not be assignable with a std::uint32_t");
|
||||
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt1, std::uint64_t>::value,
|
||||
"TagUInt1 should not be assignable with a std::uint64_t");
|
||||
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt3, std::uint32_t>::value,
|
||||
"TagUInt3 should not be assignable with a std::uint32_t");
|
||||
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt3, std::uint64_t>::value,
|
||||
"TagUInt3 should not be assignable with a std::uint64_t");
|
||||
|
||||
static_assert(
|
||||
std::is_assignable<TagUInt1, TagUInt1>::value,
|
||||
"TagUInt1 should be assignable with a TagUInt1");
|
||||
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt1, TagUInt2>::value,
|
||||
"TagUInt1 should not be assignable with a TagUInt2");
|
||||
|
||||
static_assert(
|
||||
std::is_assignable<TagUInt3, TagUInt3>::value,
|
||||
"TagUInt3 should be assignable with a TagUInt1");
|
||||
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt1, TagUInt3>::value,
|
||||
"TagUInt1 should not be assignable with a TagUInt3");
|
||||
|
||||
static_assert(
|
||||
!std::is_assignable<TagUInt3, TagUInt1>::value,
|
||||
"TagUInt3 should not be assignable with a TagUInt1");
|
||||
|
||||
// Check convertibility of tagged_integers
|
||||
static_assert(
|
||||
!std::is_convertible<std::uint32_t, TagUInt1>::value,
|
||||
"std::uint32_t should not be convertible to a TagUInt1");
|
||||
|
||||
static_assert(
|
||||
!std::is_convertible<std::uint32_t, TagUInt3>::value,
|
||||
"std::uint32_t should not be convertible to a TagUInt3");
|
||||
|
||||
static_assert(
|
||||
!std::is_convertible<std::uint64_t, TagUInt3>::value,
|
||||
"std::uint64_t should not be convertible to a TagUInt3");
|
||||
|
||||
static_assert(
|
||||
!std::is_convertible<std::uint64_t, TagUInt2>::value,
|
||||
"std::uint64_t should not be convertible to a TagUInt2");
|
||||
|
||||
static_assert(
|
||||
!std::is_convertible<TagUInt1, TagUInt2>::value,
|
||||
"TagUInt1 should not be convertible to TagUInt2");
|
||||
|
||||
static_assert(
|
||||
!std::is_convertible<TagUInt1, TagUInt3>::value,
|
||||
"TagUInt1 should not be convertible to TagUInt3");
|
||||
|
||||
static_assert(
|
||||
!std::is_convertible<TagUInt2, TagUInt3>::value,
|
||||
"TagUInt2 should not be convertible to a TagUInt3");
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
using TagInt = tagged_integer<std::int32_t, Tag1>;
|
||||
|
||||
{
|
||||
testcase("Comparison Operators");
|
||||
|
||||
TagInt const zero(0);
|
||||
TagInt const one(1);
|
||||
|
||||
BEAST_EXPECT(one == one);
|
||||
BEAST_EXPECT(!(one == zero));
|
||||
|
||||
BEAST_EXPECT(one != zero);
|
||||
BEAST_EXPECT(!(one != one));
|
||||
|
||||
BEAST_EXPECT(zero < one);
|
||||
BEAST_EXPECT(!(one < zero));
|
||||
|
||||
BEAST_EXPECT(one > zero);
|
||||
BEAST_EXPECT(!(zero > one));
|
||||
|
||||
BEAST_EXPECT(one >= one);
|
||||
BEAST_EXPECT(one >= zero);
|
||||
BEAST_EXPECT(!(zero >= one));
|
||||
|
||||
BEAST_EXPECT(zero <= one);
|
||||
BEAST_EXPECT(zero <= zero);
|
||||
BEAST_EXPECT(!(one <= zero));
|
||||
}
|
||||
|
||||
{
|
||||
testcase("Increment/Decrement Operators");
|
||||
TagInt const zero(0);
|
||||
TagInt const one(1);
|
||||
TagInt a{0};
|
||||
++a;
|
||||
BEAST_EXPECT(a == one);
|
||||
--a;
|
||||
BEAST_EXPECT(a == zero);
|
||||
a++;
|
||||
BEAST_EXPECT(a == one);
|
||||
a--;
|
||||
BEAST_EXPECT(a == zero);
|
||||
}
|
||||
|
||||
{
|
||||
testcase("Arithmetic Operators");
|
||||
TagInt a{-2};
|
||||
BEAST_EXPECT(+a == TagInt{-2});
|
||||
BEAST_EXPECT(-a == TagInt{2});
|
||||
BEAST_EXPECT(TagInt{-3} + TagInt{4} == TagInt{1});
|
||||
BEAST_EXPECT(TagInt{-3} - TagInt{4} == TagInt{-7});
|
||||
BEAST_EXPECT(TagInt{-3} * TagInt{4} == TagInt{-12});
|
||||
BEAST_EXPECT(TagInt{8} / TagInt{4} == TagInt{2});
|
||||
BEAST_EXPECT(TagInt{7} % TagInt{4} == TagInt{3});
|
||||
|
||||
BEAST_EXPECT(~TagInt{8} == TagInt{~TagInt::value_type{8}});
|
||||
BEAST_EXPECT((TagInt{6} & TagInt{3}) == TagInt{2});
|
||||
BEAST_EXPECT((TagInt{6} | TagInt{3}) == TagInt{7});
|
||||
BEAST_EXPECT((TagInt{6} ^ TagInt{3}) == TagInt{5});
|
||||
|
||||
BEAST_EXPECT((TagInt{4} << TagInt{2}) == TagInt{16});
|
||||
BEAST_EXPECT((TagInt{16} >> TagInt{2}) == TagInt{4});
|
||||
}
|
||||
{
|
||||
testcase("Assignment Operators");
|
||||
TagInt a{-2};
|
||||
TagInt b{0};
|
||||
b = a;
|
||||
BEAST_EXPECT(b == TagInt{-2});
|
||||
|
||||
// -3 + 4 == 1
|
||||
a = TagInt{-3};
|
||||
a += TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{1});
|
||||
|
||||
// -3 - 4 == -7
|
||||
a = TagInt{-3};
|
||||
a -= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{-7});
|
||||
|
||||
// -3 * 4 == -12
|
||||
a = TagInt{-3};
|
||||
a *= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{-12});
|
||||
|
||||
// 8/4 == 2
|
||||
a = TagInt{8};
|
||||
a /= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{2});
|
||||
|
||||
// 7 % 4 == 3
|
||||
a = TagInt{7};
|
||||
a %= TagInt{4};
|
||||
BEAST_EXPECT(a == TagInt{3});
|
||||
|
||||
// 6 & 3 == 2
|
||||
a = TagInt{6};
|
||||
a /= TagInt{3};
|
||||
BEAST_EXPECT(a == TagInt{2});
|
||||
|
||||
// 6 | 3 == 7
|
||||
a = TagInt{6};
|
||||
a |= TagInt{3};
|
||||
BEAST_EXPECT(a == TagInt{7});
|
||||
|
||||
// 6 ^ 3 == 5
|
||||
a = TagInt{6};
|
||||
a ^= TagInt{3};
|
||||
BEAST_EXPECT(a == TagInt{5});
|
||||
|
||||
// 4 << 2 == 16
|
||||
a = TagInt{4};
|
||||
a <<= TagInt{2};
|
||||
BEAST_EXPECT(a == TagInt{16});
|
||||
|
||||
// 16 >> 2 == 4
|
||||
a = TagInt{16};
|
||||
a >>= TagInt{2};
|
||||
BEAST_EXPECT(a == TagInt{4});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(tagged_integer, ripple_basics, ripple);
|
||||
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
@@ -1,60 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012-2017 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <test/jtx/Env.h>
|
||||
|
||||
#include <xrpl/beast/utility/temp_dir.h>
|
||||
#include <xrpl/crypto/csprng.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
class CryptoPRNG_test : public beast::unit_test::suite
|
||||
{
|
||||
void
|
||||
testGetValues()
|
||||
{
|
||||
testcase("Get Values");
|
||||
try
|
||||
{
|
||||
auto& engine = crypto_prng();
|
||||
auto rand_val = engine();
|
||||
BEAST_EXPECT(rand_val >= engine.min());
|
||||
BEAST_EXPECT(rand_val <= engine.max());
|
||||
|
||||
uint16_t twoByte{0};
|
||||
engine(&twoByte, sizeof(uint16_t));
|
||||
pass();
|
||||
}
|
||||
catch (std::exception&)
|
||||
{
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testGetValues();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(CryptoPRNG, core, ripple);
|
||||
|
||||
} // namespace ripple
|
||||
Reference in New Issue
Block a user