/////////////////////////////////////////////////////////////// // Copyright 2024 John Maddock. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt #include #include #include "test.hpp" template void test_subtract_underflow() { T val = 42; int sub = 43; BOOST_CHECK_THROW(static_cast(val - sub), std::range_error); BOOST_CHECK_THROW(static_cast(val - static_cast(sub)), std::range_error); BOOST_CHECK_THROW(static_cast(val - static_cast(sub)), std::range_error); BOOST_CHECK_THROW(static_cast(val - static_cast(sub)), std::range_error); BOOST_IF_CONSTEXPR((std::numeric_limits::digits < 500) && std::numeric_limits::digits) { val <<= std::numeric_limits::digits - 10; } else val <<= 500; T sub2 = val + 1; BOOST_CHECK_THROW(static_cast(val - sub2), std::range_error); } template void test_overflow() { using uint_t = boost::multiprecision::number>; uint_t value = (std::numeric_limits::max)(); BOOST_CHECK_THROW(value += 1, std::overflow_error); // // We don't care what the value is, but it must be sane and normalized // to be within the range of the type under test: // BOOST_CHECK(value <= (std::numeric_limits::max)()); } template struct TestOverflow { static void run() { test_overflow(); TestOverflow::run(); } }; template <> struct TestOverflow<1032> { static void run() {} }; int main() { using checked_uint64_t = boost::multiprecision::number>; test_subtract_underflow(); test_subtract_underflow(); test_subtract_underflow(); test_subtract_underflow(); TestOverflow<8>::run(); return boost::report_errors(); }