Skip to content

Commit 9a35fa2

Browse files
committed
Add test set
1 parent ed55bfb commit 9a35fa2

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ run test_log1p.cpp ;
110110
run test_pow.cpp ;
111111
run test_promotion.cpp ;
112112
run test_remainder_remquo.cpp ;
113+
run test_remove_trailing_zeros.cpp ;
113114
run test_sin_cos.cpp ;
114115
run test_sinh.cpp ;
115116
run test_snprintf.cpp ;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2024 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#include <boost/decimal.hpp>
6+
#include <boost/core/lightweight_test.hpp>
7+
#include <array>
8+
#include <limits>
9+
#include <cstdint>
10+
11+
template <typename T>
12+
void test()
13+
{
14+
constexpr std::array<std::uint64_t, 20> powers_of_10 =
15+
{{
16+
UINT64_C(1), UINT64_C(10), UINT64_C(100), UINT64_C(1000), UINT64_C(10000), UINT64_C(100000), UINT64_C(1000000),
17+
UINT64_C(10000000), UINT64_C(100000000), UINT64_C(1000000000), UINT64_C(10000000000), UINT64_C(100000000000),
18+
UINT64_C(1000000000000), UINT64_C(10000000000000), UINT64_C(100000000000000), UINT64_C(1000000000000000),
19+
UINT64_C(10000000000000000), UINT64_C(100000000000000000), UINT64_C(1000000000000000000), UINT64_C(10000000000000000000)
20+
}};
21+
22+
for (const auto& val : powers_of_10)
23+
{
24+
if (val < std::numeric_limits<T>::max())
25+
{
26+
const auto temp {boost::decimal::detail::remove_trailing_zeros(static_cast<T>(val))};
27+
if (!BOOST_TEST_EQ(temp.trimmed_number, T(1)))
28+
{
29+
std::cerr << "Input Number: " << val
30+
<< "\nOutput Number: " << temp.trimmed_number
31+
<< "\nZeros removed: " << temp.number_of_removed_zeros << std::endl;
32+
}
33+
}
34+
}
35+
}
36+
37+
void test_extended()
38+
{
39+
using namespace boost::decimal;
40+
constexpr std::array<detail::uint128, 18> powers_of_10 =
41+
{{
42+
detail::uint128 {UINT64_C(0x5), UINT64_C(0x6BC75E2D63100000)},
43+
detail::uint128 {UINT64_C(0x36), UINT64_C(0x35C9ADC5DEA00000)},
44+
detail::uint128 {UINT64_C(0x21E), UINT64_C(0x19E0C9BAB2400000)},
45+
detail::uint128 {UINT64_C(0x152D), UINT64_C(0x02C7E14AF6800000)},
46+
detail::uint128 {UINT64_C(0x84595), UINT64_C(0x161401484A000000)},
47+
detail::uint128 {UINT64_C(0x52B7D2), UINT64_C(0xDCC80CD2E4000000)},
48+
detail::uint128 {UINT64_C(0x33B2E3C), UINT64_C(0x9FD0803CE8000000)},
49+
detail::uint128 {UINT64_C(0x204FCE5E), UINT64_C(0x3E25026110000000)},
50+
detail::uint128 {UINT64_C(0x1431E0FAE), UINT64_C(0x6D7217CAA0000000)},
51+
detail::uint128 {UINT64_C(0xC9F2C9CD0), UINT64_C(0x4674EDEA40000000)},
52+
detail::uint128 {UINT64_C(0x7E37BE2022), UINT64_C(0xC0914B2680000000)},
53+
detail::uint128 {UINT64_C(0x4EE2D6D415B), UINT64_C(0x85ACEF8100000000)},
54+
detail::uint128 {UINT64_C(0x314DC6448D93), UINT64_C(0x38C15B0A00000000)},
55+
detail::uint128 {UINT64_C(0x1ED09BEAD87C0), UINT64_C(0x378D8E6400000000)},
56+
detail::uint128 {UINT64_C(0x13426172C74D82), UINT64_C(0x2B878FE800000000)},
57+
detail::uint128 {UINT64_C(0xC097CE7BC90715), UINT64_C(0xB34B9F1000000000)},
58+
detail::uint128 {UINT64_C(0x785EE10D5DA46D9), UINT64_C(0x00F436A000000000)},
59+
detail::uint128 {UINT64_C(0x4B3B4CA85A86C47A), UINT64_C(0x098A224000000000)}
60+
}};
61+
62+
for (const auto& val : powers_of_10)
63+
{
64+
const auto temp {boost::decimal::detail::remove_trailing_zeros(val)};
65+
if (!BOOST_TEST_EQ(temp.trimmed_number, detail::uint128(1)))
66+
{
67+
std::cerr << "Input Number: " << val
68+
<< "\nOutput Number: " << temp.trimmed_number
69+
<< "\nZeros removed: " << temp.number_of_removed_zeros << std::endl;
70+
}
71+
}
72+
}
73+
74+
int main()
75+
{
76+
test<std::uint32_t>();
77+
test<std::uint64_t>();
78+
test<boost::decimal::detail::uint128>();
79+
80+
test_extended();
81+
82+
return boost::report_errors();
83+
}

0 commit comments

Comments
 (0)