18
18
// string to_string(double val);
19
19
// string to_string(long double val);
20
20
21
- #include < string>
22
21
#include < cassert>
22
+ #include < format>
23
+ #include < string>
23
24
#include < limits>
24
25
25
26
#include " parse_integer.h"
@@ -32,29 +33,44 @@ void test_signed() {
32
33
assert (s.size () == 1 );
33
34
assert (s[s.size ()] == 0 );
34
35
assert (s == " 0" );
36
+ #if TEST_STD_VER >= 26
37
+ assert (s == std::format (" {}" , T (0 )));
38
+ #endif
35
39
}
36
40
{
37
41
std::string s = std::to_string (T (12345 ));
38
42
assert (s.size () == 5 );
39
43
assert (s[s.size ()] == 0 );
40
44
assert (s == " 12345" );
45
+ #if TEST_STD_VER >= 26
46
+ assert (s == std::format (" {}" , T (12345 )));
47
+ #endif
41
48
}
42
49
{
43
50
std::string s = std::to_string (T (-12345 ));
44
51
assert (s.size () == 6 );
45
52
assert (s[s.size ()] == 0 );
46
53
assert (s == " -12345" );
54
+ #if TEST_STD_VER >= 26
55
+ assert (s == std::format (" {}" , T (-12345 )));
56
+ #endif
47
57
}
48
58
{
49
59
std::string s = std::to_string (std::numeric_limits<T>::max ());
50
60
assert (s.size () == std::numeric_limits<T>::digits10 + 1 );
51
61
T t = parse_integer<T>(s);
52
62
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
53
66
}
54
67
{
55
68
std::string s = std::to_string (std::numeric_limits<T>::min ());
56
69
T t = parse_integer<T>(s);
57
70
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
58
74
}
59
75
}
60
76
@@ -65,43 +81,153 @@ void test_unsigned() {
65
81
assert (s.size () == 1 );
66
82
assert (s[s.size ()] == 0 );
67
83
assert (s == " 0" );
84
+ #if TEST_STD_VER >= 26
85
+ assert (s == std::format (" {}" , T (0 )));
86
+ #endif
68
87
}
69
88
{
70
89
std::string s = std::to_string (T (12345 ));
71
90
assert (s.size () == 5 );
72
91
assert (s[s.size ()] == 0 );
73
92
assert (s == " 12345" );
93
+ #if TEST_STD_VER >= 26
94
+ assert (s == std::format (" {}" , T (12345 )));
95
+ #endif
74
96
}
75
97
{
76
98
std::string s = std::to_string (std::numeric_limits<T>::max ());
77
99
assert (s.size () == std::numeric_limits<T>::digits10 + 1 );
78
100
T t = parse_integer<T>(s);
79
101
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
80
105
}
81
106
}
82
107
83
108
template <class T >
84
109
void test_float () {
85
110
{
86
111
std::string s = std::to_string (T (0 ));
112
+ #if TEST_STD_VER < 26
87
113
assert (s.size () == 8 );
88
114
assert (s[s.size ()] == 0 );
89
115
assert (s == " 0.000000" );
116
+ #else
117
+ std::string f = std::format (" {}" , T (0 ));
118
+ assert (s == f);
119
+ assert (s == " 0" );
120
+ #endif
90
121
}
91
122
{
92
123
std::string s = std::to_string (T (12345 ));
124
+ #if TEST_STD_VER < 26
93
125
assert (s.size () == 12 );
94
126
assert (s[s.size ()] == 0 );
95
127
assert (s == " 12345.000000" );
128
+ #else
129
+ std::string f = std::format (" {}" , T (12345 ));
130
+ assert (s == f);
131
+ assert (s == " 12345" );
132
+ #endif
96
133
}
97
134
{
98
135
std::string s = std::to_string (T (-12345 ));
136
+ #if TEST_STD_VER < 26
99
137
assert (s.size () == 13 );
100
138
assert (s[s.size ()] == 0 );
101
139
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" );
102
153
}
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);
103
173
}
104
174
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
+
105
231
int main (int , char **) {
106
232
test_signed<int >();
107
233
test_signed<long >();
@@ -112,6 +238,9 @@ int main(int, char**) {
112
238
test_float<float >();
113
239
test_float<double >();
114
240
test_float<long double >();
241
+ #if TEST_STD_VER >= 26
242
+ test_float_with_locale ();
243
+ #endif
115
244
116
245
return 0 ;
117
246
}
0 commit comments