Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
scriptnum_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-present 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 <script/script.h>
6#include <test/scriptnum10.h>
8
9#include <boost/test/unit_test.hpp>
10#include <climits>
11#include <cstdint>
12
14
15
17static const int64_t values[] = { 0, 1, -2, 127, 128, -255, 256, (1LL << 15) - 1, -(1LL << 16), (1LL << 24) - 1, (1LL << 31), 1 - (1LL << 32), 1LL << 40 };
18
19static const int64_t offsets[] = { 1, 0x79, 0x80, 0x81, 0xFF, 0x7FFF, 0x8000, 0xFFFF, 0x10000};
20
21static bool verify(const CScriptNum10& bignum, const CScriptNum& scriptnum)
22{
23 return bignum.getvch() == scriptnum.getvch() && bignum.getint() == scriptnum.getint();
24}
25
26static void CheckCreateVch(const int64_t& num)
27{
31
32 CScriptNum10 bignum2(bignum.getvch(), false);
33 CScriptNum scriptnum2(scriptnum.getvch(), false);
35
36 CScriptNum10 bignum3(scriptnum2.getvch(), false);
37 CScriptNum scriptnum3(bignum2.getvch(), false);
39}
40
50
51
52static void CheckAdd(const int64_t& num1, const int64_t& num2)
53{
62
63 // int64_t overflow is undefined.
64 bool invalid = (((num2 > 0) && (num1 > (std::numeric_limits<int64_t>::max() - num2))) ||
65 ((num2 < 0) && (num1 < (std::numeric_limits<int64_t>::min() - num2))));
66 if (!invalid)
67 {
71 }
72}
73
74static void CheckNegate(const int64_t& num)
75{
76 const CScriptNum10 bignum(num);
77 const CScriptNum scriptnum(num);
78
79 // -INT64_MIN is undefined
80 if (num != std::numeric_limits<int64_t>::min())
82}
83
84static void CheckSubtract(const int64_t& num1, const int64_t& num2)
85{
90
91 // int64_t overflow is undefined.
92 bool invalid = ((num2 > 0 && num1 < std::numeric_limits<int64_t>::min() + num2) ||
93 (num2 < 0 && num1 > std::numeric_limits<int64_t>::max() + num2));
94 if (!invalid)
95 {
98 }
99
100 invalid = ((num1 > 0 && num2 < std::numeric_limits<int64_t>::min() + num1) ||
101 (num1 < 0 && num2 > std::numeric_limits<int64_t>::max() + num1));
102 if (!invalid)
103 {
106 }
107}
108
109static void CheckCompare(const int64_t& num1, const int64_t& num2)
110{
115
122
129
136
143}
144
145static void RunCreate(const int64_t& num)
146{
147 CheckCreateInt(num);
149 if (scriptnum.getvch().size() <= CScriptNum::nDefaultMaxNumSize)
150 CheckCreateVch(num);
151 else
152 {
154 }
155}
156
157static void RunOperators(const int64_t& num1, const int64_t& num2)
158{
163}
164
166{
167 for(size_t i = 0; i < std::size(values); ++i)
168 {
169 for(size_t j = 0; j < std::size(offsets); ++j)
170 {
171 RunCreate(values[i]);
172 RunCreate(values[i] + offsets[j]);
173 RunCreate(values[i] - offsets[j]);
174 }
175 }
176}
177
179{
180 for(size_t i = 0; i < std::size(values); ++i)
181 {
182 for(size_t j = 0; j < std::size(offsets); ++j)
183 {
184 RunOperators(values[i], values[i]);
185 RunOperators(values[i], -values[i]);
192 RunOperators(values[i] + values[j], values[i] + values[j]);
193 RunOperators(values[i] + values[j], values[i] - values[j]);
194 RunOperators(values[i] - values[j], values[i] + values[j]);
195 RunOperators(values[i] - values[j], values[i] - values[j]);
196 }
197 }
198}
199
int getint() const
static const size_t nDefaultMaxNumSize
Definition script.h:243
int getint() const
Definition script.h:325
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:16
static void CheckAdd(const int64_t &num1, const int64_t &num2)
static void RunOperators(const int64_t &num1, const int64_t &num2)
static const int64_t offsets[]
static void CheckCompare(const int64_t &num1, const int64_t &num2)
static void CheckCreateVch(const int64_t &num)
static void CheckSubtract(const int64_t &num1, const int64_t &num2)
static void RunCreate(const int64_t &num)
static void CheckCreateInt(const int64_t &num)
static void CheckNegate(const int64_t &num)
static bool verify(const CScriptNum10 &bignum, const CScriptNum &scriptnum)
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
BOOST_AUTO_TEST_CASE(creation)
Basic testing setup.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73