1+ //
2+ // Copyright (c) 2024 Alexander Grund
3+ //
4+ // Distributed under the Boost Software License, Version 1.0.
5+ // https://www.boost.org/LICENSE_1_0.txt
6+
7+ #include < boost/locale/formatting.hpp>
8+ #include < boost/locale/generator.hpp>
9+ #include < cstdint>
10+ #include < limits>
11+ #include < sstream>
12+
13+ #include " ../src/boost/locale/util/foreach_char.hpp"
14+ #include " boostLocale/test/tools.hpp"
15+ #include " boostLocale/test/unit_test.hpp"
16+
17+ template <typename CharType, typename IntType>
18+ void test_parse_multi_number_by_char (const std::locale& locale)
19+ {
20+ std::basic_istringstream<CharType> stream;
21+ stream.imbue (locale);
22+ stream.str (ascii_to<CharType>(" 42.12,345" ));
23+ stream >> boost::locale::as::number;
24+
25+ IntType value;
26+ TEST_REQUIRE (stream >> value);
27+ TEST_EQ (value, IntType (42 ));
28+ TEST_EQ (static_cast <char >(stream.get ()), ' .' );
29+ TEST_REQUIRE (stream >> value);
30+ TEST_EQ (value, IntType (12345 ));
31+ TEST_REQUIRE (!(stream >> value));
32+ TEST (stream.eof ());
33+
34+ stream.str (ascii_to<CharType>(" 42.25,678" ));
35+ stream.clear ();
36+ float fValue ;
37+ TEST_REQUIRE (stream >> fValue );
38+ TEST_EQ (fValue , 42.25 );
39+ TEST_EQ (static_cast <char >(stream.get ()), ' ,' );
40+ TEST_REQUIRE (stream >> value);
41+ TEST_EQ (value, IntType (678 ));
42+ TEST_REQUIRE (!(stream >> value));
43+ TEST (stream.eof ());
44+
45+ // Parsing a floating point currency to integer truncates the floating point value but fully parses it
46+ stream.str (ascii_to<CharType>(" USD1,234.55,67.89" ));
47+ stream.clear ();
48+ TEST_REQUIRE (!(stream >> value));
49+ stream.clear ();
50+ stream >> boost::locale::as::currency >> boost::locale::as::currency_iso;
51+ if (stream >> value) { // Parsing currencies not fully supported by WinAPI backend
52+ TEST_EQ (value, IntType (1234 ));
53+ TEST_EQ (static_cast <char >(stream.get ()), ' ,' );
54+ TEST_REQUIRE (stream >> boost::locale::as::number >> value);
55+ TEST_EQ (value, IntType (67 ));
56+ TEST (!stream.eof ());
57+ }
58+ }
59+
60+ // / Test that parsing multiple numbers without any spaces works as expected
61+ void test_parse_multi_number ()
62+ {
63+ const auto locale = boost::locale::generator{}(" en_US.UTF-8" );
64+
65+ #define BOOST_LOCALE_CALL_I (T, I ) \
66+ std::cout << " \t " #I << std::endl; \
67+ test_parse_multi_number_by_char<T, I>(locale);
68+
69+ #define BOOST_LOCALE_CALL (T ) \
70+ std::cout << " test_parse_multi_number " #T << std::endl; \
71+ BOOST_LOCALE_CALL_I (T, int16_t ); \
72+ BOOST_LOCALE_CALL_I (T, uint16_t ); \
73+ BOOST_LOCALE_CALL_I (T, int32_t ); \
74+ BOOST_LOCALE_CALL_I (T, uint32_t ); \
75+ BOOST_LOCALE_CALL_I (T, int64_t ); \
76+ BOOST_LOCALE_CALL_I (T, uint64_t );
77+
78+ BOOST_LOCALE_CALL (char );
79+ BOOST_LOCALE_CALL (wchar_t );
80+ #undef BOOST_LOCALE_CALL
81+ #undef BOOST_LOCALE_CALL_I
82+ }
0 commit comments