|
| 1 | +// Copyright 2026 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/int128.hpp> |
| 6 | +#include <boost/int128/charconv.hpp> |
| 7 | +#include <boost/charconv.hpp> |
| 8 | +#include <boost/core/lightweight_test.hpp> |
| 9 | +#include <string> |
| 10 | +#include <limits> |
| 11 | +#include <array> |
| 12 | +#include <cstdint> |
| 13 | + |
| 14 | +using namespace boost::int128; |
| 15 | + |
| 16 | +template <typename T> |
| 17 | +void test() |
| 18 | +{ |
| 19 | + constexpr T min {std::numeric_limits<T>::min()}; |
| 20 | + constexpr T max {std::numeric_limits<T>::max()}; |
| 21 | + constexpr std::array<T, 4> values = {{ min, max, T{0}, T{42} }}; |
| 22 | + |
| 23 | + for (const auto value : values) |
| 24 | + { |
| 25 | + char buffer[64]; |
| 26 | + const auto r {boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, 10)}; |
| 27 | + BOOST_TEST(r); |
| 28 | + *r.ptr = '\0'; |
| 29 | + |
| 30 | + const auto value_str {to_string(value)}; |
| 31 | + |
| 32 | + BOOST_TEST_CSTR_EQ(buffer, value_str.c_str()); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +template <typename T, std::enable_if_t<std::is_signed<T>::value, bool> = true> |
| 37 | +void test_builtin() |
| 38 | +{ |
| 39 | + constexpr T min {std::numeric_limits<T>::min()}; |
| 40 | + constexpr T max {std::numeric_limits<T>::max()}; |
| 41 | + constexpr std::array<T, 4> values = {{ min, max, T{0}, T{42} }}; |
| 42 | + |
| 43 | + for (const auto value : values) |
| 44 | + { |
| 45 | + char buffer[64]; |
| 46 | + const auto r {boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, 10)}; |
| 47 | + BOOST_TEST(r); |
| 48 | + *r.ptr = '\0'; |
| 49 | + |
| 50 | + const auto value_str {to_string(int128_t{value})}; |
| 51 | + |
| 52 | + BOOST_TEST_CSTR_EQ(buffer, value_str.c_str()); |
| 53 | + BOOST_TEST_CSTR_EQ(std::to_string(value).c_str(), value_str.c_str()); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +template <typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true> |
| 58 | +void test_builtin() |
| 59 | +{ |
| 60 | + constexpr T min {std::numeric_limits<T>::min()}; |
| 61 | + constexpr T max {std::numeric_limits<T>::max()}; |
| 62 | + constexpr std::array<T, 4> values = {{ min, max, T{0}, T{42} }}; |
| 63 | + |
| 64 | + for (const auto value : values) |
| 65 | + { |
| 66 | + char buffer[64]; |
| 67 | + const auto r {boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, 10)}; |
| 68 | + BOOST_TEST(r); |
| 69 | + *r.ptr = '\0'; |
| 70 | + |
| 71 | + { |
| 72 | + const auto value_str {to_string(int128_t{value})}; |
| 73 | + |
| 74 | + BOOST_TEST_CSTR_EQ(buffer, value_str.c_str()); |
| 75 | + BOOST_TEST_CSTR_EQ(std::to_string(value).c_str(), value_str.c_str()); |
| 76 | + } |
| 77 | + { |
| 78 | + const auto value_str {to_string(uint128_t{value})}; |
| 79 | + |
| 80 | + BOOST_TEST_CSTR_EQ(buffer, value_str.c_str()); |
| 81 | + BOOST_TEST_CSTR_EQ(std::to_string(value).c_str(), value_str.c_str()); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +int main() |
| 87 | +{ |
| 88 | + test<uint128_t>(); |
| 89 | + test<int128_t>(); |
| 90 | + |
| 91 | + test_builtin<std::int8_t>(); |
| 92 | + test_builtin<std::uint8_t>(); |
| 93 | + test_builtin<std::int16_t>(); |
| 94 | + test_builtin<std::uint16_t>(); |
| 95 | + test_builtin<std::int32_t>(); |
| 96 | + test_builtin<std::uint32_t>(); |
| 97 | + test_builtin<std::int64_t>(); |
| 98 | + test_builtin<std::uint64_t>(); |
| 99 | + |
| 100 | + return boost::report_errors(); |
| 101 | +} |
0 commit comments