|
| 1 | +// Copyright (c) 2011-2013 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 | + |
| 8 | +#include <boost/test/unit_test.hpp> |
| 9 | +#include <stdint.h> |
| 10 | +#include <sstream> |
| 11 | +#include <iomanip> |
| 12 | +#include <limits> |
| 13 | +#include <cmath> |
| 14 | +#include <string> |
| 15 | +#include <stdio.h> |
| 16 | + |
| 17 | +BOOST_AUTO_TEST_SUITE(uint256_tests) |
| 18 | + |
| 19 | +const unsigned char R1Array[] = |
| 20 | + "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2" |
| 21 | + "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; |
| 22 | +const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"; |
| 23 | +const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32)); |
| 24 | +const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20)); |
| 25 | + |
| 26 | +const unsigned char R2Array[] = |
| 27 | + "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf" |
| 28 | + "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"; |
| 29 | +const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32)); |
| 30 | +const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20)); |
| 31 | + |
| 32 | +const unsigned char ZeroArray[] = |
| 33 | + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 34 | + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; |
| 35 | +const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32)); |
| 36 | +const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20)); |
| 37 | + |
| 38 | +const unsigned char OneArray[] = |
| 39 | + "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 40 | + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; |
| 41 | +const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32)); |
| 42 | +const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20)); |
| 43 | + |
| 44 | +const unsigned char MaxArray[] = |
| 45 | + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" |
| 46 | + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; |
| 47 | +const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32)); |
| 48 | +const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20)); |
| 49 | + |
| 50 | +std::string ArrayToString(const unsigned char A[], unsigned int width) |
| 51 | +{ |
| 52 | + std::stringstream Stream; |
| 53 | + Stream << std::hex; |
| 54 | + for (unsigned int i = 0; i < width; ++i) |
| 55 | + { |
| 56 | + Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1]; |
| 57 | + } |
| 58 | + return Stream.str(); |
| 59 | +} |
| 60 | + |
| 61 | +inline uint160 uint160S(const char *str) |
| 62 | +{ |
| 63 | + uint160 rv; |
| 64 | + rv.SetHex(str); |
| 65 | + return rv; |
| 66 | +} |
| 67 | +inline uint160 uint160S(const std::string& str) |
| 68 | +{ |
| 69 | + uint160 rv; |
| 70 | + rv.SetHex(str); |
| 71 | + return rv; |
| 72 | +} |
| 73 | + |
| 74 | +BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality |
| 75 | +{ |
| 76 | + BOOST_CHECK(1 == 0+1); |
| 77 | + // constructor uint256(vector<char>): |
| 78 | + BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32)); |
| 79 | + BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20)); |
| 80 | + BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32)); |
| 81 | + BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20)); |
| 82 | + BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); |
| 83 | + BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20)); |
| 84 | + BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); |
| 85 | + BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20)); |
| 86 | + BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); |
| 87 | + BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20)); |
| 88 | + BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); |
| 89 | + BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20)); |
| 90 | + |
| 91 | + // == and != |
| 92 | + BOOST_CHECK(R1L != R2L && R1S != R2S); |
| 93 | + BOOST_CHECK(ZeroL != OneL && ZeroS != OneS); |
| 94 | + BOOST_CHECK(OneL != ZeroL && OneS != ZeroS); |
| 95 | + BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS); |
| 96 | + |
| 97 | + // String Constructor and Copy Constructor |
| 98 | + BOOST_CHECK(uint256S("0x"+R1L.ToString()) == R1L); |
| 99 | + BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L); |
| 100 | + BOOST_CHECK(uint256S("0x"+ZeroL.ToString()) == ZeroL); |
| 101 | + BOOST_CHECK(uint256S("0x"+OneL.ToString()) == OneL); |
| 102 | + BOOST_CHECK(uint256S("0x"+MaxL.ToString()) == MaxL); |
| 103 | + BOOST_CHECK(uint256S(R1L.ToString()) == R1L); |
| 104 | + BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L); |
| 105 | + BOOST_CHECK(uint256S("") == ZeroL); |
| 106 | + BOOST_CHECK(R1L == uint256S(R1ArrayHex)); |
| 107 | + BOOST_CHECK(uint256(R1L) == R1L); |
| 108 | + BOOST_CHECK(uint256(ZeroL) == ZeroL); |
| 109 | + BOOST_CHECK(uint256(OneL) == OneL); |
| 110 | + |
| 111 | + BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S); |
| 112 | + BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S); |
| 113 | + BOOST_CHECK(uint160S("0x"+ZeroS.ToString()) == ZeroS); |
| 114 | + BOOST_CHECK(uint160S("0x"+OneS.ToString()) == OneS); |
| 115 | + BOOST_CHECK(uint160S("0x"+MaxS.ToString()) == MaxS); |
| 116 | + BOOST_CHECK(uint160S(R1S.ToString()) == R1S); |
| 117 | + BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S); |
| 118 | + BOOST_CHECK(uint160S("") == ZeroS); |
| 119 | + BOOST_CHECK(R1S == uint160S(R1ArrayHex)); |
| 120 | + |
| 121 | + BOOST_CHECK(uint160(R1S) == R1S); |
| 122 | + BOOST_CHECK(uint160(ZeroS) == ZeroS); |
| 123 | + BOOST_CHECK(uint160(OneS) == OneS); |
| 124 | +} |
| 125 | + |
| 126 | +BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > |
| 127 | +{ |
| 128 | + uint256 LastL; |
| 129 | + for (int i = 255; i >= 0; --i) { |
| 130 | + uint256 TmpL; |
| 131 | + *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7)); |
| 132 | + BOOST_CHECK( LastL < TmpL ); |
| 133 | + LastL = TmpL; |
| 134 | + } |
| 135 | + |
| 136 | + BOOST_CHECK( ZeroL < R1L ); |
| 137 | + BOOST_CHECK( R2L < R1L ); |
| 138 | + BOOST_CHECK( ZeroL < OneL ); |
| 139 | + BOOST_CHECK( OneL < MaxL ); |
| 140 | + BOOST_CHECK( R1L < MaxL ); |
| 141 | + BOOST_CHECK( R2L < MaxL ); |
| 142 | + |
| 143 | + uint160 LastS; |
| 144 | + for (int i = 159; i >= 0; --i) { |
| 145 | + uint160 TmpS; |
| 146 | + *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7)); |
| 147 | + BOOST_CHECK( LastS < TmpS ); |
| 148 | + LastS = TmpS; |
| 149 | + } |
| 150 | + BOOST_CHECK( ZeroS < R1S ); |
| 151 | + BOOST_CHECK( R2S < R1S ); |
| 152 | + BOOST_CHECK( ZeroS < OneS ); |
| 153 | + BOOST_CHECK( OneS < MaxS ); |
| 154 | + BOOST_CHECK( R1S < MaxS ); |
| 155 | + BOOST_CHECK( R2S < MaxS ); |
| 156 | +} |
| 157 | + |
| 158 | +BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize |
| 159 | +{ |
| 160 | + BOOST_CHECK(R1L.GetHex() == R1L.ToString()); |
| 161 | + BOOST_CHECK(R2L.GetHex() == R2L.ToString()); |
| 162 | + BOOST_CHECK(OneL.GetHex() == OneL.ToString()); |
| 163 | + BOOST_CHECK(MaxL.GetHex() == MaxL.ToString()); |
| 164 | + uint256 TmpL(R1L); |
| 165 | + BOOST_CHECK(TmpL == R1L); |
| 166 | + TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L); |
| 167 | + TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == uint256()); |
| 168 | + |
| 169 | + TmpL.SetHex(R1L.ToString()); |
| 170 | + BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0); |
| 171 | + BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0); |
| 172 | + BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0); |
| 173 | + BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0); |
| 174 | + BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0); |
| 175 | + BOOST_CHECK(R1L.size() == sizeof(R1L)); |
| 176 | + BOOST_CHECK(sizeof(R1L) == 32); |
| 177 | + BOOST_CHECK(R1L.size() == 32); |
| 178 | + BOOST_CHECK(R2L.size() == 32); |
| 179 | + BOOST_CHECK(ZeroL.size() == 32); |
| 180 | + BOOST_CHECK(MaxL.size() == 32); |
| 181 | + BOOST_CHECK(R1L.begin() + 32 == R1L.end()); |
| 182 | + BOOST_CHECK(R2L.begin() + 32 == R2L.end()); |
| 183 | + BOOST_CHECK(OneL.begin() + 32 == OneL.end()); |
| 184 | + BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); |
| 185 | + BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); |
| 186 | + BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32); |
| 187 | + BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32); |
| 188 | + |
| 189 | + std::stringstream ss; |
| 190 | + R1L.Serialize(ss,0,PROTOCOL_VERSION); |
| 191 | + BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32)); |
| 192 | + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); |
| 193 | + BOOST_CHECK(R1L == TmpL); |
| 194 | + ss.str(""); |
| 195 | + ZeroL.Serialize(ss,0,PROTOCOL_VERSION); |
| 196 | + BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32)); |
| 197 | + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); |
| 198 | + BOOST_CHECK(ZeroL == TmpL); |
| 199 | + ss.str(""); |
| 200 | + MaxL.Serialize(ss,0,PROTOCOL_VERSION); |
| 201 | + BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32)); |
| 202 | + TmpL.Unserialize(ss,0,PROTOCOL_VERSION); |
| 203 | + BOOST_CHECK(MaxL == TmpL); |
| 204 | + ss.str(""); |
| 205 | + |
| 206 | + BOOST_CHECK(R1S.GetHex() == R1S.ToString()); |
| 207 | + BOOST_CHECK(R2S.GetHex() == R2S.ToString()); |
| 208 | + BOOST_CHECK(OneS.GetHex() == OneS.ToString()); |
| 209 | + BOOST_CHECK(MaxS.GetHex() == MaxS.ToString()); |
| 210 | + uint160 TmpS(R1S); |
| 211 | + BOOST_CHECK(TmpS == R1S); |
| 212 | + TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S); |
| 213 | + TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == uint160()); |
| 214 | + |
| 215 | + TmpS.SetHex(R1S.ToString()); |
| 216 | + BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0); |
| 217 | + BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0); |
| 218 | + BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0); |
| 219 | + BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0); |
| 220 | + BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0); |
| 221 | + BOOST_CHECK(R1S.size() == sizeof(R1S)); |
| 222 | + BOOST_CHECK(sizeof(R1S) == 20); |
| 223 | + BOOST_CHECK(R1S.size() == 20); |
| 224 | + BOOST_CHECK(R2S.size() == 20); |
| 225 | + BOOST_CHECK(ZeroS.size() == 20); |
| 226 | + BOOST_CHECK(MaxS.size() == 20); |
| 227 | + BOOST_CHECK(R1S.begin() + 20 == R1S.end()); |
| 228 | + BOOST_CHECK(R2S.begin() + 20 == R2S.end()); |
| 229 | + BOOST_CHECK(OneS.begin() + 20 == OneS.end()); |
| 230 | + BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); |
| 231 | + BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); |
| 232 | + BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20); |
| 233 | + BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20); |
| 234 | + |
| 235 | + R1S.Serialize(ss,0,PROTOCOL_VERSION); |
| 236 | + BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); |
| 237 | + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); |
| 238 | + BOOST_CHECK(R1S == TmpS); |
| 239 | + ss.str(""); |
| 240 | + ZeroS.Serialize(ss,0,PROTOCOL_VERSION); |
| 241 | + BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20)); |
| 242 | + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); |
| 243 | + BOOST_CHECK(ZeroS == TmpS); |
| 244 | + ss.str(""); |
| 245 | + MaxS.Serialize(ss,0,PROTOCOL_VERSION); |
| 246 | + BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20)); |
| 247 | + TmpS.Unserialize(ss,0,PROTOCOL_VERSION); |
| 248 | + BOOST_CHECK(MaxS == TmpS); |
| 249 | + ss.str(""); |
| 250 | +} |
| 251 | + |
| 252 | +BOOST_AUTO_TEST_CASE( conversion ) |
| 253 | +{ |
| 254 | + BOOST_CHECK(ArithToUint256(UintToArith256(ZeroL)) == ZeroL); |
| 255 | + BOOST_CHECK(ArithToUint256(UintToArith256(OneL)) == OneL); |
| 256 | + BOOST_CHECK(ArithToUint256(UintToArith256(R1L)) == R1L); |
| 257 | + BOOST_CHECK(ArithToUint256(UintToArith256(R2L)) == R2L); |
| 258 | + BOOST_CHECK(UintToArith256(ZeroL) == 0); |
| 259 | + BOOST_CHECK(UintToArith256(OneL) == 1); |
| 260 | + BOOST_CHECK(ArithToUint256(0) == ZeroL); |
| 261 | + BOOST_CHECK(ArithToUint256(1) == OneL); |
| 262 | + BOOST_CHECK(arith_uint256(R1L.GetHex()) == UintToArith256(R1L)); |
| 263 | + BOOST_CHECK(arith_uint256(R2L.GetHex()) == UintToArith256(R2L)); |
| 264 | + BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex()); |
| 265 | + BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex()); |
| 266 | +} |
| 267 | + |
| 268 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments