|
| 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 | +// See: https://github.com/boostorg/charconv/issues/166 |
| 6 | + |
| 7 | +#include <boost/charconv.hpp> |
| 8 | +#include <boost/core/lightweight_test.hpp> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +template <typename T> |
| 12 | +void test() |
| 13 | +{ |
| 14 | + constexpr T value = 3746.348756384763L; |
| 15 | + constexpr int precision = 6; |
| 16 | + |
| 17 | + char buffer[1024]; |
| 18 | + const auto result = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision); |
| 19 | + *result.ptr = '\0'; |
| 20 | + BOOST_TEST(result.ec == std::errc()); |
| 21 | + BOOST_TEST_EQ(std::string{buffer}, std::to_string(3746.348756)); |
| 22 | +} |
| 23 | + |
| 24 | +template <typename T> |
| 25 | +void rounding() |
| 26 | +{ |
| 27 | + constexpr T value = 3746.348759784763L; |
| 28 | + constexpr int precision = 6; |
| 29 | + |
| 30 | + char buffer[1024]; |
| 31 | + const auto result = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision); |
| 32 | + *result.ptr = '\0'; |
| 33 | + BOOST_TEST(result.ec == std::errc()); |
| 34 | + BOOST_TEST_EQ(std::string{buffer}, std::to_string(3746.348760)); |
| 35 | +} |
| 36 | + |
| 37 | +template <typename T> |
| 38 | +void more_rounding() |
| 39 | +{ |
| 40 | + constexpr T value = 3746.89999999999999999L; |
| 41 | + constexpr int precision = 6; |
| 42 | + |
| 43 | + char buffer[1024]; |
| 44 | + const auto result = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision); |
| 45 | + *result.ptr = '\0'; |
| 46 | + BOOST_TEST(result.ec == std::errc()); |
| 47 | + BOOST_TEST_EQ(std::string{buffer}, std::to_string(3746.900000)); |
| 48 | +} |
| 49 | + |
| 50 | +template <typename T> |
| 51 | +void further_rounding() |
| 52 | +{ |
| 53 | + const std::array<std::string, 8U> solutions = {{ |
| 54 | + "3331.5", |
| 55 | + "3331.52", |
| 56 | + "3331.520", |
| 57 | + "3331.5199", |
| 58 | + "3331.51989", |
| 59 | + "3331.519894", |
| 60 | + "3331.5198945", |
| 61 | + "3331.51989448" |
| 62 | + }}; |
| 63 | + |
| 64 | + constexpr T value = 3331.519894481067353L; |
| 65 | + |
| 66 | + for (int i = 1; i < 9; ++i) |
| 67 | + { |
| 68 | + char buffer[1024]; |
| 69 | + const auto result = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, i); |
| 70 | + *result.ptr = '\0'; |
| 71 | + BOOST_TEST(result.ec == std::errc()); |
| 72 | + if (!BOOST_TEST_EQ(std::string(buffer), solutions[static_cast<std::size_t>(i - 1)])) |
| 73 | + { |
| 74 | + // LCOV_EXCL_START |
| 75 | + std::cerr << "Precision: " << i |
| 76 | + << "\nExpected: " << solutions[static_cast<std::size_t>(i - 1)] |
| 77 | + << "\n Got: " << buffer << std::endl; |
| 78 | + // LCOV_EXCL_STOP |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +template <typename T> |
| 84 | +void full_rounding_test() |
| 85 | +{ |
| 86 | + constexpr T value = 9999.999999999999999999L; |
| 87 | + constexpr int precision = 6; |
| 88 | + |
| 89 | + char buffer[1024]; |
| 90 | + const auto result = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision); |
| 91 | + *result.ptr = '\0'; |
| 92 | + BOOST_TEST(result.ec == std::errc()); |
| 93 | + BOOST_TEST_EQ(std::string{buffer}, std::to_string(10000.000000)); |
| 94 | +} |
| 95 | + |
| 96 | +template <typename T> |
| 97 | +void test_zeros_path() |
| 98 | +{ |
| 99 | + constexpr T value = 123456789.000000L; |
| 100 | + constexpr int precision = 6; |
| 101 | + |
| 102 | + char buffer[1024]; |
| 103 | + const auto result = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision); |
| 104 | + *result.ptr = '\0'; |
| 105 | + BOOST_TEST(result.ec == std::errc()); |
| 106 | + BOOST_TEST_EQ(std::string{buffer}, std::to_string(123456789.000000)); |
| 107 | +} |
| 108 | + |
| 109 | +int main() |
| 110 | +{ |
| 111 | + #if BOOST_CHARCONV_LDBL_BITS == 80 |
| 112 | + test<long double>(); |
| 113 | + rounding<long double>(); |
| 114 | + more_rounding<long double>(); |
| 115 | + further_rounding<long double>(); |
| 116 | + full_rounding_test<long double>(); |
| 117 | + test_zeros_path<long double>(); |
| 118 | + #endif |
| 119 | + |
| 120 | + #ifdef BOOST_CHARCONV_HAS_QUADMATH |
| 121 | + test<__float128>(); |
| 122 | + rounding<__float128>(); |
| 123 | + more_rounding<__float128>(); |
| 124 | + further_rounding<__float128>(); |
| 125 | + full_rounding_test<__float128>(); |
| 126 | + test_zeros_path<__float128>(); |
| 127 | + #endif |
| 128 | + |
| 129 | + #if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_QUADMATH) |
| 130 | + test<std::float128_t>(); |
| 131 | + rounding<std::float128_t>(); |
| 132 | + more_rounding<std::float128_t>(); |
| 133 | + further_rounding<std::float128_t>(); |
| 134 | + full_rounding_test<std::float128_t>(); |
| 135 | + test_zeros_path<std::float128_t>(); |
| 136 | + #endif |
| 137 | + |
| 138 | + return boost::report_errors(); |
| 139 | +} |
0 commit comments