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