Merge #9240: Remove txConflicted
[bitcoinplatinum.git] / src / test / util_tests.cpp
blobbad72ffc0fc5fbef7a67ccecda42134db3affc1a
1 // Copyright (c) 2011-2015 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"
13 #include "test/test_random.h"
15 #include <stdint.h>
16 #include <vector>
18 #include <boost/test/unit_test.hpp>
20 using namespace std;
22 BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
24 BOOST_AUTO_TEST_CASE(util_criticalsection)
26 CCriticalSection cs;
28 do {
29 LOCK(cs);
30 break;
32 BOOST_ERROR("break was swallowed!");
33 } while(0);
35 do {
36 TRY_LOCK(cs, lockTest);
37 if (lockTest)
38 break;
40 BOOST_ERROR("break was swallowed!");
41 } while(0);
44 static const unsigned char ParseHex_expected[65] = {
45 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
46 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
47 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
48 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
49 0x5f
51 BOOST_AUTO_TEST_CASE(util_ParseHex)
53 std::vector<unsigned char> result;
54 std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
55 // Basic test vector
56 result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
57 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
59 // Spaces between bytes must be supported
60 result = ParseHex("12 34 56 78");
61 BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
63 // Leading space must be supported (used in CDBEnv::Salvage)
64 result = ParseHex(" 89 34 56 78");
65 BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
67 // Stop parsing at invalid value
68 result = ParseHex("1234 invalid 1234");
69 BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
72 BOOST_AUTO_TEST_CASE(util_HexStr)
74 BOOST_CHECK_EQUAL(
75 HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
76 "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
78 BOOST_CHECK_EQUAL(
79 HexStr(ParseHex_expected, ParseHex_expected + 5, true),
80 "04 67 8a fd b0");
82 BOOST_CHECK_EQUAL(
83 HexStr(ParseHex_expected, ParseHex_expected, true),
84 "");
86 std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
88 BOOST_CHECK_EQUAL(
89 HexStr(ParseHex_vec, true),
90 "04 67 8a fd b0");
94 BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
96 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), "1970-01-01 00:00:00");
97 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), "2038-01-19 03:14:07");
98 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17");
99 BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), "2011-09-30 23:36");
100 BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000");
103 BOOST_AUTO_TEST_CASE(util_ParseParameters)
105 const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
107 ParseParameters(0, (char**)argv_test);
108 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
110 ParseParameters(1, (char**)argv_test);
111 BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
113 ParseParameters(5, (char**)argv_test);
114 // expectation: -ignored is ignored (program name argument),
115 // -a, -b and -ccc end up in map, -d ignored because it is after
116 // a non-option argument (non-GNU option parsing)
117 BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
118 BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc")
119 && !mapArgs.count("f") && !mapArgs.count("-d"));
120 BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
121 && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
123 BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
124 BOOST_CHECK(mapMultiArgs["-ccc"].size() == 2);
127 BOOST_AUTO_TEST_CASE(util_GetArg)
129 mapArgs.clear();
130 mapArgs["strtest1"] = "string...";
131 // strtest2 undefined on purpose
132 mapArgs["inttest1"] = "12345";
133 mapArgs["inttest2"] = "81985529216486895";
134 // inttest3 undefined on purpose
135 mapArgs["booltest1"] = "";
136 // booltest2 undefined on purpose
137 mapArgs["booltest3"] = "0";
138 mapArgs["booltest4"] = "1";
140 BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
141 BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
142 BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
143 BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
144 BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
145 BOOST_CHECK_EQUAL(GetBoolArg("booltest1", false), true);
146 BOOST_CHECK_EQUAL(GetBoolArg("booltest2", false), false);
147 BOOST_CHECK_EQUAL(GetBoolArg("booltest3", false), false);
148 BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true);
151 BOOST_AUTO_TEST_CASE(util_FormatMoney)
153 BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
154 BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789), "12345.6789");
155 BOOST_CHECK_EQUAL(FormatMoney(-COIN), "-1.00");
157 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000), "100000000.00");
158 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000), "10000000.00");
159 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000), "1000000.00");
160 BOOST_CHECK_EQUAL(FormatMoney(COIN*100000), "100000.00");
161 BOOST_CHECK_EQUAL(FormatMoney(COIN*10000), "10000.00");
162 BOOST_CHECK_EQUAL(FormatMoney(COIN*1000), "1000.00");
163 BOOST_CHECK_EQUAL(FormatMoney(COIN*100), "100.00");
164 BOOST_CHECK_EQUAL(FormatMoney(COIN*10), "10.00");
165 BOOST_CHECK_EQUAL(FormatMoney(COIN), "1.00");
166 BOOST_CHECK_EQUAL(FormatMoney(COIN/10), "0.10");
167 BOOST_CHECK_EQUAL(FormatMoney(COIN/100), "0.01");
168 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000), "0.001");
169 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000), "0.0001");
170 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000), "0.00001");
171 BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
172 BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
173 BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
176 BOOST_AUTO_TEST_CASE(util_ParseMoney)
178 CAmount ret = 0;
179 BOOST_CHECK(ParseMoney("0.0", ret));
180 BOOST_CHECK_EQUAL(ret, 0);
182 BOOST_CHECK(ParseMoney("12345.6789", ret));
183 BOOST_CHECK_EQUAL(ret, (COIN/10000)*123456789);
185 BOOST_CHECK(ParseMoney("100000000.00", ret));
186 BOOST_CHECK_EQUAL(ret, COIN*100000000);
187 BOOST_CHECK(ParseMoney("10000000.00", ret));
188 BOOST_CHECK_EQUAL(ret, COIN*10000000);
189 BOOST_CHECK(ParseMoney("1000000.00", ret));
190 BOOST_CHECK_EQUAL(ret, COIN*1000000);
191 BOOST_CHECK(ParseMoney("100000.00", ret));
192 BOOST_CHECK_EQUAL(ret, COIN*100000);
193 BOOST_CHECK(ParseMoney("10000.00", ret));
194 BOOST_CHECK_EQUAL(ret, COIN*10000);
195 BOOST_CHECK(ParseMoney("1000.00", ret));
196 BOOST_CHECK_EQUAL(ret, COIN*1000);
197 BOOST_CHECK(ParseMoney("100.00", ret));
198 BOOST_CHECK_EQUAL(ret, COIN*100);
199 BOOST_CHECK(ParseMoney("10.00", ret));
200 BOOST_CHECK_EQUAL(ret, COIN*10);
201 BOOST_CHECK(ParseMoney("1.00", ret));
202 BOOST_CHECK_EQUAL(ret, COIN);
203 BOOST_CHECK(ParseMoney("1", ret));
204 BOOST_CHECK_EQUAL(ret, COIN);
205 BOOST_CHECK(ParseMoney("0.1", ret));
206 BOOST_CHECK_EQUAL(ret, COIN/10);
207 BOOST_CHECK(ParseMoney("0.01", ret));
208 BOOST_CHECK_EQUAL(ret, COIN/100);
209 BOOST_CHECK(ParseMoney("0.001", ret));
210 BOOST_CHECK_EQUAL(ret, COIN/1000);
211 BOOST_CHECK(ParseMoney("0.0001", ret));
212 BOOST_CHECK_EQUAL(ret, COIN/10000);
213 BOOST_CHECK(ParseMoney("0.00001", ret));
214 BOOST_CHECK_EQUAL(ret, COIN/100000);
215 BOOST_CHECK(ParseMoney("0.000001", ret));
216 BOOST_CHECK_EQUAL(ret, COIN/1000000);
217 BOOST_CHECK(ParseMoney("0.0000001", ret));
218 BOOST_CHECK_EQUAL(ret, COIN/10000000);
219 BOOST_CHECK(ParseMoney("0.00000001", ret));
220 BOOST_CHECK_EQUAL(ret, COIN/100000000);
222 // Attempted 63 bit overflow should fail
223 BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
225 // Parsing negative amounts must fail
226 BOOST_CHECK(!ParseMoney("-1", ret));
229 BOOST_AUTO_TEST_CASE(util_IsHex)
231 BOOST_CHECK(IsHex("00"));
232 BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
233 BOOST_CHECK(IsHex("ff"));
234 BOOST_CHECK(IsHex("FF"));
236 BOOST_CHECK(!IsHex(""));
237 BOOST_CHECK(!IsHex("0"));
238 BOOST_CHECK(!IsHex("a"));
239 BOOST_CHECK(!IsHex("eleven"));
240 BOOST_CHECK(!IsHex("00xx00"));
241 BOOST_CHECK(!IsHex("0x0000"));
244 BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
246 seed_insecure_rand(true);
247 for (int mod=2;mod<11;mod++)
249 int mask = 1;
250 // Really rough binomal confidence approximation.
251 int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
252 //mask is 2^ceil(log2(mod))-1
253 while(mask<mod-1)mask=(mask<<1)+1;
255 int count = 0;
256 //How often does it get a zero from the uniform range [0,mod)?
257 for (int i = 0; i < 10000; i++) {
258 uint32_t rval;
260 rval=insecure_rand()&mask;
261 }while(rval>=(uint32_t)mod);
262 count += rval==0;
264 BOOST_CHECK(count<=10000/mod+err);
265 BOOST_CHECK(count>=10000/mod-err);
269 BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
271 BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
272 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
273 BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
274 BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
275 BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
276 BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
277 BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
280 /* Test strprintf formatting directives.
281 * Put a string before and after to ensure sanity of element sizes on stack. */
282 #define B "check_prefix"
283 #define E "check_postfix"
284 BOOST_AUTO_TEST_CASE(strprintf_numbers)
286 int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
287 uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
288 BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
289 BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
290 BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
292 size_t st = 12345678; /* unsigned size_t test value */
293 ssize_t sst = -12345678; /* signed size_t test value */
294 BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
295 BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
296 BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
298 ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
299 ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
300 BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
301 BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
302 BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
304 #undef B
305 #undef E
307 /* Check for mingw/wine issue #3494
308 * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106'
310 BOOST_AUTO_TEST_CASE(gettime)
312 BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
315 BOOST_AUTO_TEST_CASE(test_ParseInt32)
317 int32_t n;
318 // Valid values
319 BOOST_CHECK(ParseInt32("1234", NULL));
320 BOOST_CHECK(ParseInt32("0", &n) && n == 0);
321 BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
322 BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
323 BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
324 BOOST_CHECK(ParseInt32("-2147483648", &n) && n == -2147483648);
325 BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
326 // Invalid values
327 BOOST_CHECK(!ParseInt32("", &n));
328 BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
329 BOOST_CHECK(!ParseInt32("1 ", &n));
330 BOOST_CHECK(!ParseInt32("1a", &n));
331 BOOST_CHECK(!ParseInt32("aap", &n));
332 BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
333 BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
334 const char test_bytes[] = {'1', 0, '1'};
335 std::string teststr(test_bytes, sizeof(test_bytes));
336 BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs
337 // Overflow and underflow
338 BOOST_CHECK(!ParseInt32("-2147483649", NULL));
339 BOOST_CHECK(!ParseInt32("2147483648", NULL));
340 BOOST_CHECK(!ParseInt32("-32482348723847471234", NULL));
341 BOOST_CHECK(!ParseInt32("32482348723847471234", NULL));
344 BOOST_AUTO_TEST_CASE(test_ParseInt64)
346 int64_t n;
347 // Valid values
348 BOOST_CHECK(ParseInt64("1234", NULL));
349 BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
350 BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
351 BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
352 BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
353 BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
354 BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == (int64_t)9223372036854775807);
355 BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == (int64_t)-9223372036854775807-1);
356 BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
357 // Invalid values
358 BOOST_CHECK(!ParseInt64("", &n));
359 BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
360 BOOST_CHECK(!ParseInt64("1 ", &n));
361 BOOST_CHECK(!ParseInt64("1a", &n));
362 BOOST_CHECK(!ParseInt64("aap", &n));
363 BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
364 const char test_bytes[] = {'1', 0, '1'};
365 std::string teststr(test_bytes, sizeof(test_bytes));
366 BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs
367 // Overflow and underflow
368 BOOST_CHECK(!ParseInt64("-9223372036854775809", NULL));
369 BOOST_CHECK(!ParseInt64("9223372036854775808", NULL));
370 BOOST_CHECK(!ParseInt64("-32482348723847471234", NULL));
371 BOOST_CHECK(!ParseInt64("32482348723847471234", NULL));
374 BOOST_AUTO_TEST_CASE(test_ParseUInt32)
376 uint32_t n;
377 // Valid values
378 BOOST_CHECK(ParseUInt32("1234", NULL));
379 BOOST_CHECK(ParseUInt32("0", &n) && n == 0);
380 BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234);
381 BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal
382 BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647);
383 BOOST_CHECK(ParseUInt32("2147483648", &n) && n == (uint32_t)2147483648);
384 BOOST_CHECK(ParseUInt32("4294967295", &n) && n == (uint32_t)4294967295);
385 // Invalid values
386 BOOST_CHECK(!ParseUInt32("", &n));
387 BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside
388 BOOST_CHECK(!ParseUInt32(" -1", &n));
389 BOOST_CHECK(!ParseUInt32("1 ", &n));
390 BOOST_CHECK(!ParseUInt32("1a", &n));
391 BOOST_CHECK(!ParseUInt32("aap", &n));
392 BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
393 BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
394 const char test_bytes[] = {'1', 0, '1'};
395 std::string teststr(test_bytes, sizeof(test_bytes));
396 BOOST_CHECK(!ParseUInt32(teststr, &n)); // no embedded NULs
397 // Overflow and underflow
398 BOOST_CHECK(!ParseUInt32("-2147483648", &n));
399 BOOST_CHECK(!ParseUInt32("4294967296", &n));
400 BOOST_CHECK(!ParseUInt32("-1234", &n));
401 BOOST_CHECK(!ParseUInt32("-32482348723847471234", NULL));
402 BOOST_CHECK(!ParseUInt32("32482348723847471234", NULL));
405 BOOST_AUTO_TEST_CASE(test_ParseUInt64)
407 uint64_t n;
408 // Valid values
409 BOOST_CHECK(ParseUInt64("1234", NULL));
410 BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL);
411 BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL);
412 BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal
413 BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL);
414 BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL);
415 BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL);
416 BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL);
417 // Invalid values
418 BOOST_CHECK(!ParseUInt64("", &n));
419 BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside
420 BOOST_CHECK(!ParseUInt64(" -1", &n));
421 BOOST_CHECK(!ParseUInt64("1 ", &n));
422 BOOST_CHECK(!ParseUInt64("1a", &n));
423 BOOST_CHECK(!ParseUInt64("aap", &n));
424 BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
425 const char test_bytes[] = {'1', 0, '1'};
426 std::string teststr(test_bytes, sizeof(test_bytes));
427 BOOST_CHECK(!ParseUInt64(teststr, &n)); // no embedded NULs
428 // Overflow and underflow
429 BOOST_CHECK(!ParseUInt64("-9223372036854775809", NULL));
430 BOOST_CHECK(!ParseUInt64("18446744073709551616", NULL));
431 BOOST_CHECK(!ParseUInt64("-32482348723847471234", NULL));
432 BOOST_CHECK(!ParseUInt64("-2147483648", &n));
433 BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n));
434 BOOST_CHECK(!ParseUInt64("-1234", &n));
437 BOOST_AUTO_TEST_CASE(test_ParseDouble)
439 double n;
440 // Valid values
441 BOOST_CHECK(ParseDouble("1234", NULL));
442 BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
443 BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
444 BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
445 BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
446 BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
447 BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
448 BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
449 BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
450 // Invalid values
451 BOOST_CHECK(!ParseDouble("", &n));
452 BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
453 BOOST_CHECK(!ParseDouble("1 ", &n));
454 BOOST_CHECK(!ParseDouble("1a", &n));
455 BOOST_CHECK(!ParseDouble("aap", &n));
456 BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
457 const char test_bytes[] = {'1', 0, '1'};
458 std::string teststr(test_bytes, sizeof(test_bytes));
459 BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs
460 // Overflow and underflow
461 BOOST_CHECK(!ParseDouble("-1e10000", NULL));
462 BOOST_CHECK(!ParseDouble("1e10000", NULL));
465 BOOST_AUTO_TEST_CASE(test_FormatParagraph)
467 BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
468 BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
469 BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
470 BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
471 BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
472 BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
473 BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test");
475 // Make sure we don't indent a fully-new line following a too-long line ending
476 BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n test\nabc");
478 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");
480 // Test wrap length is exact
481 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");
482 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");
483 // Indent should be included in length of lines
484 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");
486 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.");
487 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.");
488 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.");
489 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.");
492 BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
494 std::vector<std::string> comments;
495 comments.push_back(std::string("comment1"));
496 std::vector<std::string> comments2;
497 comments2.push_back(std::string("comment1"));
498 comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
499 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:0.9.99/"));
500 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
501 BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));
504 BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
506 int64_t amount = 0;
507 BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
508 BOOST_CHECK_EQUAL(amount, 0LL);
509 BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
510 BOOST_CHECK_EQUAL(amount, 100000000LL);
511 BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
512 BOOST_CHECK_EQUAL(amount, 0LL);
513 BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
514 BOOST_CHECK_EQUAL(amount, -10000000LL);
515 BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
516 BOOST_CHECK_EQUAL(amount, 110000000LL);
517 BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
518 BOOST_CHECK_EQUAL(amount, 110000000LL);
519 BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
520 BOOST_CHECK_EQUAL(amount, 1100000000LL);
521 BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
522 BOOST_CHECK_EQUAL(amount, 11000000LL);
523 BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
524 BOOST_CHECK_EQUAL(amount, 100000000000LL);
525 BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
526 BOOST_CHECK_EQUAL(amount, -100000000000LL);
527 BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
528 BOOST_CHECK_EQUAL(amount, 1LL);
529 BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
530 BOOST_CHECK_EQUAL(amount, 1LL);
531 BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
532 BOOST_CHECK_EQUAL(amount, -1LL);
533 BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
534 BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
535 BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
536 BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
537 BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
538 BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
540 BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
541 BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
542 BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
543 BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
544 BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
545 BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
546 BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
547 BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
548 BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
549 BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
550 BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
551 BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
552 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
553 BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
554 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
555 BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
556 BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
557 BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
558 BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
559 BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
560 BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
561 BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
562 BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
563 BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
564 BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
565 BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
566 BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
569 BOOST_AUTO_TEST_SUITE_END()