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
18BOOST_AUTO_TEST_SUITE(uint256_tests)
19
20const 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";
23const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
24const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32));
25const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20));
26
27const 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";
30const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32));
31const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20));
32
33const 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";
36const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
37const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
38
39const 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";
42const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32));
43const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20));
44
45const 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";
48const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
49const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
50
51static 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
62inline uint160 uint160S(const char *str)
63{
64 uint160 rv;
65 rv.SetHex(str);
66 return rv;
67}
68inline uint160 uint160S(const std::string& str)
69{
70 uint160 rv;
71 rv.SetHex(str);
72 return rv;
73}
74
75BOOST_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);
97
98 // String Constructor and Copy Constructor
100 BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L);
105 BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L);
106 BOOST_CHECK(uint256S("") == ZeroL);
111
112 BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S);
113 BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S);
118 BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S);
119 BOOST_CHECK(uint160S("") == ZeroS);
121
125}
126
127BOOST_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 );
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 );
154 BOOST_CHECK( OneS < MaxS );
155 BOOST_CHECK( R1S < MaxS );
156 BOOST_CHECK( R2S < MaxS );
157}
158
159BOOST_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
254{
265 BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex());
266 BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
267}
268
269BOOST_AUTO_TEST_CASE( operator_with_self )
270{
272 v *= v;
274 v /= v;
276 v += v;
278 v -= v;
280}
281
283{
284 uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
286}
287
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
std::string str() const
Definition: streams.h:242
void clear()
Definition: streams.h:261
256-bit unsigned big integer.
static constexpr unsigned int size()
Definition: uint256.h:78
unsigned char * end()
Definition: uint256.h:63
void SetHex(const char *psz)
Definition: uint256.cpp:30
unsigned char * begin()
Definition: uint256.h:58
std::string ToString() const
Definition: uint256.cpp:64
std::string GetHex() const
Definition: uint256.cpp:20
160-bit opaque blob.
Definition: uint256.h:113
256-bit opaque blob.
Definition: uint256.h:124
static const uint256 ONE
Definition: uint256.h:130
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1080
uint256 uint256S(const char *str)
Definition: uint256.h:137
uint160 uint160S(const char *str)
const unsigned char ZeroArray[]
const unsigned char R1Array[]
BOOST_AUTO_TEST_CASE(basics)
const unsigned char OneArray[]
const uint160 OneS
const uint160 MaxS
const char R1ArrayHex[]
const uint160 ZeroS
const uint256 MaxL
const uint160 R1S
const unsigned char R2Array[]
const uint256 R2L
const uint160 R2S
const uint256 R1L
static std::string ArrayToString(const unsigned char A[], unsigned int width)
const unsigned char MaxArray[]
const uint256 ZeroL
const uint256 OneL
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12