diff options
author | Hodlinator <172445034+hodlinator@users.noreply.github.com> | 2024-08-21 14:55:10 +0200 |
---|---|---|
committer | Hodlinator <172445034+hodlinator@users.noreply.github.com> | 2024-08-28 19:09:51 +0200 |
commit | dc5f6f681275f56ff389500e3dd98fbe791f4a45 (patch) | |
tree | 39898bd391a5a3346f1ca9aacb2d333c8b258735 /src/test | |
parent | 2b5e6eff36abe4c23b8789ef1babfafedc90b973 (diff) |
test refactor: util_tests - parse_hex clean up
* Use BOOST_CHECK_EQUAL_COLLECTIONS and BOOST_CHECK_EQUAL instead of deprecated BOOST_CHECK.
* Avoid repeating expected values.
* Break out repeated HEX_PARSE_INPUT and rename ParseHex_expected to HEX_PARSE_OUTPUT.
Done in preparation for adding a couple more tests in the next commit.
Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/util_tests.cpp | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 9598b9c182..fbc8af789d 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -136,46 +136,51 @@ BOOST_AUTO_TEST_CASE(util_criticalsection) } while(0); } -static const unsigned char ParseHex_expected[65] = { +constexpr char HEX_PARSE_INPUT[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"; +constexpr uint8_t HEX_PARSE_OUTPUT[] = { 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f }; +static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT)); BOOST_AUTO_TEST_CASE(parse_hex) { std::vector<unsigned char> result; - std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)); + // Basic test vector - result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); + std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT)); + result = ParseHex(HEX_PARSE_INPUT); BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); - result = TryParseHex<uint8_t>("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").value(); + + result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value(); BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Spaces between bytes must be supported + expected = {0x12, 0x34, 0x56, 0x78}; result = ParseHex("12 34 56 78"); - BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); result = TryParseHex<uint8_t>("12 34 56 78").value(); - BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Leading space must be supported (used in BerkeleyEnvironment::Salvage) + expected = {0x89, 0x34, 0x56, 0x78}; result = ParseHex(" 89 34 56 78"); - BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); result = TryParseHex<uint8_t>(" 89 34 56 78").value(); - BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Mixed case and spaces are supported + expected = {0xff, 0xaa}; result = ParseHex(" Ff aA "); - BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); result = TryParseHex<uint8_t>(" Ff aA ").value(); - BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Empty string is supported - result = ParseHex(""); - BOOST_CHECK(result.size() == 0); - result = TryParseHex<uint8_t>("").value(); - BOOST_CHECK(result.size() == 0); + BOOST_CHECK_EQUAL(ParseHex("").size(), 0); + BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0); // Spaces between nibbles is treated as invalid BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0); @@ -200,23 +205,15 @@ BOOST_AUTO_TEST_CASE(parse_hex) BOOST_AUTO_TEST_CASE(util_HexStr) { - BOOST_CHECK_EQUAL( - HexStr(ParseHex_expected), - "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); - - BOOST_CHECK_EQUAL( - HexStr(Span{ParseHex_expected}.last(0)), - ""); - - BOOST_CHECK_EQUAL( - HexStr(Span{ParseHex_expected}.first(0)), - ""); + BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT); + BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.last(0)), ""); + BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.first(0)), ""); { - const std::vector<char> in_s{ParseHex_expected, ParseHex_expected + 5}; + constexpr std::string_view out_exp{"04678afdb0"}; + constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2}; const Span<const uint8_t> in_u{MakeUCharSpan(in_s)}; const Span<const std::byte> in_b{MakeByteSpan(in_s)}; - const std::string out_exp{"04678afdb0"}; BOOST_CHECK_EQUAL(HexStr(in_u), out_exp); BOOST_CHECK_EQUAL(HexStr(in_s), out_exp); |