PIVX Core  5.6.99
P2P Digital Currency
arith_uint256_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Copyright (c) 2017-2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <boost/test/unit_test.hpp>
7 #include <stdint.h>
8 #include <sstream>
9 #include <iomanip>
10 #include <limits>
11 #include <cmath>
12 #include "uint256.h"
13 #include "arith_uint256.h"
14 #include <string>
15 #include "version.h"
16 #include "test/test_pivx.h"
17 
18 BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup)
19 
20 inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
22 {
23  return UintToArith256(uint256(vch));
24 }
25 
26 const 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";
29 const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
30 const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
31 const double R1Sdouble = 0.7096329412477836074;
32 const arith_uint256 R1L = arith_uint256(std::vector<unsigned char>(R1Array,R1Array+32));
33 const arith_uint160 R1S = arith_uint160(std::vector<unsigned char>(R1Array,R1Array+20));
34 const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
35 
36 const unsigned char R2Array[] =
37  "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
38  "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
39 const arith_uint256 R2L = arith_uint256(std::vector<unsigned char>(R2Array,R2Array+32));
40 const arith_uint160 R2S = arith_uint160(std::vector<unsigned char>(R2Array,R2Array+20));
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_uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
48 const arith_uint160 ZeroS = arith_uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
49 
50 const unsigned char OneArray[] =
51  "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
52  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
53 const arith_uint256 OneL = arith_uint256(std::vector<unsigned char>(OneArray,OneArray+32));
54 const arith_uint160 OneS = arith_uint160(std::vector<unsigned char>(OneArray,OneArray+20));
55 
56 const unsigned char MaxArray[] =
57  "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
58  "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
59 const arith_uint256 MaxL = arith_uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
60 const arith_uint160 MaxS = arith_uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
61 
62 const arith_uint256 HalfL = (OneL << 255);
63 const arith_uint160 HalfS = (OneS << 159);
64 std::string ArrayToString(const unsigned char A[], unsigned int width)
65 {
66  std::stringstream Stream;
67  Stream << std::hex;
68  for (unsigned int i = 0; i < width; ++i)
69  {
70  Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
71  }
72  return Stream.str();
73 }
74 
75 BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
76 {
77  BOOST_CHECK(1 == 0+1);
78  // constructor arith_uint256(vector<char>):
91 
92  // == and !=
93  BOOST_CHECK(R1L != R2L && R1S != R2S);
94  BOOST_CHECK(ZeroL != OneL && ZeroS != OneS);
95  BOOST_CHECK(OneL != ZeroL && OneS != ZeroS);
96  BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS);
97  BOOST_CHECK(~MaxL == ZeroL && ~MaxS == ZeroS);
98  BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
99  BOOST_CHECK( ((R1S ^ R2S) ^ R1S) == R2S);
100 
101  uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
102  for (unsigned int i = 0; i < 256; ++i)
103  {
104  BOOST_CHECK(ZeroL != (OneL << i));
105  BOOST_CHECK((OneL << i) != ZeroL);
106  BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
107  BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
108  }
109  BOOST_CHECK(ZeroL == (OneL << 256));
110 
111  for (unsigned int i = 0; i < 160; ++i)
112  {
113  BOOST_CHECK(ZeroS != (OneS << i));
114  BOOST_CHECK((OneS << i) != ZeroS);
115  BOOST_CHECK(R1S != (R1S ^ (OneS << i)));
116  BOOST_CHECK(((arith_uint160(Tmp64) ^ (OneS << i) ) != Tmp64 ));
117  }
118  BOOST_CHECK(ZeroS == (OneS << 256));
119 
120  // String Constructor and Copy Constructor
127  BOOST_CHECK(arith_uint256(" 0x"+R1L.ToString()+" ") == R1L);
134 
141  BOOST_CHECK(arith_uint160(" 0x"+R1S.ToString()+" ") == R1S);
144 
149 
150  // uint64_t constructor
151  BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
154  BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL));
155  BOOST_CHECK( (R1S & arith_uint160("0xffffffffffffffff")) == arith_uint160(R1LLow64));
158  BOOST_CHECK(arith_uint160("0xffffffffffffffff") = arith_uint160(0xffffffffffffffffULL));
159 
160  // Assignment (from base_uint)
161  arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
162  tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
163  tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
164  tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
165  tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
166  arith_uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS);
167  tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS);
168  tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S);
169  tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S);
170  tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS);
171 
172  // Wrong length must throw exception.
173  BOOST_CHECK_THROW(arith_uint256(std::vector<unsigned char>(OneArray,OneArray+31)), uint_error);
174  BOOST_CHECK_THROW(arith_uint256(std::vector<unsigned char>(OneArray,OneArray+20)), uint_error);
175  BOOST_CHECK_THROW(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+32)), uint_error);
176  BOOST_CHECK_THROW(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+19)), uint_error);
177 }
178 
179 void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
180 {
181  for (unsigned int T=0; T < arrayLength; ++T)
182  {
183  unsigned int F = (T+bitsToShift/8);
184  if (F < arrayLength)
185  to[T] = from[F] >> (bitsToShift%8);
186  else
187  to[T] = 0;
188  if (F + 1 < arrayLength)
189  to[T] |= from[(F+1)] << (8-bitsToShift%8);
190  }
191 }
192 
193 void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
194 {
195  for (unsigned int T=0; T < arrayLength; ++T)
196  {
197  if (T >= bitsToShift/8)
198  {
199  unsigned int F = T-bitsToShift/8;
200  to[T] = from[F] << (bitsToShift%8);
201  if (T >= bitsToShift/8+1)
202  to[T] |= from[F-1] >> (8-bitsToShift%8);
203  }
204  else {
205  to[T] = 0;
206  }
207  }
208 }
209 
210 BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
211  unsigned char TmpArray[32];
212  arith_uint256 TmpL;
213  for (unsigned int i = 0; i < 256; ++i)
214  {
215  shiftArrayLeft(TmpArray, OneArray, 32, i);
216  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
217  TmpL = OneL; TmpL <<= i;
218  BOOST_CHECK(TmpL == (OneL << i));
219  BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
220  TmpL = HalfL; TmpL >>= (255-i);
221  BOOST_CHECK(TmpL == (OneL << i));
222 
223  shiftArrayLeft(TmpArray, R1Array, 32, i);
224  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
225  TmpL = R1L; TmpL <<= i;
226  BOOST_CHECK(TmpL == (R1L << i));
227 
228  shiftArrayRight(TmpArray, R1Array, 32, i);
229  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
230  TmpL = R1L; TmpL >>= i;
231  BOOST_CHECK(TmpL == (R1L >> i));
232 
233  shiftArrayLeft(TmpArray, MaxArray, 32, i);
234  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
235  TmpL = MaxL; TmpL <<= i;
236  BOOST_CHECK(TmpL == (MaxL << i));
237 
238  shiftArrayRight(TmpArray, MaxArray, 32, i);
239  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
240  TmpL = MaxL; TmpL >>= i;
241  BOOST_CHECK(TmpL == (MaxL >> i));
242  }
243  arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
244  arith_uint256 c2L = c1L << 128;
245  for (unsigned int i = 0; i < 128; ++i) {
246  BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
247  }
248  for (unsigned int i = 128; i < 256; ++i) {
249  BOOST_CHECK((c1L << i) == (c2L << (i-128)));
250  }
251 
252  arith_uint160 TmpS;
253  for (unsigned int i = 0; i < 160; ++i)
254  {
255  shiftArrayLeft(TmpArray, OneArray, 20, i);
256  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i));
257  TmpS = OneS; TmpS <<= i;
258  BOOST_CHECK(TmpS == (OneS << i));
259  BOOST_CHECK((HalfS >> (159-i)) == (OneS << i));
260  TmpS = HalfS; TmpS >>= (159-i);
261  BOOST_CHECK(TmpS == (OneS << i));
262 
263  shiftArrayLeft(TmpArray, R1Array, 20, i);
264  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i));
265  TmpS = R1S; TmpS <<= i;
266  BOOST_CHECK(TmpS == (R1S << i));
267 
268  shiftArrayRight(TmpArray, R1Array, 20, i);
269  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i));
270  TmpS = R1S; TmpS >>= i;
271  BOOST_CHECK(TmpS == (R1S >> i));
272 
273  shiftArrayLeft(TmpArray, MaxArray, 20, i);
274  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i));
275  TmpS = MaxS; TmpS <<= i;
276  BOOST_CHECK(TmpS == (MaxS << i));
277 
278  shiftArrayRight(TmpArray, MaxArray, 20, i);
279  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i));
280  TmpS = MaxS; TmpS >>= i;
281  BOOST_CHECK(TmpS == (MaxS >> i));
282  }
283  arith_uint160 c1S = arith_uint160(0x0123456789abcdefULL);
284  arith_uint160 c2S = c1S << 80;
285  for (unsigned int i = 0; i < 80; ++i) {
286  BOOST_CHECK((c1S << i) == (c2S >> (80-i)));
287  }
288  for (unsigned int i = 80; i < 160; ++i) {
289  BOOST_CHECK((c1S << i) == (c2S << (i-80)));
290  }
291 }
292 
293 BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
294 {
296  BOOST_CHECK(!(!OneL));BOOST_CHECK(!(!OneS));
297  for (unsigned int i = 0; i < 256; ++i)
298  BOOST_CHECK(!(!(OneL<<i)));
299  for (unsigned int i = 0; i < 160; ++i)
300  BOOST_CHECK(!(!(OneS<<i)));
301  BOOST_CHECK(!(!R1L));BOOST_CHECK(!(!R1S));
302  BOOST_CHECK(!(!R2S));BOOST_CHECK(!(!R2S));
303  BOOST_CHECK(!(!MaxL));BOOST_CHECK(!(!MaxS));
304 
306 
307  unsigned char TmpArray[32];
308  for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
309  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
310  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S));
311 
313  BOOST_CHECK(-R1L == (~R1L)+1);
314  BOOST_CHECK(-R1S == (~R1S)+1);
315  for (unsigned int i = 0; i < 256; ++i)
316  BOOST_CHECK(-(OneL<<i) == (MaxL << i));
317  for (unsigned int i = 0; i < 160; ++i)
318  BOOST_CHECK(-(OneS<<i) == (MaxS << i));
319 }
320 
321 
322 // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
323 // element of Aarray and Barray, and then converting the result into a arith_uint256.
324 #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
325  for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
326  BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \
327  for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
328  BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S));
329 
330 #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
331  TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \
332  TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S));
333 
334 BOOST_AUTO_TEST_CASE( bitwiseOperators )
335 {
336  unsigned char TmpArray[32];
337 
338  CHECKBITWISEOPERATOR(R1,R2,|)
339  CHECKBITWISEOPERATOR(R1,R2,^)
340  CHECKBITWISEOPERATOR(R1,R2,&)
341  CHECKBITWISEOPERATOR(R1,Zero,|)
342  CHECKBITWISEOPERATOR(R1,Zero,^)
343  CHECKBITWISEOPERATOR(R1,Zero,&)
344  CHECKBITWISEOPERATOR(R1,Max,|)
345  CHECKBITWISEOPERATOR(R1,Max,^)
346  CHECKBITWISEOPERATOR(R1,Max,&)
347  CHECKBITWISEOPERATOR(Zero,R1,|)
348  CHECKBITWISEOPERATOR(Zero,R1,^)
349  CHECKBITWISEOPERATOR(Zero,R1,&)
350  CHECKBITWISEOPERATOR(Max,R1,|)
351  CHECKBITWISEOPERATOR(Max,R1,^)
352  CHECKBITWISEOPERATOR(Max,R1,&)
353 
354  arith_uint256 TmpL;
355  arith_uint160 TmpS;
356  CHECKASSIGNMENTOPERATOR(R1,R2,|)
357  CHECKASSIGNMENTOPERATOR(R1,R2,^)
358  CHECKASSIGNMENTOPERATOR(R1,R2,&)
359  CHECKASSIGNMENTOPERATOR(R1,Zero,|)
360  CHECKASSIGNMENTOPERATOR(R1,Zero,^)
361  CHECKASSIGNMENTOPERATOR(R1,Zero,&)
362  CHECKASSIGNMENTOPERATOR(R1,Max,|)
363  CHECKASSIGNMENTOPERATOR(R1,Max,^)
364  CHECKASSIGNMENTOPERATOR(R1,Max,&)
365  CHECKASSIGNMENTOPERATOR(Zero,R1,|)
366  CHECKASSIGNMENTOPERATOR(Zero,R1,^)
367  CHECKASSIGNMENTOPERATOR(Zero,R1,&)
368  CHECKASSIGNMENTOPERATOR(Max,R1,|)
369  CHECKASSIGNMENTOPERATOR(Max,R1,^)
370  CHECKASSIGNMENTOPERATOR(Max,R1,&)
371 
372  uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
373  TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
374  TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | arith_uint160(Tmp64)));
375  TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
376  TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S);
377  TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
378  TmpS ^= 0; BOOST_CHECK(TmpS == R1S);
379  TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
380  TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ arith_uint160(Tmp64)));
381 }
382 
383 BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
384 {
385  arith_uint256 TmpL;
386  for (unsigned int i = 0; i < 256; ++i) {
387  TmpL= OneL<< i;
388  BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
389  BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
390  TmpL |= R1L;
391  BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
392  BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
393  BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
394  }
395  arith_uint160 TmpS;
396  for (unsigned int i = 0; i < 160; ++i) {
397  TmpS= OneS<< i;
398  BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS);
399  BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS);
400  TmpS |= R1S;
401  BOOST_CHECK( TmpS >= R1S ); BOOST_CHECK( (TmpS == R1S) != (TmpS > R1S)); BOOST_CHECK( (TmpS == R1S) || !( TmpS <= R1S));
402  BOOST_CHECK( R1S <= TmpS ); BOOST_CHECK( (R1S == TmpS) != (R1S < TmpS)); BOOST_CHECK( (TmpS == R1S) || !( R1S >= TmpS));
403  BOOST_CHECK(! (TmpS < R1S)); BOOST_CHECK(! (R1S > TmpS));
404  }
405 }
406 
408 {
409  arith_uint256 TmpL = 0;
411  TmpL += R1L;
412  BOOST_CHECK(TmpL == R1L);
413  TmpL += R2L;
414  BOOST_CHECK(TmpL == R1L + R2L);
417  for (unsigned int i = 1; i < 256; ++i) {
418  BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
419  BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
420  TmpL = (MaxL>>i); TmpL += OneL;
421  BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
422  TmpL = (MaxL>>i); TmpL += 1;
423  BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
424  TmpL = (MaxL>>i);
425  BOOST_CHECK( TmpL++ == (MaxL>>i) );
426  BOOST_CHECK( TmpL == (HalfL >> (i-1)));
427  }
428  BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
429  TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
430  BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
431  TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
432  TmpL = R1L;
433  BOOST_CHECK(++TmpL == R1L+1);
434 
435  BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
436  BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
437  BOOST_CHECK(R1L - OneL == R1L+(-OneL));
438  for (unsigned int i = 1; i < 256; ++i) {
439  BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1)));
440  BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
441  TmpL = (HalfL >> (i-1));
442  BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
443  BOOST_CHECK(TmpL == (MaxL >> i));
444  TmpL = (HalfL >> (i-1));
445  BOOST_CHECK(--TmpL == (MaxL >> i));
446  }
447  TmpL = R1L;
448  BOOST_CHECK(--TmpL == R1L-1);
449 
450  // 160-bit; copy-pasted
451  arith_uint160 TmpS = 0;
453  TmpS += R1S;
454  BOOST_CHECK(TmpS == R1S);
455  TmpS += R2S;
456  BOOST_CHECK(TmpS == R1S + R2S);
459  for (unsigned int i = 1; i < 160; ++i) {
460  BOOST_CHECK( (MaxS >> i) + OneS == (HalfS >> (i-1)) );
461  BOOST_CHECK( OneS + (MaxS >> i) == (HalfS >> (i-1)) );
462  TmpS = (MaxS>>i); TmpS += OneS;
463  BOOST_CHECK( TmpS == (HalfS >> (i-1)) );
464  TmpS = (MaxS>>i); TmpS += 1;
465  BOOST_CHECK( TmpS == (HalfS >> (i-1)) );
466  TmpS = (MaxS>>i);
467  BOOST_CHECK( TmpS++ == (MaxS>>i) );
468  BOOST_CHECK( TmpS == (HalfS >> (i-1)));
469  }
470  BOOST_CHECK(arith_uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
471  TmpS = arith_uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL;
472  BOOST_CHECK(TmpS == arith_uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
473  TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL);
474  TmpS = R1S;
475  BOOST_CHECK(++TmpS == R1S+1);
476 
477  BOOST_CHECK(R1S -(-R2S) == R1S+R2S);
478  BOOST_CHECK(R1S -(-OneS) == R1S+OneS);
479  BOOST_CHECK(R1S - OneS == R1S+(-OneS));
480  for (unsigned int i = 1; i < 160; ++i) {
481  BOOST_CHECK((MaxS>>i) - (-OneS) == (HalfS >> (i-1)));
482  BOOST_CHECK((HalfS >> (i-1)) - OneS == (MaxS>>i));
483  TmpS = (HalfS >> (i-1));
484  BOOST_CHECK(TmpS-- == (HalfS >> (i-1)));
485  BOOST_CHECK(TmpS == (MaxS >> i));
486  TmpS = (HalfS >> (i-1));
487  BOOST_CHECK(--TmpS == (MaxS >> i));
488  }
489  TmpS = R1S;
490  BOOST_CHECK(--TmpS == R1S-1);
491 
492 }
493 
495 {
496  BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
497  BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
498  BOOST_CHECK((R1L * ZeroL) == ZeroL);
499  BOOST_CHECK((R1L * OneL) == R1L);
500  BOOST_CHECK((R1L * MaxL) == -R1L);
501  BOOST_CHECK((R2L * R1L) == (R1L * R2L));
502  BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
503  BOOST_CHECK((R2L * ZeroL) == ZeroL);
504  BOOST_CHECK((R2L * OneL) == R2L);
505  BOOST_CHECK((R2L * MaxL) == -R2L);
506 
507  BOOST_CHECK((R1S * R1S).ToString() == "a7761bf30d5237e9873f9bff3642a732c4d84f10");
508  BOOST_CHECK((R1S * R2S).ToString() == "ba51c008df851987d9dd323f0e5de07760529c40");
509  BOOST_CHECK((R1S * ZeroS) == ZeroS);
510  BOOST_CHECK((R1S * OneS) == R1S);
511  BOOST_CHECK((R1S * MaxS) == -R1S);
512  BOOST_CHECK((R2S * R1S) == (R1S * R2S));
513  BOOST_CHECK((R2S * R2S).ToString() == "c28bb2b45a1d85ab7996ccd3e102a650f74ff100");
514  BOOST_CHECK((R2S * ZeroS) == ZeroS);
515  BOOST_CHECK((R2S * OneS) == R2S);
516  BOOST_CHECK((R2S * MaxS) == -R2S);
517 
518  BOOST_CHECK(MaxL * MaxL == OneL);
519  BOOST_CHECK(MaxS * MaxS == OneS);
520 
521  BOOST_CHECK((R1L * 0) == 0);
522  BOOST_CHECK((R1L * 1) == R1L);
523  BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
524  BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
525  BOOST_CHECK((R1S * 0) == 0);
526  BOOST_CHECK((R1S * 1) == R1S);
527  BOOST_CHECK((R1S * 7).ToString() == "f7a987f3c3bf758d927f202d7e795faeff084244");
528  BOOST_CHECK((R2S * 0xFFFFFFFFUL).ToString() == "1c6f6c930353e17f7d6127213bb18d2883e2cd90");
529 }
530 
532 {
533  arith_uint256 D1L("AD7133AC1977FA2B7");
534  arith_uint256 D2L("ECD751716");
535  BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
536  BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
537  BOOST_CHECK(R1L / OneL == R1L);
538  BOOST_CHECK(R1L / MaxL == ZeroL);
539  BOOST_CHECK(MaxL / R1L == 2);
541  BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
542  BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
543  BOOST_CHECK(R2L / OneL == R2L);
544  BOOST_CHECK(R2L / MaxL == ZeroL);
545  BOOST_CHECK(MaxL / R2L == 1);
547 
548  arith_uint160 D1S("D3C5EDCDEA54EB92679F0A4B4");
549  arith_uint160 D2S("13037");
550  BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02");
551  BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c");
552  BOOST_CHECK(R1S / OneS == R1S);
553  BOOST_CHECK(R1S / MaxS == ZeroS);
554  BOOST_CHECK(MaxS / R1S == 1);
556  BOOST_CHECK((R2S / D1S).ToString() == "0000000000000000000000000c5608e781182047");
557  BOOST_CHECK((R2S / D2S).ToString() == "00008966751b7187c3c67c1fda5cea7db2c1c069");
558  BOOST_CHECK(R2S / OneS == R2S);
559  BOOST_CHECK(R2S / MaxS == ZeroS);
560  BOOST_CHECK(MaxS / R2S == 1);
562 }
563 
564 
565 bool almostEqual(double d1, double d2)
566 {
567  return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
568 }
569 
570 BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
571 {
576  arith_uint256 TmpL(R1L);
577  BOOST_CHECK(TmpL == R1L);
578  TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
579  TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
580  TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL);
581 
582  TmpL.SetHex(R1L.ToString());
583  BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0);
584  BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0);
585  BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0);
586  BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0);
587  BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0);
588  BOOST_CHECK(R1L.size() == 32);
589  BOOST_CHECK(R2L.size() == 32);
590  BOOST_CHECK(ZeroL.size() == 32);
591  BOOST_CHECK(MaxL.size() == 32);
592  BOOST_CHECK(R1L.begin() + 32 == R1L.end());
593  BOOST_CHECK(R2L.begin() + 32 == R2L.end());
594  BOOST_CHECK(OneL.begin() + 32 == OneL.end());
595  BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
596  BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
598  BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
599  BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
600  BOOST_CHECK(GetSerializeSize(R1L, PROTOCOL_VERSION) == 32);
601  BOOST_CHECK(GetSerializeSize(ZeroL, PROTOCOL_VERSION) == 32);
602 
603  CDataStream ss(0, PROTOCOL_VERSION);
604  ss << R1L;
605  BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32));
606  ss >> TmpL;
607  BOOST_CHECK(R1L == TmpL);
608  ss.clear();
609  ss << ZeroL;
610  BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32));
611  ss >> TmpL;
612  BOOST_CHECK(ZeroL == TmpL);
613  ss.clear();
614  ss << MaxL;
615  BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32));
616  ss >> TmpL;
617  BOOST_CHECK(MaxL == TmpL);
618  ss.clear();
619 
624  arith_uint160 TmpS(R1S);
625  BOOST_CHECK(TmpS == R1S);
626  TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S);
627  TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0);
628  TmpS.SetHex(HalfS.ToString()); BOOST_CHECK(TmpS == HalfS);
629 
630  TmpS.SetHex(R1S.ToString());
631  BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0);
632  BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0);
633  BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0);
634  BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0);
635  BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0);
636  BOOST_CHECK(R1S.size() == 20);
637  BOOST_CHECK(R2S.size() == 20);
638  BOOST_CHECK(ZeroS.size() == 20);
639  BOOST_CHECK(MaxS.size() == 20);
640  BOOST_CHECK(R1S.begin() + 20 == R1S.end());
641  BOOST_CHECK(R2S.begin() + 20 == R2S.end());
642  BOOST_CHECK(OneS.begin() + 20 == OneS.end());
643  BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
644  BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
646  BOOST_CHECK(HalfS.GetLow64() ==0x0000000000000000ULL);
647  BOOST_CHECK(OneS.GetLow64() ==0x0000000000000001ULL);
648  BOOST_CHECK(GetSerializeSize(R1S, PROTOCOL_VERSION) == 20);
649  BOOST_CHECK(GetSerializeSize(ZeroS, PROTOCOL_VERSION) == 20);
650 
651  ss << R1S;
652  BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
653  ss >> TmpS;
654  BOOST_CHECK(R1S == TmpS);
655  ss.clear();
656  ss << ZeroS;
657  BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
658  ss >> TmpS;
659  BOOST_CHECK(ZeroS == TmpS);
660  ss.clear();
661  ss << MaxS;
662  BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
663  ss >> TmpS;
664  BOOST_CHECK(MaxS == TmpS);
665  ss.clear();
666 
667  for (unsigned int i = 0; i < 255; ++i)
668  {
669  BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
670  if (i < 160) BOOST_CHECK((OneS << i).getdouble() == ldexp(1.0,i));
671  }
672  BOOST_CHECK(ZeroL.getdouble() == 0.0);
673  BOOST_CHECK(ZeroS.getdouble() == 0.0);
674  for (int i = 256; i > 53; --i)
675  BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
676  for (int i = 160; i > 53; --i)
677  BOOST_CHECK(almostEqual((R1S>>(160-i)).getdouble(), ldexp(R1Sdouble,i)));
678  uint64_t R1L64part = (R1L>>192).GetLow64();
679  uint64_t R1S64part = (R1S>>96).GetLow64();
680  for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
681  {
682  BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
683  BOOST_CHECK((R1S>>(160-i)).getdouble() == (double)(R1S64part >> (64-i)));
684  }
685 }
686 
687 BOOST_AUTO_TEST_CASE(bignum_SetCompact)
688 {
689  arith_uint256 num;
690  bool fNegative;
691  bool fOverflow;
692  num.SetCompact(0, &fNegative, &fOverflow);
693  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
694  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
695  BOOST_CHECK_EQUAL(fNegative, false);
696  BOOST_CHECK_EQUAL(fOverflow, false);
697 
698  num.SetCompact(0x00123456, &fNegative, &fOverflow);
699  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
700  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
701  BOOST_CHECK_EQUAL(fNegative, false);
702  BOOST_CHECK_EQUAL(fOverflow, false);
703 
704  num.SetCompact(0x01003456, &fNegative, &fOverflow);
705  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
706  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
707  BOOST_CHECK_EQUAL(fNegative, false);
708  BOOST_CHECK_EQUAL(fOverflow, false);
709 
710  num.SetCompact(0x02000056, &fNegative, &fOverflow);
711  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
712  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
713  BOOST_CHECK_EQUAL(fNegative, false);
714  BOOST_CHECK_EQUAL(fOverflow, false);
715 
716  num.SetCompact(0x03000000, &fNegative, &fOverflow);
717  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
718  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
719  BOOST_CHECK_EQUAL(fNegative, false);
720  BOOST_CHECK_EQUAL(fOverflow, false);
721 
722  num.SetCompact(0x04000000, &fNegative, &fOverflow);
723  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
724  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
725  BOOST_CHECK_EQUAL(fNegative, false);
726  BOOST_CHECK_EQUAL(fOverflow, false);
727 
728  num.SetCompact(0x00923456, &fNegative, &fOverflow);
729  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
730  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
731  BOOST_CHECK_EQUAL(fNegative, false);
732  BOOST_CHECK_EQUAL(fOverflow, false);
733 
734  num.SetCompact(0x01803456, &fNegative, &fOverflow);
735  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
736  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
737  BOOST_CHECK_EQUAL(fNegative, false);
738  BOOST_CHECK_EQUAL(fOverflow, false);
739 
740  num.SetCompact(0x02800056, &fNegative, &fOverflow);
741  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
742  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
743  BOOST_CHECK_EQUAL(fNegative, false);
744  BOOST_CHECK_EQUAL(fOverflow, false);
745 
746  num.SetCompact(0x03800000, &fNegative, &fOverflow);
747  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
748  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
749  BOOST_CHECK_EQUAL(fNegative, false);
750  BOOST_CHECK_EQUAL(fOverflow, false);
751 
752  num.SetCompact(0x04800000, &fNegative, &fOverflow);
753  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
754  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
755  BOOST_CHECK_EQUAL(fNegative, false);
756  BOOST_CHECK_EQUAL(fOverflow, false);
757 
758  num.SetCompact(0x01123456, &fNegative, &fOverflow);
759  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
760  BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
761  BOOST_CHECK_EQUAL(fNegative, false);
762  BOOST_CHECK_EQUAL(fOverflow, false);
763 
764  // Make sure that we don't generate compacts with the 0x00800000 bit set
765  num = 0x80;
766  BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
767 
768  num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
769  BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
770  BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
771  BOOST_CHECK_EQUAL(fNegative, true);
772  BOOST_CHECK_EQUAL(fOverflow, false);
773 
774  num.SetCompact(0x02123456, &fNegative, &fOverflow);
775  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
776  BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
777  BOOST_CHECK_EQUAL(fNegative, false);
778  BOOST_CHECK_EQUAL(fOverflow, false);
779 
780  num.SetCompact(0x03123456, &fNegative, &fOverflow);
781  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
782  BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
783  BOOST_CHECK_EQUAL(fNegative, false);
784  BOOST_CHECK_EQUAL(fOverflow, false);
785 
786  num.SetCompact(0x04123456, &fNegative, &fOverflow);
787  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
788  BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
789  BOOST_CHECK_EQUAL(fNegative, false);
790  BOOST_CHECK_EQUAL(fOverflow, false);
791 
792  num.SetCompact(0x04923456, &fNegative, &fOverflow);
793  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
794  BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
795  BOOST_CHECK_EQUAL(fNegative, true);
796  BOOST_CHECK_EQUAL(fOverflow, false);
797 
798  num.SetCompact(0x05009234, &fNegative, &fOverflow);
799  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
800  BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
801  BOOST_CHECK_EQUAL(fNegative, false);
802  BOOST_CHECK_EQUAL(fOverflow, false);
803 
804  num.SetCompact(0x20123456, &fNegative, &fOverflow);
805  BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
806  BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
807  BOOST_CHECK_EQUAL(fNegative, false);
808  BOOST_CHECK_EQUAL(fOverflow, false);
809 
810  num.SetCompact(0xff123456, &fNegative, &fOverflow);
811  BOOST_CHECK_EQUAL(fNegative, false);
812  BOOST_CHECK_EQUAL(fOverflow, true);
813 }
814 
815 
816 BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
817 {
818  // ~R1L give a base_uint<256>
819  BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1S >> 10) == (R1S >> 10));
820  BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK((~~R1S << 10) == (R1S << 10));
821  BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(!(~~R1S < R1S));
822  BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(~~R1S <= R1S);
823  BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(!(~~R1S > R1S));
824  BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(~~R1S >= R1S);
825  BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(!(R1S < ~~R1S));
826  BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(R1S <= ~~R1S);
827  BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(!(R1S > ~~R1S));
828  BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(R1S >= ~~R1S);
829 
830  BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
831  BOOST_CHECK(~~R1S + R2S == R1S + ~~R2S);
832  BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
833  BOOST_CHECK(~~R1S - R2S == R1S - ~~R2S);
834  BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
835  BOOST_CHECK(~R1S != R1S); BOOST_CHECK(R1S != ~R1S);
836  unsigned char TmpArray[32];
837  CHECKBITWISEOPERATOR(~R1,R2,|)
838  CHECKBITWISEOPERATOR(~R1,R2,^)
839  CHECKBITWISEOPERATOR(~R1,R2,&)
840  CHECKBITWISEOPERATOR(R1,~R2,|)
841  CHECKBITWISEOPERATOR(R1,~R2,^)
842  CHECKBITWISEOPERATOR(R1,~R2,&)
843 }
844 
arith_uint256 UintToArith256(const uint256 &a)
std::string ArrayToString(const unsigned char A[], unsigned int width)
bool almostEqual(double d1, double d2)
const arith_uint256 OneL
#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_)
const arith_uint160 R2S
const arith_uint256 MaxL
#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_)
const arith_uint160 ZeroS
const arith_uint256 R1L
const unsigned char ZeroArray[]
const char R1LplusR2L[]
const unsigned char R1Array[]
const uint64_t R1LLow64
const double R1Sdouble
BOOST_AUTO_TEST_CASE(basics)
const arith_uint256 HalfL
const unsigned char OneArray[]
void shiftArrayRight(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
const arith_uint160 R1S
const char R1ArrayHex[]
const arith_uint160 MaxS
const arith_uint160 OneS
arith_uint256 arith_uint256V(const std::vector< unsigned char > &vch)
Convert vector to arith_uint256, via uint256 blob.
const unsigned char R2Array[]
const arith_uint256 R2L
const double R1Ldouble
const unsigned char MaxArray[]
void shiftArrayLeft(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
const arith_uint256 ZeroL
const arith_uint160 HalfS
std::string str() const
Definition: streams.h:152
void clear()
Definition: streams.h:171
160-bit unsigned big integer.
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
unsigned char * end()
unsigned char * begin()
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:138
BOOST_AUTO_TEST_SUITE_END()
#define T(expected, seed, data)
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
Definition: serialize.h:847
Basic testing setup.
Definition: test_pivx.h:51