Changeset View
Changeset View
Standalone View
Standalone View
src/test/scriptnum_tests.cpp
| Show All 26 Lines | static const int64_t values[] = {0, | ||||
| (1LL << 24) - 1, | (1LL << 24) - 1, | ||||
| (1LL << 31), | (1LL << 31), | ||||
| 1 - (1LL << 32), | 1 - (1LL << 32), | ||||
| 1LL << 40}; | 1LL << 40}; | ||||
| static const int64_t offsets[] = {1, 0x79, 0x80, 0x81, 0xFF, | static const int64_t offsets[] = {1, 0x79, 0x80, 0x81, 0xFF, | ||||
| 0x7FFF, 0x8000, 0xFFFF, 0x10000}; | 0x7FFF, 0x8000, 0xFFFF, 0x10000}; | ||||
| static void CheckCreateVch(const int64_t &num) { | static void CheckCreateVch(const int64_t &num, const size_t nMaxNumSize) { | ||||
| CScriptNum scriptnum(num); | // If scriptnum doesn't fit in nMaxNumSize, this will throw scriptnum_error | ||||
| CScriptNum scriptnum2(scriptnum.getvch(), false, MAX_SCRIPTNUM_BYTE_SIZE); | CScriptNum(CScriptNum(num).getvch(), false, nMaxNumSize); | ||||
| CScriptNum scriptnum3(scriptnum2.getvch(), false, MAX_SCRIPTNUM_BYTE_SIZE); | |||||
| } | } | ||||
| static void CheckAdd(const int64_t &num1, const int64_t &num2) { | static void CheckAdd(const int64_t &num1, const int64_t &num2) { | ||||
| const CScriptNum scriptnum1(num1); | const CScriptNum scriptnum1(num1); | ||||
| const CScriptNum scriptnum2(num2); | const CScriptNum scriptnum2(num2); | ||||
| CScriptNum scriptnum3(num1); | CScriptNum scriptnum3(num1); | ||||
| // int64_t overflow is undefined. | // int64_t overflow is undefined. | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | static void CheckCompare(const int64_t &num1, const int64_t &num2) { | ||||
| BOOST_CHECK((num1 < num2) == (scriptnum1 < num2)); | BOOST_CHECK((num1 < num2) == (scriptnum1 < num2)); | ||||
| BOOST_CHECK((num1 > num2) == (scriptnum1 > num2)); | BOOST_CHECK((num1 > num2) == (scriptnum1 > num2)); | ||||
| BOOST_CHECK((num1 >= num2) == (scriptnum1 >= num2)); | BOOST_CHECK((num1 >= num2) == (scriptnum1 >= num2)); | ||||
| BOOST_CHECK((num1 <= num2) == (scriptnum1 <= num2)); | BOOST_CHECK((num1 <= num2) == (scriptnum1 <= num2)); | ||||
| } | } | ||||
| static void RunCreate(const int64_t &num) { | static void RunCreate(const int64_t &num) { | ||||
| CScriptNum scriptnum(num); | CScriptNum scriptnum(num); | ||||
| if (scriptnum.getvch().size() <= MAX_SCRIPTNUM_BYTE_SIZE) { | |||||
| CheckCreateVch(num); | // Test in 31+sign-bit context | ||||
| if (scriptnum.getvch().size() <= MAX_SCRIPTNUM_BYTE_SIZE_31_BIT) { | |||||
| CheckCreateVch(num, MAX_SCRIPTNUM_BYTE_SIZE_31_BIT); | |||||
| } else { | |||||
| BOOST_CHECK_EXCEPTION( | |||||
| CheckCreateVch(num, MAX_SCRIPTNUM_BYTE_SIZE_31_BIT), | |||||
| scriptnum_error, HasReason("script number overflow")); | |||||
| } | |||||
| // Test in 63+sign-bit context | |||||
| if (scriptnum.getvch().size() <= MAX_SCRIPTNUM_BYTE_SIZE_63_BIT) { | |||||
| CheckCreateVch(num, MAX_SCRIPTNUM_BYTE_SIZE_63_BIT); | |||||
| } else { | } else { | ||||
| BOOST_CHECK_THROW(CheckCreateVch(num), scriptnum_error); | BOOST_CHECK_EXCEPTION( | ||||
| CheckCreateVch(num, MAX_SCRIPTNUM_BYTE_SIZE_63_BIT), | |||||
| scriptnum_error, HasReason("script number overflow")); | |||||
| } | } | ||||
| } | } | ||||
| static void RunOperators(const int64_t &num1, const int64_t &num2) { | static void RunOperators(const int64_t &num1, const int64_t &num2) { | ||||
| CheckAdd(num1, num2); | CheckAdd(num1, num2); | ||||
| CheckSubtract(num1, num2); | CheckSubtract(num1, num2); | ||||
| CheckNegate(num1); | CheckNegate(num1); | ||||
| CheckCompare(num1, num2); | CheckCompare(num1, num2); | ||||
| ▲ Show 20 Lines • Show All 81 Lines • Show Last 20 Lines | |||||