1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include <clientversion.h>
8 #include <primitives/transaction.h>
10 #include <utilstrencodings.h>
11 #include <utilmoneystr.h>
12 #include <test/test_bitcoin.h>
17 #include <boost/test/unit_test.hpp>
19 BOOST_FIXTURE_TEST_SUITE(util_tests
, BasicTestingSetup
)
21 BOOST_AUTO_TEST_CASE(util_criticalsection
)
29 BOOST_ERROR("break was swallowed!");
33 TRY_LOCK(cs
, lockTest
);
37 BOOST_ERROR("break was swallowed!");
41 static const unsigned char ParseHex_expected
[65] = {
42 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
43 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
44 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
45 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
48 BOOST_AUTO_TEST_CASE(util_ParseHex
)
50 std::vector
<unsigned char> result
;
51 std::vector
<unsigned char> expected(ParseHex_expected
, ParseHex_expected
+ sizeof(ParseHex_expected
));
53 result
= ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
54 BOOST_CHECK_EQUAL_COLLECTIONS(result
.begin(), result
.end(), expected
.begin(), expected
.end());
56 // Spaces between bytes must be supported
57 result
= ParseHex("12 34 56 78");
58 BOOST_CHECK(result
.size() == 4 && result
[0] == 0x12 && result
[1] == 0x34 && result
[2] == 0x56 && result
[3] == 0x78);
60 // Leading space must be supported (used in CDBEnv::Salvage)
61 result
= ParseHex(" 89 34 56 78");
62 BOOST_CHECK(result
.size() == 4 && result
[0] == 0x89 && result
[1] == 0x34 && result
[2] == 0x56 && result
[3] == 0x78);
64 // Stop parsing at invalid value
65 result
= ParseHex("1234 invalid 1234");
66 BOOST_CHECK(result
.size() == 2 && result
[0] == 0x12 && result
[1] == 0x34);
69 BOOST_AUTO_TEST_CASE(util_HexStr
)
72 HexStr(ParseHex_expected
, ParseHex_expected
+ sizeof(ParseHex_expected
)),
73 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
76 HexStr(ParseHex_expected
, ParseHex_expected
+ 5, true),
80 HexStr(ParseHex_expected
, ParseHex_expected
, true),
83 std::vector
<unsigned char> ParseHex_vec(ParseHex_expected
, ParseHex_expected
+ 5);
86 HexStr(ParseHex_vec
, true),
91 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat
)
93 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), "1970-01-01 00:00:00");
94 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), "2038-01-19 03:14:07");
95 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17");
96 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), "2011-09-30 23:36");
97 BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000");
100 class TestArgsManager
: public ArgsManager
103 std::map
<std::string
, std::string
>& GetMapArgs()
107 const std::map
<std::string
, std::vector
<std::string
> >& GetMapMultiArgs()
113 BOOST_AUTO_TEST_CASE(util_ParseParameters
)
115 TestArgsManager testArgs
;
116 const char *argv_test
[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
118 testArgs
.ParseParameters(0, (char**)argv_test
);
119 BOOST_CHECK(testArgs
.GetMapArgs().empty() && testArgs
.GetMapMultiArgs().empty());
121 testArgs
.ParseParameters(1, (char**)argv_test
);
122 BOOST_CHECK(testArgs
.GetMapArgs().empty() && testArgs
.GetMapMultiArgs().empty());
124 testArgs
.ParseParameters(5, (char**)argv_test
);
125 // expectation: -ignored is ignored (program name argument),
126 // -a, -b and -ccc end up in map, -d ignored because it is after
127 // a non-option argument (non-GNU option parsing)
128 BOOST_CHECK(testArgs
.GetMapArgs().size() == 3 && testArgs
.GetMapMultiArgs().size() == 3);
129 BOOST_CHECK(testArgs
.IsArgSet("-a") && testArgs
.IsArgSet("-b") && testArgs
.IsArgSet("-ccc")
130 && !testArgs
.IsArgSet("f") && !testArgs
.IsArgSet("-d"));
131 BOOST_CHECK(testArgs
.GetMapMultiArgs().count("-a") && testArgs
.GetMapMultiArgs().count("-b") && testArgs
.GetMapMultiArgs().count("-ccc")
132 && !testArgs
.GetMapMultiArgs().count("f") && !testArgs
.GetMapMultiArgs().count("-d"));
134 BOOST_CHECK(testArgs
.GetMapArgs()["-a"] == "" && testArgs
.GetMapArgs()["-ccc"] == "multiple");
135 BOOST_CHECK(testArgs
.GetArgs("-ccc").size() == 2);
138 BOOST_AUTO_TEST_CASE(util_GetArg
)
140 TestArgsManager testArgs
;
141 testArgs
.GetMapArgs().clear();
142 testArgs
.GetMapArgs()["strtest1"] = "string...";
143 // strtest2 undefined on purpose
144 testArgs
.GetMapArgs()["inttest1"] = "12345";
145 testArgs
.GetMapArgs()["inttest2"] = "81985529216486895";
146 // inttest3 undefined on purpose
147 testArgs
.GetMapArgs()["booltest1"] = "";
148 // booltest2 undefined on purpose
149 testArgs
.GetMapArgs()["booltest3"] = "0";
150 testArgs
.GetMapArgs()["booltest4"] = "1";
152 BOOST_CHECK_EQUAL(testArgs
.GetArg("strtest1", "default"), "string...");
153 BOOST_CHECK_EQUAL(testArgs
.GetArg("strtest2", "default"), "default");
154 BOOST_CHECK_EQUAL(testArgs
.GetArg("inttest1", -1), 12345);
155 BOOST_CHECK_EQUAL(testArgs
.GetArg("inttest2", -1), 81985529216486895LL);
156 BOOST_CHECK_EQUAL(testArgs
.GetArg("inttest3", -1), -1);
157 BOOST_CHECK_EQUAL(testArgs
.GetBoolArg("booltest1", false), true);
158 BOOST_CHECK_EQUAL(testArgs
.GetBoolArg("booltest2", false), false);
159 BOOST_CHECK_EQUAL(testArgs
.GetBoolArg("booltest3", false), false);
160 BOOST_CHECK_EQUAL(testArgs
.GetBoolArg("booltest4", false), true);
163 BOOST_AUTO_TEST_CASE(util_FormatMoney
)
165 BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
166 BOOST_CHECK_EQUAL(FormatMoney((COIN
/10000)*123456789), "12345.6789");
167 BOOST_CHECK_EQUAL(FormatMoney(-COIN
), "-1.00");
169 BOOST_CHECK_EQUAL(FormatMoney(COIN
*100000000), "100000000.00");
170 BOOST_CHECK_EQUAL(FormatMoney(COIN
*10000000), "10000000.00");
171 BOOST_CHECK_EQUAL(FormatMoney(COIN
*1000000), "1000000.00");
172 BOOST_CHECK_EQUAL(FormatMoney(COIN
*100000), "100000.00");
173 BOOST_CHECK_EQUAL(FormatMoney(COIN
*10000), "10000.00");
174 BOOST_CHECK_EQUAL(FormatMoney(COIN
*1000), "1000.00");
175 BOOST_CHECK_EQUAL(FormatMoney(COIN
*100), "100.00");
176 BOOST_CHECK_EQUAL(FormatMoney(COIN
*10), "10.00");
177 BOOST_CHECK_EQUAL(FormatMoney(COIN
), "1.00");
178 BOOST_CHECK_EQUAL(FormatMoney(COIN
/10), "0.10");
179 BOOST_CHECK_EQUAL(FormatMoney(COIN
/100), "0.01");
180 BOOST_CHECK_EQUAL(FormatMoney(COIN
/1000), "0.001");
181 BOOST_CHECK_EQUAL(FormatMoney(COIN
/10000), "0.0001");
182 BOOST_CHECK_EQUAL(FormatMoney(COIN
/100000), "0.00001");
183 BOOST_CHECK_EQUAL(FormatMoney(COIN
/1000000), "0.000001");
184 BOOST_CHECK_EQUAL(FormatMoney(COIN
/10000000), "0.0000001");
185 BOOST_CHECK_EQUAL(FormatMoney(COIN
/100000000), "0.00000001");
188 BOOST_AUTO_TEST_CASE(util_ParseMoney
)
191 BOOST_CHECK(ParseMoney("0.0", ret
));
192 BOOST_CHECK_EQUAL(ret
, 0);
194 BOOST_CHECK(ParseMoney("12345.6789", ret
));
195 BOOST_CHECK_EQUAL(ret
, (COIN
/10000)*123456789);
197 BOOST_CHECK(ParseMoney("100000000.00", ret
));
198 BOOST_CHECK_EQUAL(ret
, COIN
*100000000);
199 BOOST_CHECK(ParseMoney("10000000.00", ret
));
200 BOOST_CHECK_EQUAL(ret
, COIN
*10000000);
201 BOOST_CHECK(ParseMoney("1000000.00", ret
));
202 BOOST_CHECK_EQUAL(ret
, COIN
*1000000);
203 BOOST_CHECK(ParseMoney("100000.00", ret
));
204 BOOST_CHECK_EQUAL(ret
, COIN
*100000);
205 BOOST_CHECK(ParseMoney("10000.00", ret
));
206 BOOST_CHECK_EQUAL(ret
, COIN
*10000);
207 BOOST_CHECK(ParseMoney("1000.00", ret
));
208 BOOST_CHECK_EQUAL(ret
, COIN
*1000);
209 BOOST_CHECK(ParseMoney("100.00", ret
));
210 BOOST_CHECK_EQUAL(ret
, COIN
*100);
211 BOOST_CHECK(ParseMoney("10.00", ret
));
212 BOOST_CHECK_EQUAL(ret
, COIN
*10);
213 BOOST_CHECK(ParseMoney("1.00", ret
));
214 BOOST_CHECK_EQUAL(ret
, COIN
);
215 BOOST_CHECK(ParseMoney("1", ret
));
216 BOOST_CHECK_EQUAL(ret
, COIN
);
217 BOOST_CHECK(ParseMoney("0.1", ret
));
218 BOOST_CHECK_EQUAL(ret
, COIN
/10);
219 BOOST_CHECK(ParseMoney("0.01", ret
));
220 BOOST_CHECK_EQUAL(ret
, COIN
/100);
221 BOOST_CHECK(ParseMoney("0.001", ret
));
222 BOOST_CHECK_EQUAL(ret
, COIN
/1000);
223 BOOST_CHECK(ParseMoney("0.0001", ret
));
224 BOOST_CHECK_EQUAL(ret
, COIN
/10000);
225 BOOST_CHECK(ParseMoney("0.00001", ret
));
226 BOOST_CHECK_EQUAL(ret
, COIN
/100000);
227 BOOST_CHECK(ParseMoney("0.000001", ret
));
228 BOOST_CHECK_EQUAL(ret
, COIN
/1000000);
229 BOOST_CHECK(ParseMoney("0.0000001", ret
));
230 BOOST_CHECK_EQUAL(ret
, COIN
/10000000);
231 BOOST_CHECK(ParseMoney("0.00000001", ret
));
232 BOOST_CHECK_EQUAL(ret
, COIN
/100000000);
234 // Attempted 63 bit overflow should fail
235 BOOST_CHECK(!ParseMoney("92233720368.54775808", ret
));
237 // Parsing negative amounts must fail
238 BOOST_CHECK(!ParseMoney("-1", ret
));
241 BOOST_AUTO_TEST_CASE(util_IsHex
)
243 BOOST_CHECK(IsHex("00"));
244 BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
245 BOOST_CHECK(IsHex("ff"));
246 BOOST_CHECK(IsHex("FF"));
248 BOOST_CHECK(!IsHex(""));
249 BOOST_CHECK(!IsHex("0"));
250 BOOST_CHECK(!IsHex("a"));
251 BOOST_CHECK(!IsHex("eleven"));
252 BOOST_CHECK(!IsHex("00xx00"));
253 BOOST_CHECK(!IsHex("0x0000"));
256 BOOST_AUTO_TEST_CASE(util_IsHexNumber
)
258 BOOST_CHECK(IsHexNumber("0x0"));
259 BOOST_CHECK(IsHexNumber("0"));
260 BOOST_CHECK(IsHexNumber("0x10"));
261 BOOST_CHECK(IsHexNumber("10"));
262 BOOST_CHECK(IsHexNumber("0xff"));
263 BOOST_CHECK(IsHexNumber("ff"));
264 BOOST_CHECK(IsHexNumber("0xFfa"));
265 BOOST_CHECK(IsHexNumber("Ffa"));
266 BOOST_CHECK(IsHexNumber("0x00112233445566778899aabbccddeeffAABBCCDDEEFF"));
267 BOOST_CHECK(IsHexNumber("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
269 BOOST_CHECK(!IsHexNumber("")); // empty string not allowed
270 BOOST_CHECK(!IsHexNumber("0x")); // empty string after prefix not allowed
271 BOOST_CHECK(!IsHexNumber("0x0 ")); // no spaces at end,
272 BOOST_CHECK(!IsHexNumber(" 0x0")); // or beginning,
273 BOOST_CHECK(!IsHexNumber("0x 0")); // or middle,
274 BOOST_CHECK(!IsHexNumber(" ")); // etc.
275 BOOST_CHECK(!IsHexNumber("0x0ga")); // invalid character
276 BOOST_CHECK(!IsHexNumber("x0")); // broken prefix
277 BOOST_CHECK(!IsHexNumber("0x0x00")); // two prefixes not allowed
281 BOOST_AUTO_TEST_CASE(util_seed_insecure_rand
)
283 SeedInsecureRand(true);
284 for (int mod
=2;mod
<11;mod
++)
287 // Really rough binomial confidence approximation.
288 int err
= 30*10000./mod
*sqrt((1./mod
*(1-1./mod
))/10000.);
289 //mask is 2^ceil(log2(mod))-1
290 while(mask
<mod
-1)mask
=(mask
<<1)+1;
293 //How often does it get a zero from the uniform range [0,mod)?
294 for (int i
= 0; i
< 10000; i
++) {
297 rval
=InsecureRand32()&mask
;
298 }while(rval
>=(uint32_t)mod
);
301 BOOST_CHECK(count
<=10000/mod
+err
);
302 BOOST_CHECK(count
>=10000/mod
-err
);
306 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual
)
308 BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
309 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
310 BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
311 BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
312 BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
313 BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
314 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
317 /* Test strprintf formatting directives.
318 * Put a string before and after to ensure sanity of element sizes on stack. */
319 #define B "check_prefix"
320 #define E "check_postfix"
321 BOOST_AUTO_TEST_CASE(strprintf_numbers
)
323 int64_t s64t
= -9223372036854775807LL; /* signed 64 bit test value */
324 uint64_t u64t
= 18446744073709551615ULL; /* unsigned 64 bit test value */
325 BOOST_CHECK(strprintf("%s %d %s", B
, s64t
, E
) == B
" -9223372036854775807 " E
);
326 BOOST_CHECK(strprintf("%s %u %s", B
, u64t
, E
) == B
" 18446744073709551615 " E
);
327 BOOST_CHECK(strprintf("%s %x %s", B
, u64t
, E
) == B
" ffffffffffffffff " E
);
329 size_t st
= 12345678; /* unsigned size_t test value */
330 ssize_t sst
= -12345678; /* signed size_t test value */
331 BOOST_CHECK(strprintf("%s %d %s", B
, sst
, E
) == B
" -12345678 " E
);
332 BOOST_CHECK(strprintf("%s %u %s", B
, st
, E
) == B
" 12345678 " E
);
333 BOOST_CHECK(strprintf("%s %x %s", B
, st
, E
) == B
" bc614e " E
);
335 ptrdiff_t pt
= 87654321; /* positive ptrdiff_t test value */
336 ptrdiff_t spt
= -87654321; /* negative ptrdiff_t test value */
337 BOOST_CHECK(strprintf("%s %d %s", B
, spt
, E
) == B
" -87654321 " E
);
338 BOOST_CHECK(strprintf("%s %u %s", B
, pt
, E
) == B
" 87654321 " E
);
339 BOOST_CHECK(strprintf("%s %x %s", B
, pt
, E
) == B
" 5397fb1 " E
);
344 /* Check for mingw/wine issue #3494
345 * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106'
347 BOOST_AUTO_TEST_CASE(gettime
)
349 BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL
) == 0);
352 BOOST_AUTO_TEST_CASE(test_ParseInt32
)
356 BOOST_CHECK(ParseInt32("1234", nullptr));
357 BOOST_CHECK(ParseInt32("0", &n
) && n
== 0);
358 BOOST_CHECK(ParseInt32("1234", &n
) && n
== 1234);
359 BOOST_CHECK(ParseInt32("01234", &n
) && n
== 1234); // no octal
360 BOOST_CHECK(ParseInt32("2147483647", &n
) && n
== 2147483647);
361 BOOST_CHECK(ParseInt32("-2147483648", &n
) && n
== (-2147483647 - 1)); // (-2147483647 - 1) equals INT_MIN
362 BOOST_CHECK(ParseInt32("-1234", &n
) && n
== -1234);
364 BOOST_CHECK(!ParseInt32("", &n
));
365 BOOST_CHECK(!ParseInt32(" 1", &n
)); // no padding inside
366 BOOST_CHECK(!ParseInt32("1 ", &n
));
367 BOOST_CHECK(!ParseInt32("1a", &n
));
368 BOOST_CHECK(!ParseInt32("aap", &n
));
369 BOOST_CHECK(!ParseInt32("0x1", &n
)); // no hex
370 BOOST_CHECK(!ParseInt32("0x1", &n
)); // no hex
371 const char test_bytes
[] = {'1', 0, '1'};
372 std::string
teststr(test_bytes
, sizeof(test_bytes
));
373 BOOST_CHECK(!ParseInt32(teststr
, &n
)); // no embedded NULs
374 // Overflow and underflow
375 BOOST_CHECK(!ParseInt32("-2147483649", nullptr));
376 BOOST_CHECK(!ParseInt32("2147483648", nullptr));
377 BOOST_CHECK(!ParseInt32("-32482348723847471234", nullptr));
378 BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
381 BOOST_AUTO_TEST_CASE(test_ParseInt64
)
385 BOOST_CHECK(ParseInt64("1234", nullptr));
386 BOOST_CHECK(ParseInt64("0", &n
) && n
== 0LL);
387 BOOST_CHECK(ParseInt64("1234", &n
) && n
== 1234LL);
388 BOOST_CHECK(ParseInt64("01234", &n
) && n
== 1234LL); // no octal
389 BOOST_CHECK(ParseInt64("2147483647", &n
) && n
== 2147483647LL);
390 BOOST_CHECK(ParseInt64("-2147483648", &n
) && n
== -2147483648LL);
391 BOOST_CHECK(ParseInt64("9223372036854775807", &n
) && n
== (int64_t)9223372036854775807);
392 BOOST_CHECK(ParseInt64("-9223372036854775808", &n
) && n
== (int64_t)-9223372036854775807-1);
393 BOOST_CHECK(ParseInt64("-1234", &n
) && n
== -1234LL);
395 BOOST_CHECK(!ParseInt64("", &n
));
396 BOOST_CHECK(!ParseInt64(" 1", &n
)); // no padding inside
397 BOOST_CHECK(!ParseInt64("1 ", &n
));
398 BOOST_CHECK(!ParseInt64("1a", &n
));
399 BOOST_CHECK(!ParseInt64("aap", &n
));
400 BOOST_CHECK(!ParseInt64("0x1", &n
)); // no hex
401 const char test_bytes
[] = {'1', 0, '1'};
402 std::string
teststr(test_bytes
, sizeof(test_bytes
));
403 BOOST_CHECK(!ParseInt64(teststr
, &n
)); // no embedded NULs
404 // Overflow and underflow
405 BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr));
406 BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr));
407 BOOST_CHECK(!ParseInt64("-32482348723847471234", nullptr));
408 BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr));
411 BOOST_AUTO_TEST_CASE(test_ParseUInt32
)
415 BOOST_CHECK(ParseUInt32("1234", nullptr));
416 BOOST_CHECK(ParseUInt32("0", &n
) && n
== 0);
417 BOOST_CHECK(ParseUInt32("1234", &n
) && n
== 1234);
418 BOOST_CHECK(ParseUInt32("01234", &n
) && n
== 1234); // no octal
419 BOOST_CHECK(ParseUInt32("2147483647", &n
) && n
== 2147483647);
420 BOOST_CHECK(ParseUInt32("2147483648", &n
) && n
== (uint32_t)2147483648);
421 BOOST_CHECK(ParseUInt32("4294967295", &n
) && n
== (uint32_t)4294967295);
423 BOOST_CHECK(!ParseUInt32("", &n
));
424 BOOST_CHECK(!ParseUInt32(" 1", &n
)); // no padding inside
425 BOOST_CHECK(!ParseUInt32(" -1", &n
));
426 BOOST_CHECK(!ParseUInt32("1 ", &n
));
427 BOOST_CHECK(!ParseUInt32("1a", &n
));
428 BOOST_CHECK(!ParseUInt32("aap", &n
));
429 BOOST_CHECK(!ParseUInt32("0x1", &n
)); // no hex
430 BOOST_CHECK(!ParseUInt32("0x1", &n
)); // no hex
431 const char test_bytes
[] = {'1', 0, '1'};
432 std::string
teststr(test_bytes
, sizeof(test_bytes
));
433 BOOST_CHECK(!ParseUInt32(teststr
, &n
)); // no embedded NULs
434 // Overflow and underflow
435 BOOST_CHECK(!ParseUInt32("-2147483648", &n
));
436 BOOST_CHECK(!ParseUInt32("4294967296", &n
));
437 BOOST_CHECK(!ParseUInt32("-1234", &n
));
438 BOOST_CHECK(!ParseUInt32("-32482348723847471234", nullptr));
439 BOOST_CHECK(!ParseUInt32("32482348723847471234", nullptr));
442 BOOST_AUTO_TEST_CASE(test_ParseUInt64
)
446 BOOST_CHECK(ParseUInt64("1234", nullptr));
447 BOOST_CHECK(ParseUInt64("0", &n
) && n
== 0LL);
448 BOOST_CHECK(ParseUInt64("1234", &n
) && n
== 1234LL);
449 BOOST_CHECK(ParseUInt64("01234", &n
) && n
== 1234LL); // no octal
450 BOOST_CHECK(ParseUInt64("2147483647", &n
) && n
== 2147483647LL);
451 BOOST_CHECK(ParseUInt64("9223372036854775807", &n
) && n
== 9223372036854775807ULL);
452 BOOST_CHECK(ParseUInt64("9223372036854775808", &n
) && n
== 9223372036854775808ULL);
453 BOOST_CHECK(ParseUInt64("18446744073709551615", &n
) && n
== 18446744073709551615ULL);
455 BOOST_CHECK(!ParseUInt64("", &n
));
456 BOOST_CHECK(!ParseUInt64(" 1", &n
)); // no padding inside
457 BOOST_CHECK(!ParseUInt64(" -1", &n
));
458 BOOST_CHECK(!ParseUInt64("1 ", &n
));
459 BOOST_CHECK(!ParseUInt64("1a", &n
));
460 BOOST_CHECK(!ParseUInt64("aap", &n
));
461 BOOST_CHECK(!ParseUInt64("0x1", &n
)); // no hex
462 const char test_bytes
[] = {'1', 0, '1'};
463 std::string
teststr(test_bytes
, sizeof(test_bytes
));
464 BOOST_CHECK(!ParseUInt64(teststr
, &n
)); // no embedded NULs
465 // Overflow and underflow
466 BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr));
467 BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr));
468 BOOST_CHECK(!ParseUInt64("-32482348723847471234", nullptr));
469 BOOST_CHECK(!ParseUInt64("-2147483648", &n
));
470 BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n
));
471 BOOST_CHECK(!ParseUInt64("-1234", &n
));
474 BOOST_AUTO_TEST_CASE(test_ParseDouble
)
478 BOOST_CHECK(ParseDouble("1234", nullptr));
479 BOOST_CHECK(ParseDouble("0", &n
) && n
== 0.0);
480 BOOST_CHECK(ParseDouble("1234", &n
) && n
== 1234.0);
481 BOOST_CHECK(ParseDouble("01234", &n
) && n
== 1234.0); // no octal
482 BOOST_CHECK(ParseDouble("2147483647", &n
) && n
== 2147483647.0);
483 BOOST_CHECK(ParseDouble("-2147483648", &n
) && n
== -2147483648.0);
484 BOOST_CHECK(ParseDouble("-1234", &n
) && n
== -1234.0);
485 BOOST_CHECK(ParseDouble("1e6", &n
) && n
== 1e6
);
486 BOOST_CHECK(ParseDouble("-1e6", &n
) && n
== -1e6
);
488 BOOST_CHECK(!ParseDouble("", &n
));
489 BOOST_CHECK(!ParseDouble(" 1", &n
)); // no padding inside
490 BOOST_CHECK(!ParseDouble("1 ", &n
));
491 BOOST_CHECK(!ParseDouble("1a", &n
));
492 BOOST_CHECK(!ParseDouble("aap", &n
));
493 BOOST_CHECK(!ParseDouble("0x1", &n
)); // no hex
494 const char test_bytes
[] = {'1', 0, '1'};
495 std::string
teststr(test_bytes
, sizeof(test_bytes
));
496 BOOST_CHECK(!ParseDouble(teststr
, &n
)); // no embedded NULs
497 // Overflow and underflow
498 BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
499 BOOST_CHECK(!ParseDouble("1e10000", nullptr));
502 BOOST_AUTO_TEST_CASE(test_FormatParagraph
)
504 BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
505 BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
506 BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
507 BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
508 BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
509 BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
510 BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test");
512 // Make sure we don't indent a fully-new line following a too-long line ending
513 BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n test\nabc");
515 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");
517 // Test wrap length is exact
518 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");
519 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");
520 // Indent should be included in length of lines
521 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");
523 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.");
524 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.");
525 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.");
526 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.");
529 BOOST_AUTO_TEST_CASE(test_FormatSubVersion
)
531 std::vector
<std::string
> comments
;
532 comments
.push_back(std::string("comment1"));
533 std::vector
<std::string
> comments2
;
534 comments2
.push_back(std::string("comment1"));
535 comments2
.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT
)); // Semicolon is discouraged but not forbidden by BIP-0014
536 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector
<std::string
>()),std::string("/Test:0.9.99/"));
537 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments
),std::string("/Test:0.9.99(comment1)/"));
538 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2
),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));
541 BOOST_AUTO_TEST_CASE(test_ParseFixedPoint
)
544 BOOST_CHECK(ParseFixedPoint("0", 8, &amount
));
545 BOOST_CHECK_EQUAL(amount
, 0LL);
546 BOOST_CHECK(ParseFixedPoint("1", 8, &amount
));
547 BOOST_CHECK_EQUAL(amount
, 100000000LL);
548 BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount
));
549 BOOST_CHECK_EQUAL(amount
, 0LL);
550 BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount
));
551 BOOST_CHECK_EQUAL(amount
, -10000000LL);
552 BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount
));
553 BOOST_CHECK_EQUAL(amount
, 110000000LL);
554 BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount
));
555 BOOST_CHECK_EQUAL(amount
, 110000000LL);
556 BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount
));
557 BOOST_CHECK_EQUAL(amount
, 1100000000LL);
558 BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount
));
559 BOOST_CHECK_EQUAL(amount
, 11000000LL);
560 BOOST_CHECK(ParseFixedPoint("1000", 8, &amount
));
561 BOOST_CHECK_EQUAL(amount
, 100000000000LL);
562 BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount
));
563 BOOST_CHECK_EQUAL(amount
, -100000000000LL);
564 BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount
));
565 BOOST_CHECK_EQUAL(amount
, 1LL);
566 BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount
));
567 BOOST_CHECK_EQUAL(amount
, 1LL);
568 BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount
));
569 BOOST_CHECK_EQUAL(amount
, -1LL);
570 BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount
));
571 BOOST_CHECK_EQUAL(amount
, 100000000000000001LL);
572 BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount
));
573 BOOST_CHECK_EQUAL(amount
, 999999999999999999LL);
574 BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount
));
575 BOOST_CHECK_EQUAL(amount
, -999999999999999999LL);
577 BOOST_CHECK(!ParseFixedPoint("", 8, &amount
));
578 BOOST_CHECK(!ParseFixedPoint("-", 8, &amount
));
579 BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount
));
580 BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount
));
581 BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount
));
582 BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount
));
583 BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount
));
584 BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount
));
585 BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount
));
586 BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount
));
587 BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount
));
588 BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount
));
589 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount
));
590 BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount
));
591 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount
));
592 BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount
));
593 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount
));
594 BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount
));
595 BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount
));
596 BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount
));
597 BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount
));
598 BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount
));
599 BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount
));
600 BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount
));
601 BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount
));
602 BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount
));
603 BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount
));
606 BOOST_AUTO_TEST_SUITE_END()