diff options
author | MarcoFalke <falke.marco@gmail.com> | 2022-02-01 13:43:42 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2022-02-21 14:32:53 +0100 |
commit | faa7d8a3f7cba02eca7e247108a6b98ea9daf373 (patch) | |
tree | 8826fb85e35da9fb2bee74a5da27cbf39b0c1b2a /src/test/util_tests.cpp | |
parent | 1337b93f50d9874fa3bce7c0b5bb141aad9b1c49 (diff) | |
download | bitcoin-faa7d8a3f7cba02eca7e247108a6b98ea9daf373.tar.xz |
util: Add SaturatingAdd helper
Diffstat (limited to 'src/test/util_tests.cpp')
-rw-r--r-- | src/test/util_tests.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 52b24327ec..22ce9b0582 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1474,9 +1474,17 @@ static void TestAddMatrixOverflow() constexpr T MAXI{std::numeric_limits<T>::max()}; BOOST_CHECK(!CheckedAdd(T{1}, MAXI)); BOOST_CHECK(!CheckedAdd(MAXI, MAXI)); + BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI)); + BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(MAXI, MAXI)); + BOOST_CHECK_EQUAL(0, CheckedAdd(T{0}, T{0}).value()); BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{0}, MAXI).value()); BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{1}, MAXI - 1).value()); + BOOST_CHECK_EQUAL(MAXI - 1, CheckedAdd(T{1}, MAXI - 2).value()); + BOOST_CHECK_EQUAL(0, SaturatingAdd(T{0}, T{0})); + BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{0}, MAXI)); + BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI - 1)); + BOOST_CHECK_EQUAL(MAXI - 1, SaturatingAdd(T{1}, MAXI - 2)); } /* Check for overflow or underflow */ @@ -1488,9 +1496,17 @@ static void TestAddMatrix() constexpr T MAXI{std::numeric_limits<T>::max()}; BOOST_CHECK(!CheckedAdd(T{-1}, MINI)); BOOST_CHECK(!CheckedAdd(MINI, MINI)); + BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI)); + BOOST_CHECK_EQUAL(MINI, SaturatingAdd(MINI, MINI)); + BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{0}, MINI).value()); BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{-1}, MINI + 1).value()); BOOST_CHECK_EQUAL(-1, CheckedAdd(MINI, MAXI).value()); + BOOST_CHECK_EQUAL(MINI + 1, CheckedAdd(T{-1}, MINI + 2).value()); + BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{0}, MINI)); + BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI + 1)); + BOOST_CHECK_EQUAL(MINI + 1, SaturatingAdd(T{-1}, MINI + 2)); + BOOST_CHECK_EQUAL(-1, SaturatingAdd(MINI, MAXI)); } BOOST_AUTO_TEST_CASE(util_overflow) |