// Copyright 2024 Matt Borland // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #include #include #include #include #include #include #include #include #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) # include #endif static std::mt19937_64 rng(42); constexpr std::size_t N = 1024; template void test_int() { std::uniform_int_distribution dist((std::numeric_limits::min)(), (std::numeric_limits::max)()); for (std::size_t i = 0; i < N; ++i) { const auto value = dist(rng); std::string str_value = std::to_string(value); StringViewType sv = str_value; T v = 0; auto r = boost::charconv::from_chars(sv, v); BOOST_TEST(r); BOOST_TEST_EQ(v, value); } } template void test_float() { std::uniform_real_distribution dist(T(-1e3), T(1e3)); for (std::size_t i = 0; i < N; ++i) { const auto value = dist(rng); std::string str_value = std::to_string(value); StringViewType sv = str_value; T v = 0; auto r = boost::charconv::from_chars(sv, v); T v2 = 0; auto r2 = boost::charconv::from_chars(str_value.data(), str_value.data() + str_value.size(), v2); if( BOOST_TEST( r.ec == std::errc() ) && BOOST_TEST( r2.ec == std::errc() ) && BOOST_TEST( v == v2 ) ) { } else { std::cerr << std::setprecision(std::numeric_limits::digits10) << "... test failure for value=" << value << "; buffer='" << str_value << "'; errc=" << static_cast(r.ec) << "," << static_cast(r2.ec) << std::endl; // LCOV_EXCL_LINE } } } int main() { // MSVC does not allow (un)signed char in uniform_int_distribution // GCC and clang do #ifndef _MSC_VER test_int(); test_int(); #endif test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); #ifndef _MSC_VER test_int(); test_int(); #endif test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) #ifndef _MSC_VER test_int(); test_int(); #endif test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); test_int(); #endif test_float(); test_float(); test_float(); test_float(); #ifndef BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE test_float(); test_float(); #endif #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) test_float(); test_float(); #ifndef BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE test_float(); #endif #endif #ifdef BOOST_CHARCONV_HAS_FLOAT32 test_float(); test_float(); test_float(); #endif #ifdef BOOST_CHARCONV_HAS_FLOAT64 test_float(); test_float(); test_float(); #endif return boost::report_errors(); }