Bitcoin Core 22.99.0
P2P Digital Currency
arith_uint256_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 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.
4
5#include <arith_uint256.h>
6#include <uint256.h>
7
8#include <boost/test/unit_test.hpp>
9
10#include <cmath>
11#include <cstdint>
12#include <iomanip>
13#include <limits>
14#include <sstream>
15#include <string>
16#include <vector>
17
18BOOST_AUTO_TEST_SUITE(arith_uint256_tests)
19
20
21static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
22{
23 return UintToArith256(uint256(vch));
24}
25
26const unsigned char R1Array[] =
27 "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
28 "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
29const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
30const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
31const arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32));
32const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
33
34const unsigned char R2Array[] =
35 "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
36 "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
37const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));
38
39const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
40
41const unsigned char ZeroArray[] =
42 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
43 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
44const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
45
46const unsigned char OneArray[] =
47 "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
48 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
49const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32));
50
51const unsigned char MaxArray[] =
52 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
53 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
54const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32));
55
56const arith_uint256 HalfL = (OneL << 255);
57static std::string ArrayToString(const unsigned char A[], unsigned int width)
58{
59 std::stringstream Stream;
60 Stream << std::hex;
61 for (unsigned int i = 0; i < width; ++i)
62 {
63 Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
64 }
65 return Stream.str();
66}
67
68BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
69{
70 BOOST_CHECK(1 == 0+1);
71 // constructor arith_uint256(vector<char>):
78
79 // == and !=
85 BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
86
87 uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
88 for (unsigned int i = 0; i < 256; ++i)
89 {
90 BOOST_CHECK(ZeroL != (OneL << i));
91 BOOST_CHECK((OneL << i) != ZeroL);
92 BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
93 BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
94 }
95 BOOST_CHECK(ZeroL == (OneL << 256));
96
97 // String Constructor and Copy Constructor
104 BOOST_CHECK(arith_uint256(" 0x"+R1L.ToString()+" ") == R1L);
111
112 // uint64_t constructor
113 BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
116 BOOST_CHECK(arith_uint256("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL));
117
118 // Assignment (from base_uint)
119 arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
120 tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
121 tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
122 tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
123 tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
124}
125
126static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
127{
128 for (unsigned int T=0; T < arrayLength; ++T)
129 {
130 unsigned int F = (T+bitsToShift/8);
131 if (F < arrayLength)
132 to[T] = from[F] >> (bitsToShift%8);
133 else
134 to[T] = 0;
135 if (F + 1 < arrayLength)
136 to[T] |= from[(F+1)] << (8-bitsToShift%8);
137 }
138}
139
140static void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
141{
142 for (unsigned int T=0; T < arrayLength; ++T)
143 {
144 if (T >= bitsToShift/8)
145 {
146 unsigned int F = T-bitsToShift/8;
147 to[T] = from[F] << (bitsToShift%8);
148 if (T >= bitsToShift/8+1)
149 to[T] |= from[F-1] >> (8-bitsToShift%8);
150 }
151 else {
152 to[T] = 0;
153 }
154 }
155}
156
157BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
158 unsigned char TmpArray[32];
159 arith_uint256 TmpL;
160 for (unsigned int i = 0; i < 256; ++i)
161 {
162 shiftArrayLeft(TmpArray, OneArray, 32, i);
163 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
164 TmpL = OneL; TmpL <<= i;
165 BOOST_CHECK(TmpL == (OneL << i));
166 BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
167 TmpL = HalfL; TmpL >>= (255-i);
168 BOOST_CHECK(TmpL == (OneL << i));
169
170 shiftArrayLeft(TmpArray, R1Array, 32, i);
171 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
172 TmpL = R1L; TmpL <<= i;
173 BOOST_CHECK(TmpL == (R1L << i));
174
175 shiftArrayRight(TmpArray, R1Array, 32, i);
176 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
177 TmpL = R1L; TmpL >>= i;
178 BOOST_CHECK(TmpL == (R1L >> i));
179
180 shiftArrayLeft(TmpArray, MaxArray, 32, i);
181 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
182 TmpL = MaxL; TmpL <<= i;
183 BOOST_CHECK(TmpL == (MaxL << i));
184
185 shiftArrayRight(TmpArray, MaxArray, 32, i);
186 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
187 TmpL = MaxL; TmpL >>= i;
188 BOOST_CHECK(TmpL == (MaxL >> i));
189 }
190 arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
191 arith_uint256 c2L = c1L << 128;
192 for (unsigned int i = 0; i < 128; ++i) {
193 BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
194 }
195 for (unsigned int i = 128; i < 256; ++i) {
196 BOOST_CHECK((c1L << i) == (c2L << (i-128)));
197 }
198}
199
200BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
201{
203
204 unsigned char TmpArray[32];
205 for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
206 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
207
209 BOOST_CHECK(-R1L == (~R1L)+1);
210 for (unsigned int i = 0; i < 256; ++i)
211 BOOST_CHECK(-(OneL<<i) == (MaxL << i));
212}
213
214
215// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
216// element of Aarray and Barray, and then converting the result into an arith_uint256.
217#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
218 for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
219 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
220
221#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
222 TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
223
224BOOST_AUTO_TEST_CASE( bitwiseOperators )
225{
226 unsigned char TmpArray[32];
227
228 CHECKBITWISEOPERATOR(R1,R2,|)
229 CHECKBITWISEOPERATOR(R1,R2,^)
230 CHECKBITWISEOPERATOR(R1,R2,&)
231 CHECKBITWISEOPERATOR(R1,Zero,|)
232 CHECKBITWISEOPERATOR(R1,Zero,^)
233 CHECKBITWISEOPERATOR(R1,Zero,&)
234 CHECKBITWISEOPERATOR(R1,Max,|)
235 CHECKBITWISEOPERATOR(R1,Max,^)
236 CHECKBITWISEOPERATOR(R1,Max,&)
237 CHECKBITWISEOPERATOR(Zero,R1,|)
238 CHECKBITWISEOPERATOR(Zero,R1,^)
239 CHECKBITWISEOPERATOR(Zero,R1,&)
240 CHECKBITWISEOPERATOR(Max,R1,|)
241 CHECKBITWISEOPERATOR(Max,R1,^)
242 CHECKBITWISEOPERATOR(Max,R1,&)
243
244 arith_uint256 TmpL;
248 CHECKASSIGNMENTOPERATOR(R1,Zero,|)
249 CHECKASSIGNMENTOPERATOR(R1,Zero,^)
250 CHECKASSIGNMENTOPERATOR(R1,Zero,&)
254 CHECKASSIGNMENTOPERATOR(Zero,R1,|)
255 CHECKASSIGNMENTOPERATOR(Zero,R1,^)
256 CHECKASSIGNMENTOPERATOR(Zero,R1,&)
260
261 uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
262 TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
263 TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
264 TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
265 TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
266}
267
268BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
269{
270 arith_uint256 TmpL;
271 for (unsigned int i = 0; i < 256; ++i) {
272 TmpL= OneL<< i;
273 BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
274 BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
275 TmpL |= R1L;
276 BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
277 BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
278 BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
279 }
280}
281
283{
284 arith_uint256 TmpL = 0;
286 TmpL += R1L;
287 BOOST_CHECK(TmpL == R1L);
288 TmpL += R2L;
289 BOOST_CHECK(TmpL == R1L + R2L);
292 for (unsigned int i = 1; i < 256; ++i) {
293 BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
294 BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
295 TmpL = (MaxL>>i); TmpL += OneL;
296 BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
297 TmpL = (MaxL>>i); TmpL += 1;
298 BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
299 TmpL = (MaxL>>i);
300 BOOST_CHECK( TmpL++ == (MaxL>>i) );
301 BOOST_CHECK( TmpL == (HalfL >> (i-1)));
302 }
303 BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
304 TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
305 BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
306 TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
307 TmpL = R1L;
308 BOOST_CHECK(++TmpL == R1L+1);
309
310 BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
311 BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
312 BOOST_CHECK(R1L - OneL == R1L+(-OneL));
313 for (unsigned int i = 1; i < 256; ++i) {
314 BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1)));
315 BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
316 TmpL = (HalfL >> (i-1));
317 BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
318 BOOST_CHECK(TmpL == (MaxL >> i));
319 TmpL = (HalfL >> (i-1));
320 BOOST_CHECK(--TmpL == (MaxL >> i));
321 }
322 TmpL = R1L;
323 BOOST_CHECK(--TmpL == R1L-1);
324}
325
327{
328 BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
329 BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
330 BOOST_CHECK((R1L * ZeroL) == ZeroL);
331 BOOST_CHECK((R1L * OneL) == R1L);
332 BOOST_CHECK((R1L * MaxL) == -R1L);
333 BOOST_CHECK((R2L * R1L) == (R1L * R2L));
334 BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
335 BOOST_CHECK((R2L * ZeroL) == ZeroL);
336 BOOST_CHECK((R2L * OneL) == R2L);
337 BOOST_CHECK((R2L * MaxL) == -R2L);
338
340
341 BOOST_CHECK((R1L * 0) == 0);
342 BOOST_CHECK((R1L * 1) == R1L);
343 BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
344 BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
345}
346
348{
349 arith_uint256 D1L("AD7133AC1977FA2B7");
350 arith_uint256 D2L("ECD751716");
351 BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
352 BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
353 BOOST_CHECK(R1L / OneL == R1L);
355 BOOST_CHECK(MaxL / R1L == 2);
357 BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
358 BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
359 BOOST_CHECK(R2L / OneL == R2L);
361 BOOST_CHECK(MaxL / R2L == 1);
363}
364
365
366static bool almostEqual(double d1, double d2)
367{
368 return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
369}
370
371BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize
372{
377 arith_uint256 TmpL(R1L);
378 BOOST_CHECK(TmpL == R1L);
379 TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
380 TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
381 TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL);
382
383 TmpL.SetHex(R1L.ToString());
384 BOOST_CHECK(R1L.size() == 32);
385 BOOST_CHECK(R2L.size() == 32);
386 BOOST_CHECK(ZeroL.size() == 32);
387 BOOST_CHECK(MaxL.size() == 32);
389 BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
390 BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
391
392 for (unsigned int i = 0; i < 255; ++i)
393 {
394 BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
395 }
396 BOOST_CHECK(ZeroL.getdouble() == 0.0);
397 for (int i = 256; i > 53; --i)
398 BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
399 uint64_t R1L64part = (R1L>>192).GetLow64();
400 for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
401 {
402 BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
403 }
404}
405
406BOOST_AUTO_TEST_CASE(bignum_SetCompact)
407{
408 arith_uint256 num;
409 bool fNegative;
410 bool fOverflow;
411 num.SetCompact(0, &fNegative, &fOverflow);
412 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
413 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
414 BOOST_CHECK_EQUAL(fNegative, false);
415 BOOST_CHECK_EQUAL(fOverflow, false);
416
417 num.SetCompact(0x00123456, &fNegative, &fOverflow);
418 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
419 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
420 BOOST_CHECK_EQUAL(fNegative, false);
421 BOOST_CHECK_EQUAL(fOverflow, false);
422
423 num.SetCompact(0x01003456, &fNegative, &fOverflow);
424 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
425 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
426 BOOST_CHECK_EQUAL(fNegative, false);
427 BOOST_CHECK_EQUAL(fOverflow, false);
428
429 num.SetCompact(0x02000056, &fNegative, &fOverflow);
430 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
431 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
432 BOOST_CHECK_EQUAL(fNegative, false);
433 BOOST_CHECK_EQUAL(fOverflow, false);
434
435 num.SetCompact(0x03000000, &fNegative, &fOverflow);
436 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
437 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
438 BOOST_CHECK_EQUAL(fNegative, false);
439 BOOST_CHECK_EQUAL(fOverflow, false);
440
441 num.SetCompact(0x04000000, &fNegative, &fOverflow);
442 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
443 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
444 BOOST_CHECK_EQUAL(fNegative, false);
445 BOOST_CHECK_EQUAL(fOverflow, false);
446
447 num.SetCompact(0x00923456, &fNegative, &fOverflow);
448 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
449 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
450 BOOST_CHECK_EQUAL(fNegative, false);
451 BOOST_CHECK_EQUAL(fOverflow, false);
452
453 num.SetCompact(0x01803456, &fNegative, &fOverflow);
454 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
455 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
456 BOOST_CHECK_EQUAL(fNegative, false);
457 BOOST_CHECK_EQUAL(fOverflow, false);
458
459 num.SetCompact(0x02800056, &fNegative, &fOverflow);
460 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
461 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
462 BOOST_CHECK_EQUAL(fNegative, false);
463 BOOST_CHECK_EQUAL(fOverflow, false);
464
465 num.SetCompact(0x03800000, &fNegative, &fOverflow);
466 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
467 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
468 BOOST_CHECK_EQUAL(fNegative, false);
469 BOOST_CHECK_EQUAL(fOverflow, false);
470
471 num.SetCompact(0x04800000, &fNegative, &fOverflow);
472 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
473 BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
474 BOOST_CHECK_EQUAL(fNegative, false);
475 BOOST_CHECK_EQUAL(fOverflow, false);
476
477 num.SetCompact(0x01123456, &fNegative, &fOverflow);
478 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
479 BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
480 BOOST_CHECK_EQUAL(fNegative, false);
481 BOOST_CHECK_EQUAL(fOverflow, false);
482
483 // Make sure that we don't generate compacts with the 0x00800000 bit set
484 num = 0x80;
485 BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
486
487 num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
488 BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
489 BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
490 BOOST_CHECK_EQUAL(fNegative, true);
491 BOOST_CHECK_EQUAL(fOverflow, false);
492
493 num.SetCompact(0x02123456, &fNegative, &fOverflow);
494 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
495 BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
496 BOOST_CHECK_EQUAL(fNegative, false);
497 BOOST_CHECK_EQUAL(fOverflow, false);
498
499 num.SetCompact(0x03123456, &fNegative, &fOverflow);
500 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
501 BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
502 BOOST_CHECK_EQUAL(fNegative, false);
503 BOOST_CHECK_EQUAL(fOverflow, false);
504
505 num.SetCompact(0x04123456, &fNegative, &fOverflow);
506 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
507 BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
508 BOOST_CHECK_EQUAL(fNegative, false);
509 BOOST_CHECK_EQUAL(fOverflow, false);
510
511 num.SetCompact(0x04923456, &fNegative, &fOverflow);
512 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
513 BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
514 BOOST_CHECK_EQUAL(fNegative, true);
515 BOOST_CHECK_EQUAL(fOverflow, false);
516
517 num.SetCompact(0x05009234, &fNegative, &fOverflow);
518 BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
519 BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
520 BOOST_CHECK_EQUAL(fNegative, false);
521 BOOST_CHECK_EQUAL(fOverflow, false);
522
523 num.SetCompact(0x20123456, &fNegative, &fOverflow);
524 BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
525 BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
526 BOOST_CHECK_EQUAL(fNegative, false);
527 BOOST_CHECK_EQUAL(fOverflow, false);
528
529 num.SetCompact(0xff123456, &fNegative, &fOverflow);
530 BOOST_CHECK_EQUAL(fNegative, false);
531 BOOST_CHECK_EQUAL(fOverflow, true);
532}
533
534
535BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
536{
537 // ~R1L give a base_uint<256>
538 BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
539 BOOST_CHECK((~~R1L << 10) == (R1L << 10));
540 BOOST_CHECK(!(~~R1L < R1L));
541 BOOST_CHECK(~~R1L <= R1L);
542 BOOST_CHECK(!(~~R1L > R1L));
543 BOOST_CHECK(~~R1L >= R1L);
544 BOOST_CHECK(!(R1L < ~~R1L));
545 BOOST_CHECK(R1L <= ~~R1L);
546 BOOST_CHECK(!(R1L > ~~R1L));
547 BOOST_CHECK(R1L >= ~~R1L);
548
549 BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
550 BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
552 unsigned char TmpArray[32];
553 CHECKBITWISEOPERATOR(~R1,R2,|)
554 CHECKBITWISEOPERATOR(~R1,R2,^)
555 CHECKBITWISEOPERATOR(~R1,R2,&)
556 CHECKBITWISEOPERATOR(R1,~R2,|)
557 CHECKBITWISEOPERATOR(R1,~R2,^)
558 CHECKBITWISEOPERATOR(R1,~R2,&)
559}
560
arith_uint256 UintToArith256(const uint256 &a)
static void shiftArrayLeft(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
static void shiftArrayRight(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
const arith_uint256 OneL
#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_)
const arith_uint256 MaxL
#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_)
const arith_uint256 R1L
const unsigned char ZeroArray[]
const char R1LplusR2L[]
const unsigned char R1Array[]
const uint64_t R1LLow64
BOOST_AUTO_TEST_CASE(basics)
const arith_uint256 HalfL
const unsigned char OneArray[]
const char R1ArrayHex[]
static bool almostEqual(double d1, double d2)
const unsigned char R2Array[]
const arith_uint256 R2L
const double R1Ldouble
static std::string ArrayToString(const unsigned char A[], unsigned int width)
static arith_uint256 arith_uint256V(const std::vector< unsigned char > &vch)
Convert vector to arith_uint256, via uint256 blob.
const unsigned char MaxArray[]
const arith_uint256 ZeroL
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
unsigned int size() const
double getdouble() const
std::string ToString() const
uint64_t GetLow64() const
void SetHex(const char *psz)
std::string GetHex() const
256-bit opaque blob.
Definition: uint256.h:124
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define T(expected, seed, data)
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87