xref: /llvm-project/libcxx/test/std/utilities/charconv/charconv.from.chars/integral.roundtrip.pass.cpp (revision 5e19fd172063c8957a35c7fa3596620f79ebba97)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=12712420
12 
13 // <charconv>
14 
15 // constexpr from_chars_result from_chars(const char* first, const char* last,
16 //                                        Integral& value, int base = 10)
17 
18 #include <charconv>
19 #include <system_error>
20 
21 #include "test_macros.h"
22 #include "charconv_test_helpers.h"
23 
24 template <typename T>
25 struct test_basics : roundtrip_test_base<T>
26 {
27     using roundtrip_test_base<T>::test;
28 
29     TEST_CONSTEXPR_CXX23 void operator()()
30     {
31         test(0);
32         test(42);
33         test(32768);
34         test(0, 10);
35         test(42, 10);
36         test(32768, 10);
37         test(0xf, 16);
38         test(0xdeadbeaf, 16);
39         test(0755, 8);
40 
41         for (int b = 2; b < 37; ++b)
42         {
43             using xl = std::numeric_limits<T>;
44 
45             test(1, b);
46             test(-1, b);
47             test(xl::lowest(), b);
48             test((xl::max)(), b);
49             test((xl::max)() / 2, b);
50         }
51     }
52 };
53 
54 template <typename T>
55 struct test_signed : roundtrip_test_base<T>
56 {
57     using roundtrip_test_base<T>::test;
58 
59     TEST_CONSTEXPR_CXX23 void operator()()
60     {
61         test(-1);
62         test(-12);
63         test(-1, 10);
64         test(-12, 10);
65         test(-21734634, 10);
66         test(-2647, 2);
67         test(-0xcc1, 16);
68 
69         for (int b = 2; b < 37; ++b)
70         {
71             using xl = std::numeric_limits<T>;
72 
73             test(0, b);
74             test(xl::lowest(), b);
75             test((xl::max)(), b);
76         }
77     }
78 };
79 
80 TEST_CONSTEXPR_CXX23 bool test()
81 {
82     run<test_basics>(integrals);
83     run<test_signed>(all_signed);
84 
85     return true;
86 }
87 
88 int main(int, char**) {
89     test();
90 #if TEST_STD_VER > 20
91     static_assert(test());
92 #endif
93 
94     return 0;
95 }
96