1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include <boost/test/unit_test.hpp>
12 #include "arith_uint256.h"
15 #include "test/test_bitcoin.h"
17 BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests
, BasicTestingSetup
)
19 /// Convert vector to arith_uint256, via uint256 blob
20 inline arith_uint256
arith_uint256V(const std::vector
<unsigned char>& vch
)
22 return UintToArith256(uint256(vch
));
25 const unsigned char R1Array
[] =
26 "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
27 "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
28 const char R1ArrayHex
[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
29 const double R1Ldouble
= 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
30 const arith_uint256 R1L
= arith_uint256V(std::vector
<unsigned char>(R1Array
,R1Array
+32));
31 const uint64_t R1LLow64
= 0x121156cfdb4a529cULL
;
33 const unsigned char R2Array
[] =
34 "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
35 "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
36 const arith_uint256 R2L
= arith_uint256V(std::vector
<unsigned char>(R2Array
,R2Array
+32));
38 const char R1LplusR2L
[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
40 const unsigned char ZeroArray
[] =
41 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
42 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
43 const arith_uint256 ZeroL
= arith_uint256V(std::vector
<unsigned char>(ZeroArray
,ZeroArray
+32));
45 const unsigned char OneArray
[] =
46 "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
47 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
48 const arith_uint256 OneL
= arith_uint256V(std::vector
<unsigned char>(OneArray
,OneArray
+32));
50 const unsigned char MaxArray
[] =
51 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
52 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
53 const arith_uint256 MaxL
= arith_uint256V(std::vector
<unsigned char>(MaxArray
,MaxArray
+32));
55 const arith_uint256 HalfL
= (OneL
<< 255);
56 std::string
ArrayToString(const unsigned char A
[], unsigned int width
)
58 std::stringstream Stream
;
60 for (unsigned int i
= 0; i
< width
; ++i
)
62 Stream
<<std::setw(2)<<std::setfill('0')<<(unsigned int)A
[width
-i
-1];
67 BOOST_AUTO_TEST_CASE( basics
) // constructors, equality, inequality
69 BOOST_CHECK(1 == 0+1);
70 // constructor arith_uint256(vector<char>):
71 BOOST_CHECK(R1L
.ToString() == ArrayToString(R1Array
,32));
72 BOOST_CHECK(R2L
.ToString() == ArrayToString(R2Array
,32));
73 BOOST_CHECK(ZeroL
.ToString() == ArrayToString(ZeroArray
,32));
74 BOOST_CHECK(OneL
.ToString() == ArrayToString(OneArray
,32));
75 BOOST_CHECK(MaxL
.ToString() == ArrayToString(MaxArray
,32));
76 BOOST_CHECK(OneL
.ToString() != ArrayToString(ZeroArray
,32));
79 BOOST_CHECK(R1L
!= R2L
);
80 BOOST_CHECK(ZeroL
!= OneL
);
81 BOOST_CHECK(OneL
!= ZeroL
);
82 BOOST_CHECK(MaxL
!= ZeroL
);
83 BOOST_CHECK(~MaxL
== ZeroL
);
84 BOOST_CHECK( ((R1L
^ R2L
) ^ R1L
) == R2L
);
86 uint64_t Tmp64
= 0xc4dab720d9c7acaaULL
;
87 for (unsigned int i
= 0; i
< 256; ++i
)
89 BOOST_CHECK(ZeroL
!= (OneL
<< i
));
90 BOOST_CHECK((OneL
<< i
) != ZeroL
);
91 BOOST_CHECK(R1L
!= (R1L
^ (OneL
<< i
)));
92 BOOST_CHECK(((arith_uint256(Tmp64
) ^ (OneL
<< i
) ) != Tmp64
));
94 BOOST_CHECK(ZeroL
== (OneL
<< 256));
96 // String Constructor and Copy Constructor
97 BOOST_CHECK(arith_uint256("0x"+R1L
.ToString()) == R1L
);
98 BOOST_CHECK(arith_uint256("0x"+R2L
.ToString()) == R2L
);
99 BOOST_CHECK(arith_uint256("0x"+ZeroL
.ToString()) == ZeroL
);
100 BOOST_CHECK(arith_uint256("0x"+OneL
.ToString()) == OneL
);
101 BOOST_CHECK(arith_uint256("0x"+MaxL
.ToString()) == MaxL
);
102 BOOST_CHECK(arith_uint256(R1L
.ToString()) == R1L
);
103 BOOST_CHECK(arith_uint256(" 0x"+R1L
.ToString()+" ") == R1L
);
104 BOOST_CHECK(arith_uint256("") == ZeroL
);
105 BOOST_CHECK(R1L
== arith_uint256(R1ArrayHex
));
106 BOOST_CHECK(arith_uint256(R1L
) == R1L
);
107 BOOST_CHECK((arith_uint256(R1L
^R2L
)^R2L
) == R1L
);
108 BOOST_CHECK(arith_uint256(ZeroL
) == ZeroL
);
109 BOOST_CHECK(arith_uint256(OneL
) == OneL
);
111 // uint64_t constructor
112 BOOST_CHECK( (R1L
& arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64
));
113 BOOST_CHECK(ZeroL
== arith_uint256(0));
114 BOOST_CHECK(OneL
== arith_uint256(1));
115 BOOST_CHECK(arith_uint256("0xffffffffffffffff") == arith_uint256(0xffffffffffffffffULL
));
117 // Assignment (from base_uint)
118 arith_uint256 tmpL
= ~ZeroL
; BOOST_CHECK(tmpL
== ~ZeroL
);
119 tmpL
= ~OneL
; BOOST_CHECK(tmpL
== ~OneL
);
120 tmpL
= ~R1L
; BOOST_CHECK(tmpL
== ~R1L
);
121 tmpL
= ~R2L
; BOOST_CHECK(tmpL
== ~R2L
);
122 tmpL
= ~MaxL
; BOOST_CHECK(tmpL
== ~MaxL
);
125 void shiftArrayRight(unsigned char* to
, const unsigned char* from
, unsigned int arrayLength
, unsigned int bitsToShift
)
127 for (unsigned int T
=0; T
< arrayLength
; ++T
)
129 unsigned int F
= (T
+bitsToShift
/8);
131 to
[T
] = from
[F
] >> (bitsToShift
%8);
134 if (F
+ 1 < arrayLength
)
135 to
[T
] |= from
[(F
+1)] << (8-bitsToShift
%8);
139 void shiftArrayLeft(unsigned char* to
, const unsigned char* from
, unsigned int arrayLength
, unsigned int bitsToShift
)
141 for (unsigned int T
=0; T
< arrayLength
; ++T
)
143 if (T
>= bitsToShift
/8)
145 unsigned int F
= T
-bitsToShift
/8;
146 to
[T
] = from
[F
] << (bitsToShift
%8);
147 if (T
>= bitsToShift
/8+1)
148 to
[T
] |= from
[F
-1] >> (8-bitsToShift
%8);
156 BOOST_AUTO_TEST_CASE( shifts
) { // "<<" ">>" "<<=" ">>="
157 unsigned char TmpArray
[32];
159 for (unsigned int i
= 0; i
< 256; ++i
)
161 shiftArrayLeft(TmpArray
, OneArray
, 32, i
);
162 BOOST_CHECK(arith_uint256V(std::vector
<unsigned char>(TmpArray
,TmpArray
+32)) == (OneL
<< i
));
163 TmpL
= OneL
; TmpL
<<= i
;
164 BOOST_CHECK(TmpL
== (OneL
<< i
));
165 BOOST_CHECK((HalfL
>> (255-i
)) == (OneL
<< i
));
166 TmpL
= HalfL
; TmpL
>>= (255-i
);
167 BOOST_CHECK(TmpL
== (OneL
<< i
));
169 shiftArrayLeft(TmpArray
, R1Array
, 32, i
);
170 BOOST_CHECK(arith_uint256V(std::vector
<unsigned char>(TmpArray
,TmpArray
+32)) == (R1L
<< i
));
171 TmpL
= R1L
; TmpL
<<= i
;
172 BOOST_CHECK(TmpL
== (R1L
<< i
));
174 shiftArrayRight(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
));
179 shiftArrayLeft(TmpArray
, MaxArray
, 32, i
);
180 BOOST_CHECK(arith_uint256V(std::vector
<unsigned char>(TmpArray
,TmpArray
+32)) == (MaxL
<< i
));
181 TmpL
= MaxL
; TmpL
<<= i
;
182 BOOST_CHECK(TmpL
== (MaxL
<< i
));
184 shiftArrayRight(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
));
189 arith_uint256 c1L
= arith_uint256(0x0123456789abcdefULL
);
190 arith_uint256 c2L
= c1L
<< 128;
191 for (unsigned int i
= 0; i
< 128; ++i
) {
192 BOOST_CHECK((c1L
<< i
) == (c2L
>> (128-i
)));
194 for (unsigned int i
= 128; i
< 256; ++i
) {
195 BOOST_CHECK((c1L
<< i
) == (c2L
<< (i
-128)));
199 BOOST_AUTO_TEST_CASE( unaryOperators
) // ! ~ -
202 BOOST_CHECK(!(!OneL
));
203 for (unsigned int i
= 0; i
< 256; ++i
)
204 BOOST_CHECK(!(!(OneL
<<i
)));
205 BOOST_CHECK(!(!R1L
));
206 BOOST_CHECK(!(!MaxL
));
208 BOOST_CHECK(~ZeroL
== MaxL
);
210 unsigned char TmpArray
[32];
211 for (unsigned int i
= 0; i
< 32; ++i
) { TmpArray
[i
] = ~R1Array
[i
]; }
212 BOOST_CHECK(arith_uint256V(std::vector
<unsigned char>(TmpArray
,TmpArray
+32)) == (~R1L
));
214 BOOST_CHECK(-ZeroL
== ZeroL
);
215 BOOST_CHECK(-R1L
== (~R1L
)+1);
216 for (unsigned int i
= 0; i
< 256; ++i
)
217 BOOST_CHECK(-(OneL
<<i
) == (MaxL
<< i
));
221 // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
222 // element of Aarray and Barray, and then converting the result into a arith_uint256.
223 #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
224 for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
225 BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
227 #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
228 TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
230 BOOST_AUTO_TEST_CASE( bitwiseOperators
)
232 unsigned char TmpArray
[32];
234 CHECKBITWISEOPERATOR(R1
,R2
,|)
235 CHECKBITWISEOPERATOR(R1
,R2
,^)
236 CHECKBITWISEOPERATOR(R1
,R2
,&)
237 CHECKBITWISEOPERATOR(R1
,Zero
,|)
238 CHECKBITWISEOPERATOR(R1
,Zero
,^)
239 CHECKBITWISEOPERATOR(R1
,Zero
,&)
240 CHECKBITWISEOPERATOR(R1
,Max
,|)
241 CHECKBITWISEOPERATOR(R1
,Max
,^)
242 CHECKBITWISEOPERATOR(R1
,Max
,&)
243 CHECKBITWISEOPERATOR(Zero
,R1
,|)
244 CHECKBITWISEOPERATOR(Zero
,R1
,^)
245 CHECKBITWISEOPERATOR(Zero
,R1
,&)
246 CHECKBITWISEOPERATOR(Max
,R1
,|)
247 CHECKBITWISEOPERATOR(Max
,R1
,^)
248 CHECKBITWISEOPERATOR(Max
,R1
,&)
251 CHECKASSIGNMENTOPERATOR(R1
,R2
,|)
252 CHECKASSIGNMENTOPERATOR(R1
,R2
,^)
253 CHECKASSIGNMENTOPERATOR(R1
,R2
,&)
254 CHECKASSIGNMENTOPERATOR(R1
,Zero
,|)
255 CHECKASSIGNMENTOPERATOR(R1
,Zero
,^)
256 CHECKASSIGNMENTOPERATOR(R1
,Zero
,&)
257 CHECKASSIGNMENTOPERATOR(R1
,Max
,|)
258 CHECKASSIGNMENTOPERATOR(R1
,Max
,^)
259 CHECKASSIGNMENTOPERATOR(R1
,Max
,&)
260 CHECKASSIGNMENTOPERATOR(Zero
,R1
,|)
261 CHECKASSIGNMENTOPERATOR(Zero
,R1
,^)
262 CHECKASSIGNMENTOPERATOR(Zero
,R1
,&)
263 CHECKASSIGNMENTOPERATOR(Max
,R1
,|)
264 CHECKASSIGNMENTOPERATOR(Max
,R1
,^)
265 CHECKASSIGNMENTOPERATOR(Max
,R1
,&)
267 uint64_t Tmp64
= 0xe1db685c9a0b47a2ULL
;
268 TmpL
= R1L
; TmpL
|= Tmp64
; BOOST_CHECK(TmpL
== (R1L
| arith_uint256(Tmp64
)));
269 TmpL
= R1L
; TmpL
|= 0; BOOST_CHECK(TmpL
== R1L
);
270 TmpL
^= 0; BOOST_CHECK(TmpL
== R1L
);
271 TmpL
^= Tmp64
; BOOST_CHECK(TmpL
== (R1L
^ arith_uint256(Tmp64
)));
274 BOOST_AUTO_TEST_CASE( comparison
) // <= >= < >
277 for (unsigned int i
= 0; i
< 256; ++i
) {
279 BOOST_CHECK( TmpL
>= ZeroL
&& TmpL
> ZeroL
&& ZeroL
< TmpL
&& ZeroL
<= TmpL
);
280 BOOST_CHECK( TmpL
>= 0 && TmpL
> 0 && 0 < TmpL
&& 0 <= TmpL
);
282 BOOST_CHECK( TmpL
>= R1L
); BOOST_CHECK( (TmpL
== R1L
) != (TmpL
> R1L
)); BOOST_CHECK( (TmpL
== R1L
) || !( TmpL
<= R1L
));
283 BOOST_CHECK( R1L
<= TmpL
); BOOST_CHECK( (R1L
== TmpL
) != (R1L
< TmpL
)); BOOST_CHECK( (TmpL
== R1L
) || !( R1L
>= TmpL
));
284 BOOST_CHECK(! (TmpL
< R1L
)); BOOST_CHECK(! (R1L
> TmpL
));
288 BOOST_AUTO_TEST_CASE( plusMinus
)
290 arith_uint256 TmpL
= 0;
291 BOOST_CHECK(R1L
+R2L
== arith_uint256(R1LplusR2L
));
293 BOOST_CHECK(TmpL
== R1L
);
295 BOOST_CHECK(TmpL
== R1L
+ R2L
);
296 BOOST_CHECK(OneL
+MaxL
== ZeroL
);
297 BOOST_CHECK(MaxL
+OneL
== ZeroL
);
298 for (unsigned int i
= 1; i
< 256; ++i
) {
299 BOOST_CHECK( (MaxL
>> i
) + OneL
== (HalfL
>> (i
-1)) );
300 BOOST_CHECK( OneL
+ (MaxL
>> i
) == (HalfL
>> (i
-1)) );
301 TmpL
= (MaxL
>>i
); TmpL
+= OneL
;
302 BOOST_CHECK( TmpL
== (HalfL
>> (i
-1)) );
303 TmpL
= (MaxL
>>i
); TmpL
+= 1;
304 BOOST_CHECK( TmpL
== (HalfL
>> (i
-1)) );
306 BOOST_CHECK( TmpL
++ == (MaxL
>>i
) );
307 BOOST_CHECK( TmpL
== (HalfL
>> (i
-1)));
309 BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL
) + 0xee8d836fce66fbULL
== arith_uint256(0xbedc77e27940a7ULL
+ 0xee8d836fce66fbULL
));
310 TmpL
= arith_uint256(0xbedc77e27940a7ULL
); TmpL
+= 0xee8d836fce66fbULL
;
311 BOOST_CHECK(TmpL
== arith_uint256(0xbedc77e27940a7ULL
+0xee8d836fce66fbULL
));
312 TmpL
-= 0xee8d836fce66fbULL
; BOOST_CHECK(TmpL
== 0xbedc77e27940a7ULL
);
314 BOOST_CHECK(++TmpL
== R1L
+1);
316 BOOST_CHECK(R1L
-(-R2L
) == R1L
+R2L
);
317 BOOST_CHECK(R1L
-(-OneL
) == R1L
+OneL
);
318 BOOST_CHECK(R1L
- OneL
== R1L
+(-OneL
));
319 for (unsigned int i
= 1; i
< 256; ++i
) {
320 BOOST_CHECK((MaxL
>>i
) - (-OneL
) == (HalfL
>> (i
-1)));
321 BOOST_CHECK((HalfL
>> (i
-1)) - OneL
== (MaxL
>>i
));
322 TmpL
= (HalfL
>> (i
-1));
323 BOOST_CHECK(TmpL
-- == (HalfL
>> (i
-1)));
324 BOOST_CHECK(TmpL
== (MaxL
>> i
));
325 TmpL
= (HalfL
>> (i
-1));
326 BOOST_CHECK(--TmpL
== (MaxL
>> i
));
329 BOOST_CHECK(--TmpL
== R1L
-1);
332 BOOST_AUTO_TEST_CASE( multiply
)
334 BOOST_CHECK((R1L
* R1L
).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
335 BOOST_CHECK((R1L
* R2L
).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
336 BOOST_CHECK((R1L
* ZeroL
) == ZeroL
);
337 BOOST_CHECK((R1L
* OneL
) == R1L
);
338 BOOST_CHECK((R1L
* MaxL
) == -R1L
);
339 BOOST_CHECK((R2L
* R1L
) == (R1L
* R2L
));
340 BOOST_CHECK((R2L
* R2L
).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
341 BOOST_CHECK((R2L
* ZeroL
) == ZeroL
);
342 BOOST_CHECK((R2L
* OneL
) == R2L
);
343 BOOST_CHECK((R2L
* MaxL
) == -R2L
);
345 BOOST_CHECK(MaxL
* MaxL
== OneL
);
347 BOOST_CHECK((R1L
* 0) == 0);
348 BOOST_CHECK((R1L
* 1) == R1L
);
349 BOOST_CHECK((R1L
* 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
350 BOOST_CHECK((R2L
* 0x87654321UL
).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
353 BOOST_AUTO_TEST_CASE( divide
)
355 arith_uint256
D1L("AD7133AC1977FA2B7");
356 arith_uint256
D2L("ECD751716");
357 BOOST_CHECK((R1L
/ D1L
).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
358 BOOST_CHECK((R1L
/ D2L
).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
359 BOOST_CHECK(R1L
/ OneL
== R1L
);
360 BOOST_CHECK(R1L
/ MaxL
== ZeroL
);
361 BOOST_CHECK(MaxL
/ R1L
== 2);
362 BOOST_CHECK_THROW(R1L
/ ZeroL
, uint_error
);
363 BOOST_CHECK((R2L
/ D1L
).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
364 BOOST_CHECK((R2L
/ D2L
).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
365 BOOST_CHECK(R2L
/ OneL
== R2L
);
366 BOOST_CHECK(R2L
/ MaxL
== ZeroL
);
367 BOOST_CHECK(MaxL
/ R2L
== 1);
368 BOOST_CHECK_THROW(R2L
/ ZeroL
, uint_error
);
372 bool almostEqual(double d1
, double d2
)
374 return fabs(d1
-d2
) <= 4*fabs(d1
)*std::numeric_limits
<double>::epsilon();
377 BOOST_AUTO_TEST_CASE( methods
) // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize
379 BOOST_CHECK(R1L
.GetHex() == R1L
.ToString());
380 BOOST_CHECK(R2L
.GetHex() == R2L
.ToString());
381 BOOST_CHECK(OneL
.GetHex() == OneL
.ToString());
382 BOOST_CHECK(MaxL
.GetHex() == MaxL
.ToString());
383 arith_uint256
TmpL(R1L
);
384 BOOST_CHECK(TmpL
== R1L
);
385 TmpL
.SetHex(R2L
.ToString()); BOOST_CHECK(TmpL
== R2L
);
386 TmpL
.SetHex(ZeroL
.ToString()); BOOST_CHECK(TmpL
== 0);
387 TmpL
.SetHex(HalfL
.ToString()); BOOST_CHECK(TmpL
== HalfL
);
389 TmpL
.SetHex(R1L
.ToString());
390 BOOST_CHECK(R1L
.size() == 32);
391 BOOST_CHECK(R2L
.size() == 32);
392 BOOST_CHECK(ZeroL
.size() == 32);
393 BOOST_CHECK(MaxL
.size() == 32);
394 BOOST_CHECK(R1L
.GetLow64() == R1LLow64
);
395 BOOST_CHECK(HalfL
.GetLow64() ==0x0000000000000000ULL
);
396 BOOST_CHECK(OneL
.GetLow64() ==0x0000000000000001ULL
);
398 for (unsigned int i
= 0; i
< 255; ++i
)
400 BOOST_CHECK((OneL
<< i
).getdouble() == ldexp(1.0,i
));
402 BOOST_CHECK(ZeroL
.getdouble() == 0.0);
403 for (int i
= 256; i
> 53; --i
)
404 BOOST_CHECK(almostEqual((R1L
>>(256-i
)).getdouble(), ldexp(R1Ldouble
,i
)));
405 uint64_t R1L64part
= (R1L
>>192).GetLow64();
406 for (int i
= 53; i
> 0; --i
) // doubles can store all integers in {0,...,2^54-1} exactly
408 BOOST_CHECK((R1L
>>(256-i
)).getdouble() == (double)(R1L64part
>> (64-i
)));
412 BOOST_AUTO_TEST_CASE(bignum_SetCompact
)
417 num
.SetCompact(0, &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);
423 num
.SetCompact(0x00123456, &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);
429 num
.SetCompact(0x01003456, &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);
435 num
.SetCompact(0x02000056, &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);
441 num
.SetCompact(0x03000000, &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);
447 num
.SetCompact(0x04000000, &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);
453 num
.SetCompact(0x00923456, &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);
459 num
.SetCompact(0x01803456, &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);
465 num
.SetCompact(0x02800056, &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);
471 num
.SetCompact(0x03800000, &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);
477 num
.SetCompact(0x04800000, &fNegative
, &fOverflow
);
478 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
479 BOOST_CHECK_EQUAL(num
.GetCompact(), 0U);
480 BOOST_CHECK_EQUAL(fNegative
, false);
481 BOOST_CHECK_EQUAL(fOverflow
, false);
483 num
.SetCompact(0x01123456, &fNegative
, &fOverflow
);
484 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
485 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x01120000U
);
486 BOOST_CHECK_EQUAL(fNegative
, false);
487 BOOST_CHECK_EQUAL(fOverflow
, false);
489 // Make sure that we don't generate compacts with the 0x00800000 bit set
491 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x02008000U
);
493 num
.SetCompact(0x01fedcba, &fNegative
, &fOverflow
);
494 BOOST_CHECK_EQUAL(num
.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
495 BOOST_CHECK_EQUAL(num
.GetCompact(true), 0x01fe0000U
);
496 BOOST_CHECK_EQUAL(fNegative
, true);
497 BOOST_CHECK_EQUAL(fOverflow
, false);
499 num
.SetCompact(0x02123456, &fNegative
, &fOverflow
);
500 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
501 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x02123400U
);
502 BOOST_CHECK_EQUAL(fNegative
, false);
503 BOOST_CHECK_EQUAL(fOverflow
, false);
505 num
.SetCompact(0x03123456, &fNegative
, &fOverflow
);
506 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
507 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x03123456U
);
508 BOOST_CHECK_EQUAL(fNegative
, false);
509 BOOST_CHECK_EQUAL(fOverflow
, false);
511 num
.SetCompact(0x04123456, &fNegative
, &fOverflow
);
512 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
513 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x04123456U
);
514 BOOST_CHECK_EQUAL(fNegative
, false);
515 BOOST_CHECK_EQUAL(fOverflow
, false);
517 num
.SetCompact(0x04923456, &fNegative
, &fOverflow
);
518 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
519 BOOST_CHECK_EQUAL(num
.GetCompact(true), 0x04923456U
);
520 BOOST_CHECK_EQUAL(fNegative
, true);
521 BOOST_CHECK_EQUAL(fOverflow
, false);
523 num
.SetCompact(0x05009234, &fNegative
, &fOverflow
);
524 BOOST_CHECK_EQUAL(num
.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
525 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x05009234U
);
526 BOOST_CHECK_EQUAL(fNegative
, false);
527 BOOST_CHECK_EQUAL(fOverflow
, false);
529 num
.SetCompact(0x20123456, &fNegative
, &fOverflow
);
530 BOOST_CHECK_EQUAL(num
.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
531 BOOST_CHECK_EQUAL(num
.GetCompact(), 0x20123456U
);
532 BOOST_CHECK_EQUAL(fNegative
, false);
533 BOOST_CHECK_EQUAL(fOverflow
, false);
535 num
.SetCompact(0xff123456, &fNegative
, &fOverflow
);
536 BOOST_CHECK_EQUAL(fNegative
, false);
537 BOOST_CHECK_EQUAL(fOverflow
, true);
541 BOOST_AUTO_TEST_CASE( getmaxcoverage
) // some more tests just to get 100% coverage
543 // ~R1L give a base_uint<256>
544 BOOST_CHECK((~~R1L
>> 10) == (R1L
>> 10));
545 BOOST_CHECK((~~R1L
<< 10) == (R1L
<< 10));
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 BOOST_CHECK(!(R1L
> ~~R1L
));
553 BOOST_CHECK(R1L
>= ~~R1L
);
555 BOOST_CHECK(~~R1L
+ R2L
== R1L
+ ~~R2L
);
556 BOOST_CHECK(~~R1L
- R2L
== R1L
- ~~R2L
);
557 BOOST_CHECK(~R1L
!= R1L
); BOOST_CHECK(R1L
!= ~R1L
);
558 unsigned char TmpArray
[32];
559 CHECKBITWISEOPERATOR(~R1
,R2
,|)
560 CHECKBITWISEOPERATOR(~R1
,R2
,^)
561 CHECKBITWISEOPERATOR(~R1
,R2
,&)
562 CHECKBITWISEOPERATOR(R1
,~R2
,|)
563 CHECKBITWISEOPERATOR(R1
,~R2
,^)
564 CHECKBITWISEOPERATOR(R1
,~R2
,&)
567 BOOST_AUTO_TEST_SUITE_END()