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