xref: /llvm-project/libcxx/test/std/utilities/charconv/charconv.to.chars/integral.pass.cpp (revision c360553c15a8e5aa94d2236eb73e7dfeab9543e5)
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
10 // UNSUPPORTED: !libc++ && c++11
11 // UNSUPPORTED: !libc++ && c++14
12 
13 // to_chars requires functions in the dylib that were introduced in Mac OS 10.15.
14 //
15 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14}}
16 
17 // <charconv>
18 
19 // to_chars_result to_chars(char* first, char* last, Integral value,
20 //                          int base = 10)
21 
22 #include <charconv>
23 #include "test_macros.h"
24 #include "charconv_test_helpers.h"
25 
26 template <typename T>
27 struct test_basics : to_chars_test_base<T>
28 {
29     using to_chars_test_base<T>::test;
30     using to_chars_test_base<T>::test_value;
31 
32     void operator()()
33     {
34         test(0, "0");
35         test(42, "42");
36         test(32768, "32768");
37         test(0, "0", 10);
38         test(42, "42", 10);
39         test(32768, "32768", 10);
40         test(0xf, "f", 16);
41         test(0xdeadbeaf, "deadbeaf", 16);
42         test(0755, "755", 8);
43 
44         // Test each len till len of UINT64_MAX = 20 because to_chars algorithm
45         // makes branches based on decimal digits count in the value string
46         // representation.
47         // Test driver automatically skips values not fitting into source type.
48         test(1UL, "1");
49         test(12UL, "12");
50         test(123UL, "123");
51         test(1234UL, "1234");
52         test(12345UL, "12345");
53         test(123456UL, "123456");
54         test(1234567UL, "1234567");
55         test(12345678UL, "12345678");
56         test(123456789UL, "123456789");
57         test(1234567890UL, "1234567890");
58         test(12345678901UL, "12345678901");
59         test(123456789012UL, "123456789012");
60         test(1234567890123UL, "1234567890123");
61         test(12345678901234UL, "12345678901234");
62         test(123456789012345UL, "123456789012345");
63         test(1234567890123456UL, "1234567890123456");
64         test(12345678901234567UL, "12345678901234567");
65         test(123456789012345678UL, "123456789012345678");
66         test(1234567890123456789UL, "1234567890123456789");
67         test(12345678901234567890UL, "12345678901234567890");
68 
69         // Test special cases with zeros inside a value string representation,
70         // to_chars algorithm processes them in a special way and should not
71         // skip trailing zeros
72         // Test driver automatically skips values not fitting into source type.
73         test(0UL, "0");
74         test(10UL, "10");
75         test(100UL, "100");
76         test(1000UL, "1000");
77         test(10000UL, "10000");
78         test(100000UL, "100000");
79         test(1000000UL, "1000000");
80         test(10000000UL, "10000000");
81         test(100000000UL, "100000000");
82         test(1000000000UL, "1000000000");
83         test(10000000000UL, "10000000000");
84         test(100000000000UL, "100000000000");
85         test(1000000000000UL, "1000000000000");
86         test(10000000000000UL, "10000000000000");
87         test(100000000000000UL, "100000000000000");
88         test(1000000000000000UL, "1000000000000000");
89         test(10000000000000000UL, "10000000000000000");
90         test(100000000000000000UL, "100000000000000000");
91         test(1000000000000000000UL, "1000000000000000000");
92         test(10000000000000000000UL, "10000000000000000000");
93 
94         for (int b = 2; b < 37; ++b)
95         {
96             using xl = std::numeric_limits<T>;
97 
98             test_value(1, b);
99             test_value(xl::lowest(), b);
100             test_value((xl::max)(), b);
101             test_value((xl::max)() / 2, b);
102         }
103     }
104 };
105 
106 template <typename T>
107 struct test_signed : to_chars_test_base<T>
108 {
109     using to_chars_test_base<T>::test;
110     using to_chars_test_base<T>::test_value;
111 
112     void operator()()
113     {
114         test(-1, "-1");
115         test(-12, "-12");
116         test(-1, "-1", 10);
117         test(-12, "-12", 10);
118         test(-21734634, "-21734634", 10);
119         test(-2647, "-101001010111", 2);
120         test(-0xcc1, "-cc1", 16);
121 
122         // Test each len till len of INT64_MAX = 19 because to_chars algorithm
123         // makes branches based on decimal digits count in the value string
124         // representation.
125         // Test driver automatically skips values not fitting into source type.
126         test(-1L, "-1");
127         test(-12L, "-12");
128         test(-123L, "-123");
129         test(-1234L, "-1234");
130         test(-12345L, "-12345");
131         test(-123456L, "-123456");
132         test(-1234567L, "-1234567");
133         test(-12345678L, "-12345678");
134         test(-123456789L, "-123456789");
135         test(-1234567890L, "-1234567890");
136         test(-12345678901L, "-12345678901");
137         test(-123456789012L, "-123456789012");
138         test(-1234567890123L, "-1234567890123");
139         test(-12345678901234L, "-12345678901234");
140         test(-123456789012345L, "-123456789012345");
141         test(-1234567890123456L, "-1234567890123456");
142         test(-12345678901234567L, "-12345678901234567");
143         test(-123456789012345678L, "-123456789012345678");
144         test(-1234567890123456789L, "-1234567890123456789");
145 
146         // Test special cases with zeros inside a value string representation,
147         // to_chars algorithm processes them in a special way and should not
148         // skip trailing zeros
149         // Test driver automatically skips values not fitting into source type.
150         test(-10L, "-10");
151         test(-100L, "-100");
152         test(-1000L, "-1000");
153         test(-10000L, "-10000");
154         test(-100000L, "-100000");
155         test(-1000000L, "-1000000");
156         test(-10000000L, "-10000000");
157         test(-100000000L, "-100000000");
158         test(-1000000000L, "-1000000000");
159         test(-10000000000L, "-10000000000");
160         test(-100000000000L, "-100000000000");
161         test(-1000000000000L, "-1000000000000");
162         test(-10000000000000L, "-10000000000000");
163         test(-100000000000000L, "-100000000000000");
164         test(-1000000000000000L, "-1000000000000000");
165         test(-10000000000000000L, "-10000000000000000");
166         test(-100000000000000000L, "-100000000000000000");
167         test(-1000000000000000000L, "-1000000000000000000");
168 
169         for (int b = 2; b < 37; ++b)
170         {
171             using xl = std::numeric_limits<T>;
172 
173             test_value(0, b);
174             test_value(xl::lowest(), b);
175             test_value((xl::max)(), b);
176         }
177     }
178 };
179 
180 int main(int, char**)
181 {
182     run<test_basics>(integrals);
183     run<test_signed>(all_signed);
184 
185   return 0;
186 }
187