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 "utils/UnitTest/Test.h" 12 13 #include <errno.h> 14 #include <limits.h> 15 #include <stddef.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 '0' + input; 145 return 'A' + input - 10; 146 } 147 148 TEST(LlvmLibcStrToULTest, DecodeInOtherBases) { 149 char small_string[4] = {'\0', '\0', '\0', '\0'}; 150 for (unsigned long base = 2; base <= 36; ++base) { 151 for (unsigned long 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 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 (unsigned long base = 2; base <= 36; ++base) { 167 for (unsigned long first_digit = 0; first_digit <= 36; ++first_digit) { 168 small_string[0] = int_to_b36_char(first_digit); 169 for (unsigned long 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 second_digit + (first_digit * base)); 175 ASSERT_EQ(errno, 0); 176 } else if (first_digit < base) { 177 errno = 0; 178 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 179 first_digit); 180 ASSERT_EQ(errno, 0); 181 } else { 182 errno = 0; 183 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 0ul); 184 ASSERT_EQ(errno, 0); 185 } 186 } 187 } 188 } 189 190 for (unsigned long base = 2; base <= 36; ++base) { 191 for (unsigned long first_digit = 0; first_digit <= 36; ++first_digit) { 192 small_string[0] = int_to_b36_char(first_digit); 193 for (unsigned long second_digit = 0; second_digit <= 36; ++second_digit) { 194 small_string[1] = int_to_b36_char(second_digit); 195 for (unsigned long third_digit = 0; third_digit <= 36; ++third_digit) { 196 small_string[2] = int_to_b36_char(third_digit); 197 198 if (first_digit < base && second_digit < base && third_digit < base) { 199 errno = 0; 200 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 201 third_digit + (second_digit * base) + 202 (first_digit * base * base)); 203 ASSERT_EQ(errno, 0); 204 } else if (first_digit < base && second_digit < base) { 205 errno = 0; 206 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 207 second_digit + (first_digit * base)); 208 ASSERT_EQ(errno, 0); 209 } else if (first_digit < base) { 210 // if the base is 16 there is a special case for the prefix 0X. 211 // The number is treated as a one digit hexadecimal. 212 if (base == 16 && first_digit == 0 && second_digit == 33) { 213 if (third_digit < base) { 214 errno = 0; 215 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 216 third_digit); 217 ASSERT_EQ(errno, 0); 218 } else { 219 errno = 0; 220 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 221 0ul); 222 ASSERT_EQ(errno, 0); 223 } 224 } else { 225 errno = 0; 226 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 227 first_digit); 228 ASSERT_EQ(errno, 0); 229 } 230 } else { 231 errno = 0; 232 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base), 0ul); 233 ASSERT_EQ(errno, 0); 234 } 235 } 236 } 237 } 238 } 239 } 240 241 TEST(LlvmLibcStrToULTest, CleanBaseSixteenDecode) { 242 char *str_end = nullptr; 243 244 const char *no_prefix = "123abc"; 245 errno = 0; 246 ASSERT_EQ(__llvm_libc::strtoul(no_prefix, &str_end, 16), 0x123abcul); 247 ASSERT_EQ(errno, 0); 248 EXPECT_EQ(str_end - no_prefix, ptrdiff_t(6)); 249 250 const char *yes_prefix = "0x456def"; 251 errno = 0; 252 ASSERT_EQ(__llvm_libc::strtoul(yes_prefix, &str_end, 16), 0x456deful); 253 ASSERT_EQ(errno, 0); 254 EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8)); 255 } 256 257 TEST(LlvmLibcStrToULTest, AutomaticBaseSelection) { 258 char *str_end = nullptr; 259 260 const char *base_ten = "12345"; 261 errno = 0; 262 ASSERT_EQ(__llvm_libc::strtoul(base_ten, &str_end, 0), 12345ul); 263 ASSERT_EQ(errno, 0); 264 EXPECT_EQ(str_end - base_ten, ptrdiff_t(5)); 265 266 const char *base_sixteen_no_prefix = "123abc"; 267 errno = 0; 268 ASSERT_EQ(__llvm_libc::strtoul(base_sixteen_no_prefix, &str_end, 0), 123ul); 269 ASSERT_EQ(errno, 0); 270 EXPECT_EQ(str_end - base_sixteen_no_prefix, ptrdiff_t(3)); 271 272 const char *base_sixteen_with_prefix = "0x456def"; 273 errno = 0; 274 ASSERT_EQ(__llvm_libc::strtoul(base_sixteen_with_prefix, &str_end, 0), 275 0x456deful); 276 ASSERT_EQ(errno, 0); 277 EXPECT_EQ(str_end - base_sixteen_with_prefix, ptrdiff_t(8)); 278 279 const char *base_eight_with_prefix = "012345"; 280 errno = 0; 281 ASSERT_EQ(__llvm_libc::strtoul(base_eight_with_prefix, &str_end, 0), 282 012345ul); 283 ASSERT_EQ(errno, 0); 284 EXPECT_EQ(str_end - base_eight_with_prefix, ptrdiff_t(6)); 285 } 286