@@ -136,46 +136,51 @@ BOOST_AUTO_TEST_CASE(util_criticalsection)
136
136
} while (0 );
137
137
}
138
138
139
- static const unsigned char ParseHex_expected[65 ] = {
139
+ constexpr char HEX_PARSE_INPUT[] = " 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" ;
140
+ constexpr uint8_t HEX_PARSE_OUTPUT[] = {
140
141
0x04 , 0x67 , 0x8a , 0xfd , 0xb0 , 0xfe , 0x55 , 0x48 , 0x27 , 0x19 , 0x67 , 0xf1 , 0xa6 , 0x71 , 0x30 , 0xb7 ,
141
142
0x10 , 0x5c , 0xd6 , 0xa8 , 0x28 , 0xe0 , 0x39 , 0x09 , 0xa6 , 0x79 , 0x62 , 0xe0 , 0xea , 0x1f , 0x61 , 0xde ,
142
143
0xb6 , 0x49 , 0xf6 , 0xbc , 0x3f , 0x4c , 0xef , 0x38 , 0xc4 , 0xf3 , 0x55 , 0x04 , 0xe5 , 0x1e , 0xc1 , 0x12 ,
143
144
0xde , 0x5c , 0x38 , 0x4d , 0xf7 , 0xba , 0x0b , 0x8d , 0x57 , 0x8a , 0x4c , 0x70 , 0x2b , 0x6b , 0xf1 , 0x1d ,
144
145
0x5f
145
146
};
147
+ static_assert ((sizeof (HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT));
146
148
BOOST_AUTO_TEST_CASE (parse_hex)
147
149
{
148
150
std::vector<unsigned char > result;
149
- std::vector< unsigned char > expected (ParseHex_expected, ParseHex_expected + sizeof (ParseHex_expected));
151
+
150
152
// 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);
152
155
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 ();
154
158
BOOST_CHECK_EQUAL_COLLECTIONS (result.begin (), result.end (), expected.begin (), expected.end ());
155
159
156
160
// Spaces between bytes must be supported
161
+ expected = {0x12 , 0x34 , 0x56 , 0x78 };
157
162
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 () );
159
164
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 () );
161
166
162
167
// Leading space must be supported (used in BerkeleyEnvironment::Salvage)
168
+ expected = {0x89 , 0x34 , 0x56 , 0x78 };
163
169
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 () );
165
171
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 () );
167
173
168
174
// Mixed case and spaces are supported
175
+ expected = {0xff , 0xaa };
169
176
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 () );
171
178
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 () );
173
180
174
181
// 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 );
179
184
180
185
// Spaces between nibbles is treated as invalid
181
186
BOOST_CHECK_EQUAL (ParseHex (" AAF F" ).size (), 0 );
@@ -200,23 +205,15 @@ BOOST_AUTO_TEST_CASE(parse_hex)
200
205
201
206
BOOST_AUTO_TEST_CASE (util_HexStr)
202
207
{
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 )), " " );
214
211
215
212
{
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 };
217
215
const Span<const uint8_t > in_u{MakeUCharSpan (in_s)};
218
216
const Span<const std::byte> in_b{MakeByteSpan (in_s)};
219
- const std::string out_exp{" 04678afdb0" };
220
217
221
218
BOOST_CHECK_EQUAL (HexStr (in_u), out_exp);
222
219
BOOST_CHECK_EQUAL (HexStr (in_s), out_exp);
0 commit comments