1// Copyright 2024 Alexander Grund
2// Copyright 2024 Matt Borland
3// Distributed under the Boost Software License, Version 1.0.
4// https://www.boost.org/LICENSE_1_0.txt
5
6#include <locale>
7#include <iostream>
8#include <boost/charconv.hpp>
9#include <boost/charconv/detail/fallback_routines.hpp>
10#include <boost/core/lightweight_test.hpp>
11
12template <typename T>
13void test()
14{
15 const char buffer[] = "1.1897e+2";
16 constexpr auto valdiation_value = static_cast<T>(1.1897e+2L);
17
18 try
19 {
20 #ifdef BOOST_MSVC
21 std::locale::global(std::locale("German"));
22 #else
23 std::locale::global(loc: std::locale("de_DE.UTF-8"));
24 #endif
25 }
26 // LCOV_EXCL_START
27 catch (...)
28 {
29 std::cerr << "Locale not installed. Skipping test." << std::endl;
30 return;
31 }
32 // LCOV_EXCL_STOP
33
34 T v = 0;
35 auto r = boost::charconv::detail::from_chars_strtod(buffer, buffer + sizeof(buffer), v);
36 BOOST_TEST(r);
37
38 std::locale::global(loc: std::locale::classic());
39 T v2 = 0;
40 auto r2 = boost::charconv::detail::from_chars_strtod(buffer, buffer + sizeof(buffer), v2);
41 BOOST_TEST(r2);
42
43 BOOST_TEST_EQ(v, v2);
44 BOOST_TEST_EQ(v, valdiation_value);
45 BOOST_TEST(r.ptr == r2.ptr);
46}
47
48// See: https://stackoverflow.com/questions/1745045/stdlocale-breakage-on-macos-10-6-with-lang-en-us-utf-8
49#if !defined(__APPLE__) || (defined(__APPLE__) && defined(__clang__))
50
51int main()
52{
53 test<float>();
54 test<double>();
55 test<long double>();
56
57 return boost::report_errors();
58}
59
60#else
61
62int main()
63{
64 return 0;
65}
66
67#endif
68

source code of boost/libs/charconv/test/github_issue_122.cpp