Fix new testsuite failure on newlib targets
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / to_chars / 1.cc
blob12dfd3d49f33d77bfe71d6dbe2f695492ab7ed2e
1 // Copyright (C) 2017 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-string-conversions "" }
22 #include <charconv>
23 #include <string_view>
25 template<typename I>
26 bool
27 check_to_chars(I val, std::string_view expected, int base = 0)
29 // Space for minus sign, 64 binary digits, final '*', and null terminator:
30 char buf[67] = "******************************************************************";
31 std::to_chars_result r = base == 0
32 ? std::to_chars(buf, buf+sizeof(buf), val)
33 : std::to_chars(buf, buf+sizeof(buf), val, base);
34 return r.ec == std::errc{} && *r.ptr == '*' && std::string_view(buf, r.ptr - buf) == expected;
37 #include <string>
38 #include <climits>
39 #include <testsuite_hooks.h>
41 // Using default base 10
42 void
43 test01()
45 VERIFY( check_to_chars<char>(0, "0") );
46 VERIFY( check_to_chars<signed char>(0, "0") );
47 VERIFY( check_to_chars<unsigned char>(0, "0") );
48 VERIFY( check_to_chars<signed short>(0, "0") );
49 VERIFY( check_to_chars<unsigned short>(0, "0") );
50 VERIFY( check_to_chars<signed int>(0, "0") );
51 VERIFY( check_to_chars<unsigned int>(0, "0") );
52 VERIFY( check_to_chars<signed long>(0, "0") );
53 VERIFY( check_to_chars<unsigned long>(0, "0") );
54 VERIFY( check_to_chars<signed long long>(0, "0") );
55 VERIFY( check_to_chars<unsigned long long>(0, "0") );
57 VERIFY( check_to_chars<char>(1, "1") );
58 VERIFY( check_to_chars<signed char>(1, "1") );
59 VERIFY( check_to_chars<unsigned char>(1, "1") );
60 VERIFY( check_to_chars<signed short>(1, "1") );
61 VERIFY( check_to_chars<unsigned short>(1, "1") );
62 VERIFY( check_to_chars<signed int>(1, "1") );
63 VERIFY( check_to_chars<unsigned int>(1, "1") );
64 VERIFY( check_to_chars<signed long>(1, "1") );
65 VERIFY( check_to_chars<unsigned long>(1, "1") );
66 VERIFY( check_to_chars<signed long long>(1, "1") );
67 VERIFY( check_to_chars<unsigned long long>(1, "1") );
69 VERIFY( check_to_chars<char>(123, "123") );
70 VERIFY( check_to_chars<signed char>(123, "123") );
71 VERIFY( check_to_chars<unsigned char>(123, "123") );
72 VERIFY( check_to_chars<signed short>(123, "123") );
73 VERIFY( check_to_chars<unsigned short>(123, "123") );
74 VERIFY( check_to_chars<signed int>(123, "123") );
75 VERIFY( check_to_chars<unsigned int>(123, "123") );
76 VERIFY( check_to_chars<signed long>(123, "123") );
77 VERIFY( check_to_chars<unsigned long>(123, "123") );
78 VERIFY( check_to_chars<signed long long>(123, "123") );
79 VERIFY( check_to_chars<unsigned long long>(123, "123") );
81 if constexpr (std::is_signed_v<char>)
82 VERIFY( check_to_chars<char>(-79, "-79") );
83 VERIFY( check_to_chars<signed char>(-79, "-79") );
84 VERIFY( check_to_chars<signed short>(-79, "-79") );
85 VERIFY( check_to_chars<signed int>(-79, "-79") );
86 VERIFY( check_to_chars<signed long>(-79, "-79") );
87 VERIFY( check_to_chars<signed long long>(-79, "-79") );
89 using std::to_string;
91 VERIFY( check_to_chars<char>(CHAR_MAX, to_string(CHAR_MAX)) );
92 VERIFY( check_to_chars<signed char>(SCHAR_MAX, to_string(SCHAR_MAX)) );
93 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX, to_string(UCHAR_MAX)) );
94 VERIFY( check_to_chars<signed short>(SHRT_MAX, to_string(SHRT_MAX)) );
95 VERIFY( check_to_chars<unsigned short>(USHRT_MAX, to_string(USHRT_MAX)) );
96 VERIFY( check_to_chars<signed int>(INT_MAX, to_string(INT_MAX)) );
97 VERIFY( check_to_chars<unsigned int>(UINT_MAX, to_string(UINT_MAX)) );
98 VERIFY( check_to_chars<signed long>(LONG_MAX, to_string(LONG_MAX)) );
99 VERIFY( check_to_chars<unsigned long>(ULONG_MAX, to_string(ULONG_MAX)) );
100 VERIFY( check_to_chars<signed long long>(LLONG_MAX, to_string(LLONG_MAX)) );
101 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX, to_string(ULLONG_MAX)) );
103 VERIFY( check_to_chars<char>(CHAR_MIN, to_string(CHAR_MIN)) );
104 VERIFY( check_to_chars<signed char>(SCHAR_MIN, to_string(SCHAR_MIN)) );
105 VERIFY( check_to_chars<signed short>(SHRT_MIN, to_string(SHRT_MIN)) );
106 VERIFY( check_to_chars<signed int>(INT_MIN, to_string(INT_MIN)) );
107 VERIFY( check_to_chars<signed long>(LONG_MIN, to_string(LONG_MIN)) );
108 VERIFY( check_to_chars<signed long long>(LLONG_MIN, to_string(LLONG_MIN)) );
110 VERIFY( check_to_chars<char>(CHAR_MAX/2, to_string(CHAR_MAX/2)) );
111 VERIFY( check_to_chars<signed char>(SCHAR_MAX/2, to_string(SCHAR_MAX/2)) );
112 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX/2, to_string(UCHAR_MAX/2)) );
113 VERIFY( check_to_chars<signed short>(SHRT_MAX/2, to_string(SHRT_MAX/2)) );
114 VERIFY( check_to_chars<unsigned short>(USHRT_MAX/2, to_string(USHRT_MAX/2)) );
115 VERIFY( check_to_chars<signed int>(INT_MAX/2, to_string(INT_MAX/2)) );
116 VERIFY( check_to_chars<unsigned int>(UINT_MAX/2, to_string(UINT_MAX/2)) );
117 VERIFY( check_to_chars<signed long>(LONG_MAX/2, to_string(LONG_MAX/2)) );
118 VERIFY( check_to_chars<unsigned long>(ULONG_MAX/2, to_string(ULONG_MAX/2)) );
119 VERIFY( check_to_chars<signed long long>(LLONG_MAX/2, to_string(LLONG_MAX/2)) );
120 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX/2, to_string(ULLONG_MAX/2)) );
123 // Using explicit base 10
124 void
125 test02()
127 VERIFY( check_to_chars<char>(0, "0", 10) );
128 VERIFY( check_to_chars<signed char>(0, "0", 10) );
129 VERIFY( check_to_chars<unsigned char>(0, "0", 10) );
130 VERIFY( check_to_chars<signed short>(0, "0", 10) );
131 VERIFY( check_to_chars<unsigned short>(0, "0", 10) );
132 VERIFY( check_to_chars<signed int>(0, "0", 10) );
133 VERIFY( check_to_chars<unsigned int>(0, "0", 10) );
134 VERIFY( check_to_chars<signed long>(0, "0", 10) );
135 VERIFY( check_to_chars<unsigned long>(0, "0", 10) );
136 VERIFY( check_to_chars<signed long long>(0, "0", 10) );
137 VERIFY( check_to_chars<unsigned long long>(0, "0", 10) );
139 VERIFY( check_to_chars<char>(1, "1", 10) );
140 VERIFY( check_to_chars<signed char>(1, "1", 10) );
141 VERIFY( check_to_chars<unsigned char>(1, "1", 10) );
142 VERIFY( check_to_chars<signed short>(1, "1", 10) );
143 VERIFY( check_to_chars<unsigned short>(1, "1", 10) );
144 VERIFY( check_to_chars<signed int>(1, "1", 10) );
145 VERIFY( check_to_chars<unsigned int>(1, "1", 10) );
146 VERIFY( check_to_chars<signed long>(1, "1", 10) );
147 VERIFY( check_to_chars<unsigned long>(1, "1", 10) );
148 VERIFY( check_to_chars<signed long long>(1, "1", 10) );
149 VERIFY( check_to_chars<unsigned long long>(1, "1", 10) );
151 VERIFY( check_to_chars<char>(123, "123", 10) );
152 VERIFY( check_to_chars<signed char>(123, "123", 10) );
153 VERIFY( check_to_chars<unsigned char>(123, "123", 10) );
154 VERIFY( check_to_chars<signed short>(123, "123", 10) );
155 VERIFY( check_to_chars<unsigned short>(123, "123", 10) );
156 VERIFY( check_to_chars<signed int>(123, "123", 10) );
157 VERIFY( check_to_chars<unsigned int>(123, "123", 10) );
158 VERIFY( check_to_chars<signed long>(123, "123", 10) );
159 VERIFY( check_to_chars<unsigned long>(123, "123", 10) );
160 VERIFY( check_to_chars<signed long long>(123, "123", 10) );
161 VERIFY( check_to_chars<unsigned long long>(123, "123", 10) );
163 if constexpr (std::is_signed_v<char>)
164 VERIFY( check_to_chars<char>(-79, "-79", 10) );
165 VERIFY( check_to_chars<signed char>(-79, "-79", 10) );
166 VERIFY( check_to_chars<signed short>(-79, "-79", 10) );
167 VERIFY( check_to_chars<signed int>(-79, "-79", 10) );
168 VERIFY( check_to_chars<signed long>(-79, "-79", 10) );
169 VERIFY( check_to_chars<signed long long>(-79, "-79", 10) );
171 using std::to_string;
173 VERIFY( check_to_chars<char>(CHAR_MAX, to_string(CHAR_MAX), 10) );
174 VERIFY( check_to_chars<signed char>(SCHAR_MAX, to_string(SCHAR_MAX), 10) );
175 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX, to_string(UCHAR_MAX), 10) );
176 VERIFY( check_to_chars<signed short>(SHRT_MAX, to_string(SHRT_MAX), 10) );
177 VERIFY( check_to_chars<unsigned short>(USHRT_MAX, to_string(USHRT_MAX), 10) );
178 VERIFY( check_to_chars<signed int>(INT_MAX, to_string(INT_MAX), 10) );
179 VERIFY( check_to_chars<unsigned int>(UINT_MAX, to_string(UINT_MAX), 10) );
180 VERIFY( check_to_chars<signed long>(LONG_MAX, to_string(LONG_MAX), 10) );
181 VERIFY( check_to_chars<unsigned long>(ULONG_MAX, to_string(ULONG_MAX), 10) );
182 VERIFY( check_to_chars<signed long long>(LLONG_MAX, to_string(LLONG_MAX), 10) );
183 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX, to_string(ULLONG_MAX), 10) );
185 VERIFY( check_to_chars<char>(CHAR_MIN, to_string(CHAR_MIN), 10) );
186 VERIFY( check_to_chars<signed char>(SCHAR_MIN, to_string(SCHAR_MIN), 10) );
187 VERIFY( check_to_chars<signed short>(SHRT_MIN, to_string(SHRT_MIN), 10) );
188 VERIFY( check_to_chars<signed int>(INT_MIN, to_string(INT_MIN), 10) );
189 VERIFY( check_to_chars<signed long>(LONG_MIN, to_string(LONG_MIN), 10) );
190 VERIFY( check_to_chars<signed long long>(LLONG_MIN, to_string(LLONG_MIN), 10) );
192 VERIFY( check_to_chars<char>(CHAR_MAX/2, to_string(CHAR_MAX/2), 10) );
193 VERIFY( check_to_chars<signed char>(SCHAR_MAX/2, to_string(SCHAR_MAX/2), 10) );
194 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX/2, to_string(UCHAR_MAX/2), 10) );
195 VERIFY( check_to_chars<signed short>(SHRT_MAX/2, to_string(SHRT_MAX/2), 10) );
196 VERIFY( check_to_chars<unsigned short>(USHRT_MAX/2, to_string(USHRT_MAX/2), 10) );
197 VERIFY( check_to_chars<signed int>(INT_MAX/2, to_string(INT_MAX/2), 10) );
198 VERIFY( check_to_chars<unsigned int>(UINT_MAX/2, to_string(UINT_MAX/2), 10) );
199 VERIFY( check_to_chars<signed long>(LONG_MAX/2, to_string(LONG_MAX/2), 10) );
200 VERIFY( check_to_chars<unsigned long>(ULONG_MAX/2, to_string(ULONG_MAX/2), 10) );
201 VERIFY( check_to_chars<signed long long>(LLONG_MAX/2, to_string(LLONG_MAX/2), 10) );
202 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX/2, to_string(ULLONG_MAX/2), 10) );
205 // Using all bases
206 void
207 test03()
209 // -2017 in all bases from [2,36]
210 const char* str2017[37] = { nullptr, nullptr,
211 "-11111100001",
212 "-2202201",
213 "-133201",
214 "-31032",
215 "-13201",
216 "-5611",
217 "-3741",
218 "-2681",
219 "-2017",
220 "-1574",
221 "-1201",
222 "-bc2",
223 "-a41",
224 "-8e7",
225 "-7e1",
226 "-6gb",
227 "-641",
228 "-5b3",
229 "-50h",
230 "-4c1",
231 "-43f",
232 "-3ig",
233 "-3c1",
234 "-35h",
235 "-2pf",
236 "-2kj",
237 "-2g1",
238 "-2bg",
239 "-277",
240 "-232",
241 "-1v1",
242 "-1s4",
243 "-1pb",
244 "-1mm",
245 "-1k1"
247 // -12345 in all bases from [2,36]
248 const char* str12345[37] = { nullptr, nullptr,
249 "-11000000111001",
250 "-121221020",
251 "-3000321",
252 "-343340",
253 "-133053",
254 "-50664",
255 "-30071",
256 "-17836",
257 "-12345",
258 "-9303",
259 "-7189",
260 "-5808",
261 "-46db",
262 "-39d0",
263 "-3039",
264 "-28c3",
265 "-221f",
266 "-1f3e",
267 "-1ah5",
268 "-16ki",
269 "-13b3",
270 "-107h",
271 "-la9",
272 "-jik",
273 "-i6l",
274 "-gp6",
275 "-fkp",
276 "-ejk",
277 "-dlf",
278 "-cq7",
279 "-c1p",
280 "-bb3",
281 "-an3",
282 "-a2p",
283 "-9ix"
285 // -23456 in all bases from [2,36]
286 const char* str23456[37] = { nullptr, nullptr,
287 "-101101110100000",
288 "-1012011202",
289 "-11232200",
290 "-1222311",
291 "-300332",
292 "-125246",
293 "-55640",
294 "-35152",
295 "-23456",
296 "-16694",
297 "-116a8",
298 "-a8a4",
299 "-8796",
300 "-6e3b",
301 "-5ba0",
302 "-4d2d",
303 "-4072",
304 "-37ia",
305 "-2icg",
306 "-2b3k",
307 "-24a4",
308 "-1l7j",
309 "-1gh8",
310 "-1cd6",
311 "-18i4",
312 "-154k",
313 "-11pk",
314 "-rpo",
315 "-q1q",
316 "-ock",
317 "-mt0",
318 "-lhq",
319 "-k9u",
320 "-j56",
321 "-i3k"
323 // INT_MIN in all bases from [2,36]
324 const char* strINT_MIN[37] = { nullptr, nullptr,
325 "-10000000000000000000000000000000",
326 "-12112122212110202102",
327 "-2000000000000000",
328 "-13344223434043",
329 "-553032005532",
330 "-104134211162",
331 "-20000000000",
332 "-5478773672",
333 "-2147483648",
334 "-a02220282",
335 "-4bb2308a8",
336 "-282ba4aab",
337 "-1652ca932",
338 "-c87e66b8",
339 "-80000000",
340 "-53g7f549",
341 "-3928g3h2",
342 "-27c57h33",
343 "-1db1f928",
344 "-140h2d92",
345 "-ikf5bf2",
346 "-ebelf96",
347 "-b5gge58",
348 "-8jmdnkn",
349 "-6oj8ioo",
350 "-5ehnckb",
351 "-4clm98g",
352 "-3hk7988",
353 "-2sb6cs8",
354 "-2d09uc2",
355 "-2000000",
356 "-1lsqtl2",
357 "-1d8xqrq",
358 "-15v22un",
359 "-zik0zk"
362 for (int base = 2; base <= 36; ++base)
364 VERIFY( check_to_chars<char>(0, "0", base) );
365 VERIFY( check_to_chars<signed char>(0, "0", base) );
366 VERIFY( check_to_chars<unsigned char>(0, "0", base) );
367 VERIFY( check_to_chars<signed short>(0, "0", base) );
368 VERIFY( check_to_chars<unsigned short>(0, "0", base) );
369 VERIFY( check_to_chars<signed int>(0, "0", base) );
370 VERIFY( check_to_chars<unsigned int>(0, "0", base) );
371 VERIFY( check_to_chars<signed long>(0, "0", base) );
372 VERIFY( check_to_chars<unsigned long>(0, "0", base) );
373 VERIFY( check_to_chars<signed long long>(0, "0", base) );
374 VERIFY( check_to_chars<unsigned long long>(0, "0", base) );
376 VERIFY( check_to_chars<char>(1, "1", base) );
377 VERIFY( check_to_chars<signed char>(1, "1", base) );
378 VERIFY( check_to_chars<unsigned char>(1, "1", base) );
379 VERIFY( check_to_chars<signed short>(1, "1", base) );
380 VERIFY( check_to_chars<unsigned short>(1, "1", base) );
381 VERIFY( check_to_chars<signed int>(1, "1", base) );
382 VERIFY( check_to_chars<unsigned int>(1, "1", base) );
383 VERIFY( check_to_chars<signed long>(1, "1", base) );
384 VERIFY( check_to_chars<unsigned long>(1, "1", base) );
385 VERIFY( check_to_chars<signed long long>(1, "1", base) );
386 VERIFY( check_to_chars<unsigned long long>(1, "1", base) );
388 if constexpr (std::is_signed_v<char>)
389 VERIFY( check_to_chars<char>(-1, "-1", base) );
390 VERIFY( check_to_chars<signed char>(-1, "-1", base) );
391 VERIFY( check_to_chars<signed short>(-1, "-1", base) );
392 VERIFY( check_to_chars<signed int>(-1, "-1", base) );
393 VERIFY( check_to_chars<signed long>(-1, "-1", base) );
394 VERIFY( check_to_chars<signed long long>(-1, "-1", base) );
396 if (base > 2)
398 VERIFY( check_to_chars<char>(2, "2", base) );
399 VERIFY( check_to_chars<signed char>(2, "2", base) );
400 VERIFY( check_to_chars<unsigned char>(2, "2", base) );
401 VERIFY( check_to_chars<signed short>(2, "2", base) );
402 VERIFY( check_to_chars<unsigned short>(2, "2", base) );
403 VERIFY( check_to_chars<signed int>(2, "2", base) );
404 VERIFY( check_to_chars<unsigned int>(2, "2", base) );
405 VERIFY( check_to_chars<signed long>(2, "2", base) );
406 VERIFY( check_to_chars<unsigned long>(2, "2", base) );
407 VERIFY( check_to_chars<signed long long>(2, "2", base) );
408 VERIFY( check_to_chars<unsigned long long>(2, "2", base) );
410 if constexpr (std::is_signed_v<char>)
411 VERIFY( check_to_chars<char>(-2, "-2", base) );
412 VERIFY( check_to_chars<signed char>(-2, "-2", base) );
413 VERIFY( check_to_chars<signed short>(-2, "-2", base) );
414 VERIFY( check_to_chars<signed int>(-2, "-2", base) );
415 VERIFY( check_to_chars<signed long>(-2, "-2", base) );
416 VERIFY( check_to_chars<signed long long>(-2, "-2", base) );
419 VERIFY( check_to_chars(2017u, str2017[base]+1, base) );
420 VERIFY( check_to_chars(2017, str2017[base]+1, base) );
421 VERIFY( check_to_chars(-2017, str2017[base], base) );
422 VERIFY( check_to_chars(12345u, str12345[base]+1, base) );
423 VERIFY( check_to_chars(12345, str12345[base]+1, base) );
424 VERIFY( check_to_chars(-12345, str12345[base], base) );
425 VERIFY( check_to_chars(23456u, str23456[base]+1, base) );
426 VERIFY( check_to_chars(23456, str23456[base]+1, base) );
427 VERIFY( check_to_chars(-23456, str23456[base], base) );
428 VERIFY( check_to_chars(INT_MAX + 1ull, strINT_MIN[base]+1, base) );
429 VERIFY( check_to_chars(INT_MAX + 1ll, strINT_MIN[base]+1, base) );
430 VERIFY( check_to_chars(INT_MIN, strINT_MIN[base], base) );
433 VERIFY( check_to_chars(1155, "xx", 34) );
434 VERIFY( check_to_chars(1224, "yy", 35) );
435 VERIFY( check_to_chars(1295, "zz", 36) );
438 #include <sstream>
439 #include <ios>
441 // base 8
442 void
443 test04()
445 auto to_string = [](auto val) {
446 std::ostringstream ss;
447 ss << std::oct;
448 if (val < 0)
449 ss << '-' << (~val + 1ull);
450 else if (sizeof(val) == 1)
451 ss << (int)val;
452 else
453 ss << val;
454 return ss.str();
457 VERIFY( check_to_chars<char>(123, to_string(123), 8) );
458 VERIFY( check_to_chars<signed char>(123, to_string(123), 8) );
459 VERIFY( check_to_chars<unsigned char>(123, to_string(123), 8) );
460 VERIFY( check_to_chars<signed short>(123, to_string(123), 8) );
461 VERIFY( check_to_chars<unsigned short>(123, to_string(123), 8) );
462 VERIFY( check_to_chars<signed int>(123, to_string(123), 8) );
463 VERIFY( check_to_chars<unsigned int>(123, to_string(123), 8) );
464 VERIFY( check_to_chars<signed long>(123, to_string(123), 8) );
465 VERIFY( check_to_chars<unsigned long>(123, to_string(123), 8) );
466 VERIFY( check_to_chars<signed long long>(123, to_string(123), 8) );
467 VERIFY( check_to_chars<unsigned long long>(123, to_string(123), 8) );
469 if constexpr (std::is_signed_v<char>)
470 VERIFY( check_to_chars<char>(-79, to_string(-79), 8) );
471 VERIFY( check_to_chars<signed char>(-79, to_string(-79), 8) );
472 VERIFY( check_to_chars<signed short>(-79, to_string(-79), 8) );
473 VERIFY( check_to_chars<signed int>(-79, to_string(-79), 8) );
474 VERIFY( check_to_chars<signed long>(-79, to_string(-79), 8) );
475 VERIFY( check_to_chars<signed long long>(-79, to_string(-79), 8) );
477 VERIFY( check_to_chars<char>(CHAR_MAX, to_string(CHAR_MAX), 8) );
478 VERIFY( check_to_chars<signed char>(SCHAR_MAX, to_string(SCHAR_MAX), 8) );
479 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX, to_string(UCHAR_MAX), 8) );
480 VERIFY( check_to_chars<signed short>(SHRT_MAX, to_string(SHRT_MAX), 8) );
481 VERIFY( check_to_chars<unsigned short>(USHRT_MAX, to_string(USHRT_MAX), 8) );
482 VERIFY( check_to_chars<signed int>(INT_MAX, to_string(INT_MAX), 8) );
483 VERIFY( check_to_chars<unsigned int>(UINT_MAX, to_string(UINT_MAX), 8) );
484 VERIFY( check_to_chars<signed long>(LONG_MAX, to_string(LONG_MAX), 8) );
485 VERIFY( check_to_chars<unsigned long>(ULONG_MAX, to_string(ULONG_MAX), 8) );
486 VERIFY( check_to_chars<signed long long>(LLONG_MAX, to_string(LLONG_MAX), 8) );
487 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX, to_string(ULLONG_MAX), 8) );
489 VERIFY( check_to_chars<char>(CHAR_MIN, to_string(CHAR_MIN), 8) );
490 VERIFY( check_to_chars<signed char>(SCHAR_MIN, to_string(SCHAR_MIN), 8) );
491 VERIFY( check_to_chars<signed short>(SHRT_MIN, to_string(SHRT_MIN), 8) );
492 VERIFY( check_to_chars<signed int>(INT_MIN, to_string(INT_MIN), 8) );
493 VERIFY( check_to_chars<signed long>(LONG_MIN, to_string(LONG_MIN), 8) );
494 VERIFY( check_to_chars<signed long long>(LLONG_MIN, to_string(LLONG_MIN), 8) );
496 VERIFY( check_to_chars<char>(CHAR_MAX/2, to_string(CHAR_MAX/2), 8) );
497 VERIFY( check_to_chars<signed char>(SCHAR_MAX/2, to_string(SCHAR_MAX/2), 8) );
498 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX/2, to_string(UCHAR_MAX/2), 8) );
499 VERIFY( check_to_chars<signed short>(SHRT_MAX/2, to_string(SHRT_MAX/2), 8) );
500 VERIFY( check_to_chars<unsigned short>(USHRT_MAX/2, to_string(USHRT_MAX/2), 8) );
501 VERIFY( check_to_chars<signed int>(INT_MAX/2, to_string(INT_MAX/2), 8) );
502 VERIFY( check_to_chars<unsigned int>(UINT_MAX/2, to_string(UINT_MAX/2), 8) );
503 VERIFY( check_to_chars<signed long>(LONG_MAX/2, to_string(LONG_MAX/2), 8) );
504 VERIFY( check_to_chars<unsigned long>(ULONG_MAX/2, to_string(ULONG_MAX/2), 8) );
505 VERIFY( check_to_chars<signed long long>(LLONG_MAX/2, to_string(LLONG_MAX/2), 8) );
506 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX/2, to_string(ULLONG_MAX/2), 8) );
509 // base 16
510 void
511 test05()
513 auto to_string = [](auto val) {
514 std::ostringstream ss;
515 ss << std::hex;
516 if (val < 0)
517 ss << '-' << (~val + 1ull);
518 else if (sizeof(val) == 1)
519 ss << (int)val;
520 else
521 ss << val;
522 return ss.str();
525 VERIFY( check_to_chars<char>(123, to_string(123), 16) );
526 VERIFY( check_to_chars<signed char>(123, to_string(123), 16) );
527 VERIFY( check_to_chars<unsigned char>(123, to_string(123), 16) );
528 VERIFY( check_to_chars<signed short>(123, to_string(123), 16) );
529 VERIFY( check_to_chars<unsigned short>(123, to_string(123), 16) );
530 VERIFY( check_to_chars<signed int>(123, to_string(123), 16) );
531 VERIFY( check_to_chars<unsigned int>(123, to_string(123), 16) );
532 VERIFY( check_to_chars<signed long>(123, to_string(123), 16) );
533 VERIFY( check_to_chars<unsigned long>(123, to_string(123), 16) );
534 VERIFY( check_to_chars<signed long long>(123, to_string(123), 16) );
535 VERIFY( check_to_chars<unsigned long long>(123, to_string(123), 16) );
537 if constexpr (std::is_signed_v<char>)
538 VERIFY( check_to_chars<char>(-79, to_string(-79), 16) );
539 VERIFY( check_to_chars<signed char>(-79, to_string(-79), 16) );
540 VERIFY( check_to_chars<signed short>(-79, to_string(-79), 16) );
541 VERIFY( check_to_chars<signed int>(-79, to_string(-79), 16) );
542 VERIFY( check_to_chars<signed long>(-79, to_string(-79), 16) );
543 VERIFY( check_to_chars<signed long long>(-79, to_string(-79), 16) );
545 VERIFY( check_to_chars<char>(CHAR_MAX, to_string(CHAR_MAX), 16) );
546 VERIFY( check_to_chars<signed char>(SCHAR_MAX, to_string(SCHAR_MAX), 16) );
547 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX, to_string(UCHAR_MAX), 16) );
548 VERIFY( check_to_chars<signed short>(SHRT_MAX, to_string(SHRT_MAX), 16) );
549 VERIFY( check_to_chars<unsigned short>(USHRT_MAX, to_string(USHRT_MAX), 16) );
550 VERIFY( check_to_chars<signed int>(INT_MAX, to_string(INT_MAX), 16) );
551 VERIFY( check_to_chars<unsigned int>(UINT_MAX, to_string(UINT_MAX), 16) );
552 VERIFY( check_to_chars<signed long>(LONG_MAX, to_string(LONG_MAX), 16) );
553 VERIFY( check_to_chars<unsigned long>(ULONG_MAX, to_string(ULONG_MAX), 16) );
554 VERIFY( check_to_chars<signed long long>(LLONG_MAX, to_string(LLONG_MAX), 16) );
555 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX, to_string(ULLONG_MAX), 16) );
557 VERIFY( check_to_chars<char>(CHAR_MIN, to_string(CHAR_MIN), 16) );
558 VERIFY( check_to_chars<signed char>(SCHAR_MIN, to_string(SCHAR_MIN), 16) );
559 VERIFY( check_to_chars<signed short>(SHRT_MIN, to_string(SHRT_MIN), 16) );
560 VERIFY( check_to_chars<signed int>(INT_MIN, to_string(INT_MIN), 16) );
561 VERIFY( check_to_chars<signed long>(LONG_MIN, to_string(LONG_MIN), 16) );
562 VERIFY( check_to_chars<signed long long>(LLONG_MIN, to_string(LLONG_MIN), 16) );
564 VERIFY( check_to_chars<char>(CHAR_MAX/2, to_string(CHAR_MAX/2), 16) );
565 VERIFY( check_to_chars<signed char>(SCHAR_MAX/2, to_string(SCHAR_MAX/2), 16) );
566 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX/2, to_string(UCHAR_MAX/2), 16) );
567 VERIFY( check_to_chars<signed short>(SHRT_MAX/2, to_string(SHRT_MAX/2), 16) );
568 VERIFY( check_to_chars<unsigned short>(USHRT_MAX/2, to_string(USHRT_MAX/2), 16) );
569 VERIFY( check_to_chars<signed int>(INT_MAX/2, to_string(INT_MAX/2), 16) );
570 VERIFY( check_to_chars<unsigned int>(UINT_MAX/2, to_string(UINT_MAX/2), 16) );
571 VERIFY( check_to_chars<signed long>(LONG_MAX/2, to_string(LONG_MAX/2), 16) );
572 VERIFY( check_to_chars<unsigned long>(ULONG_MAX/2, to_string(ULONG_MAX/2), 16) );
573 VERIFY( check_to_chars<signed long long>(LLONG_MAX/2, to_string(LLONG_MAX/2), 16) );
574 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX/2, to_string(ULLONG_MAX/2), 16) );
577 #include <bitset>
579 // base 2
580 void
581 test06()
583 auto to_string = [](auto val) {
584 std::string s, sign;
585 if (val < 0)
587 auto absval = ~val + 1ull;
588 s = std::bitset<sizeof(absval) * CHAR_BIT>(absval).to_string();
589 sign = '-';
591 else
592 s = std::bitset<sizeof(val) * CHAR_BIT>(val).to_string();
593 auto pos = s.find_first_not_of("0");
594 if (pos == std::string::npos)
595 s.resize(1);
596 else
597 s.erase(0, pos);
598 return sign + s;
601 VERIFY( check_to_chars<char>(123, to_string(123), 2) );
602 VERIFY( check_to_chars<signed char>(123, to_string(123), 2) );
603 VERIFY( check_to_chars<unsigned char>(123, to_string(123), 2) );
604 VERIFY( check_to_chars<signed short>(123, to_string(123), 2) );
605 VERIFY( check_to_chars<unsigned short>(123, to_string(123), 2) );
606 VERIFY( check_to_chars<signed int>(123, to_string(123), 2) );
607 VERIFY( check_to_chars<unsigned int>(123, to_string(123), 2) );
608 VERIFY( check_to_chars<signed long>(123, to_string(123), 2) );
609 VERIFY( check_to_chars<unsigned long>(123, to_string(123), 2) );
610 VERIFY( check_to_chars<signed long long>(123, to_string(123), 2) );
611 VERIFY( check_to_chars<unsigned long long>(123, to_string(123), 2) );
613 if constexpr (std::is_signed_v<char>)
614 VERIFY( check_to_chars<char>(-79, to_string(-79), 2) );
615 VERIFY( check_to_chars<signed char>(-79, to_string(-79), 2) );
616 VERIFY( check_to_chars<signed short>(-79, to_string(-79), 2) );
617 VERIFY( check_to_chars<signed int>(-79, to_string(-79), 2) );
618 VERIFY( check_to_chars<signed long>(-79, to_string(-79), 2) );
619 VERIFY( check_to_chars<signed long long>(-79, to_string(-79), 2) );
621 VERIFY( check_to_chars<char>(CHAR_MAX, to_string(CHAR_MAX), 2) );
622 VERIFY( check_to_chars<signed char>(SCHAR_MAX, to_string(SCHAR_MAX), 2) );
623 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX, to_string(UCHAR_MAX), 2) );
624 VERIFY( check_to_chars<signed short>(SHRT_MAX, to_string(SHRT_MAX), 2) );
625 VERIFY( check_to_chars<unsigned short>(USHRT_MAX, to_string(USHRT_MAX), 2) );
626 VERIFY( check_to_chars<signed int>(INT_MAX, to_string(INT_MAX), 2) );
627 VERIFY( check_to_chars<unsigned int>(UINT_MAX, to_string(UINT_MAX), 2) );
628 VERIFY( check_to_chars<signed long>(LONG_MAX, to_string(LONG_MAX), 2) );
629 VERIFY( check_to_chars<unsigned long>(ULONG_MAX, to_string(ULONG_MAX), 2) );
630 VERIFY( check_to_chars<signed long long>(LLONG_MAX, to_string(LLONG_MAX), 2) );
631 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX, to_string(ULLONG_MAX), 2) );
633 VERIFY( check_to_chars<char>(CHAR_MIN, to_string(CHAR_MIN), 2) );
634 VERIFY( check_to_chars<signed char>(SCHAR_MIN, to_string(SCHAR_MIN), 2) );
635 VERIFY( check_to_chars<signed short>(SHRT_MIN, to_string(SHRT_MIN), 2) );
636 VERIFY( check_to_chars<signed int>(INT_MIN, to_string(INT_MIN), 2) );
637 VERIFY( check_to_chars<signed long>(LONG_MIN, to_string(LONG_MIN), 2) );
638 VERIFY( check_to_chars<signed long long>(LLONG_MIN, to_string(LLONG_MIN), 2) );
640 VERIFY( check_to_chars<char>(CHAR_MAX/2, to_string(CHAR_MAX/2), 2) );
641 VERIFY( check_to_chars<signed char>(SCHAR_MAX/2, to_string(SCHAR_MAX/2), 2) );
642 VERIFY( check_to_chars<unsigned char>(UCHAR_MAX/2, to_string(UCHAR_MAX/2), 2) );
643 VERIFY( check_to_chars<signed short>(SHRT_MAX/2, to_string(SHRT_MAX/2), 2) );
644 VERIFY( check_to_chars<unsigned short>(USHRT_MAX/2, to_string(USHRT_MAX/2), 2) );
645 VERIFY( check_to_chars<signed int>(INT_MAX/2, to_string(INT_MAX/2), 2) );
646 VERIFY( check_to_chars<unsigned int>(UINT_MAX/2, to_string(UINT_MAX/2), 2) );
647 VERIFY( check_to_chars<signed long>(LONG_MAX/2, to_string(LONG_MAX/2), 2) );
648 VERIFY( check_to_chars<unsigned long>(ULONG_MAX/2, to_string(ULONG_MAX/2), 2) );
649 VERIFY( check_to_chars<signed long long>(LLONG_MAX/2, to_string(LLONG_MAX/2), 2) );
650 VERIFY( check_to_chars<unsigned long long>(ULLONG_MAX/2, to_string(ULLONG_MAX/2), 2) );
654 main()
656 test01();
657 test02();
658 test03();
659 test04();
660 test05();
661 test06();