Debian Codegraph @

libcxx/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp

raw
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 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=12712420
13 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=50000000
14
15 // <charconv>
16
17 // constexpr to_chars_result to_chars(char* first, char* last, Integral value,
18 // int base = 10)
19
20 #include <charconv>
21 #include "test_macros.h"
22 #include "charconv_test_helpers.h"
23
24 #ifndef TEST_HAS_NO_INT128
25 TEST_CONSTEXPR_CXX23 __uint128_t make_u128(__uint128_t a, std::uint64_t b) {
26 a *= 1000000000000000000UL;
27 a *= 10;
28 return a + b;
29 }
30
31 TEST_CONSTEXPR_CXX23 __uint128_t make_u128(__uint128_t a, std::uint64_t b, std::uint64_t c) {
32 a *= 10000000000000ULL;
33 a += b;
34 a *= 10000000000000ULL;
35 return a + c;
36 }
37
38 TEST_CONSTEXPR_CXX23 __int128_t make_i128(__int128_t a, std::int64_t b) {
39 if (a < 0)
40 return -make_u128(-a, b);
41 return make_u128(a, b);
42 }
43
44 TEST_CONSTEXPR_CXX23 __int128_t make_i128(__int128_t a, __int128_t b, std::int64_t c) {
45 if (a < 0)
46 return -make_u128(-a, b, c);
47 return make_u128(a, b, c);
48 }
49 #endif
50
51 template <typename T>
52 struct test_basics : to_chars_test_base<T>
53 {
54 using to_chars_test_base<T>::test;
55 using to_chars_test_base<T>::test_value;
56
57 TEST_CONSTEXPR_CXX23 void operator()()
58 {
59 test(0, "0");
60 test(42, "42");
61 test(32768, "32768");
62 test(0, "0", 10);
63 test(42, "42", 10);
64 test(32768, "32768", 10);
65 test(0xf, "f", 16);
66 test(0xdeadbeaf, "deadbeaf", 16);
67 test(0755, "755", 8);
68
69 // Test each len till len of UINT64_MAX = 20 because to_chars algorithm
70 // makes branches based on decimal digits count in the value string
71 // representation.
72 // Test driver automatically skips values not fitting into source type.
73 test(1UL, "1");
74 test(12UL, "12");
75 test(123UL, "123");
76 test(1234UL, "1234");
77 test(12345UL, "12345");
78 test(123456UL, "123456");
79 test(1234567UL, "1234567");
80 test(12345678UL, "12345678");
81 test(123456789UL, "123456789");
82 test(1234567890UL, "1234567890");
83 test(12345678901UL, "12345678901");
84 test(123456789012UL, "123456789012");
85 test(1234567890123UL, "1234567890123");
86 test(12345678901234UL, "12345678901234");
87 test(123456789012345UL, "123456789012345");
88 test(1234567890123456UL, "1234567890123456");
89 test(12345678901234567UL, "12345678901234567");
90 test(123456789012345678UL, "123456789012345678");
91 test(1234567890123456789UL, "1234567890123456789");
92 test(12345678901234567890UL, "12345678901234567890");
93 #ifndef TEST_HAS_NO_INT128
94 test(make_u128(12UL, 3456789012345678901UL), "123456789012345678901");
95 test(make_u128(123UL, 4567890123456789012UL), "1234567890123456789012");
96 test(make_u128(1234UL, 5678901234567890123UL), "12345678901234567890123");
97 test(make_u128(12345UL, 6789012345678901234UL), "123456789012345678901234");
98 test(make_u128(123456UL, 7890123456789012345UL), "1234567890123456789012345");
99 test(make_u128(1234567UL, 8901234567890123456UL), "12345678901234567890123456");
100 test(make_u128(12345678UL, 9012345678901234567UL), "123456789012345678901234567");
101 test(make_u128(123456789UL, 123456789012345678UL), "1234567890123456789012345678");
102 test(make_u128(123UL, 4567890123456UL, 7890123456789UL), "12345678901234567890123456789");
103 test(make_u128(1234UL, 5678901234567UL, 8901234567890UL), "123456789012345678901234567890");
104 test(make_u128(12345UL, 6789012345678UL, 9012345678901UL), "1234567890123456789012345678901");
105 test(make_u128(123456UL, 7890123456789UL, 123456789012UL), "12345678901234567890123456789012");
106 test(make_u128(1234567UL, 8901234567890UL, 1234567890123UL), "123456789012345678901234567890123");
107 test(make_u128(12345678UL, 9012345678901UL, 2345678901234UL), "1234567890123456789012345678901234");
108 test(make_u128(123456789UL, 123456789012UL, 3456789012345UL), "12345678901234567890123456789012345");
109 test(make_u128(1234567890UL, 1234567890123UL, 4567890123456UL), "123456789012345678901234567890123456");
110 test(make_u128(12345678901UL, 2345678901234UL, 5678901234567UL), "1234567890123456789012345678901234567");
111 test(make_u128(123456789012UL, 3456789012345UL, 6789012345678UL), "12345678901234567890123456789012345678");
112 test(make_u128(1234567890123UL, 4567890123456UL, 7890123456789UL), "123456789012345678901234567890123456789");
113 #endif
114
115 // Test special cases with zeros inside a value string representation,
116 // to_chars algorithm processes them in a special way and should not
117 // skip trailing zeros
118 // Test driver automatically skips values not fitting into source type.
119 test(0UL, "0");
120 test(10UL, "10");
121 test(100UL, "100");
122 test(1000UL, "1000");
123 test(10000UL, "10000");
124 test(100000UL, "100000");
125 test(1000000UL, "1000000");
126 test(10000000UL, "10000000");
127 test(100000000UL, "100000000");
128 test(1000000000UL, "1000000000");
129 test(10000000000UL, "10000000000");
130 test(100000000000UL, "100000000000");
131 test(1000000000000UL, "1000000000000");
132 test(10000000000000UL, "10000000000000");
133 test(100000000000000UL, "100000000000000");
134 test(1000000000000000UL, "1000000000000000");
135 test(10000000000000000UL, "10000000000000000");
136 test(100000000000000000UL, "100000000000000000");
137 test(1000000000000000000UL, "1000000000000000000");
138 test(10000000000000000000UL, "10000000000000000000");
139 #ifndef TEST_HAS_NO_INT128
140 test(make_u128(10UL, 0), "100000000000000000000");
141 test(make_u128(100UL, 0), "1000000000000000000000");
142 test(make_u128(1000UL, 0), "10000000000000000000000");
143 test(make_u128(10000UL, 0), "100000000000000000000000");
144 test(make_u128(100000UL, 0), "1000000000000000000000000");
145 test(make_u128(1000000UL, 0), "10000000000000000000000000");
146 test(make_u128(10000000UL, 0), "100000000000000000000000000");
147 test(make_u128(100000000UL, 0), "1000000000000000000000000000");
148 test(make_u128(100UL, 0, 0), "10000000000000000000000000000");
149 test(make_u128(1000UL, 0, 0), "100000000000000000000000000000");
150 test(make_u128(10000UL, 0, 0), "1000000000000000000000000000000");
151 test(make_u128(100000UL, 0, 0), "10000000000000000000000000000000");
152 test(make_u128(1000000UL, 0, 0), "100000000000000000000000000000000");
153 test(make_u128(10000000UL, 0, 0), "1000000000000000000000000000000000");
154 test(make_u128(100000000UL, 0, 0), "10000000000000000000000000000000000");
155 test(make_u128(1000000000UL, 0, 0), "100000000000000000000000000000000000");
156 test(make_u128(10000000000UL, 0, 0), "1000000000000000000000000000000000000");
157 test(make_u128(100000000000UL, 0, 0), "10000000000000000000000000000000000000");
158 test(make_u128(1000000000000UL, 0, 0), "100000000000000000000000000000000000000");
159 #endif
160
161 for (int b = 2; b < 37; ++b)
162 {
163 using xl = std::numeric_limits<T>;
164
165 test_value(1, b);
166 test_value(xl::lowest(), b);
167 test_value((xl::max)(), b);
168 test_value((xl::max)() / 2, b);
169 }
170 }
171 };
172
173 template <typename T>
174 struct test_signed : to_chars_test_base<T>
175 {
176 using to_chars_test_base<T>::test;
177 using to_chars_test_base<T>::test_value;
178
179 TEST_CONSTEXPR_CXX23 void operator()()
180 {
181 test(-1, "-1");
182 test(-12, "-12");
183 test(-1, "-1", 10);
184 test(-12, "-12", 10);
185 test(-21734634, "-21734634", 10);
186 test(-2647, "-101001010111", 2);
187 test(-0xcc1, "-cc1", 16);
188
189 // Test each len till len of INT64_MAX = 19 because to_chars algorithm
190 // makes branches based on decimal digits count in the value string
191 // representation.
192 // Test driver automatically skips values not fitting into source type.
193 test(-1L, "-1");
194 test(-12L, "-12");
195 test(-123L, "-123");
196 test(-1234L, "-1234");
197 test(-12345L, "-12345");
198 test(-123456L, "-123456");
199 test(-1234567L, "-1234567");
200 test(-12345678L, "-12345678");
201 test(-123456789L, "-123456789");
202 test(-1234567890L, "-1234567890");
203 test(-12345678901L, "-12345678901");
204 test(-123456789012L, "-123456789012");
205 test(-1234567890123L, "-1234567890123");
206 test(-12345678901234L, "-12345678901234");
207 test(-123456789012345L, "-123456789012345");
208 test(-1234567890123456L, "-1234567890123456");
209 test(-12345678901234567L, "-12345678901234567");
210 test(-123456789012345678L, "-123456789012345678");
211 test(-1234567890123456789L, "-1234567890123456789");
212 #ifndef TEST_HAS_NO_INT128
213 test(make_i128(-1L, 2345678901234567890L), "-12345678901234567890");
214 test(make_i128(-12L, 3456789012345678901L), "-123456789012345678901");
215 test(make_i128(-123L, 4567890123456789012L), "-1234567890123456789012");
216 test(make_i128(-1234L, 5678901234567890123L), "-12345678901234567890123");
217 test(make_i128(-12345L, 6789012345678901234L), "-123456789012345678901234");
218 test(make_i128(-123456L, 7890123456789012345L), "-1234567890123456789012345");
219 test(make_i128(-1234567L, 8901234567890123456L), "-12345678901234567890123456");
220 test(make_i128(-12345678L, 9012345678901234567L), "-123456789012345678901234567");
221 test(make_i128(-123456789L, 123456789012345678L), "-1234567890123456789012345678");
222 test(make_i128(-1234567890L, 1234567890123456789L), "-12345678901234567890123456789");
223 test(make_i128(-123L, 4567890123456L, 7890123456789L), "-12345678901234567890123456789");
224 test(make_i128(-1234L, 5678901234567L, 8901234567890L), "-123456789012345678901234567890");
225 test(make_i128(-12345L, 6789012345678L, 9012345678901L), "-1234567890123456789012345678901");
226 test(make_i128(-123456L, 7890123456789L, 123456789012L), "-12345678901234567890123456789012");
227 test(make_i128(-1234567L, 8901234567890L, 1234567890123L), "-123456789012345678901234567890123");
228 test(make_i128(-12345678L, 9012345678901L, 2345678901234L), "-1234567890123456789012345678901234");
229 test(make_i128(-123456789L, 123456789012L, 3456789012345L), "-12345678901234567890123456789012345");
230 test(make_i128(-1234567890L, 1234567890123L, 4567890123456L), "-123456789012345678901234567890123456");
231 test(make_i128(-12345678901L, 2345678901234L, 5678901234567L), "-1234567890123456789012345678901234567");
232 test(make_i128(-123456789012L, 3456789012345L, 6789012345678L), "-12345678901234567890123456789012345678");
233 test(make_i128(-1234567890123L, 4567890123456L, 7890123456789L), "-123456789012345678901234567890123456789");
234 #endif
235
236 // Test special cases with zeros inside a value string representation,
237 // to_chars algorithm processes them in a special way and should not
238 // skip trailing zeros
239 // Test driver automatically skips values not fitting into source type.
240 test(-10L, "-10");
241 test(-100L, "-100");
242 test(-1000L, "-1000");
243 test(-10000L, "-10000");
244 test(-100000L, "-100000");
245 test(-1000000L, "-1000000");
246 test(-10000000L, "-10000000");
247 test(-100000000L, "-100000000");
248 test(-1000000000L, "-1000000000");
249 test(-10000000000L, "-10000000000");
250 test(-100000000000L, "-100000000000");
251 test(-1000000000000L, "-1000000000000");
252 test(-10000000000000L, "-10000000000000");
253 test(-100000000000000L, "-100000000000000");
254 test(-1000000000000000L, "-1000000000000000");
255 test(-10000000000000000L, "-10000000000000000");
256 test(-100000000000000000L, "-100000000000000000");
257 test(-1000000000000000000L, "-1000000000000000000");
258 #ifndef TEST_HAS_NO_INT128
259 test(make_i128(-1L, 0L), "-10000000000000000000");
260 test(make_i128(-10L, 0L), "-100000000000000000000");
261 test(make_i128(-100L, 0L), "-1000000000000000000000");
262 test(make_i128(-1000L, 0L), "-10000000000000000000000");
263 test(make_i128(-10000L, 0L), "-100000000000000000000000");
264 test(make_i128(-100000L, 0L), "-1000000000000000000000000");
265 test(make_i128(-1000000L, 0L), "-10000000000000000000000000");
266 test(make_i128(-10000000L, 0L), "-100000000000000000000000000");
267 test(make_i128(-100000000L, 0L), "-1000000000000000000000000000");
268 test(make_i128(-1000000000L, 0L), "-10000000000000000000000000000");
269 test(make_i128(-100L, 0L, 0L), "-10000000000000000000000000000");
270 test(make_i128(-1000L, 0L, 0L), "-100000000000000000000000000000");
271 test(make_i128(-10000L, 0L, 0L), "-1000000000000000000000000000000");
272 test(make_i128(-100000L, 0L, 0L), "-10000000000000000000000000000000");
273 test(make_i128(-1000000L, 0L, 0L), "-100000000000000000000000000000000");
274 test(make_i128(-10000000L, 0L, 0L), "-1000000000000000000000000000000000");
275 test(make_i128(-100000000L, 0L, 0L), "-10000000000000000000000000000000000");
276 test(make_i128(-1000000000L, 0L, 0L), "-100000000000000000000000000000000000");
277 test(make_i128(-10000000000L, 0L, 0L), "-1000000000000000000000000000000000000");
278 test(make_i128(-100000000000L, 0L, 0L), "-10000000000000000000000000000000000000");
279 test(make_i128(-1000000000000L, 0L, 0L), "-100000000000000000000000000000000000000");
280 #endif
281
282 for (int b = 2; b < 37; ++b)
283 {
284 using xl = std::numeric_limits<T>;
285
286 test_value(0, b);
287 test_value(xl::lowest(), b);
288 test_value((xl::max)(), b);
289 }
290 }
291 };
292
293 TEST_CONSTEXPR_CXX23 bool test()
294 {
295 run<test_basics>(integrals);
296 run<test_signed>(all_signed);
297
298 return true;
299 }
300
301 int main(int, char**)
302 {
303 test();
304 #if TEST_STD_VER > 20
305 static_assert(test());
306 #endif
307
308 return 0;
309 }