52#include <boost/test/unit_test.hpp>
54using namespace std::literals;
80 explicit NoCopyOrMove(
int i) : i{i} { }
82 NoCopyOrMove() =
delete;
83 NoCopyOrMove(
const NoCopyOrMove&) =
delete;
84 NoCopyOrMove(NoCopyOrMove&&) =
delete;
85 NoCopyOrMove& operator=(
const NoCopyOrMove&) =
delete;
86 NoCopyOrMove& operator=(NoCopyOrMove&&) =
delete;
88 operator bool()
const {
return i != 0; }
90 int get_ip1() {
return i + 1; }
94 [&]() {
Assume(get_ip1()); }();
95 return Assume(get_ip1() != 5);
103 const std::unique_ptr<int> p_two =
Assert(std::make_unique<int>(2));
105 const int two = *
Assert(p_two);
109 const bool result{
Assume(two == 2)};
122 const int nine{*
Assert(std::optional<int>{9})};
134 BOOST_ERROR(
"break was swallowed!");
144 BOOST_ERROR(
"break was swallowed!");
148constexpr char HEX_PARSE_INPUT[] =
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
150 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
151 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
152 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
153 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
159 std::vector<unsigned char> result;
163 constexpr std::array<std::byte, 65> hex_literal_array{
operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};
165 BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
167 const std::vector<std::byte> hex_literal_vector{
operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
168 auto hex_literal_vec_span =
MakeUCharSpan(hex_literal_vector);
169 BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_vec_span.begin(), hex_literal_vec_span.end(), expected.begin(), expected.end());
171 constexpr std::array<uint8_t, 65> hex_literal_array_uint8{
operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
172 BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());
174 result =
operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>();
175 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
178 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
181 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
184 expected = {0x12, 0x34, 0x56, 0x78};
186 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
187 result = TryParseHex<uint8_t>(
"12 34 56 78").value();
188 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
191 expected = {0x89, 0x34, 0x56, 0x78};
193 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
194 result = TryParseHex<uint8_t>(
" 89 34 56 78").value();
195 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
198 expected = {0xff, 0xaa};
200 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
201 result = TryParseHex<uint8_t>(
" Ff aA ").value();
202 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
205 static_assert(
""_hex.empty());
206 static_assert(
""_hex_u8.empty());
217 const std::string with_embedded_null{
" 11 "s
248 constexpr std::string_view out_exp{
"04678afdb0"};
251 const std::span<const std::byte> in_b{
MakeByteSpan(in_s)};
259 auto input = std::string();
260 for (
size_t i=0; i<256; ++i) {
261 input.push_back(
static_cast<char>(i));
265 BOOST_TEST_REQUIRE(hex.size() == 512);
266 static constexpr auto hexmap = std::string_view(
"0123456789abcdef");
267 for (
size_t i = 0; i < 256; ++i) {
268 auto upper = hexmap.find(hex[i * 2]);
269 auto lower = hexmap.find(hex[i * 2 + 1]);
270 BOOST_TEST_REQUIRE(upper != std::string_view::npos);
271 BOOST_TEST_REQUIRE(lower != std::string_view::npos);
272 BOOST_TEST_REQUIRE(i == upper*16 + lower);
279 std::array mut_arr{uint8_t{0xaa}, uint8_t{0xbb}};
281 mut_bytes[1] = std::byte{0x11};
294 const auto op_upper = [](
const std::string&
s) {
return ToUpper(
s); };
302 const std::string original(
"A test \"%s\" string '%s'.");
303 auto test_replaceall{[](std::string test, std::string_view search, std::string_view substitute, std::string_view expected) {
308 test_replaceall(original,
"",
"foo", original);
309 test_replaceall(original,
"missing",
"foo", original);
310 test_replaceall(original, original,
"foo",
"foo");
311 test_replaceall(original,
"%s",
"foo",
"A test \"foo\" string 'foo'.");
312 test_replaceall(original,
"\"",
"foo",
"A test foo%sfoo string '%s'.");
313 test_replaceall(original,
"'",
"foo",
"A test \"%s\" string foo%sfoo.");
314 test_replaceall(
"a.b",
".",
"x",
"axb");
315 test_replaceall(
"%w and %w",
"%w",
"$&$`$'$1$$",
"$&$`$'$1$$ and $&$`$'$1$$");
316 test_replaceall(
"x",
"x",
"xx",
"xx");
331 BOOST_CHECK_EQUAL(
TrimString(std::string(
"\x05\x04\x03\x02\x01\x00", 6), std::string(
"\x05\x04\x03\x02\x01", 5)), std::string(
"\0", 1));
364 const std::string valid{
"2000-01-01T00:00:01Z"};
366 for (
auto mut{0U}; mut < valid.size(); ++mut) {
367 std::string invalid{valid};
533 for (
int mod=2;mod<11;mod++)
537 int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
539 while(mask<mod-1)mask=(mask<<1)+1;
543 for (
int i = 0; i < 10000; i++) {
546 rval=m_rng.rand32()&mask;
547 }
while(rval>=(uint32_t)mod);
568#define B "check_prefix"
569#define E "check_postfix"
572 int64_t s64t = -9223372036854775807LL;
573 uint64_t u64t = 18446744073709551615ULL;
578 size_t st = 12345678;
579 ssize_t sst = -12345678;
584 ptrdiff_t pt = 87654321;
585 ptrdiff_t spt = -87654321;
597 for (
const auto& num_sleep : {0ms, 1ms}) {
638 constexpr T MAXI{std::numeric_limits<T>::max()};
658 TestAddMatrixOverflow<T>();
659 constexpr T MINI{std::numeric_limits<T>::min()};
660 constexpr T MAXI{std::numeric_limits<T>::max()};
678 TestAddMatrixOverflow<unsigned>();
679 TestAddMatrix<signed>();
707 BOOST_CHECK(!ToIntegral<T>(
"-32482348723847471234"));
708 BOOST_CHECK(!ToIntegral<T>(
"32482348723847471234"));
723 RunToIntegralTests<uint64_t>();
724 RunToIntegralTests<int64_t>();
725 RunToIntegralTests<uint32_t>();
726 RunToIntegralTests<int32_t>();
727 RunToIntegralTests<uint16_t>();
728 RunToIntegralTests<int16_t>();
729 RunToIntegralTests<uint8_t>();
730 RunToIntegralTests<int8_t>();
732 BOOST_CHECK(!ToIntegral<int64_t>(
"-9223372036854775809"));
733 BOOST_CHECK_EQUAL(ToIntegral<int64_t>(
"-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);
734 BOOST_CHECK_EQUAL(ToIntegral<int64_t>(
"9223372036854775807").value(), 9'223'372'036'854'775'807);
735 BOOST_CHECK(!ToIntegral<int64_t>(
"9223372036854775808"));
739 BOOST_CHECK_EQUAL(ToIntegral<uint64_t>(
"18446744073709551615").value(), 18'446'744'073'709'551'615ULL);
740 BOOST_CHECK(!ToIntegral<uint64_t>(
"18446744073709551616"));
775 return strtoll(str.c_str(),
nullptr, 10);
805 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-32482348723847471234"), -2'147'483'647 - 1);
806 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"32482348723847471234"), 2'147'483'647);
808 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"-9223372036854775809"), -9'223'372'036'854'775'807LL - 1LL);
809 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL);
810 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"9223372036854775807"), 9'223'372'036'854'775'807);
811 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(
"9223372036854775808"), 9'223'372'036'854'775'807);
813 std::map<std::string, int64_t> atoi64_test_pairs = {
814 {
"-9223372036854775809", std::numeric_limits<int64_t>::min()},
815 {
"-9223372036854775808", -9'223'372'036'854'775'807LL - 1LL},
816 {
"9223372036854775807", 9'223'372'036'854'775'807},
817 {
"9223372036854775808", std::numeric_limits<int64_t>::max()},
824 for (
const auto& pair : atoi64_test_pairs) {
829 for (
const auto& pair : atoi64_test_pairs) {
835 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>(
"18446744073709551615"), 18'446'744'073'709'551'615ULL);
836 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>(
"18446744073709551616"), 18'446'744'073'709'551'615ULL);
838 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-2147483649"), -2'147'483'648LL);
839 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(
"-2147483648"), -2'147'483'648LL);
845 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>(
"4294967295"), 4'294'967'295U);
846 BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>(
"4294967296"), 4'294'967'295U);
871 std::optional<uint64_t> n;
873 n = ToIntegral<uint64_t>(
"1234", 16);
875 n = ToIntegral<uint64_t>(
"a", 16);
877 n = ToIntegral<uint64_t>(
"0000000a", 16);
879 n = ToIntegral<uint64_t>(
"100", 16);
881 n = ToIntegral<uint64_t>(
"DEADbeef", 16);
883 n = ToIntegral<uint64_t>(
"FfFfFfFf", 16);
885 n = ToIntegral<uint64_t>(
"123456789", 16);
887 n = ToIntegral<uint64_t>(
"0", 16);
889 n = ToIntegral<uint64_t>(
"FfFfFfFfFfFfFfFf", 16);
898 BOOST_CHECK(!ToIntegral<uint64_t>(
"FfFfFfFfFfFfFfFf0", 16));
914 BOOST_CHECK_EQUAL(
FormatParagraph(
"This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length until it gets here", 79),
"This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length\nuntil it gets here");
917 BOOST_CHECK_EQUAL(
FormatParagraph(
"a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79),
"a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p");
918 BOOST_CHECK_EQUAL(
FormatParagraph(
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79),
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p");
920 BOOST_CHECK_EQUAL(
FormatParagraph(
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg h i j k", 79, 4),
"x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\n f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg\n h i j k");
922 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string. This is a second sentence in the very long test string.", 79),
"This is a very long test string. This is a second sentence in the very long\ntest string.");
923 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string.\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79),
"This is a very long test string.\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string.");
924 BOOST_CHECK_EQUAL(
FormatParagraph(
"This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79),
"This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string.");
925 BOOST_CHECK_EQUAL(
FormatParagraph(
"Testing that normal newlines do not get indented.\nLike here.", 79),
"Testing that normal newlines do not get indented.\nLike here.");
930 std::vector<std::string> comments;
931 comments.emplace_back(
"comment1");
932 std::vector<std::string> comments2;
933 comments2.emplace_back(
"comment1");
1029 int rv = read(fd, &ch, 1);
1041 rv = write(fd, &ch, 1);
1047 rv = write(fd, &ch, 1);
1062 fs::path dirname = m_args.GetDataDirBase() /
"lock_dir";
1063 const fs::path lockname =
".lock";
1086 fs::create_directories(dirname);
1146 fs::remove(dirname / lockname);
1147 fs::remove(dirname);
1185static std::string
SpanToStr(
const std::span<const char>& span)
1187 return std::string(span.begin(), span.end());
1194 std::span<const char> sp;
1198 input =
"MilkToastHoney";
1200 success =
Const(
"", sp);
1204 success =
Const(
"Milk", sp,
false);
1208 success =
Const(
"Milk", sp);
1212 success =
Const(
"Bread", sp,
false);
1215 success =
Const(
"Bread", sp);
1218 success =
Const(
"Toast", sp,
false);
1222 success =
Const(
"Toast", sp);
1226 success =
Const(
"Honeybadger", sp);
1229 success =
Const(
"Honey", sp,
false);
1233 success =
Const(
"Honey", sp);
1237 input =
"Foo(Bar(xy,z()))";
1240 success =
Func(
"FooBar", sp);
1243 success =
Func(
"Foo(", sp);
1246 success =
Func(
"Foo", sp);
1250 success =
Func(
"Bar", sp);
1254 success =
Func(
"xy", sp);
1258 std::span<const char> result;
1260 input =
"(n*(n-1))/2";
1272 input =
"(aaaaa,bbbbb()),c";
1284 input =
"((a),(b),(c)),xxx";
1291 std::vector<std::span<const char>> results;
1294 results =
Split(input,
'x');
1301 input =
"one#two#three";
1302 results =
Split(input,
'-');
1306 input =
"one#two#three";
1307 results =
Split(input,
'#');
1313 results =
Split(input,
'#',
true);
1319 input =
"*foo*bar*";
1320 results =
Split(input,
'*');
1327 results =
Split(input,
'*',
true);
1339 std::vector<std::string> result =
SplitString(
"",
'-');
1346 std::vector<std::string> result =
SplitString(
"-",
'-');
1354 std::vector<std::string> result =
SplitString(
"--",
'-');
1363 std::vector<std::string> result =
SplitString(
"abc",
'-');
1370 std::vector<std::string> result =
SplitString(
"a-b",
'-');
1378 std::vector<std::string> result =
SplitString(
"AAA",
'a');
1385 using V = std::vector<std::string>;
1386 BOOST_TEST(
SplitString(
"a,b.c:d;e",
",;") == V({
"a",
"b.c:d",
"e"}));
1387 BOOST_TEST(
SplitString(
"a,b.c:d;e",
",;:.") == V({
"a",
"b",
"c",
"d",
"e"}));
1388 BOOST_TEST(
SplitString(
"a,b.c:d;e",
"") == V({
"a,b.c:d;e"}));
1389 BOOST_TEST(
SplitString(
"aaa",
"bcdefg") == V({
"aaa"}));
1390 BOOST_TEST(
SplitString(
"x\0a,b"s,
"\0"s) == V({
"x",
"a,b"}));
1391 BOOST_TEST(
SplitString(
"x\0a,b"s,
'\0') == V({
"x",
"a,b"}));
1392 BOOST_TEST(
SplitString(
"x\0a,b"s,
"\0,"s) == V({
"x",
"a",
"b"}));
1393 BOOST_TEST(
SplitString(
"abcdefg",
"bcd") == V({
"a",
"",
"",
"efg"}));
1406 const std::string NUL(
"O\x00O", 3);
1415 const Tracker* origin;
1419 Tracker() noexcept : origin(this) {}
1420 Tracker(
const Tracker&
t) noexcept : origin(
t.origin), copies(
t.copies + 1) {}
1421 Tracker(Tracker&&
t) noexcept : origin(
t.origin), copies(
t.copies) {}
1422 Tracker& operator=(
const Tracker&
t)
noexcept
1426 copies =
t.copies + 1;
1449 auto v2 =
Vector(std::move(t2));
1454 auto v3 =
Vector(t1, std::move(t2));
1461 auto v4 =
Vector(std::move(v3[0]), v3[1], std::move(t3));
1470 auto v5 =
Cat(v1, v4);
1481 auto v6 =
Cat(std::move(v1), v3);
1490 auto v7 =
Cat(v2, std::move(v4));
1501 auto v8 =
Cat(std::move(v2), std::move(v3));
1513 const std::array<unsigned char, 32> privkey_bytes = {
1516 0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E,
1517 0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26,
1518 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F,
1519 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66
1522 const std::string message =
"Trust no one";
1524 const std::string expected_signature =
1525 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=";
1528 std::string generated_signature;
1530 BOOST_REQUIRE_MESSAGE(!privkey.
IsValid(),
1531 "Confirm the private key is invalid");
1533 BOOST_CHECK_MESSAGE(!
MessageSign(privkey, message, generated_signature),
1534 "Sign with an invalid private key");
1536 privkey.
Set(privkey_bytes.begin(), privkey_bytes.end(),
true);
1538 BOOST_REQUIRE_MESSAGE(privkey.
IsValid(),
1539 "Confirm the private key is valid");
1541 BOOST_CHECK_MESSAGE(
MessageSign(privkey, message, generated_signature),
1542 "Sign with a valid private key");
1552 "signature should be irrelevant",
1558 "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
1559 "signature should be irrelevant",
1565 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1566 "invalid signature, not in base64 encoding",
1567 "message should be irrelevant"),
1572 "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1573 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
1574 "message should be irrelevant"),
1579 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1580 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1581 "I never signed this"),
1586 "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1587 "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1593 "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3",
1594 "IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
1601 const std::string unsigned_tx =
"...";
1602 const std::string prefixed_message =
1605 std::string(1, (
char)unsigned_tx.length()) +
1609 const uint256 message_hash1 =
Hash(prefixed_message);
1613 BOOST_CHECK_NE(message_hash1, signature_hash);
1679 fs::path tmpfolder = m_args.GetDataDirBase();
1680 fs::path tmpfile = tmpfolder /
"read_binary.dat";
1681 std::string expected_text;
1682 for (
int i = 0; i < 30; i++) {
1683 expected_text +=
"0123456789";
1686 std::ofstream file{tmpfile.std_path()};
1687 file << expected_text;
1697 auto [valid, text] =
ReadBinaryFile(tmpfile, expected_text.size() / 2);
1703 fs::path invalid_file = tmpfolder /
"invalid_binary.dat";
1712 fs::path tmpfolder = m_args.GetDataDirBase();
1713 fs::path tmpfile = tmpfolder /
"write_binary.dat";
1714 std::string expected_text =
"bitcoin";
1716 std::string actual_text;
1717 std::ifstream file{tmpfile.std_path()};
1718 file >> actual_text;
1726 std::vector<uint8_t> v = {1, 2, 3};
1733 std::vector<bool> v = {
false,
true,
false,
false,
true,
true};
1740 std::deque<int> v = {1, 3, 3, 7};
1747template <
typename T>
1750 constexpr auto MAX{std::numeric_limits<T>::max()};
1760 BOOST_CHECK_EQUAL(CheckedLeftShift<T>(1, std::numeric_limits<T>::digits - 1), MAX / 2 + 1);
1763 BOOST_CHECK(!CheckedLeftShift<T>((MAX >> 1) + 1, 1));
1765 BOOST_CHECK(!CheckedLeftShift<T>(1, std::numeric_limits<T>::digits));
1766 BOOST_CHECK(!CheckedLeftShift<T>(1, std::numeric_limits<T>::digits + 1));
1768 if constexpr (std::is_signed_v<T>) {
1769 constexpr auto MIN{std::numeric_limits<T>::min()};
1776 BOOST_CHECK(!CheckedLeftShift<T>((MIN >> 1) - 1, 1));
1782template <
typename T>
1785 constexpr auto MAX{std::numeric_limits<T>::max()};
1795 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits - 1), MAX / 2 + 1);
1800 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits), MAX);
1801 BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits + 1), MAX);
1803 if constexpr (std::is_signed_v<T>) {
1804 constexpr auto MIN{std::numeric_limits<T>::min()};
1819 TestCheckedLeftShift<uint8_t>();
1820 TestCheckedLeftShift<int8_t>();
1821 TestCheckedLeftShift<size_t>();
1822 TestCheckedLeftShift<uint64_t>();
1823 TestCheckedLeftShift<int64_t>();
1828 TestSaturatingLeftShift<uint8_t>();
1829 TestSaturatingLeftShift<int8_t>();
1830 TestSaturatingLeftShift<size_t>();
1831 TestSaturatingLeftShift<uint64_t>();
1832 TestSaturatingLeftShift<int64_t>();
1835template <
class Int, auto
bytes>
1847 BOOST_CHECK_EQUAL((3_MiB + 123) /
double(1_MiB), (3_MiB + 123) / 1024.0 / 1024.0);
1891 constexpr int64_t
fee{12345};
1892 constexpr int32_t at_size{67};
1893 constexpr int32_t size{10};
1895 (uint64_t(
fee) * at_size + uint32_t(size) - 1) / uint32_t(size));
1898 constexpr unsigned bits{129};
1899 constexpr size_t digits{std::numeric_limits<size_t>::digits};
1916 BOOST_CHECK_EQUAL((1_GiB + 123) /
double(1_GiB), (1_GiB + 123) / 1024.0 / 1024.0 / 1024.0);
int64_t CAmount
Amount in satoshis (Can be negative)
constexpr CAmount COIN
The amount of satoshis in one BTC.
#define Assert(val)
Identity function.
#define Assume(val)
Assume is the identity function.
An encapsulated private key.
bool IsValid() const
Check whether this private key is valid.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
A token bucket rate limiter.
bool decrement(double n=1.0, double floor=0.0)
Consume n tokens.
void increment(const time_point &now)
Refill tokens based on elapsed time since last call.
double value() const
Current token balance.
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
Format the subversion field according to BIP 14 spec (https://github.com/bitcoin/bips/blob/master/bip...
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
const std::string MESSAGE_MAGIC
Text used to signify that a signed message follows and to prevent inadvertently signing a transaction...
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
void ReleaseDirectoryLocks()
Release all directory locks.
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
#define T(expected, seed, data)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
std::thread thread
Thread variable should be after other struct members so the thread does not start until the other mem...
std::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
std::string LogEscapeMessage(std::string_view str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
std::span< const char > Expr(std::span< const char > &sp)
Extract the expression that sp begins with.
bool Func(const std::string &str, std::span< const char > &sp)
Parse a function call.
bool Const(const std::string &str, std::span< const char > &sp, bool skip)
Parse a constant.
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
std::vector< std::string > SplitString(std::string_view str, char sep)
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
std::vector< T > Split(std::span< const char > sp LIFETIMEBOUND, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
std::string_view TrimStringView(std::string_view str LIFETIMEBOUND, std::string_view pattern=" \f\n\r\t\v")
void ReplaceAll(std::string &in_out, std::string_view search, std::string_view substitute)
Replace every non-overlapping occurrence of search with substitute, treating both literally; the repl...
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::string RemovePrefix(std::string_view str, std::string_view prefix)
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
LockResult LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
#define BOOST_CHECK(expr)
std::optional< T > CheckedAdd(const T i, const T j) noexcept
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).
T SaturatingAdd(const T i, const T j) noexcept
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
Write contents of std::string to a file.
std::pair< bool, std::string > ReadBinaryFile(const fs::path &filename, size_t maxsize)
Read full contents of a file and return them in a std::string.
@ ERR_MALFORMED_SIGNATURE
The provided signature couldn't be parsed (maybe invalid base64).
@ ERR_INVALID_ADDRESS
The provided address is invalid.
@ ERR_ADDRESS_NO_KEY
The provided address is valid but does not refer to a public key.
@ ERR_NOT_SIGNED
The message was not signed with the private key of the provided address.
@ OK
The message verification was successful.
@ ERR_PUBKEY_NOT_RECOVERED
A public key could not be recovered from the provided signature and message.
auto MakeByteSpan(const V &v) noexcept
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
auto MakeWritableByteSpan(V &&v) noexcept
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::time_point< NodeClock > time_point
#define TRY_LOCK(cs, name)
@ ZEROS
Seed with a compile time constant of zeros.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsHex(std::string_view str)
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::optional< uint64_t > ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
Parse a string with suffix unit [k|K|m|M|g|G|t|T].
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
void UninterruptibleSleep(const std::chrono::microseconds &n)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601Date(int64_t nTime)
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
std::string FormatRFC1123DateTime(int64_t time)
RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14 Used in HTTP/1....
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t TicksSeconds(Duration d)
int64_t atoi64_legacy(const std::string &str)
constexpr uint8_t HEX_PARSE_OUTPUT[]
constexpr char HEX_PARSE_INPUT[]
static void TestOtherProcess(fs::path dirname, fs::path lockname, int fd)
static constexpr char ExitCommand
static constexpr char UnlockCommand
static std::string SpanToStr(const std::span< const char > &span)
static void TestAddMatrixOverflow()
static void TestAddMatrix()
BOOST_AUTO_TEST_CASE(util_check)
static void RunToIntegralTests()
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR
static constexpr char LockCommand
void TestCheckedLeftShift()
void TestSaturatingLeftShift()
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
std::vector< std::common_type_t< Args... > > Vector(Args &&... args)
Construct a vector with the specified elements.
void ClearShrink(V &v) noexcept
Clear a vector (or std::deque) and release its allocated memory.