Debian Codegraph @

libstdc++-v3/testsuite/20_util/to_chars/constexpr.cc

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