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