Changeset View
Changeset View
Standalone View
Standalone View
src/test/intmath_tests.cpp
Show First 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | const i128_t b = b_64; | ||||
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); | ||||
} | } | ||||
} | } | ||||
{ | |||||
// Test AddInt63Overflow | |||||
int64_t result; | |||||
bool expect_overflow = !IsInScriptBounds(a + b); | |||||
bool had_overflow = AddInt63Overflow(a_64, b_64, result); | |||||
if (expect_overflow) { | |||||
BOOST_CHECK_MESSAGE( | |||||
had_overflow, strprintf("%d + %d didn't overflow", a_64, b_64)); | |||||
} else { | |||||
BOOST_CHECK_MESSAGE(!had_overflow, | |||||
strprintf("%d + %d overflowed", a_64, b_64)); | |||||
BOOST_CHECK_EQUAL(result, a + b); | |||||
} | |||||
} | |||||
{ | |||||
// Test SubInt63Overflow | |||||
int64_t result; | |||||
bool expect_overflow = !IsInScriptBounds(a - b); | |||||
bool had_overflow = SubInt63Overflow(a_64, b_64, result); | |||||
if (expect_overflow) { | |||||
BOOST_CHECK_MESSAGE( | |||||
had_overflow, strprintf("%d - %d didn't overflow", a_64, b_64)); | |||||
} else { | |||||
BOOST_CHECK_MESSAGE(!had_overflow, | |||||
strprintf("%d - %d overflowed", a_64, b_64)); | |||||
BOOST_CHECK_EQUAL(result, a - b); | |||||
} | |||||
} | |||||
deadalnix: You should stick these checks just after the corresponding emulated check, so that it can be… | |||||
} | } | ||||
/** 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(); | ||||
// Make bit-length uniformly distributed for better test coverage. | // Make bit-length uniformly distributed for better test coverage. | ||||
Show All 27 Lines |
You should stick these checks just after the corresponding emulated check, so that it can be asserted that both return the same result easily.