|
| 1 | +// Copyright 2025 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +// Use compile-time rounding mode change so the test run on all platforms |
| 6 | +#define BOOST_DECIMAL_FE_DEC_DOWNWARD |
| 7 | + |
| 8 | +#include <boost/decimal.hpp> |
| 9 | +#include <boost/core/lightweight_test.hpp> |
| 10 | +#include <functional> |
| 11 | +#include <iostream> |
| 12 | +#include <iomanip> |
| 13 | + |
| 14 | +template <typename T, typename Func> |
| 15 | +void test(const char* lhs_str, const char* rhs_str, const char* result_str, Func f) |
| 16 | +{ |
| 17 | + const T lhs {lhs_str}; |
| 18 | + const T rhs {rhs_str}; |
| 19 | + const T result {result_str}; |
| 20 | + |
| 21 | + const T func_result {f(lhs, rhs)}; |
| 22 | + |
| 23 | + BOOST_TEST_EQ(func_result, result); |
| 24 | +} |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +void test_add(const char* lhs, const char* rhs, const char* result) |
| 28 | +{ |
| 29 | + std::cerr << std::setprecision(std::numeric_limits<T>::max_digits10); |
| 30 | + return test<T>(lhs, rhs, result, std::plus<>()); |
| 31 | +} |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +void test_sub(const char* lhs, const char* rhs, const char* result) |
| 35 | +{ |
| 36 | + std::cerr << std::setprecision(std::numeric_limits<T>::max_digits10); |
| 37 | + return test<T>(lhs, rhs, result, std::minus<>()); |
| 38 | +} |
| 39 | + |
| 40 | +int main() |
| 41 | +{ |
| 42 | + using namespace boost::decimal; |
| 43 | + |
| 44 | + test_add<decimal64_t>("1e+2", "-1e-383", "99.99999999999999"); |
| 45 | + test_add<decimal64_t>("1e+1", "-1e-383", "9.999999999999999"); |
| 46 | + |
| 47 | + test_add<decimal_fast64_t>("1e+2", "-1e-383", "99.99999999999999"); |
| 48 | + test_add<decimal_fast64_t>("1e+1", "-1e-383", "9.999999999999999"); |
| 49 | + |
| 50 | + test_sub<decimal64_t>("1e+2", "1e-383", "99.99999999999999"); |
| 51 | + test_sub<decimal64_t>("1e+1", "1e-383", "9.999999999999999"); |
| 52 | + |
| 53 | + test_sub<decimal_fast64_t>("1e+2", "1e-383", "99.99999999999999"); |
| 54 | + test_sub<decimal_fast64_t>("1e+1", "1e-383", "9.999999999999999"); |
| 55 | + |
| 56 | + return boost::report_errors(); |
| 57 | +} |
0 commit comments