Changeset View
Changeset View
Standalone View
Standalone View
src/test/opcode_tests.cpp
Show All 24 Lines | |||||
static void CheckTestResultForAllFlags(const stacktype &original_stack, | static void CheckTestResultForAllFlags(const stacktype &original_stack, | ||||
const CScript &script, | const CScript &script, | ||||
const stacktype &expected) { | const stacktype &expected) { | ||||
BaseSignatureChecker sigchecker; | BaseSignatureChecker sigchecker; | ||||
for (uint32_t flags : flagset) { | for (uint32_t flags : flagset) { | ||||
ScriptError err = SCRIPT_ERR_OK; | ScriptError err = SCRIPT_ERR_OK; | ||||
stacktype stack{original_stack}; | stacktype stack{original_stack}; | ||||
bool r = EvalScript(stack, script, flags, sigchecker, &err); | bool r = | ||||
EvalScript(stack, script, flags | SCRIPT_ENABLE_MAGNETIC_OPCODES, | |||||
sigchecker, &err); | |||||
BOOST_CHECK(r); | BOOST_CHECK(r); | ||||
BOOST_CHECK(stack == expected); | BOOST_CHECK(stack == expected); | ||||
} | } | ||||
} | } | ||||
static void CheckError(uint32_t flags, const stacktype &original_stack, | static void CheckError(uint32_t flags, const stacktype &original_stack, | ||||
const CScript &script, ScriptError expected_error) { | const CScript &script, ScriptError expected_error) { | ||||
BaseSignatureChecker sigchecker; | BaseSignatureChecker sigchecker; | ||||
ScriptError err = SCRIPT_ERR_OK; | ScriptError err = SCRIPT_ERR_OK; | ||||
stacktype stack{original_stack}; | stacktype stack{original_stack}; | ||||
bool r = EvalScript(stack, script, flags, sigchecker, &err); | bool r = EvalScript(stack, script, flags | SCRIPT_ENABLE_MAGNETIC_OPCODES, | ||||
sigchecker, &err); | |||||
BOOST_CHECK(!r); | BOOST_CHECK(!r); | ||||
BOOST_CHECK_EQUAL(err, expected_error); | BOOST_CHECK_EQUAL(err, expected_error); | ||||
} | } | ||||
static void CheckErrorForAllFlags(const stacktype &original_stack, | static void CheckErrorForAllFlags(const stacktype &original_stack, | ||||
const CScript &script, | const CScript &script, | ||||
ScriptError expected_error) { | ScriptError expected_error) { | ||||
for (uint32_t flags : flagset) { | for (uint32_t flags : flagset) { | ||||
▲ Show 20 Lines • Show All 577 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(type_conversion_test) { | ||||
// Check that the requested encoding is possible. | // Check that the requested encoding is possible. | ||||
CheckNum2BinError({{0xab, 0xcd, 0xef, 0x80}, {0x03}}, | CheckNum2BinError({{0xab, 0xcd, 0xef, 0x80}, {0x03}}, | ||||
SCRIPT_ERR_IMPOSSIBLE_ENCODING); | SCRIPT_ERR_IMPOSSIBLE_ENCODING); | ||||
} | } | ||||
/** | /** | ||||
* Arithmetic Opcodes | * Arithmetic Opcodes | ||||
*/ | */ | ||||
static void CheckMul(const valtype &a, const valtype &b, | |||||
const valtype &expected) { | |||||
// Negative values for division | |||||
CheckBinaryOp(a, b, OP_MUL, expected); | |||||
CheckBinaryOp(a, NegativeValtype(b), OP_MUL, NegativeValtype(expected)); | |||||
CheckBinaryOp(NegativeValtype(a), b, OP_MUL, NegativeValtype(expected)); | |||||
CheckBinaryOp(NegativeValtype(a), NegativeValtype(b), OP_MUL, expected); | |||||
// Commutativity | |||||
CheckBinaryOp(b, a, OP_MUL, expected); | |||||
CheckBinaryOp(b, NegativeValtype(a), OP_MUL, NegativeValtype(expected)); | |||||
CheckBinaryOp(NegativeValtype(b), a, OP_MUL, NegativeValtype(expected)); | |||||
CheckBinaryOp(NegativeValtype(b), NegativeValtype(a), OP_MUL, expected); | |||||
// Multiplication identities | |||||
CheckBinaryOp(a, {0x01}, OP_MUL, a); | |||||
CheckBinaryOp(a, {0x81}, OP_MUL, NegativeValtype(a)); | |||||
CheckBinaryOp(a, {}, OP_MUL, {}); | |||||
CheckBinaryOp({0x01}, b, OP_MUL, b); | |||||
CheckBinaryOp({0x81}, b, OP_MUL, NegativeValtype(b)); | |||||
CheckBinaryOp({}, b, OP_MUL, {}); | |||||
} | |||||
static void CheckDivMod(const valtype &a, const valtype &b, | static void CheckDivMod(const valtype &a, const valtype &b, | ||||
const valtype &divExpected, | const valtype &divExpected, | ||||
const valtype &modExpected) { | const valtype &modExpected) { | ||||
// Negative values for division | // Negative values for division | ||||
CheckBinaryOp(a, b, OP_DIV, divExpected); | CheckBinaryOp(a, b, OP_DIV, divExpected); | ||||
CheckBinaryOp(a, NegativeValtype(b), OP_DIV, NegativeValtype(divExpected)); | CheckBinaryOp(a, NegativeValtype(b), OP_DIV, NegativeValtype(divExpected)); | ||||
CheckBinaryOp(NegativeValtype(a), b, OP_DIV, NegativeValtype(divExpected)); | CheckBinaryOp(NegativeValtype(a), b, OP_DIV, NegativeValtype(divExpected)); | ||||
CheckBinaryOp(NegativeValtype(a), NegativeValtype(b), OP_DIV, divExpected); | CheckBinaryOp(NegativeValtype(a), NegativeValtype(b), OP_DIV, divExpected); | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(div_and_mod_opcode_tests) { | ||||
// 56488123 % 3 = 1 (and negative operands) | // 56488123 % 3 = 1 (and negative operands) | ||||
CheckDivMod({0xbb, 0xf0, 0x5d, 0x03}, {0x03}, {0x3e, 0x50, 0x1f, 0x01}, | CheckDivMod({0xbb, 0xf0, 0x5d, 0x03}, {0x03}, {0x3e, 0x50, 0x1f, 0x01}, | ||||
{0x01}); | {0x01}); | ||||
// 56488123 % 564881230 = 56488123 (and negative operands) | // 56488123 % 564881230 = 56488123 (and negative operands) | ||||
CheckDivMod({0xbb, 0xf0, 0x5d, 0x03}, {0x4e, 0x67, 0xab, 0x21}, {}, | CheckDivMod({0xbb, 0xf0, 0x5d, 0x03}, {0x4e, 0x67, 0xab, 0x21}, {}, | ||||
{0xbb, 0xf0, 0x5d, 0x03}); | {0xbb, 0xf0, 0x5d, 0x03}); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(mul_test) { | |||||
CheckMul({0xA0, 0xA0}, {0xF5, 0xE4}, | |||||
{0x20, 0xB9, 0xDD, 0x0C}); // -20A0*-64F5=0CDDB920 | |||||
CheckMul({0x02}, {0x56, 0x24}, {0xAC, 0x48}); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |