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