Branch data Line data Source code
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 <test/util/setup_common.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 : : /// Convert vector to arith_uint256, via uint256 blob
22 : 1947 : static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
23 : : {
24 : 1947 : return UintToArith256(uint256(vch));
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 arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32));
32 : : const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
33 : :
34 : : const unsigned char R2Array[] =
35 : : "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
36 : : "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
37 : : const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));
38 : :
39 : : const unsigned char ZeroArray[] =
40 : : "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
41 : : "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
42 : : const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
43 : :
44 : : const unsigned char OneArray[] =
45 : : "\x01\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 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32));
48 : :
49 : : const unsigned char MaxArray[] =
50 : : "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
51 : : "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
52 : : const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32));
53 : :
54 : : const arith_uint256 HalfL = (OneL << 255);
55 : 6 : static std::string ArrayToString(const unsigned char A[], unsigned int width)
56 : : {
57 : 6 : std::stringstream Stream;
58 : 6 : Stream << std::hex;
59 [ + + ]: 198 : for (unsigned int i = 0; i < width; ++i)
60 : : {
61 [ + - + - ]: 192 : Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
62 : : }
63 [ + - ]: 12 : return Stream.str();
64 : 6 : }
65 : :
66 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
67 : : {
68 [ + - ]: 2 : BOOST_CHECK(1 == 0+1);
69 : : // constructor arith_uint256(vector<char>):
70 [ + - + - : 2 : BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
+ - ]
71 [ + - + - : 2 : BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
+ - ]
72 [ + - + - : 2 : BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
+ - ]
73 [ + - + - : 2 : BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
+ - ]
74 [ + - + - : 2 : BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
+ - ]
75 [ + - + - : 2 : BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
+ - ]
76 : :
77 : : // == and !=
78 [ + - ]: 2 : BOOST_CHECK(R1L != R2L);
79 [ + - ]: 2 : BOOST_CHECK(ZeroL != OneL);
80 [ + - ]: 2 : BOOST_CHECK(OneL != ZeroL);
81 [ + - ]: 2 : BOOST_CHECK(MaxL != ZeroL);
82 [ + - ]: 2 : BOOST_CHECK(~MaxL == ZeroL);
83 [ + - + - ]: 2 : BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
84 : :
85 : 1 : uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
86 [ + + ]: 257 : for (unsigned int i = 0; i < 256; ++i)
87 : : {
88 [ + - + - ]: 512 : BOOST_CHECK(ZeroL != (OneL << i));
89 [ + - + - ]: 512 : BOOST_CHECK((OneL << i) != ZeroL);
90 [ + - + - ]: 512 : BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
91 [ + - + - : 768 : BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
+ - ]
92 : : }
93 [ + - + - ]: 2 : BOOST_CHECK(ZeroL == (OneL << 256));
94 : :
95 : : // Construct from hex string
96 [ + - + - : 2 : BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1L.ToString()).value()), R1L);
+ - ]
97 [ + - + - : 2 : BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R2L.ToString()).value()), R2L);
+ - ]
98 [ + - + - : 2 : BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(ZeroL.ToString()).value()), ZeroL);
+ - ]
99 [ + - + - : 2 : BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(OneL.ToString()).value()), OneL);
+ - ]
100 [ + - + - : 2 : BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(MaxL.ToString()).value()), MaxL);
+ - ]
101 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1ArrayHex).value()), R1L);
102 : :
103 : : // Copy constructor
104 [ + - ]: 3 : BOOST_CHECK(arith_uint256(R1L) == R1L);
105 [ + - ]: 3 : BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
106 [ + - ]: 3 : BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
107 [ + - ]: 3 : BOOST_CHECK(arith_uint256(OneL) == OneL);
108 : :
109 : : // uint64_t constructor
110 [ + - ]: 3 : BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});
111 [ + - ]: 2 : BOOST_CHECK_EQUAL(ZeroL, arith_uint256{0});
112 [ + - ]: 2 : BOOST_CHECK_EQUAL(OneL, arith_uint256{1});
113 [ + - ]: 3 : BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});
114 : :
115 : : // Assignment (from base_uint)
116 [ + - ]: 3 : arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
117 [ + + + - ]: 11 : tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
118 [ + + + - ]: 11 : tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
119 [ + + + - ]: 11 : tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
120 [ + + + - ]: 11 : tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
121 : 1 : }
122 : :
123 : 512 : static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
124 : : {
125 [ + + ]: 16896 : for (unsigned int T=0; T < arrayLength; ++T)
126 : : {
127 : 16384 : unsigned int F = (T+bitsToShift/8);
128 [ + + ]: 16384 : if (F < arrayLength)
129 : 8448 : to[T] = uint8_t(from[F] >> (bitsToShift % 8));
130 : : else
131 : 7936 : to[T] = 0;
132 [ + + ]: 16384 : if (F + 1 < arrayLength)
133 : 7936 : to[T] |= uint8_t(from[(F + 1)] << (8 - bitsToShift % 8));
134 : : }
135 : 512 : }
136 : :
137 : 768 : static void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
138 : : {
139 [ + + ]: 25344 : for (unsigned int T=0; T < arrayLength; ++T)
140 : : {
141 [ + + ]: 24576 : if (T >= bitsToShift/8)
142 : : {
143 : 12672 : unsigned int F = T-bitsToShift/8;
144 : 12672 : to[T] = uint8_t(from[F] << (bitsToShift % 8));
145 [ + + ]: 12672 : if (T >= bitsToShift/8+1)
146 : 11904 : to[T] |= uint8_t(from[F - 1] >> (8 - bitsToShift % 8));
147 : : }
148 : : else {
149 : 11904 : to[T] = 0;
150 : : }
151 : : }
152 : 768 : }
153 : :
154 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
155 : 1 : unsigned char TmpArray[32];
156 : 1 : arith_uint256 TmpL;
157 [ + + ]: 257 : for (unsigned int i = 0; i < 256; ++i)
158 : : {
159 : 256 : shiftArrayLeft(TmpArray, OneArray, 32, i);
160 [ + - + - : 512 : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
+ - + - ]
161 : 512 : TmpL = OneL; TmpL <<= i;
162 [ + - + - ]: 512 : BOOST_CHECK(TmpL == (OneL << i));
163 [ + - + - : 512 : BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
+ - ]
164 : 512 : TmpL = HalfL; TmpL >>= (255-i);
165 [ + - + - ]: 512 : BOOST_CHECK(TmpL == (OneL << i));
166 : :
167 : 256 : shiftArrayLeft(TmpArray, R1Array, 32, i);
168 [ + - + - : 512 : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
+ - + - ]
169 : 512 : TmpL = R1L; TmpL <<= i;
170 [ + - + - ]: 512 : BOOST_CHECK(TmpL == (R1L << i));
171 : :
172 : 256 : shiftArrayRight(TmpArray, R1Array, 32, i);
173 [ + - + - : 512 : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
+ - + - ]
174 : 512 : TmpL = R1L; TmpL >>= i;
175 [ + - + - ]: 512 : BOOST_CHECK(TmpL == (R1L >> i));
176 : :
177 : 256 : shiftArrayLeft(TmpArray, MaxArray, 32, i);
178 [ + - + - : 512 : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
+ - + - ]
179 : 512 : TmpL = MaxL; TmpL <<= i;
180 [ + - + - ]: 512 : BOOST_CHECK(TmpL == (MaxL << i));
181 : :
182 : 256 : shiftArrayRight(TmpArray, MaxArray, 32, i);
183 [ + - + - : 512 : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
+ - + - ]
184 : 512 : TmpL = MaxL; TmpL >>= i;
185 [ + - + - ]: 512 : BOOST_CHECK(TmpL == (MaxL >> i));
186 : : }
187 : 1 : arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
188 : 1 : arith_uint256 c2L = c1L << 128;
189 [ + + ]: 129 : for (unsigned int i = 0; i < 128; ++i) {
190 [ + - + - : 256 : BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
+ - ]
191 : : }
192 [ + + ]: 129 : for (unsigned int i = 128; i < 256; ++i) {
193 [ + - + - : 256 : BOOST_CHECK((c1L << i) == (c2L << (i-128)));
+ - ]
194 : : }
195 : 1 : }
196 : :
197 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
198 : : {
199 [ + - ]: 2 : BOOST_CHECK(~ZeroL == MaxL);
200 : :
201 : 1 : unsigned char TmpArray[32];
202 [ + + ]: 33 : for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(~R1Array[i]); }
203 [ + - + - : 2 : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
+ - ]
204 : :
205 [ + - ]: 2 : BOOST_CHECK(-ZeroL == ZeroL);
206 [ + - ]: 3 : BOOST_CHECK(-R1L == (~R1L)+1);
207 [ + + ]: 257 : for (unsigned int i = 0; i < 256; ++i)
208 [ + - + - : 512 : BOOST_CHECK(-(OneL<<i) == (MaxL << i));
+ - ]
209 : 1 : }
210 : :
211 : :
212 : : // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
213 : : // element of Aarray and Barray, and then converting the result into an arith_uint256.
214 : : #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
215 : : for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(_A_##Array[i] _OP_ _B_##Array[i]); } \
216 : : BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
217 : :
218 : : #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
219 : : TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
220 : :
221 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( bitwiseOperators )
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
222 : : {
223 : 1 : unsigned char TmpArray[32];
224 : :
225 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,R2,|)
+ - + - ]
226 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,R2,^)
+ - + - ]
227 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,R2,&)
+ - + - ]
228 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,Zero,|)
+ - + - ]
229 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,Zero,^)
+ - + - ]
230 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,Zero,&)
+ - + - ]
231 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,Max,|)
+ - + - ]
232 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,Max,^)
+ - + - ]
233 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,Max,&)
+ - + - ]
234 [ + + + - : 34 : CHECKBITWISEOPERATOR(Zero,R1,|)
+ - + - ]
235 [ + + + - : 34 : CHECKBITWISEOPERATOR(Zero,R1,^)
+ - + - ]
236 [ + + + - : 34 : CHECKBITWISEOPERATOR(Zero,R1,&)
+ - + - ]
237 [ + + + - : 34 : CHECKBITWISEOPERATOR(Max,R1,|)
+ - + - ]
238 [ + + + - : 34 : CHECKBITWISEOPERATOR(Max,R1,^)
+ - + - ]
239 [ + + + - : 34 : CHECKBITWISEOPERATOR(Max,R1,&)
+ - + - ]
240 : :
241 [ + + ]: 10 : arith_uint256 TmpL;
242 [ + + + - ]: 10 : CHECKASSIGNMENTOPERATOR(R1,R2,|)
243 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,R2,^)
244 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,R2,&)
245 [ + - ]: 3 : CHECKASSIGNMENTOPERATOR(R1,Zero,|)
246 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,Zero,^)
247 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,Zero,&)
248 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,Max,|)
249 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,Max,^)
250 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(R1,Max,&)
251 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(Zero,R1,|)
252 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(Zero,R1,^)
253 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(Zero,R1,&)
254 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(Max,R1,|)
255 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(Max,R1,^)
256 [ + + + - ]: 11 : CHECKASSIGNMENTOPERATOR(Max,R1,&)
257 : :
258 : 1 : uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
259 [ + - ]: 4 : TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
260 [ + - ]: 3 : TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
261 [ + - ]: 2 : TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
262 [ + - ]: 3 : TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
263 : 1 : }
264 : :
265 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
266 : : {
267 : 1 : arith_uint256 TmpL;
268 [ + + ]: 257 : for (unsigned int i = 0; i < 256; ++i) {
269 [ + + ]: 2560 : TmpL= OneL<< i;
270 [ + - + - : 1280 : BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
+ - + - -
+ + - ]
271 [ + - + - : 1536 : BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
+ - + - +
- + - + -
- + + - ]
272 : 256 : TmpL |= R1L;
273 [ + - + - : 1286 : BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
+ - + - +
+ + - +
- ]
274 [ + - + - : 1286 : BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
+ - + - +
+ + - +
- ]
275 [ + - + - : 768 : BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
+ - + - ]
276 : : }
277 : :
278 [ + - ]: 1 : BOOST_CHECK_LT(ZeroL,
279 : : OneL);
280 : 1 : }
281 : :
282 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( plusMinus )
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
283 : : {
284 : 1 : arith_uint256 TmpL = 0;
285 [ + - ]: 1 : BOOST_CHECK_EQUAL(R1L + R2L, UintToArith256(uint256{"549fb09fea236a1ea3e31d4d58f1b1369288d204211ca751527cfc175767850c"}));
286 : 1 : TmpL += R1L;
287 [ + - ]: 2 : BOOST_CHECK(TmpL == R1L);
288 : 1 : TmpL += R2L;
289 [ + - ]: 2 : BOOST_CHECK(TmpL == R1L + R2L);
290 [ + - ]: 2 : BOOST_CHECK(OneL+MaxL == ZeroL);
291 [ + - ]: 2 : BOOST_CHECK(MaxL+OneL == ZeroL);
292 [ + + ]: 256 : for (unsigned int i = 1; i < 256; ++i) {
293 [ + - + - : 510 : BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
+ - ]
294 [ + - + - : 510 : BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
+ - ]
295 [ + + ]: 2550 : TmpL = (MaxL>>i); TmpL += OneL;
296 [ + - + - ]: 510 : BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
297 [ + + ]: 2550 : TmpL = (MaxL>>i); TmpL += 1;
298 [ + - + - ]: 510 : BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
299 [ + + ]: 2550 : TmpL = (MaxL>>i);
300 [ + - + - ]: 765 : BOOST_CHECK( TmpL++ == (MaxL>>i) );
301 [ + - + - ]: 510 : BOOST_CHECK( TmpL == (HalfL >> (i-1)));
302 : : }
303 [ + - ]: 5 : BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
304 [ + + ]: 10 : TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
305 [ + - ]: 3 : BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
306 [ + - + - ]: 2 : TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
307 : 1 : TmpL = R1L;
308 [ + - ]: 4 : BOOST_CHECK(++TmpL == R1L+1);
309 : :
310 [ + - ]: 2 : BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
311 [ + - ]: 2 : BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
312 [ + - ]: 2 : BOOST_CHECK(R1L - OneL == R1L+(-OneL));
313 [ + + ]: 256 : for (unsigned int i = 1; i < 256; ++i) {
314 [ + - + - : 510 : BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1)));
+ - ]
315 [ + - + - : 510 : BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
+ - ]
316 [ + + ]: 2550 : TmpL = (HalfL >> (i-1));
317 [ + - + - ]: 765 : BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
318 [ + - + - ]: 510 : BOOST_CHECK(TmpL == (MaxL >> i));
319 [ + + ]: 2550 : TmpL = (HalfL >> (i-1));
320 [ + - + - ]: 765 : BOOST_CHECK(--TmpL == (MaxL >> i));
321 : : }
322 : 1 : TmpL = R1L;
323 [ + - ]: 4 : BOOST_CHECK(--TmpL == R1L-1);
324 : 1 : }
325 : :
326 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( multiply )
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
327 : : {
328 [ + - + - : 2 : BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
+ - ]
329 [ + - + - : 2 : BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
+ - ]
330 [ + - + - ]: 2 : BOOST_CHECK((R1L * ZeroL) == ZeroL);
331 [ + - + - ]: 2 : BOOST_CHECK((R1L * OneL) == R1L);
332 [ + - + - ]: 2 : BOOST_CHECK((R1L * MaxL) == -R1L);
333 [ + - + - : 2 : BOOST_CHECK((R2L * R1L) == (R1L * R2L));
+ - ]
334 [ + - + - : 2 : BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
+ - ]
335 [ + - + - ]: 2 : BOOST_CHECK((R2L * ZeroL) == ZeroL);
336 [ + - + - ]: 2 : BOOST_CHECK((R2L * OneL) == R2L);
337 [ + - + - ]: 2 : BOOST_CHECK((R2L * MaxL) == -R2L);
338 : :
339 [ + - + - ]: 2 : BOOST_CHECK(MaxL * MaxL == OneL);
340 : :
341 [ + - + - ]: 3 : BOOST_CHECK((R1L * 0) == 0);
342 [ + - + - ]: 2 : BOOST_CHECK((R1L * 1) == R1L);
343 [ + - + - : 2 : BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
+ - ]
344 [ + - + - : 2 : BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
+ - ]
345 : 1 : }
346 : :
347 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( divide )
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
348 : : {
349 : 1 : arith_uint256 D1L{UintToArith256(uint256{"00000000000000000000000000000000000000000000000ad7133ac1977fa2b7"})};
350 : 1 : arith_uint256 D2L{UintToArith256(uint256{"0000000000000000000000000000000000000000000000000000000ecd751716"})};
351 [ + - + - : 2 : BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
+ - ]
352 [ + - + - : 2 : BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
+ - ]
353 [ + - + - ]: 2 : BOOST_CHECK(R1L / OneL == R1L);
354 [ + - + - ]: 2 : BOOST_CHECK(R1L / MaxL == ZeroL);
355 [ + - + - : 3 : BOOST_CHECK(MaxL / R1L == 2);
+ - ]
356 [ + - - + : 2 : BOOST_CHECK_THROW(R1L / ZeroL, uint_error);
- - - - -
+ + - +
- ]
357 [ + - + - : 2 : BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
+ - ]
358 [ + - + - : 2 : BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
+ - ]
359 [ + - + - ]: 2 : BOOST_CHECK(R2L / OneL == R2L);
360 [ + - + - ]: 2 : BOOST_CHECK(R2L / MaxL == ZeroL);
361 [ + - + - : 3 : BOOST_CHECK(MaxL / R2L == 1);
+ - ]
362 [ + - - + : 2 : BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
- - - - -
+ + - +
- ]
363 : 1 : }
364 : :
365 : :
366 : 203 : static bool almostEqual(double d1, double d2)
367 : : {
368 : 203 : return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
369 : : }
370 : :
371 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(methods) // GetHex operator= size() GetLow64 GetSerializeSize, Serialize, Unserialize
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
372 : : {
373 [ + - + - : 2 : BOOST_CHECK(R1L.GetHex() == R1L.ToString());
+ - ]
374 [ + - + - : 2 : BOOST_CHECK(R2L.GetHex() == R2L.ToString());
+ - ]
375 [ + - + - : 2 : BOOST_CHECK(OneL.GetHex() == OneL.ToString());
+ - ]
376 [ + - + - : 2 : BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
+ - ]
377 : 1 : arith_uint256 TmpL(R1L);
378 [ + - ]: 2 : BOOST_CHECK(TmpL == R1L);
379 : 1 : TmpL = R2L;
380 [ + - ]: 2 : BOOST_CHECK(TmpL == R2L);
381 : 1 : TmpL = ZeroL;
382 [ + - + - ]: 2 : BOOST_CHECK(TmpL == 0);
383 : 1 : TmpL = HalfL;
384 [ + - ]: 2 : BOOST_CHECK(TmpL == HalfL);
385 : :
386 : 1 : TmpL = R1L;
387 [ + - ]: 2 : BOOST_CHECK(R1L.size() == 32);
388 [ + - ]: 2 : BOOST_CHECK(R2L.size() == 32);
389 [ + - ]: 2 : BOOST_CHECK(ZeroL.size() == 32);
390 [ + - ]: 2 : BOOST_CHECK(MaxL.size() == 32);
391 [ + - ]: 2 : BOOST_CHECK(R1L.GetLow64() == R1LLow64);
392 [ + - ]: 2 : BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
393 [ + - ]: 2 : BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
394 : :
395 [ + + ]: 256 : for (unsigned int i = 0; i < 255; ++i)
396 : : {
397 [ + - + - : 510 : BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
+ - ]
398 : : }
399 [ + - + - ]: 2 : BOOST_CHECK(ZeroL.getdouble() == 0.0);
400 [ + + ]: 204 : for (int i = 256; i > 53; --i)
401 [ + - + - : 406 : BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
+ - ]
402 : 1 : uint64_t R1L64part = (R1L>>192).GetLow64();
403 [ + + ]: 54 : for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
404 : : {
405 [ + - + - : 106 : BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
+ - ]
406 : : }
407 : 1 : }
408 : :
409 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(bignum_SetCompact)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
410 : : {
411 : 1 : arith_uint256 num;
412 : 1 : bool fNegative;
413 : 1 : bool fOverflow;
414 : 1 : num.SetCompact(0, &fNegative, &fOverflow);
415 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
416 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
417 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
418 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
419 : :
420 : 1 : num.SetCompact(0x00123456, &fNegative, &fOverflow);
421 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
422 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
423 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
424 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
425 : :
426 : 1 : num.SetCompact(0x01003456, &fNegative, &fOverflow);
427 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
428 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
429 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
430 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
431 : :
432 : 1 : num.SetCompact(0x02000056, &fNegative, &fOverflow);
433 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
434 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
435 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
436 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
437 : :
438 : 1 : num.SetCompact(0x03000000, &fNegative, &fOverflow);
439 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
440 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
441 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
442 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
443 : :
444 : 1 : num.SetCompact(0x04000000, &fNegative, &fOverflow);
445 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
446 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
447 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
448 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
449 : :
450 : 1 : num.SetCompact(0x00923456, &fNegative, &fOverflow);
451 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
452 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
453 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
454 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
455 : :
456 : 1 : num.SetCompact(0x01803456, &fNegative, &fOverflow);
457 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
458 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
459 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
460 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
461 : :
462 : 1 : num.SetCompact(0x02800056, &fNegative, &fOverflow);
463 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
464 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
465 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
466 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
467 : :
468 : 1 : num.SetCompact(0x03800000, &fNegative, &fOverflow);
469 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
470 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
471 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
472 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
473 : :
474 : 1 : num.SetCompact(0x04800000, &fNegative, &fOverflow);
475 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
476 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
477 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
478 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
479 : :
480 : 1 : num.SetCompact(0x01123456, &fNegative, &fOverflow);
481 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
482 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
483 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
484 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
485 : :
486 : : // Make sure that we don't generate compacts with the 0x00800000 bit set
487 [ + + ]: 10 : num = 0x80;
488 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
489 : :
490 : 1 : num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
491 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
492 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
493 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, true);
494 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
495 : :
496 : 1 : num.SetCompact(0x02123456, &fNegative, &fOverflow);
497 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
498 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
499 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
500 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
501 : :
502 : 1 : num.SetCompact(0x03123456, &fNegative, &fOverflow);
503 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
504 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
505 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
506 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
507 : :
508 : 1 : num.SetCompact(0x04123456, &fNegative, &fOverflow);
509 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
510 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
511 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
512 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
513 : :
514 : 1 : num.SetCompact(0x04923456, &fNegative, &fOverflow);
515 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
516 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
517 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, true);
518 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
519 : :
520 : 1 : num.SetCompact(0x05009234, &fNegative, &fOverflow);
521 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
522 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
523 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
524 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
525 : :
526 : 1 : num.SetCompact(0x20123456, &fNegative, &fOverflow);
527 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
528 [ + - ]: 1 : BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
529 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
530 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, false);
531 : :
532 : 1 : num.SetCompact(0xff123456, &fNegative, &fOverflow);
533 [ + - ]: 1 : BOOST_CHECK_EQUAL(fNegative, false);
534 [ + - ]: 1 : BOOST_CHECK_EQUAL(fOverflow, true);
535 : 1 : }
536 : :
537 : :
538 [ + - + - : 7 : BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
539 : : {
540 : : // ~R1L give a base_uint<256>
541 [ + - + - : 2 : BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
+ - ]
542 [ + - + - : 2 : BOOST_CHECK((~~R1L << 10) == (R1L << 10));
+ - ]
543 [ + - + - ]: 2 : BOOST_CHECK(!(~~R1L < R1L));
544 [ + - + - ]: 2 : BOOST_CHECK(~~R1L <= R1L);
545 [ + - + - ]: 2 : BOOST_CHECK(!(~~R1L > R1L));
546 [ + - + - ]: 2 : BOOST_CHECK(~~R1L >= R1L);
547 [ + - + - ]: 2 : BOOST_CHECK(!(R1L < ~~R1L));
548 [ + - + - ]: 2 : BOOST_CHECK(R1L <= ~~R1L);
549 [ + - + - ]: 2 : BOOST_CHECK(!(R1L > ~~R1L));
550 [ + - + - ]: 2 : BOOST_CHECK(R1L >= ~~R1L);
551 : :
552 [ + - ]: 2 : BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
553 [ + - ]: 2 : BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
554 [ + - + - ]: 3 : BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
555 : 1 : unsigned char TmpArray[32];
556 [ + + + - : 34 : CHECKBITWISEOPERATOR(~R1,R2,|)
+ - + - ]
557 [ + + + - : 34 : CHECKBITWISEOPERATOR(~R1,R2,^)
+ - + - ]
558 [ + + + - : 34 : CHECKBITWISEOPERATOR(~R1,R2,&)
+ - + - ]
559 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,~R2,|)
+ - + - ]
560 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,~R2,^)
+ - + - ]
561 [ + + + - : 34 : CHECKBITWISEOPERATOR(R1,~R2,&)
+ - + - ]
562 : 1 : }
563 : :
564 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(conversion)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
565 : : {
566 [ + + ]: 9 : for (const arith_uint256& arith : {ZeroL, OneL, R1L, R2L}) {
567 [ + - ]: 8 : const auto u256{uint256::FromHex(arith.GetHex()).value()};
568 [ + - ]: 4 : BOOST_CHECK_EQUAL(UintToArith256(ArithToUint256(arith)), arith);
569 [ + - ]: 4 : BOOST_CHECK_EQUAL(UintToArith256(u256), arith);
570 [ + - ]: 4 : BOOST_CHECK_EQUAL(u256, ArithToUint256(arith));
571 [ + - + - : 4 : BOOST_CHECK_EQUAL(ArithToUint256(arith).GetHex(), UintToArith256(u256).GetHex());
+ - ]
572 : : }
573 : :
574 [ + + ]: 4 : for (uint8_t num : {0, 1, 0xff}) {
575 [ + - ]: 6 : BOOST_CHECK_EQUAL(UintToArith256(uint256{num}), arith_uint256{num});
576 [ + - ]: 6 : BOOST_CHECK_EQUAL(uint256{num}, ArithToUint256(arith_uint256{num}));
577 [ + - ]: 3 : BOOST_CHECK_EQUAL(UintToArith256(uint256{num}), num);
578 : : }
579 : 1 : }
580 : :
581 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(operator_with_self)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
582 : : {
583 : : /* Clang 16 and earlier detects v -= v and v /= v as self-assignments
584 : : to 0 and 1 respectively.
585 : : See: https://github.com/llvm/llvm-project/issues/42469
586 : : and the fix in commit c5302325b2a62d77cf13dd16cd5c19141862fed0 .
587 : :
588 : : This makes some sense for arithmetic classes, but could be considered a bug
589 : : elsewhere. Disable the warning here so that the code can be tested, but the
590 : : warning should remain on as there will likely always be a better way to
591 : : express this.
592 : : */
593 : : #if defined(__clang__)
594 : : #pragma clang diagnostic push
595 : : #pragma clang diagnostic ignored "-Wself-assign-overloaded"
596 : : #endif
597 : 1 : arith_uint256 v{2};
598 : 1 : v *= v;
599 [ + - ]: 2 : BOOST_CHECK_EQUAL(v, arith_uint256{4});
600 : 1 : v /= v;
601 [ + - ]: 2 : BOOST_CHECK_EQUAL(v, arith_uint256{1});
602 : 1 : v += v;
603 [ + - ]: 2 : BOOST_CHECK_EQUAL(v, arith_uint256{2});
604 : 1 : v -= v;
605 [ + - ]: 2 : BOOST_CHECK_EQUAL(v, arith_uint256{0});
606 : : #if defined(__clang__)
607 : : #pragma clang diagnostic pop
608 : : #endif
609 : 1 : }
610 : :
611 : : BOOST_AUTO_TEST_SUITE_END()
|