Skip to content

Commit dc5f6f6

Browse files
hodlinatorl0rinc
andcommitted
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>
1 parent 2b5e6ef commit dc5f6f6

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/test/util_tests.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,46 +136,51 @@ BOOST_AUTO_TEST_CASE(util_criticalsection)
136136
} while(0);
137137
}
138138

139-
static const unsigned char ParseHex_expected[65] = {
139+
constexpr char HEX_PARSE_INPUT[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
140+
constexpr uint8_t HEX_PARSE_OUTPUT[] = {
140141
0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
141142
0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
142143
0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
143144
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
144145
0x5f
145146
};
147+
static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT));
146148
BOOST_AUTO_TEST_CASE(parse_hex)
147149
{
148150
std::vector<unsigned char> result;
149-
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
151+
150152
// Basic test vector
151-
result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
153+
std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT));
154+
result = ParseHex(HEX_PARSE_INPUT);
152155
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
153-
result = TryParseHex<uint8_t>("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").value();
156+
157+
result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value();
154158
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
155159

156160
// Spaces between bytes must be supported
161+
expected = {0x12, 0x34, 0x56, 0x78};
157162
result = ParseHex("12 34 56 78");
158-
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
163+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
159164
result = TryParseHex<uint8_t>("12 34 56 78").value();
160-
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
165+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
161166

162167
// Leading space must be supported (used in BerkeleyEnvironment::Salvage)
168+
expected = {0x89, 0x34, 0x56, 0x78};
163169
result = ParseHex(" 89 34 56 78");
164-
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
170+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
165171
result = TryParseHex<uint8_t>(" 89 34 56 78").value();
166-
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
172+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
167173

168174
// Mixed case and spaces are supported
175+
expected = {0xff, 0xaa};
169176
result = ParseHex(" Ff aA ");
170-
BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
177+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
171178
result = TryParseHex<uint8_t>(" Ff aA ").value();
172-
BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
179+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
173180

174181
// Empty string is supported
175-
result = ParseHex("");
176-
BOOST_CHECK(result.size() == 0);
177-
result = TryParseHex<uint8_t>("").value();
178-
BOOST_CHECK(result.size() == 0);
182+
BOOST_CHECK_EQUAL(ParseHex("").size(), 0);
183+
BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0);
179184

180185
// Spaces between nibbles is treated as invalid
181186
BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0);
@@ -200,23 +205,15 @@ BOOST_AUTO_TEST_CASE(parse_hex)
200205

201206
BOOST_AUTO_TEST_CASE(util_HexStr)
202207
{
203-
BOOST_CHECK_EQUAL(
204-
HexStr(ParseHex_expected),
205-
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
206-
207-
BOOST_CHECK_EQUAL(
208-
HexStr(Span{ParseHex_expected}.last(0)),
209-
"");
210-
211-
BOOST_CHECK_EQUAL(
212-
HexStr(Span{ParseHex_expected}.first(0)),
213-
"");
208+
BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT);
209+
BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.last(0)), "");
210+
BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.first(0)), "");
214211

215212
{
216-
const std::vector<char> in_s{ParseHex_expected, ParseHex_expected + 5};
213+
constexpr std::string_view out_exp{"04678afdb0"};
214+
constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2};
217215
const Span<const uint8_t> in_u{MakeUCharSpan(in_s)};
218216
const Span<const std::byte> in_b{MakeByteSpan(in_s)};
219-
const std::string out_exp{"04678afdb0"};
220217

221218
BOOST_CHECK_EQUAL(HexStr(in_u), out_exp);
222219
BOOST_CHECK_EQUAL(HexStr(in_s), out_exp);

0 commit comments

Comments
 (0)