| 1 | // Copyright 2023 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/charconv/detail/integer_search_trees.hpp> |
| 6 | #include <boost/charconv/detail/emulated128.hpp> |
| 7 | #include <boost/charconv/config.hpp> |
| 8 | #include <boost/core/lightweight_test.hpp> |
| 9 | #include <type_traits> |
| 10 | #include <limits> |
| 11 | |
| 12 | #if defined(__GNUC__) && (__GNUC__ < 7) |
| 13 | # pragma GCC diagnostic push |
| 14 | # pragma GCC diagnostic ignored "-Woverflow" |
| 15 | #endif |
| 16 | |
| 17 | #ifdef BOOST_CHARCONV_HAS_INT128 |
| 18 | // Test the u128 impl |
| 19 | template <typename T> |
| 20 | void test128() |
| 21 | { |
| 22 | |
| 23 | T max = BOOST_CHARCONV_UINT128_MAX; |
| 24 | max /= 10; |
| 25 | T val = 1; |
| 26 | int i = 1; |
| 27 | |
| 28 | while (val < max) |
| 29 | { |
| 30 | BOOST_TEST_EQ(boost::charconv::detail::num_digits(val), i); |
| 31 | ++i; |
| 32 | val *= 10; |
| 33 | } |
| 34 | |
| 35 | BOOST_TEST_EQ(boost::charconv::detail::num_digits(max * 10), 39); |
| 36 | } |
| 37 | #endif |
| 38 | |
| 39 | #if defined(__GNUC__) && (__GNUC__ < 7) |
| 40 | # pragma GCC diagnostic pop |
| 41 | #endif |
| 42 | |
| 43 | #if defined(__GNUC__) && (__GNUC__ >= 5) |
| 44 | # pragma GCC diagnostic push |
| 45 | # pragma GCC diagnostic ignored "-Wconversion" |
| 46 | #endif |
| 47 | |
| 48 | // Tests the generic, u32, and u64 impls |
| 49 | template <typename T> |
| 50 | void test() |
| 51 | { |
| 52 | constexpr T max = (std::numeric_limits<T>::max)() / 10; |
| 53 | T val = 1; |
| 54 | int i = 1; |
| 55 | |
| 56 | while (val < max) |
| 57 | { |
| 58 | BOOST_TEST_EQ(boost::charconv::detail::num_digits(val), i); |
| 59 | ++i; |
| 60 | val *= static_cast<T>(10); |
| 61 | } |
| 62 | |
| 63 | BOOST_TEST_EQ(boost::charconv::detail::num_digits(val), std::numeric_limits<T>::digits10 + 1); |
| 64 | } |
| 65 | |
| 66 | #if defined(__GNUC__) && (__GNUC__ >= 5) |
| 67 | # pragma GCC diagnostic pop |
| 68 | #endif |
| 69 | |
| 70 | void test_emulated128() |
| 71 | { |
| 72 | using namespace boost::charconv::detail; |
| 73 | |
| 74 | uint128 v1 {0, 1234}; |
| 75 | BOOST_TEST_EQ(num_digits(v1), 4); |
| 76 | |
| 77 | uint128 v2 {1234, 0}; |
| 78 | BOOST_TEST_EQ(num_digits(v2), 23); |
| 79 | |
| 80 | uint128 v3 {UINT64_MAX, UINT64_MAX}; |
| 81 | BOOST_TEST_EQ(num_digits(v3), 39); |
| 82 | } |
| 83 | |
| 84 | int main() |
| 85 | { |
| 86 | test<char>(); |
| 87 | test<signed char>(); |
| 88 | test<unsigned char>(); |
| 89 | test<short>(); |
| 90 | test<unsigned short>(); |
| 91 | test<int>(); |
| 92 | test<unsigned>(); |
| 93 | test<long>(); |
| 94 | test<unsigned long>(); |
| 95 | test<long long>(); |
| 96 | test<unsigned long long>(); |
| 97 | |
| 98 | // Specialized tables |
| 99 | test<std::uint32_t>(); |
| 100 | test<std::uint64_t>(); |
| 101 | #ifdef BOOST_CHARCONV_HAS_INT128 |
| 102 | test128<boost::uint128_type>(); |
| 103 | #endif |
| 104 | |
| 105 | test_emulated128(); |
| 106 | |
| 107 | return boost::report_errors(); |
| 108 | } |
| 109 | |