[tests] fixup dbcrash interaction with add_nodes()
[bitcoinplatinum.git] / src / test / util_tests.cpp
blob567908696902f89195257c4f83ea74d936df952d
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.
5 #include "util.h"
7 #include "clientversion.h"
8 #include "primitives/transaction.h"
9 #include "sync.h"
10 #include "utilstrencodings.h"
11 #include "utilmoneystr.h"
12 #include "test/test_bitcoin.h"
14 #include <stdint.h>
15 #include <vector>
17 #include <boost/test/unit_test.hpp>
19 BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
21 BOOST_AUTO_TEST_CASE(util_criticalsection)
23 CCriticalSection cs;
25 do {
26 LOCK(cs);
27 break;
29 BOOST_ERROR("break was swallowed!");
30 } while(0);
32 do {
33 TRY_LOCK(cs, lockTest);
34 if (lockTest)
35 break;
37 BOOST_ERROR("break was swallowed!");
38 } while(0);
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,
46 0x5f
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));
52 // Basic test vector
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)
71 BOOST_CHECK_EQUAL(
72 HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
73 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
75 BOOST_CHECK_EQUAL(
76 HexStr(ParseHex_expected, ParseHex_expected + 5, true),
77 "04 67 8a fd b0");
79 BOOST_CHECK_EQUAL(
80 HexStr(ParseHex_expected, ParseHex_expected, true),
81 "");
83 std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
85 BOOST_CHECK_EQUAL(
86 HexStr(ParseHex_vec, true),
87 "04 67 8a fd b0");
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
102 public:
103 std::map<std::string, std::string>& GetMapArgs()
105 return mapArgs;
107 const std::map<std::string, std::vector<std::string> >& GetMapMultiArgs()
109 return mapMultiArgs;
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)
190 CAmount ret = 0;
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_seed_insecure_rand)
258 SeedInsecureRand(true);
259 for (int mod=2;mod<11;mod++)
261 int mask = 1;
262 // Really rough binomial confidence approximation.
263 int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
264 //mask is 2^ceil(log2(mod))-1
265 while(mask<mod-1)mask=(mask<<1)+1;
267 int count = 0;
268 //How often does it get a zero from the uniform range [0,mod)?
269 for (int i = 0; i < 10000; i++) {
270 uint32_t rval;
272 rval=InsecureRand32()&mask;
273 }while(rval>=(uint32_t)mod);
274 count += rval==0;
276 BOOST_CHECK(count<=10000/mod+err);
277 BOOST_CHECK(count>=10000/mod-err);
281 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
283 BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
284 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
285 BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
286 BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
287 BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
288 BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
289 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
292 /* Test strprintf formatting directives.
293 * Put a string before and after to ensure sanity of element sizes on stack. */
294 #define B "check_prefix"
295 #define E "check_postfix"
296 BOOST_AUTO_TEST_CASE(strprintf_numbers)
298 int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
299 uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
300 BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
301 BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
302 BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
304 size_t st = 12345678; /* unsigned size_t test value */
305 ssize_t sst = -12345678; /* signed size_t test value */
306 BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
307 BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
308 BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
310 ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
311 ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
312 BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
313 BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
314 BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
316 #undef B
317 #undef E
319 /* Check for mingw/wine issue #3494
320 * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106'
322 BOOST_AUTO_TEST_CASE(gettime)
324 BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
327 BOOST_AUTO_TEST_CASE(test_ParseInt32)
329 int32_t n;
330 // Valid values
331 BOOST_CHECK(ParseInt32("1234", nullptr));
332 BOOST_CHECK(ParseInt32("0", &n) && n == 0);
333 BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
334 BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
335 BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
336 BOOST_CHECK(ParseInt32("-2147483648", &n) && n == (-2147483647 - 1)); // (-2147483647 - 1) equals INT_MIN
337 BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
338 // Invalid values
339 BOOST_CHECK(!ParseInt32("", &n));
340 BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
341 BOOST_CHECK(!ParseInt32("1 ", &n));
342 BOOST_CHECK(!ParseInt32("1a", &n));
343 BOOST_CHECK(!ParseInt32("aap", &n));
344 BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
345 BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
346 const char test_bytes[] = {'1', 0, '1'};
347 std::string teststr(test_bytes, sizeof(test_bytes));
348 BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs
349 // Overflow and underflow
350 BOOST_CHECK(!ParseInt32("-2147483649", nullptr));
351 BOOST_CHECK(!ParseInt32("2147483648", nullptr));
352 BOOST_CHECK(!ParseInt32("-32482348723847471234", nullptr));
353 BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
356 BOOST_AUTO_TEST_CASE(test_ParseInt64)
358 int64_t n;
359 // Valid values
360 BOOST_CHECK(ParseInt64("1234", nullptr));
361 BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
362 BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
363 BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
364 BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
365 BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
366 BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == (int64_t)9223372036854775807);
367 BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == (int64_t)-9223372036854775807-1);
368 BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
369 // Invalid values
370 BOOST_CHECK(!ParseInt64("", &n));
371 BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
372 BOOST_CHECK(!ParseInt64("1 ", &n));
373 BOOST_CHECK(!ParseInt64("1a", &n));
374 BOOST_CHECK(!ParseInt64("aap", &n));
375 BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
376 const char test_bytes[] = {'1', 0, '1'};
377 std::string teststr(test_bytes, sizeof(test_bytes));
378 BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs
379 // Overflow and underflow
380 BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr));
381 BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr));
382 BOOST_CHECK(!ParseInt64("-32482348723847471234", nullptr));
383 BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr));
386 BOOST_AUTO_TEST_CASE(test_ParseUInt32)
388 uint32_t n;
389 // Valid values
390 BOOST_CHECK(ParseUInt32("1234", nullptr));
391 BOOST_CHECK(ParseUInt32("0", &n) && n == 0);
392 BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234);
393 BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal
394 BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647);
395 BOOST_CHECK(ParseUInt32("2147483648", &n) && n == (uint32_t)2147483648);
396 BOOST_CHECK(ParseUInt32("4294967295", &n) && n == (uint32_t)4294967295);
397 // Invalid values
398 BOOST_CHECK(!ParseUInt32("", &n));
399 BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside
400 BOOST_CHECK(!ParseUInt32(" -1", &n));
401 BOOST_CHECK(!ParseUInt32("1 ", &n));
402 BOOST_CHECK(!ParseUInt32("1a", &n));
403 BOOST_CHECK(!ParseUInt32("aap", &n));
404 BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
405 BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
406 const char test_bytes[] = {'1', 0, '1'};
407 std::string teststr(test_bytes, sizeof(test_bytes));
408 BOOST_CHECK(!ParseUInt32(teststr, &n)); // no embedded NULs
409 // Overflow and underflow
410 BOOST_CHECK(!ParseUInt32("-2147483648", &n));
411 BOOST_CHECK(!ParseUInt32("4294967296", &n));
412 BOOST_CHECK(!ParseUInt32("-1234", &n));
413 BOOST_CHECK(!ParseUInt32("-32482348723847471234", nullptr));
414 BOOST_CHECK(!ParseUInt32("32482348723847471234", nullptr));
417 BOOST_AUTO_TEST_CASE(test_ParseUInt64)
419 uint64_t n;
420 // Valid values
421 BOOST_CHECK(ParseUInt64("1234", nullptr));
422 BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL);
423 BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL);
424 BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal
425 BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL);
426 BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL);
427 BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL);
428 BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL);
429 // Invalid values
430 BOOST_CHECK(!ParseUInt64("", &n));
431 BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside
432 BOOST_CHECK(!ParseUInt64(" -1", &n));
433 BOOST_CHECK(!ParseUInt64("1 ", &n));
434 BOOST_CHECK(!ParseUInt64("1a", &n));
435 BOOST_CHECK(!ParseUInt64("aap", &n));
436 BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
437 const char test_bytes[] = {'1', 0, '1'};
438 std::string teststr(test_bytes, sizeof(test_bytes));
439 BOOST_CHECK(!ParseUInt64(teststr, &n)); // no embedded NULs
440 // Overflow and underflow
441 BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr));
442 BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr));
443 BOOST_CHECK(!ParseUInt64("-32482348723847471234", nullptr));
444 BOOST_CHECK(!ParseUInt64("-2147483648", &n));
445 BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n));
446 BOOST_CHECK(!ParseUInt64("-1234", &n));
449 BOOST_AUTO_TEST_CASE(test_ParseDouble)
451 double n;
452 // Valid values
453 BOOST_CHECK(ParseDouble("1234", nullptr));
454 BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
455 BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
456 BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
457 BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
458 BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
459 BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
460 BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
461 BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
462 // Invalid values
463 BOOST_CHECK(!ParseDouble("", &n));
464 BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
465 BOOST_CHECK(!ParseDouble("1 ", &n));
466 BOOST_CHECK(!ParseDouble("1a", &n));
467 BOOST_CHECK(!ParseDouble("aap", &n));
468 BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
469 const char test_bytes[] = {'1', 0, '1'};
470 std::string teststr(test_bytes, sizeof(test_bytes));
471 BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs
472 // Overflow and underflow
473 BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
474 BOOST_CHECK(!ParseDouble("1e10000", nullptr));
477 BOOST_AUTO_TEST_CASE(test_FormatParagraph)
479 BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
480 BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
481 BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
482 BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
483 BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
484 BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
485 BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test");
487 // Make sure we don't indent a fully-new line following a too-long line ending
488 BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n test\nabc");
490 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");
492 // Test wrap length is exact
493 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");
494 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");
495 // Indent should be included in length of lines
496 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");
498 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.");
499 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.");
500 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.");
501 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.");
504 BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
506 std::vector<std::string> comments;
507 comments.push_back(std::string("comment1"));
508 std::vector<std::string> comments2;
509 comments2.push_back(std::string("comment1"));
510 comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
511 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:0.9.99/"));
512 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
513 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));
516 BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
518 int64_t amount = 0;
519 BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
520 BOOST_CHECK_EQUAL(amount, 0LL);
521 BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
522 BOOST_CHECK_EQUAL(amount, 100000000LL);
523 BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
524 BOOST_CHECK_EQUAL(amount, 0LL);
525 BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
526 BOOST_CHECK_EQUAL(amount, -10000000LL);
527 BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
528 BOOST_CHECK_EQUAL(amount, 110000000LL);
529 BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
530 BOOST_CHECK_EQUAL(amount, 110000000LL);
531 BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
532 BOOST_CHECK_EQUAL(amount, 1100000000LL);
533 BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
534 BOOST_CHECK_EQUAL(amount, 11000000LL);
535 BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
536 BOOST_CHECK_EQUAL(amount, 100000000000LL);
537 BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
538 BOOST_CHECK_EQUAL(amount, -100000000000LL);
539 BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
540 BOOST_CHECK_EQUAL(amount, 1LL);
541 BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
542 BOOST_CHECK_EQUAL(amount, 1LL);
543 BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
544 BOOST_CHECK_EQUAL(amount, -1LL);
545 BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
546 BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
547 BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
548 BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
549 BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
550 BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
552 BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
553 BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
554 BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
555 BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
556 BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
557 BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
558 BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
559 BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
560 BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
561 BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
562 BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
563 BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
564 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
565 BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
566 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
567 BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
568 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
569 BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
570 BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
571 BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
572 BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
573 BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
574 BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
575 BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
576 BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
577 BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
578 BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
581 BOOST_AUTO_TEST_SUITE_END()