util: Work around (virtual) memory exhaustion on 32-bit w/ glibc
[bitcoinplatinum.git] / src / test / uint256_tests.cpp
blob70d83a2e54061a82f8e429c70f34cff0b989be88
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.
4 #include "arith_uint256.h"
5 #include "uint256.h"
6 #include "version.h"
7 #include "test/test_bitcoin.h"
9 #include <boost/test/unit_test.hpp>
10 #include <stdint.h>
11 #include <sstream>
12 #include <iomanip>
13 #include <limits>
14 #include <cmath>
15 #include <string>
16 #include <stdio.h>
18 BOOST_FIXTURE_TEST_SUITE(uint256_tests, BasicTestingSetup)
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));
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));
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));
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));
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));
51 std::string ArrayToString(const unsigned char A[], unsigned int width)
53 std::stringstream Stream;
54 Stream << std::hex;
55 for (unsigned int i = 0; i < width; ++i)
57 Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
59 return Stream.str();
62 inline uint160 uint160S(const char *str)
64 uint160 rv;
65 rv.SetHex(str);
66 return rv;
68 inline uint160 uint160S(const std::string& str)
70 uint160 rv;
71 rv.SetHex(str);
72 return rv;
75 BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
77 BOOST_CHECK(1 == 0+1);
78 // constructor uint256(vector<char>):
79 BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
80 BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20));
81 BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
82 BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20));
83 BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
84 BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20));
85 BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
86 BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20));
87 BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
88 BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20));
89 BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
90 BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20));
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);
98 // String Constructor and Copy Constructor
99 BOOST_CHECK(uint256S("0x"+R1L.ToString()) == R1L);
100 BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L);
101 BOOST_CHECK(uint256S("0x"+ZeroL.ToString()) == ZeroL);
102 BOOST_CHECK(uint256S("0x"+OneL.ToString()) == OneL);
103 BOOST_CHECK(uint256S("0x"+MaxL.ToString()) == MaxL);
104 BOOST_CHECK(uint256S(R1L.ToString()) == R1L);
105 BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L);
106 BOOST_CHECK(uint256S("") == ZeroL);
107 BOOST_CHECK(R1L == uint256S(R1ArrayHex));
108 BOOST_CHECK(uint256(R1L) == R1L);
109 BOOST_CHECK(uint256(ZeroL) == ZeroL);
110 BOOST_CHECK(uint256(OneL) == OneL);
112 BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S);
113 BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S);
114 BOOST_CHECK(uint160S("0x"+ZeroS.ToString()) == ZeroS);
115 BOOST_CHECK(uint160S("0x"+OneS.ToString()) == OneS);
116 BOOST_CHECK(uint160S("0x"+MaxS.ToString()) == MaxS);
117 BOOST_CHECK(uint160S(R1S.ToString()) == R1S);
118 BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S);
119 BOOST_CHECK(uint160S("") == ZeroS);
120 BOOST_CHECK(R1S == uint160S(R1ArrayHex));
122 BOOST_CHECK(uint160(R1S) == R1S);
123 BOOST_CHECK(uint160(ZeroS) == ZeroS);
124 BOOST_CHECK(uint160(OneS) == OneS);
127 BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
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;
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 );
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;
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 );
159 BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
161 BOOST_CHECK(R1L.GetHex() == R1L.ToString());
162 BOOST_CHECK(R2L.GetHex() == R2L.ToString());
163 BOOST_CHECK(OneL.GetHex() == OneL.ToString());
164 BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
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());
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());
187 BOOST_CHECK(GetSerializeSize(R1L, 0, PROTOCOL_VERSION) == 32);
188 BOOST_CHECK(GetSerializeSize(ZeroL, 0, PROTOCOL_VERSION) == 32);
190 CDataStream ss(0, PROTOCOL_VERSION);
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();
207 BOOST_CHECK(R1S.GetHex() == R1S.ToString());
208 BOOST_CHECK(R2S.GetHex() == R2S.ToString());
209 BOOST_CHECK(OneS.GetHex() == OneS.ToString());
210 BOOST_CHECK(MaxS.GetHex() == MaxS.ToString());
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());
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());
233 BOOST_CHECK(GetSerializeSize(R1S, 0, PROTOCOL_VERSION) == 20);
234 BOOST_CHECK(GetSerializeSize(ZeroS, 0, PROTOCOL_VERSION) == 20);
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();
253 BOOST_AUTO_TEST_CASE( conversion )
255 BOOST_CHECK(ArithToUint256(UintToArith256(ZeroL)) == ZeroL);
256 BOOST_CHECK(ArithToUint256(UintToArith256(OneL)) == OneL);
257 BOOST_CHECK(ArithToUint256(UintToArith256(R1L)) == R1L);
258 BOOST_CHECK(ArithToUint256(UintToArith256(R2L)) == R2L);
259 BOOST_CHECK(UintToArith256(ZeroL) == 0);
260 BOOST_CHECK(UintToArith256(OneL) == 1);
261 BOOST_CHECK(ArithToUint256(0) == ZeroL);
262 BOOST_CHECK(ArithToUint256(1) == OneL);
263 BOOST_CHECK(arith_uint256(R1L.GetHex()) == UintToArith256(R1L));
264 BOOST_CHECK(arith_uint256(R2L.GetHex()) == UintToArith256(R2L));
265 BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex());
266 BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
269 BOOST_AUTO_TEST_SUITE_END()