1 // Copyright (c) 2012-2015 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.
5 #include "scriptnum10.h"
6 #include "script/script.h"
7 #include "test/test_bitcoin.h"
9 #include <boost/test/unit_test.hpp>
13 BOOST_FIXTURE_TEST_SUITE(scriptnum_tests
, BasicTestingSetup
)
15 static const int64_t values
[] = \
16 { 0, 1, CHAR_MIN
, CHAR_MAX
, UCHAR_MAX
, SHRT_MIN
, USHRT_MAX
, INT_MIN
, INT_MAX
, UINT_MAX
, LONG_MIN
, LONG_MAX
};
17 static const int64_t offsets
[] = { 1, 0x79, 0x80, 0x81, 0xFF, 0x7FFF, 0x8000, 0xFFFF, 0x10000};
19 static bool verify(const CScriptNum10
& bignum
, const CScriptNum
& scriptnum
)
21 return bignum
.getvch() == scriptnum
.getvch() && bignum
.getint() == scriptnum
.getint();
24 static void CheckCreateVch(const int64_t& num
)
26 CScriptNum10
bignum(num
);
27 CScriptNum
scriptnum(num
);
28 BOOST_CHECK(verify(bignum
, scriptnum
));
30 std::vector
<unsigned char> vch
= bignum
.getvch();
32 CScriptNum10
bignum2(bignum
.getvch(), false);
33 vch
= scriptnum
.getvch();
34 CScriptNum
scriptnum2(scriptnum
.getvch(), false);
35 BOOST_CHECK(verify(bignum2
, scriptnum2
));
37 CScriptNum10
bignum3(scriptnum2
.getvch(), false);
38 CScriptNum
scriptnum3(bignum2
.getvch(), false);
39 BOOST_CHECK(verify(bignum3
, scriptnum3
));
42 static void CheckCreateInt(const int64_t& num
)
44 CScriptNum10
bignum(num
);
45 CScriptNum
scriptnum(num
);
46 BOOST_CHECK(verify(bignum
, scriptnum
));
47 BOOST_CHECK(verify(CScriptNum10(bignum
.getint()), CScriptNum(scriptnum
.getint())));
48 BOOST_CHECK(verify(CScriptNum10(scriptnum
.getint()), CScriptNum(bignum
.getint())));
49 BOOST_CHECK(verify(CScriptNum10(CScriptNum10(scriptnum
.getint()).getint()), CScriptNum(CScriptNum(bignum
.getint()).getint())));
53 static void CheckAdd(const int64_t& num1
, const int64_t& num2
)
55 const CScriptNum10
bignum1(num1
);
56 const CScriptNum10
bignum2(num2
);
57 const CScriptNum
scriptnum1(num1
);
58 const CScriptNum
scriptnum2(num2
);
59 CScriptNum10
bignum3(num1
);
60 CScriptNum10
bignum4(num1
);
61 CScriptNum
scriptnum3(num1
);
62 CScriptNum
scriptnum4(num1
);
64 // int64_t overflow is undefined.
65 bool invalid
= (((num2
> 0) && (num1
> (std::numeric_limits
<int64_t>::max() - num2
))) ||
66 ((num2
< 0) && (num1
< (std::numeric_limits
<int64_t>::min() - num2
))));
69 BOOST_CHECK(verify(bignum1
+ bignum2
, scriptnum1
+ scriptnum2
));
70 BOOST_CHECK(verify(bignum1
+ bignum2
, scriptnum1
+ num2
));
71 BOOST_CHECK(verify(bignum1
+ bignum2
, scriptnum2
+ num1
));
75 static void CheckNegate(const int64_t& num
)
77 const CScriptNum10
bignum(num
);
78 const CScriptNum
scriptnum(num
);
80 // -INT64_MIN is undefined
81 if (num
!= std::numeric_limits
<int64_t>::min())
82 BOOST_CHECK(verify(-bignum
, -scriptnum
));
85 static void CheckSubtract(const int64_t& num1
, const int64_t& num2
)
87 const CScriptNum10
bignum1(num1
);
88 const CScriptNum10
bignum2(num2
);
89 const CScriptNum
scriptnum1(num1
);
90 const CScriptNum
scriptnum2(num2
);
93 // int64_t overflow is undefined.
94 invalid
= ((num2
> 0 && num1
< std::numeric_limits
<int64_t>::min() + num2
) ||
95 (num2
< 0 && num1
> std::numeric_limits
<int64_t>::max() + num2
));
98 BOOST_CHECK(verify(bignum1
- bignum2
, scriptnum1
- scriptnum2
));
99 BOOST_CHECK(verify(bignum1
- bignum2
, scriptnum1
- num2
));
102 invalid
= ((num1
> 0 && num2
< std::numeric_limits
<int64_t>::min() + num1
) ||
103 (num1
< 0 && num2
> std::numeric_limits
<int64_t>::max() + num1
));
106 BOOST_CHECK(verify(bignum2
- bignum1
, scriptnum2
- scriptnum1
));
107 BOOST_CHECK(verify(bignum2
- bignum1
, scriptnum2
- num1
));
111 static void CheckCompare(const int64_t& num1
, const int64_t& num2
)
113 const CScriptNum10
bignum1(num1
);
114 const CScriptNum10
bignum2(num2
);
115 const CScriptNum
scriptnum1(num1
);
116 const CScriptNum
scriptnum2(num2
);
118 BOOST_CHECK((bignum1
== bignum1
) == (scriptnum1
== scriptnum1
));
119 BOOST_CHECK((bignum1
!= bignum1
) == (scriptnum1
!= scriptnum1
));
120 BOOST_CHECK((bignum1
< bignum1
) == (scriptnum1
< scriptnum1
));
121 BOOST_CHECK((bignum1
> bignum1
) == (scriptnum1
> scriptnum1
));
122 BOOST_CHECK((bignum1
>= bignum1
) == (scriptnum1
>= scriptnum1
));
123 BOOST_CHECK((bignum1
<= bignum1
) == (scriptnum1
<= scriptnum1
));
125 BOOST_CHECK((bignum1
== bignum1
) == (scriptnum1
== num1
));
126 BOOST_CHECK((bignum1
!= bignum1
) == (scriptnum1
!= num1
));
127 BOOST_CHECK((bignum1
< bignum1
) == (scriptnum1
< num1
));
128 BOOST_CHECK((bignum1
> bignum1
) == (scriptnum1
> num1
));
129 BOOST_CHECK((bignum1
>= bignum1
) == (scriptnum1
>= num1
));
130 BOOST_CHECK((bignum1
<= bignum1
) == (scriptnum1
<= num1
));
132 BOOST_CHECK((bignum1
== bignum2
) == (scriptnum1
== scriptnum2
));
133 BOOST_CHECK((bignum1
!= bignum2
) == (scriptnum1
!= scriptnum2
));
134 BOOST_CHECK((bignum1
< bignum2
) == (scriptnum1
< scriptnum2
));
135 BOOST_CHECK((bignum1
> bignum2
) == (scriptnum1
> scriptnum2
));
136 BOOST_CHECK((bignum1
>= bignum2
) == (scriptnum1
>= scriptnum2
));
137 BOOST_CHECK((bignum1
<= bignum2
) == (scriptnum1
<= scriptnum2
));
139 BOOST_CHECK((bignum1
== bignum2
) == (scriptnum1
== num2
));
140 BOOST_CHECK((bignum1
!= bignum2
) == (scriptnum1
!= num2
));
141 BOOST_CHECK((bignum1
< bignum2
) == (scriptnum1
< num2
));
142 BOOST_CHECK((bignum1
> bignum2
) == (scriptnum1
> num2
));
143 BOOST_CHECK((bignum1
>= bignum2
) == (scriptnum1
>= num2
));
144 BOOST_CHECK((bignum1
<= bignum2
) == (scriptnum1
<= num2
));
147 static void RunCreate(const int64_t& num
)
150 CScriptNum
scriptnum(num
);
151 if (scriptnum
.getvch().size() <= CScriptNum::nDefaultMaxNumSize
)
155 BOOST_CHECK_THROW (CheckCreateVch(num
), scriptnum10_error
);
159 static void RunOperators(const int64_t& num1
, const int64_t& num2
)
161 CheckAdd(num1
, num2
);
162 CheckSubtract(num1
, num2
);
164 CheckCompare(num1
, num2
);
167 BOOST_AUTO_TEST_CASE(creation
)
169 for(size_t i
= 0; i
< sizeof(values
) / sizeof(values
[0]); ++i
)
171 for(size_t j
= 0; j
< sizeof(offsets
) / sizeof(offsets
[0]); ++j
)
173 RunCreate(values
[i
]);
174 RunCreate(values
[i
] + offsets
[j
]);
175 RunCreate(values
[i
] - offsets
[j
]);
180 BOOST_AUTO_TEST_CASE(operators
)
182 for(size_t i
= 0; i
< sizeof(values
) / sizeof(values
[0]); ++i
)
184 for(size_t j
= 0; j
< sizeof(offsets
) / sizeof(offsets
[0]); ++j
)
186 RunOperators(values
[i
], values
[i
]);
187 RunOperators(values
[i
], -values
[i
]);
188 RunOperators(values
[i
], values
[j
]);
189 RunOperators(values
[i
], -values
[j
]);
190 RunOperators(values
[i
] + values
[j
], values
[j
]);
191 RunOperators(values
[i
] + values
[j
], -values
[j
]);
192 RunOperators(values
[i
] - values
[j
], values
[j
]);
193 RunOperators(values
[i
] - values
[j
], -values
[j
]);
194 RunOperators(values
[i
] + values
[j
], values
[i
] + values
[j
]);
195 RunOperators(values
[i
] + values
[j
], values
[i
] - values
[j
]);
196 RunOperators(values
[i
] - values
[j
], values
[i
] + values
[j
]);
197 RunOperators(values
[i
] - values
[j
], values
[i
] - values
[j
]);
202 BOOST_AUTO_TEST_SUITE_END()