Bitcoin Core  22.99.0
P2P Digital Currency
uint256_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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 <streams.h>
8 #include <uint256.h>
9 #include <version.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 #include <iomanip>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 BOOST_AUTO_TEST_SUITE(uint256_tests)
19 
20 const unsigned char R1Array[] =
21  "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
22  "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
23 const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
24 const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32));
25 const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20));
26 
27 const unsigned char R2Array[] =
28  "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
29  "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
30 const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32));
31 const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20));
32 
33 const unsigned char ZeroArray[] =
34  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
35  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
36 const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
37 const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
38 
39 const unsigned char OneArray[] =
40  "\x01\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 uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32));
43 const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20));
44 
45 const unsigned char MaxArray[] =
46  "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
47  "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
48 const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
49 const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
50 
51 static std::string ArrayToString(const unsigned char A[], unsigned int width)
52 {
53  std::stringstream Stream;
54  Stream << std::hex;
55  for (unsigned int i = 0; i < width; ++i)
56  {
57  Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
58  }
59  return Stream.str();
60 }
61 
62 inline uint160 uint160S(const char *str)
63 {
64  uint160 rv;
65  rv.SetHex(str);
66  return rv;
67 }
68 inline uint160 uint160S(const std::string& str)
69 {
70  uint160 rv;
71  rv.SetHex(str);
72  return rv;
73 }
74 
75 BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
76 {
77  BOOST_CHECK(1 == 0+1);
78  // constructor 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 
98  // String Constructor and Copy Constructor
99  BOOST_CHECK(uint256S("0x"+R1L.ToString()) == R1L);
100  BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L);
102  BOOST_CHECK(uint256S("0x"+OneL.ToString()) == OneL);
103  BOOST_CHECK(uint256S("0x"+MaxL.ToString()) == MaxL);
105  BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L);
106  BOOST_CHECK(uint256S("") == ZeroL);
108  BOOST_CHECK(uint256(R1L) == R1L);
111 
112  BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S);
113  BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S);
115  BOOST_CHECK(uint160S("0x"+OneS.ToString()) == OneS);
116  BOOST_CHECK(uint160S("0x"+MaxS.ToString()) == MaxS);
118  BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S);
119  BOOST_CHECK(uint160S("") == ZeroS);
121 
122  BOOST_CHECK(uint160(R1S) == R1S);
125 }
126 
127 BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
128 {
129  uint256 LastL;
130  for (int i = 255; i >= 0; --i) {
131  uint256 TmpL;
132  *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7));
133  BOOST_CHECK( LastL < TmpL );
134  LastL = TmpL;
135  }
136 
137  BOOST_CHECK( ZeroL < R1L );
138  BOOST_CHECK( R2L < R1L );
139  BOOST_CHECK( ZeroL < OneL );
140  BOOST_CHECK( OneL < MaxL );
141  BOOST_CHECK( R1L < MaxL );
142  BOOST_CHECK( R2L < MaxL );
143 
144  uint160 LastS;
145  for (int i = 159; i >= 0; --i) {
146  uint160 TmpS;
147  *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7));
148  BOOST_CHECK( LastS < TmpS );
149  LastS = TmpS;
150  }
151  BOOST_CHECK( ZeroS < R1S );
152  BOOST_CHECK( R2S < R1S );
153  BOOST_CHECK( ZeroS < OneS );
154  BOOST_CHECK( OneS < MaxS );
155  BOOST_CHECK( R1S < MaxS );
156  BOOST_CHECK( R2S < MaxS );
157 }
158 
159 BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
160 {
165  uint256 TmpL(R1L);
166  BOOST_CHECK(TmpL == R1L);
167  TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
168  TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == uint256());
169 
170  TmpL.SetHex(R1L.ToString());
171  BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0);
172  BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0);
173  BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0);
174  BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0);
175  BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0);
176  BOOST_CHECK(R1L.size() == sizeof(R1L));
177  BOOST_CHECK(sizeof(R1L) == 32);
178  BOOST_CHECK(R1L.size() == 32);
179  BOOST_CHECK(R2L.size() == 32);
180  BOOST_CHECK(ZeroL.size() == 32);
181  BOOST_CHECK(MaxL.size() == 32);
182  BOOST_CHECK(R1L.begin() + 32 == R1L.end());
183  BOOST_CHECK(R2L.begin() + 32 == R2L.end());
184  BOOST_CHECK(OneL.begin() + 32 == OneL.end());
185  BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
186  BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
189 
191  ss << R1L;
192  BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32));
193  ss >> TmpL;
194  BOOST_CHECK(R1L == TmpL);
195  ss.clear();
196  ss << ZeroL;
197  BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32));
198  ss >> TmpL;
199  BOOST_CHECK(ZeroL == TmpL);
200  ss.clear();
201  ss << MaxL;
202  BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32));
203  ss >> TmpL;
204  BOOST_CHECK(MaxL == TmpL);
205  ss.clear();
206 
211  uint160 TmpS(R1S);
212  BOOST_CHECK(TmpS == R1S);
213  TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S);
214  TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == uint160());
215 
216  TmpS.SetHex(R1S.ToString());
217  BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0);
218  BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0);
219  BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0);
220  BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0);
221  BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0);
222  BOOST_CHECK(R1S.size() == sizeof(R1S));
223  BOOST_CHECK(sizeof(R1S) == 20);
224  BOOST_CHECK(R1S.size() == 20);
225  BOOST_CHECK(R2S.size() == 20);
226  BOOST_CHECK(ZeroS.size() == 20);
227  BOOST_CHECK(MaxS.size() == 20);
228  BOOST_CHECK(R1S.begin() + 20 == R1S.end());
229  BOOST_CHECK(R2S.begin() + 20 == R2S.end());
230  BOOST_CHECK(OneS.begin() + 20 == OneS.end());
231  BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
232  BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
235 
236  ss << R1S;
237  BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
238  ss >> TmpS;
239  BOOST_CHECK(R1S == TmpS);
240  ss.clear();
241  ss << ZeroS;
242  BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
243  ss >> TmpS;
244  BOOST_CHECK(ZeroS == TmpS);
245  ss.clear();
246  ss << MaxS;
247  BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
248  ss >> TmpS;
249  BOOST_CHECK(MaxS == TmpS);
250  ss.clear();
251 }
252 
253 BOOST_AUTO_TEST_CASE( conversion )
254 {
265  BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex());
266  BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
267 }
268 
269 BOOST_AUTO_TEST_CASE( operator_with_self )
270 {
272  v *= v;
273  BOOST_CHECK(v == UintToArith256(uint256S("04")));
274  v /= v;
275  BOOST_CHECK(v == UintToArith256(uint256S("01")));
276  v += v;
277  BOOST_CHECK(v == UintToArith256(uint256S("02")));
278  v -= v;
279  BOOST_CHECK(v == UintToArith256(uint256S("0")));
280 }
281 
283 {
284  uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
286 }
287 
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1080
OneArray
const unsigned char OneArray[]
Definition: uint256_tests.cpp:39
arith_uint256.h
R1Array
const unsigned char R1Array[]
Definition: uint256_tests.cpp:20
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
R1ArrayHex
const char R1ArrayHex[]
Definition: uint256_tests.cpp:23
uint160S
uint160 uint160S(const char *str)
Definition: uint256_tests.cpp:62
streams.h
setup_common.h
R2L
const uint256 R2L
Definition: uint256_tests.cpp:30
uint256.h
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:250
base_blob::size
static constexpr unsigned int size()
Definition: uint256.h:78
ZeroS
const uint160 ZeroS
Definition: uint256_tests.cpp:37
version.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
R2Array
const unsigned char R2Array[]
Definition: uint256_tests.cpp:27
ZeroL
const uint256 ZeroL
Definition: uint256_tests.cpp:36
base_blob::GetHex
std::string GetHex() const
Definition: uint256.cpp:20
MaxS
const uint160 MaxS
Definition: uint256_tests.cpp:49
R1L
const uint256 R1L
Definition: uint256_tests.cpp:24
base_blob::ToString
std::string ToString() const
Definition: uint256.cpp:64
uint256
256-bit opaque blob.
Definition: uint256.h:124
uint256S
uint256 uint256S(const char *str)
Definition: uint256.h:137
uint160
160-bit opaque blob.
Definition: uint256.h:113
CDataStream::clear
void clear()
Definition: streams.h:261
UintToArith256
arith_uint256 UintToArith256(const uint256 &a)
Definition: arith_uint256.cpp:253
MaxArray
const unsigned char MaxArray[]
Definition: uint256_tests.cpp:45
R2S
const uint160 R2S
Definition: uint256_tests.cpp:31
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
ZeroArray
const unsigned char ZeroArray[]
Definition: uint256_tests.cpp:33
R1S
const uint160 R1S
Definition: uint256_tests.cpp:25
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(basics)
Definition: uint256_tests.cpp:75
MaxL
const uint256 MaxL
Definition: uint256_tests.cpp:48
OneS
const uint160 OneS
Definition: uint256_tests.cpp:43
CDataStream::str
std::string str() const
Definition: streams.h:242
base_blob::SetHex
void SetHex(const char *psz)
Definition: uint256.cpp:30
OneL
const uint256 OneL
Definition: uint256_tests.cpp:42
base_blob::begin
unsigned char * begin()
Definition: uint256.h:58
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
ArithToUint256
uint256 ArithToUint256(const arith_uint256 &a)
Definition: arith_uint256.cpp:246
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
base_blob::end
unsigned char * end()
Definition: uint256.h:63
ArrayToString
static std::string ArrayToString(const unsigned char A[], unsigned int width)
Definition: uint256_tests.cpp:51
uint256::ONE
static const uint256 ONE
Definition: uint256.h:130