|
| 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 | + // LCOV_EXCL_START |
| 30 | + std::cerr << "Input Number: " << val |
| 31 | + << "\nOutput Number: " << temp.trimmed_number |
| 32 | + << "\nZeros removed: " << temp.number_of_removed_zeros << std::endl; |
| 33 | + // LCOV_EXCL_STOP |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void test_extended() |
| 40 | +{ |
| 41 | + using namespace boost::decimal; |
| 42 | + constexpr std::array<detail::uint128, 18> powers_of_10 = |
| 43 | + {{ |
| 44 | + detail::uint128 {UINT64_C(0x5), UINT64_C(0x6BC75E2D63100000)}, |
| 45 | + detail::uint128 {UINT64_C(0x36), UINT64_C(0x35C9ADC5DEA00000)}, |
| 46 | + detail::uint128 {UINT64_C(0x21E), UINT64_C(0x19E0C9BAB2400000)}, |
| 47 | + detail::uint128 {UINT64_C(0x152D), UINT64_C(0x02C7E14AF6800000)}, |
| 48 | + detail::uint128 {UINT64_C(0x84595), UINT64_C(0x161401484A000000)}, |
| 49 | + detail::uint128 {UINT64_C(0x52B7D2), UINT64_C(0xDCC80CD2E4000000)}, |
| 50 | + detail::uint128 {UINT64_C(0x33B2E3C), UINT64_C(0x9FD0803CE8000000)}, |
| 51 | + detail::uint128 {UINT64_C(0x204FCE5E), UINT64_C(0x3E25026110000000)}, |
| 52 | + detail::uint128 {UINT64_C(0x1431E0FAE), UINT64_C(0x6D7217CAA0000000)}, |
| 53 | + detail::uint128 {UINT64_C(0xC9F2C9CD0), UINT64_C(0x4674EDEA40000000)}, |
| 54 | + detail::uint128 {UINT64_C(0x7E37BE2022), UINT64_C(0xC0914B2680000000)}, |
| 55 | + detail::uint128 {UINT64_C(0x4EE2D6D415B), UINT64_C(0x85ACEF8100000000)}, |
| 56 | + detail::uint128 {UINT64_C(0x314DC6448D93), UINT64_C(0x38C15B0A00000000)}, |
| 57 | + detail::uint128 {UINT64_C(0x1ED09BEAD87C0), UINT64_C(0x378D8E6400000000)}, |
| 58 | + detail::uint128 {UINT64_C(0x13426172C74D82), UINT64_C(0x2B878FE800000000)}, |
| 59 | + detail::uint128 {UINT64_C(0xC097CE7BC90715), UINT64_C(0xB34B9F1000000000)}, |
| 60 | + detail::uint128 {UINT64_C(0x785EE10D5DA46D9), UINT64_C(0x00F436A000000000)}, |
| 61 | + detail::uint128 {UINT64_C(0x4B3B4CA85A86C47A), UINT64_C(0x098A224000000000)} |
| 62 | + }}; |
| 63 | + |
| 64 | + for (const auto& val : powers_of_10) |
| 65 | + { |
| 66 | + const auto temp {boost::decimal::detail::remove_trailing_zeros(val)}; |
| 67 | + if (!BOOST_TEST_EQ(temp.trimmed_number, detail::uint128(1))) |
| 68 | + { |
| 69 | + // LCOV_EXCL_START |
| 70 | + std::cerr << "Input Number: " << val |
| 71 | + << "\nOutput Number: " << temp.trimmed_number |
| 72 | + << "\nZeros removed: " << temp.number_of_removed_zeros << std::endl; |
| 73 | + // LCOV_EXCL_STOP |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +int main() |
| 79 | +{ |
| 80 | + test<std::uint32_t>(); |
| 81 | + test<std::uint64_t>(); |
| 82 | + test<boost::decimal::detail::uint128>(); |
| 83 | + |
| 84 | + test_extended(); |
| 85 | + |
| 86 | + return boost::report_errors(); |
| 87 | +} |
0 commit comments