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 static_cast<char>('0' + input);
177   return static_cast<char>('A' + input - 10);
178 }
179 
180 TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
181   char small_string[4] = {'\0', '\0', '\0', '\0'};
182   for (int base = 2; base <= 36; ++base) {
183     for (int 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                   static_cast<long long int>(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 (int base = 2; base <= 36; ++base) {
199     for (int first_digit = 0; first_digit <= 36; ++first_digit) {
200       small_string[0] = int_to_b36_char(first_digit);
201       for (int 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(
206               __llvm_libc::strtoll(small_string, nullptr, base),
207               static_cast<long long int>(second_digit + (first_digit * base)));
208           ASSERT_EQ(errno, 0);
209         } else if (first_digit < base) {
210           errno = 0;
211           ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
212                     static_cast<long long int>(first_digit));
213           ASSERT_EQ(errno, 0);
214         } else {
215           errno = 0;
216           ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
217           ASSERT_EQ(errno, 0);
218         }
219       }
220     }
221   }
222 
223   for (int base = 2; base <= 36; ++base) {
224     for (int first_digit = 0; first_digit <= 36; ++first_digit) {
225       small_string[0] = int_to_b36_char(first_digit);
226       for (int second_digit = 0; second_digit <= 36; ++second_digit) {
227         small_string[1] = int_to_b36_char(second_digit);
228         for (int third_digit = 0; third_digit <= 36; ++third_digit) {
229           small_string[2] = int_to_b36_char(third_digit);
230 
231           if (first_digit < base && second_digit < base && third_digit < base) {
232             errno = 0;
233             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
234                       static_cast<long long int>(third_digit +
235                                                  (second_digit * base) +
236                                                  (first_digit * base * base)));
237             ASSERT_EQ(errno, 0);
238           } else if (first_digit < base && second_digit < base) {
239             errno = 0;
240             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
241                       static_cast<long long int>(second_digit +
242                                                  (first_digit * base)));
243             ASSERT_EQ(errno, 0);
244           } else if (first_digit < base) {
245             // if the base is 16 there is a special case for the prefix 0X.
246             // The number is treated as a one digit hexadecimal.
247             if (base == 16 && first_digit == 0 && second_digit == 33) {
248               if (third_digit < base) {
249                 errno = 0;
250                 ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
251                           static_cast<long long int>(third_digit));
252                 ASSERT_EQ(errno, 0);
253               } else {
254                 errno = 0;
255                 ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
256                           0ll);
257                 ASSERT_EQ(errno, 0);
258               }
259             } else {
260               errno = 0;
261               ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
262                         static_cast<long long int>(first_digit));
263               ASSERT_EQ(errno, 0);
264             }
265           } else {
266             errno = 0;
267             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
268             ASSERT_EQ(errno, 0);
269           }
270         }
271       }
272     }
273   }
274 }
275 
276 TEST(LlvmLibcStrToLLTest, CleanBaseSixteenDecode) {
277   char *str_end = nullptr;
278 
279   const char *no_prefix = "123abc";
280   errno = 0;
281   ASSERT_EQ(__llvm_libc::strtoll(no_prefix, &str_end, 16), 0x123abcll);
282   ASSERT_EQ(errno, 0);
283   EXPECT_EQ(str_end - no_prefix, ptrdiff_t(6));
284 
285   const char *yes_prefix = "0x456def";
286   errno = 0;
287   ASSERT_EQ(__llvm_libc::strtoll(yes_prefix, &str_end, 16), 0x456defll);
288   ASSERT_EQ(errno, 0);
289   EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8));
290 }
291 
292 TEST(LlvmLibcStrToLLTest, AutomaticBaseSelection) {
293   char *str_end = nullptr;
294 
295   const char *base_ten = "12345";
296   errno = 0;
297   ASSERT_EQ(__llvm_libc::strtoll(base_ten, &str_end, 0), 12345ll);
298   ASSERT_EQ(errno, 0);
299   EXPECT_EQ(str_end - base_ten, ptrdiff_t(5));
300 
301   const char *base_sixteen_no_prefix = "123abc";
302   errno = 0;
303   ASSERT_EQ(__llvm_libc::strtoll(base_sixteen_no_prefix, &str_end, 0), 123ll);
304   ASSERT_EQ(errno, 0);
305   EXPECT_EQ(str_end - base_sixteen_no_prefix, ptrdiff_t(3));
306 
307   const char *base_sixteen_with_prefix = "0x456def";
308   errno = 0;
309   ASSERT_EQ(__llvm_libc::strtoll(base_sixteen_with_prefix, &str_end, 0),
310             0x456defll);
311   ASSERT_EQ(errno, 0);
312   EXPECT_EQ(str_end - base_sixteen_with_prefix, ptrdiff_t(8));
313 
314   const char *base_eight_with_prefix = "012345";
315   errno = 0;
316   ASSERT_EQ(__llvm_libc::strtoll(base_eight_with_prefix, &str_end, 0),
317             012345ll);
318   ASSERT_EQ(errno, 0);
319   EXPECT_EQ(str_end - base_eight_with_prefix, ptrdiff_t(6));
320 }
321