1 //===-- Unittests for strtoll ---------------------------------------------===//
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 #include "src/stdlib/strtoll.h"
10 
11 #include "utils/UnitTest/Test.h"
12 
13 #include <errno.h>
14 #include <limits.h>
15 #include <stddef.h>
16 
17 TEST(LlvmLibcStrToLLTest, InvalidBase) {
18   const char *ten = "10";
19   errno = 0;
20   ASSERT_EQ(__llvm_libc::strtoll(ten, nullptr, -1), 0ll);
21   ASSERT_EQ(errno, EINVAL);
22 }
23 
24 TEST(LlvmLibcStrToLLTest, CleanBaseTenDecode) {
25   char *str_end = nullptr;
26 
27   const char *ten = "10";
28   errno = 0;
29   ASSERT_EQ(__llvm_libc::strtoll(ten, &str_end, 10), 10ll);
30   ASSERT_EQ(errno, 0);
31   EXPECT_EQ(str_end - ten, ptrdiff_t(2));
32   errno = 0;
33   ASSERT_EQ(__llvm_libc::strtoll(ten, nullptr, 10), 10ll);
34   ASSERT_EQ(errno, 0);
35 
36   const char *hundred = "100";
37   errno = 0;
38   ASSERT_EQ(__llvm_libc::strtoll(hundred, &str_end, 10), 100ll);
39   ASSERT_EQ(errno, 0);
40   EXPECT_EQ(str_end - hundred, ptrdiff_t(3));
41 
42   const char *negative = "-100";
43   errno = 0;
44   ASSERT_EQ(__llvm_libc::strtoll(negative, &str_end, 10), -100ll);
45   ASSERT_EQ(errno, 0);
46   EXPECT_EQ(str_end - negative, ptrdiff_t(4));
47 
48   const char *big_number = "123456789012345";
49   errno = 0;
50   ASSERT_EQ(__llvm_libc::strtoll(big_number, &str_end, 10), 123456789012345ll);
51   ASSERT_EQ(errno, 0);
52   EXPECT_EQ(str_end - big_number, ptrdiff_t(15));
53 
54   const char *big_negative_number = "-123456789012345";
55   errno = 0;
56   ASSERT_EQ(__llvm_libc::strtoll(big_negative_number, &str_end, 10),
57             -123456789012345ll);
58   ASSERT_EQ(errno, 0);
59   EXPECT_EQ(str_end - big_negative_number, ptrdiff_t(16));
60 
61   const char *long_long_max_number = "9223372036854775807";
62   errno = 0;
63   ASSERT_EQ(__llvm_libc::strtoll(long_long_max_number, &str_end, 10),
64             9223372036854775807ll);
65   ASSERT_EQ(errno, 0);
66   EXPECT_EQ(str_end - long_long_max_number, ptrdiff_t(19));
67 
68   const char *long_long_min_number = "-9223372036854775808";
69   errno = 0;
70   ASSERT_EQ(__llvm_libc::strtoll(long_long_min_number, &str_end, 10),
71             -9223372036854775807ll - 1ll);
72   ASSERT_EQ(errno, 0);
73   EXPECT_EQ(str_end - long_long_min_number, ptrdiff_t(20));
74 
75   const char *too_big_number = "123456789012345678901";
76   errno = 0;
77   ASSERT_EQ(__llvm_libc::strtoll(too_big_number, &str_end, 10), LLONG_MAX);
78   ASSERT_EQ(errno, ERANGE);
79   EXPECT_EQ(str_end - too_big_number, ptrdiff_t(21));
80 
81   const char *too_big_negative_number = "-123456789012345678901";
82   errno = 0;
83   ASSERT_EQ(__llvm_libc::strtoll(too_big_negative_number, &str_end, 10),
84             LLONG_MIN);
85   ASSERT_EQ(errno, ERANGE);
86   EXPECT_EQ(str_end - too_big_negative_number, ptrdiff_t(22));
87 
88   const char *long_number_range_test =
89       "10000000000000000000000000000000000000000000000000";
90   errno = 0;
91   ASSERT_EQ(__llvm_libc::strtoll(long_number_range_test, &str_end, 10),
92             LLONG_MAX);
93   ASSERT_EQ(errno, ERANGE);
94   EXPECT_EQ(str_end - long_number_range_test, ptrdiff_t(50));
95 
96   const char *long_long_max_number_with_numbers_after =
97       "9223372036854775807123";
98   errno = 0;
99   ASSERT_EQ(__llvm_libc::strtoll(long_long_max_number_with_numbers_after,
100                                  &str_end, 10),
101             LLONG_MAX);
102   ASSERT_EQ(errno, ERANGE);
103   EXPECT_EQ(
104       static_cast<long long>(str_end - long_long_max_number_with_numbers_after),
105       22ll);
106 }
107 
108 TEST(LlvmLibcStrToLLTest, MessyBaseTenDecode) {
109   char *str_end = nullptr;
110 
111   const char *spaces_before = "     10";
112   errno = 0;
113   ASSERT_EQ(__llvm_libc::strtoll(spaces_before, &str_end, 10), 10ll);
114   ASSERT_EQ(errno, 0);
115   EXPECT_EQ(str_end - spaces_before, ptrdiff_t(7));
116 
117   const char *spaces_after = "10      ";
118   errno = 0;
119   ASSERT_EQ(__llvm_libc::strtoll(spaces_after, &str_end, 10), 10ll);
120   ASSERT_EQ(errno, 0);
121   EXPECT_EQ(str_end - spaces_after, ptrdiff_t(2));
122 
123   const char *word_before = "word10";
124   errno = 0;
125   ASSERT_EQ(__llvm_libc::strtoll(word_before, &str_end, 10), 0ll);
126   ASSERT_EQ(errno, 0);
127   EXPECT_EQ(str_end - word_before, ptrdiff_t(0));
128 
129   const char *word_after = "10word";
130   errno = 0;
131   ASSERT_EQ(__llvm_libc::strtoll(word_after, &str_end, 10), 10ll);
132   ASSERT_EQ(errno, 0);
133   EXPECT_EQ(str_end - word_after, ptrdiff_t(2));
134 
135   const char *two_numbers = "10 999";
136   errno = 0;
137   ASSERT_EQ(__llvm_libc::strtoll(two_numbers, &str_end, 10), 10ll);
138   ASSERT_EQ(errno, 0);
139   EXPECT_EQ(str_end - two_numbers, ptrdiff_t(2));
140 
141   const char *two_signs = "--10 999";
142   errno = 0;
143   ASSERT_EQ(__llvm_libc::strtoll(two_signs, &str_end, 10), 0ll);
144   ASSERT_EQ(errno, 0);
145   EXPECT_EQ(str_end - two_signs, ptrdiff_t(1));
146 
147   const char *sign_before = "+2=4";
148   errno = 0;
149   ASSERT_EQ(__llvm_libc::strtoll(sign_before, &str_end, 10), 2ll);
150   ASSERT_EQ(errno, 0);
151   EXPECT_EQ(str_end - sign_before, ptrdiff_t(2));
152 
153   const char *sign_after = "2+2=4";
154   errno = 0;
155   ASSERT_EQ(__llvm_libc::strtoll(sign_after, &str_end, 10), 2ll);
156   ASSERT_EQ(errno, 0);
157   EXPECT_EQ(str_end - sign_after, ptrdiff_t(1));
158 
159   const char *tab_before = "\t10";
160   errno = 0;
161   ASSERT_EQ(__llvm_libc::strtoll(tab_before, &str_end, 10), 10ll);
162   ASSERT_EQ(errno, 0);
163   EXPECT_EQ(str_end - tab_before, ptrdiff_t(3));
164 
165   const char *all_together = "\t  -12345and+67890";
166   errno = 0;
167   ASSERT_EQ(__llvm_libc::strtoll(all_together, &str_end, 10), -12345ll);
168   ASSERT_EQ(errno, 0);
169   EXPECT_EQ(str_end - all_together, ptrdiff_t(9));
170 }
171 
172 static char int_to_b36_char(int input) {
173   if (input < 0 || input > 36)
174     return '0';
175   if (input < 10)
176     return '0' + input;
177   return 'A' + input - 10;
178 }
179 
180 TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
181   char small_string[4] = {'\0', '\0', '\0', '\0'};
182   for (long long base = 2; base <= 36; ++base) {
183     for (long long first_digit = 0; first_digit <= 36; ++first_digit) {
184       small_string[0] = int_to_b36_char(first_digit);
185       if (first_digit < base) {
186         errno = 0;
187         ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
188                   first_digit);
189         ASSERT_EQ(errno, 0);
190       } else {
191         errno = 0;
192         ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
193         ASSERT_EQ(errno, 0);
194       }
195     }
196   }
197 
198   for (long long base = 2; base <= 36; ++base) {
199     for (long long first_digit = 0; first_digit <= 36; ++first_digit) {
200       small_string[0] = int_to_b36_char(first_digit);
201       for (long long second_digit = 0; second_digit <= 36; ++second_digit) {
202         small_string[1] = int_to_b36_char(second_digit);
203         if (first_digit < base && second_digit < base) {
204           errno = 0;
205           ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
206                     second_digit + (first_digit * base));
207           ASSERT_EQ(errno, 0);
208         } else if (first_digit < base) {
209           errno = 0;
210           ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
211                     first_digit);
212           ASSERT_EQ(errno, 0);
213         } else {
214           errno = 0;
215           ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
216           ASSERT_EQ(errno, 0);
217         }
218       }
219     }
220   }
221 
222   for (long long base = 2; base <= 36; ++base) {
223     for (long long first_digit = 0; first_digit <= 36; ++first_digit) {
224       small_string[0] = int_to_b36_char(first_digit);
225       for (long long second_digit = 0; second_digit <= 36; ++second_digit) {
226         small_string[1] = int_to_b36_char(second_digit);
227         for (long long third_digit = 0; third_digit <= 36; ++third_digit) {
228           small_string[2] = int_to_b36_char(third_digit);
229 
230           if (first_digit < base && second_digit < base && third_digit < base) {
231             errno = 0;
232             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
233                       third_digit + (second_digit * base) +
234                           (first_digit * base * base));
235             ASSERT_EQ(errno, 0);
236           } else if (first_digit < base && second_digit < base) {
237             errno = 0;
238             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
239                       second_digit + (first_digit * base));
240             ASSERT_EQ(errno, 0);
241           } else if (first_digit < base) {
242             // if the base is 16 there is a special case for the prefix 0X.
243             // The number is treated as a one digit hexadecimal.
244             if (base == 16 && first_digit == 0 && second_digit == 33) {
245               if (third_digit < base) {
246                 errno = 0;
247                 ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
248                           third_digit);
249                 ASSERT_EQ(errno, 0);
250               } else {
251                 errno = 0;
252                 ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
253                           0ll);
254                 ASSERT_EQ(errno, 0);
255               }
256             } else {
257               errno = 0;
258               ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
259                         first_digit);
260               ASSERT_EQ(errno, 0);
261             }
262           } else {
263             errno = 0;
264             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
265             ASSERT_EQ(errno, 0);
266           }
267         }
268       }
269     }
270   }
271 }
272 
273 TEST(LlvmLibcStrToLLTest, CleanBaseSixteenDecode) {
274   char *str_end = nullptr;
275 
276   const char *no_prefix = "123abc";
277   errno = 0;
278   ASSERT_EQ(__llvm_libc::strtoll(no_prefix, &str_end, 16), 0x123abcll);
279   ASSERT_EQ(errno, 0);
280   EXPECT_EQ(str_end - no_prefix, ptrdiff_t(6));
281 
282   const char *yes_prefix = "0x456def";
283   errno = 0;
284   ASSERT_EQ(__llvm_libc::strtoll(yes_prefix, &str_end, 16), 0x456defll);
285   ASSERT_EQ(errno, 0);
286   EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8));
287 }
288 
289 TEST(LlvmLibcStrToLLTest, AutomaticBaseSelection) {
290   char *str_end = nullptr;
291 
292   const char *base_ten = "12345";
293   errno = 0;
294   ASSERT_EQ(__llvm_libc::strtoll(base_ten, &str_end, 0), 12345ll);
295   ASSERT_EQ(errno, 0);
296   EXPECT_EQ(str_end - base_ten, ptrdiff_t(5));
297 
298   const char *base_sixteen_no_prefix = "123abc";
299   errno = 0;
300   ASSERT_EQ(__llvm_libc::strtoll(base_sixteen_no_prefix, &str_end, 0), 123ll);
301   ASSERT_EQ(errno, 0);
302   EXPECT_EQ(str_end - base_sixteen_no_prefix, ptrdiff_t(3));
303 
304   const char *base_sixteen_with_prefix = "0x456def";
305   errno = 0;
306   ASSERT_EQ(__llvm_libc::strtoll(base_sixteen_with_prefix, &str_end, 0),
307             0x456defll);
308   ASSERT_EQ(errno, 0);
309   EXPECT_EQ(str_end - base_sixteen_with_prefix, ptrdiff_t(8));
310 
311   const char *base_eight_with_prefix = "012345";
312   errno = 0;
313   ASSERT_EQ(__llvm_libc::strtoll(base_eight_with_prefix, &str_end, 0),
314             012345ll);
315   ASSERT_EQ(errno, 0);
316   EXPECT_EQ(str_end - base_eight_with_prefix, ptrdiff_t(6));
317 }
318