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