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