Changeset View
Changeset View
Standalone View
Standalone View
src/test/intmath_tests.cpp
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
bool IsInScriptBounds(const i128_t &num_int) { | bool IsInScriptBounds(const i128_t &num_int) { | ||||
return num_int >= MIN_SCRIPT_63_BIT_INT && num_int <= MAX_SCRIPT_63_BIT_INT; | return num_int >= MIN_SCRIPT_63_BIT_INT && num_int <= MAX_SCRIPT_63_BIT_INT; | ||||
} | } | ||||
void CheckArithmetic(const int64_t a_64, const int64_t b_64) { | void CheckArithmetic(const int64_t a_64, const int64_t b_64) { | ||||
const i128_t a = a_64; | const i128_t a = a_64; | ||||
const i128_t b = b_64; | const i128_t b = b_64; | ||||
{ | { | ||||
bool expect_overflow = !IsInScriptBounds(a + b); | |||||
deadalnix: This has the same problem as the previous code. You want the code to be the same for both by… | |||||
// Test AddInt63OverflowEmulated | // Test AddInt63OverflowEmulated | ||||
int64_t result_emulated; | |||||
bool had_overflow_emulated = | |||||
AddInt63OverflowEmulated(a_64, b_64, result_emulated); | |||||
if (expect_overflow) { | |||||
BOOST_CHECK_MESSAGE( | |||||
had_overflow_emulated, | |||||
strprintf("%d + %d didn't overflow", a_64, b_64)); | |||||
} else { | |||||
BOOST_CHECK_MESSAGE(!had_overflow_emulated, | |||||
strprintf("%d + %d overflowed", a_64, b_64)); | |||||
BOOST_CHECK_EQUAL(result_emulated, a + b); | |||||
} | |||||
// Test AddInt63Overflow | |||||
int64_t result; | int64_t result; | ||||
bool expect_overflow = !IsInScriptBounds(a + b); | bool had_overflow = AddInt63Overflow(a_64, b_64, result); | ||||
bool had_overflow = AddInt63OverflowEmulated(a_64, b_64, result); | |||||
if (expect_overflow) { | if (expect_overflow) { | ||||
BOOST_CHECK_MESSAGE( | BOOST_CHECK_MESSAGE( | ||||
had_overflow, strprintf("%d + %d didn't overflow", a_64, b_64)); | had_overflow, strprintf("%d + %d didn't overflow", a_64, b_64)); | ||||
} else { | } else { | ||||
BOOST_CHECK_MESSAGE(!had_overflow, | BOOST_CHECK_MESSAGE(!had_overflow, | ||||
strprintf("%d + %d overflowed", a_64, b_64)); | strprintf("%d + %d overflowed", a_64, b_64)); | ||||
BOOST_CHECK_EQUAL(result, a + b); | BOOST_CHECK_EQUAL(result, a + b); | ||||
BOOST_CHECK_EQUAL(result, result_emulated); | |||||
} | } | ||||
BOOST_CHECK_EQUAL(had_overflow, had_overflow_emulated); | |||||
} | } | ||||
{ | { | ||||
bool expect_overflow = !IsInScriptBounds(a - b); | |||||
// Test SubInt63OverflowEmulated | // Test SubInt63OverflowEmulated | ||||
int64_t result_emulated; | |||||
bool had_overflow_emulated = | |||||
SubInt63OverflowEmulated(a_64, b_64, result_emulated); | |||||
if (expect_overflow) { | |||||
BOOST_CHECK_MESSAGE( | |||||
had_overflow_emulated, | |||||
strprintf("%d - %d didn't overflow", a_64, b_64)); | |||||
} else { | |||||
BOOST_CHECK_MESSAGE(!had_overflow_emulated, | |||||
strprintf("%d - %d overflowed", a_64, b_64)); | |||||
BOOST_CHECK_EQUAL(result_emulated, a - b); | |||||
} | |||||
// Test SubInt63Overflow | |||||
int64_t result; | int64_t result; | ||||
bool expect_overflow = !IsInScriptBounds(a - b); | bool had_overflow = SubInt63Overflow(a_64, b_64, result); | ||||
bool had_overflow = SubInt63OverflowEmulated(a_64, b_64, result); | |||||
if (expect_overflow) { | if (expect_overflow) { | ||||
BOOST_CHECK_MESSAGE( | BOOST_CHECK_MESSAGE( | ||||
had_overflow, strprintf("%d - %d didn't overflow", a_64, b_64)); | had_overflow, strprintf("%d - %d didn't overflow", a_64, b_64)); | ||||
} else { | } else { | ||||
BOOST_CHECK_MESSAGE(!had_overflow, | BOOST_CHECK_MESSAGE(!had_overflow, | ||||
strprintf("%d - %d overflowed", a_64, b_64)); | strprintf("%d - %d overflowed", a_64, b_64)); | ||||
BOOST_CHECK_EQUAL(result, a - b); | BOOST_CHECK_EQUAL(result, a - b); | ||||
BOOST_CHECK_EQUAL(result, result_emulated); | |||||
} | } | ||||
BOOST_CHECK_EQUAL(had_overflow, had_overflow_emulated); | |||||
} | } | ||||
} | } | ||||
/** Generate random number in [-2^63; 2^63]. */ | /** Generate random number in [-2^63; 2^63]. */ | ||||
int64_t GenInt63(MMIXLinearCongruentialGenerator &lcg) { | int64_t GenInt63(MMIXLinearCongruentialGenerator &lcg) { | ||||
while (true) { | while (true) { | ||||
// Uniform number in [-2^63; 2^63]. | // Uniform number in [-2^63; 2^63]. | ||||
int64_t val = (int64_t(lcg.next()) << 32) | lcg.next(); | int64_t val = (int64_t(lcg.next()) << 32) | lcg.next(); | ||||
Show All 28 Lines |
This has the same problem as the previous code. You want the code to be the same for both by construct not by cutting and pasting.