1818// string to_string(double val);
1919// string to_string(long double val);
2020
21- #include < string>
2221#include < cassert>
22+ #include < format>
23+ #include < string>
2324#include < limits>
2425
2526#include " parse_integer.h"
@@ -32,29 +33,44 @@ void test_signed() {
3233 assert (s.size () == 1 );
3334 assert (s[s.size ()] == 0 );
3435 assert (s == " 0" );
36+ #if TEST_STD_VER >= 26
37+ assert (s == std::format (" {}" , T (0 )));
38+ #endif
3539 }
3640 {
3741 std::string s = std::to_string (T (12345 ));
3842 assert (s.size () == 5 );
3943 assert (s[s.size ()] == 0 );
4044 assert (s == " 12345" );
45+ #if TEST_STD_VER >= 26
46+ assert (s == std::format (" {}" , T (12345 )));
47+ #endif
4148 }
4249 {
4350 std::string s = std::to_string (T (-12345 ));
4451 assert (s.size () == 6 );
4552 assert (s[s.size ()] == 0 );
4653 assert (s == " -12345" );
54+ #if TEST_STD_VER >= 26
55+ assert (s == std::format (" {}" , T (-12345 )));
56+ #endif
4757 }
4858 {
4959 std::string s = std::to_string (std::numeric_limits<T>::max ());
5060 assert (s.size () == std::numeric_limits<T>::digits10 + 1 );
5161 T t = parse_integer<T>(s);
5262 assert (t == std::numeric_limits<T>::max ());
63+ #if TEST_STD_VER >= 26
64+ assert (s == std::format (" {}" , std::numeric_limits<T>::max ()));
65+ #endif
5366 }
5467 {
5568 std::string s = std::to_string (std::numeric_limits<T>::min ());
5669 T t = parse_integer<T>(s);
5770 assert (t == std::numeric_limits<T>::min ());
71+ #if TEST_STD_VER >= 26
72+ assert (s == std::format (" {}" , std::numeric_limits<T>::min ()));
73+ #endif
5874 }
5975}
6076
@@ -65,43 +81,153 @@ void test_unsigned() {
6581 assert (s.size () == 1 );
6682 assert (s[s.size ()] == 0 );
6783 assert (s == " 0" );
84+ #if TEST_STD_VER >= 26
85+ assert (s == std::format (" {}" , T (0 )));
86+ #endif
6887 }
6988 {
7089 std::string s = std::to_string (T (12345 ));
7190 assert (s.size () == 5 );
7291 assert (s[s.size ()] == 0 );
7392 assert (s == " 12345" );
93+ #if TEST_STD_VER >= 26
94+ assert (s == std::format (" {}" , T (12345 )));
95+ #endif
7496 }
7597 {
7698 std::string s = std::to_string (std::numeric_limits<T>::max ());
7799 assert (s.size () == std::numeric_limits<T>::digits10 + 1 );
78100 T t = parse_integer<T>(s);
79101 assert (t == std::numeric_limits<T>::max ());
102+ #if TEST_STD_VER >= 26
103+ assert (s == std::format (" {}" , std::numeric_limits<T>::max ()));
104+ #endif
80105 }
81106}
82107
83108template <class T >
84109void test_float () {
85110 {
86111 std::string s = std::to_string (T (0 ));
112+ #if TEST_STD_VER < 26
87113 assert (s.size () == 8 );
88114 assert (s[s.size ()] == 0 );
89115 assert (s == " 0.000000" );
116+ #else
117+ std::string f = std::format (" {}" , T (0 ));
118+ assert (s == f);
119+ assert (s == " 0" );
120+ #endif
90121 }
91122 {
92123 std::string s = std::to_string (T (12345 ));
124+ #if TEST_STD_VER < 26
93125 assert (s.size () == 12 );
94126 assert (s[s.size ()] == 0 );
95127 assert (s == " 12345.000000" );
128+ #else
129+ std::string f = std::format (" {}" , T (12345 ));
130+ assert (s == f);
131+ assert (s == " 12345" );
132+ #endif
96133 }
97134 {
98135 std::string s = std::to_string (T (-12345 ));
136+ #if TEST_STD_VER < 26
99137 assert (s.size () == 13 );
100138 assert (s[s.size ()] == 0 );
101139 assert (s == " -12345.000000" );
140+ #else
141+ std::string f = std::format (" {}" , T (-12345 ));
142+ assert (s == f);
143+ assert (s == " -12345" );
144+ #endif
145+ }
146+
147+ #if TEST_STD_VER >= 26
148+ {
149+ std::string s = std::to_string (T (90.84 ));
150+ std::string f = std::format (" {}" , T (90.84 ));
151+ assert (s == f);
152+ assert (s == " 90.84" );
102153 }
154+ {
155+ std::string s = std::to_string (T (-90.84 ));
156+ std::string f = std::format (" {}" , T (-90.84 ));
157+ assert (s == f);
158+ assert (s == " -90.84" );
159+ }
160+ #endif
161+ }
162+
163+ #if TEST_STD_VER >= 26
164+
165+ template <class T >
166+ void test_float_with_locale (const char * locale, T inputValue, const char * expectedValue) {
167+ setlocale (LC_ALL, locale);
168+
169+ std::string s = std::to_string (inputValue);
170+ std::string f = std::format (" {}" , inputValue);
171+ assert (s == f);
172+ assert (s == expectedValue);
103173}
104174
175+ void test_float_with_locale () {
176+ // Locale "C"
177+
178+ test_float_with_locale<float >(" C" , 0.9084 , " 0.9084" );
179+ test_float_with_locale<double >(" C" , 0.9084 , " 0.9084" );
180+ test_float_with_locale<long double >(" C" , 0.9084 , " 0.9084" );
181+
182+ test_float_with_locale<float >(" C" , -0.9084 , " -0.9084" );
183+ test_float_with_locale<double >(" C" , -0.9084 , " -0.9084" );
184+ test_float_with_locale<long double >(" C" , -0.9084 , " -0.9084" );
185+
186+ test_float_with_locale<float >(" C" , 1e-7 , " 1e-07" );
187+ test_float_with_locale<double >(" C" , 1e-7 , " 1e-07" );
188+ test_float_with_locale<long double >(" C" , 1e-7 , " 1e-07" );
189+
190+ test_float_with_locale<float >(" C" , -1e-7 , " -1e-07" );
191+ test_float_with_locale<double >(" C" , -1e-7 , " -1e-07" );
192+ test_float_with_locale<long double >(" C" , -1e-7 , " -1e-07" );
193+
194+ test_float_with_locale<float >(" C" , 1.7976931348623157e+308 , " inf" );
195+ test_float_with_locale<double >(" C" , 1.7976931348623157e+308 , " 1.7976931348623157e+308" );
196+ test_float_with_locale<long double >(" C" , 1.7976931348623157e+308 , " 1.7976931348623157e+308" );
197+
198+ test_float_with_locale<float >(" C" , -1.7976931348623157e+308 , " -inf" );
199+ test_float_with_locale<double >(" C" , -1.7976931348623157e+308 , " -1.7976931348623157e+308" );
200+ test_float_with_locale<long double >(" C" , -1.7976931348623157e+308 , " -1.7976931348623157e+308" );
201+
202+ // Locale "uk_UA.UTF-8"
203+
204+ test_float_with_locale<float >(" uk_UA.UTF-8" , 0.9084 , " 0.9084" );
205+ test_float_with_locale<double >(" uk_UA.UTF-8" , 0.9084 , " 0.9084" );
206+ test_float_with_locale<double >(" uk_UA.UTF-8" , 0.9084 , " 0.9084" );
207+
208+ test_float_with_locale<float >(" uk_UA.UTF-8" , -0.9084 , " -0.9084" );
209+ test_float_with_locale<double >(" uk_UA.UTF-8" , -0.9084 , " -0.9084" );
210+ test_float_with_locale<long double >(" uk_UA.UTF-8" , -0.9084 , " -0.9084" );
211+
212+ test_float_with_locale<float >(" uk_UA.UTF-8" , 1e-7 , " 1e-07" );
213+ test_float_with_locale<double >(" uk_UA.UTF-8" , 1e-7 , " 1e-07" );
214+ test_float_with_locale<long double >(" uk_UA.UTF-8" , 1e-7 , " 1e-07" );
215+
216+ test_float_with_locale<float >(" uk_UA.UTF-8" , -1e-7 , " -1e-07" );
217+ test_float_with_locale<double >(" uk_UA.UTF-8" , -1e-7 , " -1e-07" );
218+ test_float_with_locale<long double >(" uk_UA.UTF-8" , -1e-7 , " -1e-07" );
219+
220+ test_float_with_locale<float >(" uk_UA.UTF-8" , 1.7976931348623157e+308 , " inf" );
221+ test_float_with_locale<double >(" uk_UA.UTF-8" , 1.7976931348623157e+308 , " 1.7976931348623157e+308" );
222+ test_float_with_locale<long double >(" uk_UA.UTF-8" , 1.7976931348623157e+308 , " 1.7976931348623157e+308" );
223+
224+ test_float_with_locale<float >(" uk_UA.UTF-8" , -1.7976931348623157e+308 , " -inf" );
225+ test_float_with_locale<double >(" uk_UA.UTF-8" , -1.7976931348623157e+308 , " -1.7976931348623157e+308" );
226+ test_float_with_locale<long double >(" uk_UA.UTF-8" , -1.7976931348623157e+308 , " -1.7976931348623157e+308" );
227+ }
228+
229+ #endif
230+
105231int main (int , char **) {
106232 test_signed<int >();
107233 test_signed<long >();
@@ -112,6 +238,9 @@ int main(int, char**) {
112238 test_float<float >();
113239 test_float<double >();
114240 test_float<long double >();
241+ #if TEST_STD_VER >= 26
242+ test_float_with_locale ();
243+ #endif
115244
116245 return 0 ;
117246}
0 commit comments