Skip to content

Commit ce58f98

Browse files
committed
Add testing of upward rounding edge cases
1 parent 204d0eb commit ce58f98

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ run test_tgamma.cpp ;
188188
run test_to_chars.cpp ;
189189
run test_to_string.cpp ;
190190
run test_total_ordering.cpp ;
191+
run test_upward_rounding.cpp ;
191192
run test_zeta.cpp ;
192193

193194
run limits_link_1.cpp limits_link_2.cpp limits_link_3.cpp ;

test/test_upward_rounding.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
7+
#define BOOST_DECIMAL_FE_DEC_UPWARD
8+
9+
#include <boost/decimal.hpp>
10+
#include <boost/core/lightweight_test.hpp>
11+
#include <functional>
12+
#include <iostream>
13+
#include <iomanip>
14+
15+
template <typename T, typename Func>
16+
void test(const char* lhs_str, const char* rhs_str, const char* result_str, Func f)
17+
{
18+
const T lhs {lhs_str};
19+
const T rhs {rhs_str};
20+
const T result {result_str};
21+
22+
const T func_result {f(lhs, rhs)};
23+
24+
BOOST_TEST_EQ(func_result, result);
25+
}
26+
27+
template <typename T>
28+
void test_add(const char* lhs, const char* rhs, const char* result)
29+
{
30+
std::cerr << std::setprecision(std::numeric_limits<T>::max_digits10);
31+
test<T>(lhs, rhs, result, std::plus<>());
32+
test<T>(rhs, lhs, result, std::plus<>());
33+
}
34+
35+
template <typename T>
36+
void test_sub(const char* lhs, const char* rhs, const char* result)
37+
{
38+
std::cerr << std::setprecision(std::numeric_limits<T>::max_digits10);
39+
test<T>(lhs, rhs, result, std::minus<>());
40+
}
41+
42+
int main()
43+
{
44+
using namespace boost::decimal;
45+
46+
test_add<decimal64_t>("-1e+2", "+1e-383", "-99.99999999999999");
47+
test_add<decimal64_t>("-1e+1", "+1e-383", "-9.999999999999999");
48+
test_add<decimal64_t>("-1e+0", "+1e-383", "-0.9999999999999999");
49+
50+
return boost::report_errors();
51+
}

0 commit comments

Comments
 (0)