]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/to_chars/constexpr.cc
libstdc++: Remove dg-options "-std=gnu++23" from remaining tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / to_chars / constexpr.cc
1 // { dg-do compile { target c++23 } }
2
3 #include <charconv>
4
5 #ifndef __cpp_lib_constexpr_charconv
6 # error "Feature-test macro for constexpr charconv missing in <charconv>"
7 #elif __cpp_lib_constexpr_charconv != 202207L
8 # error "Feature-test macro for constexpr charconv has wrong value in <charconv>"
9 #endif
10
11 #include <testsuite_hooks.h>
12
13 constexpr bool
14 test_base10()
15 {
16 std::to_chars_result res;
17 char buf[10] = "XXXXXXXXX";
18 res = std::to_chars(buf, buf+3, 1234);
19 VERIFY( res.ptr == buf+3 );
20 VERIFY( res.ec == std::errc::value_too_large );
21 res = std::to_chars(buf, buf+4, -1234);
22 VERIFY( res.ptr == buf+4 );
23 VERIFY( res.ec == std::errc::value_too_large );
24 res = std::to_chars(buf, buf+4, 1234);
25 VERIFY( res.ptr == buf+4 );
26 VERIFY( res.ec == std::errc{} );
27 VERIFY( buf[0] == '1' );
28 VERIFY( buf[1] == '2' );
29 VERIFY( buf[2] == '3' );
30 VERIFY( buf[3] == '4' );
31 VERIFY( buf[4] == 'X' );
32 res = std::to_chars(buf, buf+10, -567, 10);
33 VERIFY( res.ptr == buf+4 );
34 VERIFY( res.ec == std::errc{} );
35 VERIFY( buf[0] == '-' );
36 VERIFY( buf[1] == '5' );
37 VERIFY( buf[2] == '6' );
38 VERIFY( buf[3] == '7' );
39 VERIFY( buf[4] == 'X' );
40 return true;
41 }
42
43 static_assert( test_base10() );
44
45 constexpr bool
46 test_base16()
47 {
48 std::to_chars_result res;
49 char buf[10] = "XXXXXXXXX";
50 res = std::to_chars(buf, buf+3, 0x1234, 16);
51 VERIFY( res.ptr == buf+3 );
52 VERIFY( res.ec == std::errc::value_too_large );
53 res = std::to_chars(buf, buf+4, -0x1234, 16);
54 VERIFY( res.ptr == buf+4 );
55 VERIFY( res.ec == std::errc::value_too_large );
56 res = std::to_chars(buf, buf+4, 0x1234, 16);
57 VERIFY( res.ptr == buf+4 );
58 VERIFY( res.ec == std::errc{} );
59 VERIFY( buf[0] == '1' );
60 VERIFY( buf[1] == '2' );
61 VERIFY( buf[2] == '3' );
62 VERIFY( buf[3] == '4' );
63 VERIFY( buf[4] == 'X' );
64 res = std::to_chars(buf, buf+10, -0x567, 16);
65 VERIFY( res.ptr == buf+4 );
66 VERIFY( res.ec == std::errc{} );
67 VERIFY( buf[0] == '-' );
68 VERIFY( buf[1] == '5' );
69 VERIFY( buf[2] == '6' );
70 VERIFY( buf[3] == '7' );
71 VERIFY( buf[5] == 'X' );
72 return true;
73 }
74
75 static_assert( test_base16() );
76
77 constexpr bool
78 test_base8()
79 {
80 std::to_chars_result res;
81 char buf[10] = "XXXXXXXXX";
82 res = std::to_chars(buf, buf+2, 01234, 8);
83 VERIFY( res.ptr == buf+2 );
84 VERIFY( res.ec == std::errc::value_too_large );
85 res = std::to_chars(buf, buf+3, -01234, 8);
86 VERIFY( res.ptr == buf+3 );
87 VERIFY( res.ec == std::errc::value_too_large );
88 res = std::to_chars(buf, buf+4, 01234, 8);
89 VERIFY( res.ptr == buf+4 );
90 VERIFY( res.ec == std::errc{} );
91 VERIFY( buf[0] == '1' );
92 VERIFY( buf[1] == '2' );
93 VERIFY( buf[2] == '3' );
94 VERIFY( buf[3] == '4' );
95 VERIFY( buf[4] == 'X' );
96 res = std::to_chars(buf, buf+10, -0567, 8);
97 VERIFY( res.ptr == buf+4 );
98 VERIFY( res.ec == std::errc{} );
99 VERIFY( buf[0] == '-' );
100 VERIFY( buf[1] == '5' );
101 VERIFY( buf[2] == '6' );
102 VERIFY( buf[3] == '7' );
103 VERIFY( buf[4] == 'X' );
104 return true;
105 }
106
107 static_assert( test_base8() );
108
109 constexpr bool
110 test_base2()
111 {
112 std::to_chars_result res;
113 char buf[10] = "XXXXXXXXX";
114 res = std::to_chars(buf, buf+4, 0b10001, 2);
115 VERIFY( res.ptr == buf+4 );
116 VERIFY( res.ec == std::errc::value_too_large );
117 res = std::to_chars(buf, buf+5, -0b10001, 2);
118 VERIFY( res.ptr == buf+5 );
119 VERIFY( res.ec == std::errc::value_too_large );
120 res = std::to_chars(buf, buf+5, 0b10001, 2);
121 VERIFY( res.ptr == buf+5 );
122 VERIFY( res.ec == std::errc{} );
123 VERIFY( buf[0] == '1' );
124 VERIFY( buf[1] == '0' );
125 VERIFY( buf[2] == '0' );
126 VERIFY( buf[3] == '0' );
127 VERIFY( buf[4] == '1' );
128 VERIFY( buf[5] == 'X' );
129 res = std::to_chars(buf, buf+10, -0b11011, 2);
130 VERIFY( res.ptr == buf+6 );
131 VERIFY( res.ec == std::errc{} );
132 VERIFY( buf[0] == '-' );
133 VERIFY( buf[1] == '1' );
134 VERIFY( buf[2] == '1' );
135 VERIFY( buf[3] == '0' );
136 VERIFY( buf[4] == '1' );
137 VERIFY( buf[5] == '1' );
138 VERIFY( buf[6] == 'X' );
139 return true;
140 }
141
142 static_assert( test_base2() );
143
144 constexpr bool
145 test_base36()
146 {
147 std::to_chars_result res;
148 char buf[10] = "XXXXXXXXX";
149 res = std::to_chars(buf, buf+1, 1234, 36);
150 VERIFY( res.ptr == buf+1 );
151 VERIFY( res.ec == std::errc::value_too_large );
152 res = std::to_chars(buf, buf+2, -1234, 36);
153 VERIFY( res.ptr == buf+2 );
154 VERIFY( res.ec == std::errc::value_too_large );
155 res = std::to_chars(buf, buf+3, 1234, 36);
156 VERIFY( res.ptr == buf+2 );
157 VERIFY( res.ec == std::errc{} );
158 VERIFY( buf[0] == 'y' );
159 VERIFY( buf[1] == 'a' );
160 VERIFY( buf[3] == 'X' );
161 res = std::to_chars(buf, buf+10, -567, 36);
162 VERIFY( res.ptr == buf+3 );
163 VERIFY( res.ec == std::errc{} );
164 VERIFY( buf[0] == '-' );
165 VERIFY( buf[1] == 'f' );
166 VERIFY( buf[2] == 'r' );
167 VERIFY( buf[4] == 'X' );
168 return true;
169 }
170
171 static_assert( test_base36() );