| 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/charconv.hpp> |
| 6 | #include <boost/core/lightweight_test.hpp> |
| 7 | |
| 8 | template <typename T> |
| 9 | void overflow_spot_value(const std::string& buffer, boost::charconv::chars_format fmt = boost::charconv::chars_format::general) |
| 10 | { |
| 11 | auto v = static_cast<T>(42.L); |
| 12 | auto r = boost::charconv::from_chars(buffer.c_str(), buffer.c_str() + std::strlen(s: buffer.c_str()), v, fmt); |
| 13 | |
| 14 | BOOST_TEST(v == static_cast<T>(42.L)) && BOOST_TEST(r.ec == std::errc::result_out_of_range); |
| 15 | } |
| 16 | |
| 17 | template <typename T> |
| 18 | void test() |
| 19 | { |
| 20 | const auto format_list = {boost::charconv::chars_format::general, boost::charconv::chars_format::scientific, boost::charconv::chars_format::hex}; |
| 21 | |
| 22 | for (const auto format : format_list) |
| 23 | { |
| 24 | if (format != boost::charconv::chars_format::hex) |
| 25 | { |
| 26 | overflow_spot_value<T>("1e99999" , format); |
| 27 | overflow_spot_value<T>("-1e99999" , format); |
| 28 | overflow_spot_value<T>("1e-99999" , format); |
| 29 | overflow_spot_value<T>("-1.0e-99999" , format); |
| 30 | } |
| 31 | else |
| 32 | { |
| 33 | overflow_spot_value<T>("1p99999" , format); |
| 34 | overflow_spot_value<T>("-1p99999" , format); |
| 35 | overflow_spot_value<T>("1p-99999" , format); |
| 36 | overflow_spot_value<T>("-1.0p-99999" , format); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | int main() |
| 42 | { |
| 43 | test<float>(); |
| 44 | test<double>(); |
| 45 | test<long double>(); |
| 46 | |
| 47 | #ifdef BOOST_CHARCONV_HAS_FLOAT128 |
| 48 | test<__float128>(); |
| 49 | #endif |
| 50 | |
| 51 | #ifdef BOOST_CHARCONV_HAS_FLOAT16 |
| 52 | test<std::float16_t>(); |
| 53 | #endif |
| 54 | #ifdef BOOST_CHARCONV_HAS_FLOAT32 |
| 55 | test<std::float32_t>(); |
| 56 | #endif |
| 57 | #ifdef BOOST_CHARCONV_HAS_FLOAT64 |
| 58 | test<std::float64_t>(); |
| 59 | #endif |
| 60 | #if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128) |
| 61 | test<std::float128_t>(); |
| 62 | #endif |
| 63 | #ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16 |
| 64 | test<std::bfloat16_t>(); |
| 65 | #endif |
| 66 | |
| 67 | return boost::report_errors(); |
| 68 | } |
| 69 | |