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