1b062d639SMichael Jones //===-- Unittests for strtoll ---------------------------------------------===//
2b062d639SMichael Jones //
3b062d639SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b062d639SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5b062d639SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b062d639SMichael Jones //
7b062d639SMichael Jones //===----------------------------------------------------------------------===//
8b062d639SMichael Jones
9b062d639SMichael Jones #include "src/stdlib/strtoll.h"
10b062d639SMichael Jones
11b062d639SMichael Jones #include "utils/UnitTest/Test.h"
12b062d639SMichael Jones
13b062d639SMichael Jones #include <errno.h>
14b062d639SMichael Jones #include <limits.h>
151d02a8bcSMichael Jones #include <stddef.h>
16b062d639SMichael Jones
TEST(LlvmLibcStrToLLTest,InvalidBase)17b062d639SMichael Jones TEST(LlvmLibcStrToLLTest, InvalidBase) {
18b062d639SMichael Jones const char *ten = "10";
19b062d639SMichael Jones errno = 0;
20b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(ten, nullptr, -1), 0ll);
21b062d639SMichael Jones ASSERT_EQ(errno, EINVAL);
22b062d639SMichael Jones }
23b062d639SMichael Jones
TEST(LlvmLibcStrToLLTest,CleanBaseTenDecode)24b062d639SMichael Jones TEST(LlvmLibcStrToLLTest, CleanBaseTenDecode) {
25b062d639SMichael Jones char *str_end = nullptr;
26b062d639SMichael Jones
27b062d639SMichael Jones const char *ten = "10";
28b062d639SMichael Jones errno = 0;
29b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(ten, &str_end, 10), 10ll);
30b062d639SMichael Jones ASSERT_EQ(errno, 0);
311d02a8bcSMichael Jones EXPECT_EQ(str_end - ten, ptrdiff_t(2));
32b062d639SMichael Jones errno = 0;
33b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(ten, nullptr, 10), 10ll);
34b062d639SMichael Jones ASSERT_EQ(errno, 0);
35b062d639SMichael Jones
36b062d639SMichael Jones const char *hundred = "100";
37b062d639SMichael Jones errno = 0;
38b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(hundred, &str_end, 10), 100ll);
39b062d639SMichael Jones ASSERT_EQ(errno, 0);
401d02a8bcSMichael Jones EXPECT_EQ(str_end - hundred, ptrdiff_t(3));
41b062d639SMichael Jones
42b062d639SMichael Jones const char *negative = "-100";
43b062d639SMichael Jones errno = 0;
44b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(negative, &str_end, 10), -100ll);
45b062d639SMichael Jones ASSERT_EQ(errno, 0);
461d02a8bcSMichael Jones EXPECT_EQ(str_end - negative, ptrdiff_t(4));
47b062d639SMichael Jones
48b062d639SMichael Jones const char *big_number = "123456789012345";
49b062d639SMichael Jones errno = 0;
50b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(big_number, &str_end, 10), 123456789012345ll);
51b062d639SMichael Jones ASSERT_EQ(errno, 0);
521d02a8bcSMichael Jones EXPECT_EQ(str_end - big_number, ptrdiff_t(15));
53b062d639SMichael Jones
54b062d639SMichael Jones const char *big_negative_number = "-123456789012345";
55b062d639SMichael Jones errno = 0;
56b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(big_negative_number, &str_end, 10),
57b062d639SMichael Jones -123456789012345ll);
58b062d639SMichael Jones ASSERT_EQ(errno, 0);
591d02a8bcSMichael Jones EXPECT_EQ(str_end - big_negative_number, ptrdiff_t(16));
60b062d639SMichael Jones
61d52f0aecSMichael Jones const char *long_long_max_number = "9223372036854775807";
62d52f0aecSMichael Jones errno = 0;
63d52f0aecSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(long_long_max_number, &str_end, 10),
64d52f0aecSMichael Jones 9223372036854775807ll);
65d52f0aecSMichael Jones ASSERT_EQ(errno, 0);
661d02a8bcSMichael Jones EXPECT_EQ(str_end - long_long_max_number, ptrdiff_t(19));
67d52f0aecSMichael Jones
68d52f0aecSMichael Jones const char *long_long_min_number = "-9223372036854775808";
69d52f0aecSMichael Jones errno = 0;
70d52f0aecSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(long_long_min_number, &str_end, 10),
71d52f0aecSMichael Jones -9223372036854775807ll - 1ll);
72d52f0aecSMichael Jones ASSERT_EQ(errno, 0);
731d02a8bcSMichael Jones EXPECT_EQ(str_end - long_long_min_number, ptrdiff_t(20));
74d52f0aecSMichael Jones
75b062d639SMichael Jones const char *too_big_number = "123456789012345678901";
76b062d639SMichael Jones errno = 0;
77b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(too_big_number, &str_end, 10), LLONG_MAX);
78b062d639SMichael Jones ASSERT_EQ(errno, ERANGE);
791d02a8bcSMichael Jones EXPECT_EQ(str_end - too_big_number, ptrdiff_t(21));
80b062d639SMichael Jones
81b062d639SMichael Jones const char *too_big_negative_number = "-123456789012345678901";
82b062d639SMichael Jones errno = 0;
83b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(too_big_negative_number, &str_end, 10),
84b062d639SMichael Jones LLONG_MIN);
85b062d639SMichael Jones ASSERT_EQ(errno, ERANGE);
861d02a8bcSMichael Jones EXPECT_EQ(str_end - too_big_negative_number, ptrdiff_t(22));
87d52f0aecSMichael Jones
88d52f0aecSMichael Jones const char *long_number_range_test =
89d52f0aecSMichael Jones "10000000000000000000000000000000000000000000000000";
90d52f0aecSMichael Jones errno = 0;
91d52f0aecSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(long_number_range_test, &str_end, 10),
92d52f0aecSMichael Jones LLONG_MAX);
93d52f0aecSMichael Jones ASSERT_EQ(errno, ERANGE);
941d02a8bcSMichael Jones EXPECT_EQ(str_end - long_number_range_test, ptrdiff_t(50));
95d52f0aecSMichael Jones
96d52f0aecSMichael Jones const char *long_long_max_number_with_numbers_after =
97d52f0aecSMichael Jones "9223372036854775807123";
98d52f0aecSMichael Jones errno = 0;
99d52f0aecSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(long_long_max_number_with_numbers_after,
100d52f0aecSMichael Jones &str_end, 10),
101d52f0aecSMichael Jones LLONG_MAX);
102d52f0aecSMichael Jones ASSERT_EQ(errno, ERANGE);
1031d02a8bcSMichael Jones EXPECT_EQ(
1041d02a8bcSMichael Jones static_cast<long long>(str_end - long_long_max_number_with_numbers_after),
1051d02a8bcSMichael Jones 22ll);
106b062d639SMichael Jones }
107b062d639SMichael Jones
TEST(LlvmLibcStrToLLTest,MessyBaseTenDecode)108b062d639SMichael Jones TEST(LlvmLibcStrToLLTest, MessyBaseTenDecode) {
109b062d639SMichael Jones char *str_end = nullptr;
110b062d639SMichael Jones
111b062d639SMichael Jones const char *spaces_before = " 10";
112b062d639SMichael Jones errno = 0;
113b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(spaces_before, &str_end, 10), 10ll);
114b062d639SMichael Jones ASSERT_EQ(errno, 0);
1151d02a8bcSMichael Jones EXPECT_EQ(str_end - spaces_before, ptrdiff_t(7));
116b062d639SMichael Jones
117b062d639SMichael Jones const char *spaces_after = "10 ";
118b062d639SMichael Jones errno = 0;
119b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(spaces_after, &str_end, 10), 10ll);
120b062d639SMichael Jones ASSERT_EQ(errno, 0);
1211d02a8bcSMichael Jones EXPECT_EQ(str_end - spaces_after, ptrdiff_t(2));
122b062d639SMichael Jones
123b062d639SMichael Jones const char *word_before = "word10";
124b062d639SMichael Jones errno = 0;
125b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(word_before, &str_end, 10), 0ll);
126b062d639SMichael Jones ASSERT_EQ(errno, 0);
1271d02a8bcSMichael Jones EXPECT_EQ(str_end - word_before, ptrdiff_t(0));
128b062d639SMichael Jones
129b062d639SMichael Jones const char *word_after = "10word";
130b062d639SMichael Jones errno = 0;
131b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(word_after, &str_end, 10), 10ll);
132b062d639SMichael Jones ASSERT_EQ(errno, 0);
1331d02a8bcSMichael Jones EXPECT_EQ(str_end - word_after, ptrdiff_t(2));
134b062d639SMichael Jones
135b062d639SMichael Jones const char *two_numbers = "10 999";
136b062d639SMichael Jones errno = 0;
137b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(two_numbers, &str_end, 10), 10ll);
138b062d639SMichael Jones ASSERT_EQ(errno, 0);
1391d02a8bcSMichael Jones EXPECT_EQ(str_end - two_numbers, ptrdiff_t(2));
140b062d639SMichael Jones
141b062d639SMichael Jones const char *two_signs = "--10 999";
142b062d639SMichael Jones errno = 0;
143b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(two_signs, &str_end, 10), 0ll);
144b062d639SMichael Jones ASSERT_EQ(errno, 0);
145*53804d4eSMichael Jones EXPECT_EQ(str_end - two_signs, ptrdiff_t(0));
146b062d639SMichael Jones
147b062d639SMichael Jones const char *sign_before = "+2=4";
148b062d639SMichael Jones errno = 0;
149b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(sign_before, &str_end, 10), 2ll);
150b062d639SMichael Jones ASSERT_EQ(errno, 0);
1511d02a8bcSMichael Jones EXPECT_EQ(str_end - sign_before, ptrdiff_t(2));
152b062d639SMichael Jones
153b062d639SMichael Jones const char *sign_after = "2+2=4";
154b062d639SMichael Jones errno = 0;
155b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(sign_after, &str_end, 10), 2ll);
156b062d639SMichael Jones ASSERT_EQ(errno, 0);
1571d02a8bcSMichael Jones EXPECT_EQ(str_end - sign_after, ptrdiff_t(1));
158b062d639SMichael Jones
159b062d639SMichael Jones const char *tab_before = "\t10";
160b062d639SMichael Jones errno = 0;
161b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(tab_before, &str_end, 10), 10ll);
162b062d639SMichael Jones ASSERT_EQ(errno, 0);
1631d02a8bcSMichael Jones EXPECT_EQ(str_end - tab_before, ptrdiff_t(3));
164b062d639SMichael Jones
165b062d639SMichael Jones const char *all_together = "\t -12345and+67890";
166b062d639SMichael Jones errno = 0;
167b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(all_together, &str_end, 10), -12345ll);
168b062d639SMichael Jones ASSERT_EQ(errno, 0);
1691d02a8bcSMichael Jones EXPECT_EQ(str_end - all_together, ptrdiff_t(9));
170*53804d4eSMichael Jones
171*53804d4eSMichael Jones const char *just_spaces = " ";
172*53804d4eSMichael Jones errno = 0;
173*53804d4eSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_spaces, &str_end, 10), 0ll);
174*53804d4eSMichael Jones ASSERT_EQ(errno, 0);
175*53804d4eSMichael Jones EXPECT_EQ(str_end - just_spaces, ptrdiff_t(0));
176*53804d4eSMichael Jones
177*53804d4eSMichael Jones const char *just_space_and_sign = " +";
178*53804d4eSMichael Jones errno = 0;
179*53804d4eSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_space_and_sign, &str_end, 10), 0ll);
180*53804d4eSMichael Jones ASSERT_EQ(errno, 0);
181*53804d4eSMichael Jones EXPECT_EQ(str_end - just_space_and_sign, ptrdiff_t(0));
182b062d639SMichael Jones }
183b062d639SMichael Jones
int_to_b36_char(int input)184b062d639SMichael Jones static char int_to_b36_char(int input) {
185b062d639SMichael Jones if (input < 0 || input > 36)
186b062d639SMichael Jones return '0';
187b062d639SMichael Jones if (input < 10)
1884e1a164dSRoland McGrath return static_cast<char>('0' + input);
1894e1a164dSRoland McGrath return static_cast<char>('A' + input - 10);
190b062d639SMichael Jones }
191b062d639SMichael Jones
TEST(LlvmLibcStrToLLTest,DecodeInOtherBases)192b062d639SMichael Jones TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
193b062d639SMichael Jones char small_string[4] = {'\0', '\0', '\0', '\0'};
1944e1a164dSRoland McGrath for (int base = 2; base <= 36; ++base) {
1954e1a164dSRoland McGrath for (int first_digit = 0; first_digit <= 36; ++first_digit) {
196b062d639SMichael Jones small_string[0] = int_to_b36_char(first_digit);
197b062d639SMichael Jones if (first_digit < base) {
198b062d639SMichael Jones errno = 0;
199b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
2004e1a164dSRoland McGrath static_cast<long long int>(first_digit));
201b062d639SMichael Jones ASSERT_EQ(errno, 0);
202b062d639SMichael Jones } else {
203b062d639SMichael Jones errno = 0;
204b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
205b062d639SMichael Jones ASSERT_EQ(errno, 0);
206b062d639SMichael Jones }
207b062d639SMichael Jones }
208b062d639SMichael Jones }
209b062d639SMichael Jones
2104e1a164dSRoland McGrath for (int base = 2; base <= 36; ++base) {
2114e1a164dSRoland McGrath for (int first_digit = 0; first_digit <= 36; ++first_digit) {
212b062d639SMichael Jones small_string[0] = int_to_b36_char(first_digit);
2134e1a164dSRoland McGrath for (int second_digit = 0; second_digit <= 36; ++second_digit) {
214b062d639SMichael Jones small_string[1] = int_to_b36_char(second_digit);
215b062d639SMichael Jones if (first_digit < base && second_digit < base) {
216b062d639SMichael Jones errno = 0;
2174e1a164dSRoland McGrath ASSERT_EQ(
2184e1a164dSRoland McGrath __llvm_libc::strtoll(small_string, nullptr, base),
2194e1a164dSRoland McGrath static_cast<long long int>(second_digit + (first_digit * base)));
220b062d639SMichael Jones ASSERT_EQ(errno, 0);
221b062d639SMichael Jones } else if (first_digit < base) {
222b062d639SMichael Jones errno = 0;
223b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
2244e1a164dSRoland McGrath static_cast<long long int>(first_digit));
225b062d639SMichael Jones ASSERT_EQ(errno, 0);
226b062d639SMichael Jones } else {
227b062d639SMichael Jones errno = 0;
228b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
229b062d639SMichael Jones ASSERT_EQ(errno, 0);
230b062d639SMichael Jones }
231b062d639SMichael Jones }
232b062d639SMichael Jones }
233b062d639SMichael Jones }
234b062d639SMichael Jones
2354e1a164dSRoland McGrath for (int base = 2; base <= 36; ++base) {
2364e1a164dSRoland McGrath for (int first_digit = 0; first_digit <= 36; ++first_digit) {
237b062d639SMichael Jones small_string[0] = int_to_b36_char(first_digit);
2384e1a164dSRoland McGrath for (int second_digit = 0; second_digit <= 36; ++second_digit) {
239b062d639SMichael Jones small_string[1] = int_to_b36_char(second_digit);
2404e1a164dSRoland McGrath for (int third_digit = 0; third_digit <= 36; ++third_digit) {
241b062d639SMichael Jones small_string[2] = int_to_b36_char(third_digit);
242b062d639SMichael Jones
243b062d639SMichael Jones if (first_digit < base && second_digit < base && third_digit < base) {
244b062d639SMichael Jones errno = 0;
245b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
2464e1a164dSRoland McGrath static_cast<long long int>(third_digit +
2474e1a164dSRoland McGrath (second_digit * base) +
2484e1a164dSRoland McGrath (first_digit * base * base)));
249b062d639SMichael Jones ASSERT_EQ(errno, 0);
250b062d639SMichael Jones } else if (first_digit < base && second_digit < base) {
251b062d639SMichael Jones errno = 0;
252b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
2534e1a164dSRoland McGrath static_cast<long long int>(second_digit +
2544e1a164dSRoland McGrath (first_digit * base)));
255b062d639SMichael Jones ASSERT_EQ(errno, 0);
256b062d639SMichael Jones } else if (first_digit < base) {
257b062d639SMichael Jones // if the base is 16 there is a special case for the prefix 0X.
258b062d639SMichael Jones // The number is treated as a one digit hexadecimal.
259b062d639SMichael Jones if (base == 16 && first_digit == 0 && second_digit == 33) {
260b062d639SMichael Jones if (third_digit < base) {
261b062d639SMichael Jones errno = 0;
262b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
2634e1a164dSRoland McGrath static_cast<long long int>(third_digit));
264b062d639SMichael Jones ASSERT_EQ(errno, 0);
265b062d639SMichael Jones } else {
266b062d639SMichael Jones errno = 0;
267b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
268b062d639SMichael Jones 0ll);
269b062d639SMichael Jones ASSERT_EQ(errno, 0);
270b062d639SMichael Jones }
271b062d639SMichael Jones } else {
272b062d639SMichael Jones errno = 0;
273b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
2744e1a164dSRoland McGrath static_cast<long long int>(first_digit));
275b062d639SMichael Jones ASSERT_EQ(errno, 0);
276b062d639SMichael Jones }
277b062d639SMichael Jones } else {
278b062d639SMichael Jones errno = 0;
279b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base), 0ll);
280b062d639SMichael Jones ASSERT_EQ(errno, 0);
281b062d639SMichael Jones }
282b062d639SMichael Jones }
283b062d639SMichael Jones }
284b062d639SMichael Jones }
285b062d639SMichael Jones }
286b062d639SMichael Jones }
287b062d639SMichael Jones
TEST(LlvmLibcStrToLLTest,CleanBaseSixteenDecode)288b062d639SMichael Jones TEST(LlvmLibcStrToLLTest, CleanBaseSixteenDecode) {
289b062d639SMichael Jones char *str_end = nullptr;
290b062d639SMichael Jones
291b062d639SMichael Jones const char *no_prefix = "123abc";
292b062d639SMichael Jones errno = 0;
293b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(no_prefix, &str_end, 16), 0x123abcll);
294b062d639SMichael Jones ASSERT_EQ(errno, 0);
2951d02a8bcSMichael Jones EXPECT_EQ(str_end - no_prefix, ptrdiff_t(6));
296b062d639SMichael Jones
297b062d639SMichael Jones const char *yes_prefix = "0x456def";
298b062d639SMichael Jones errno = 0;
299b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(yes_prefix, &str_end, 16), 0x456defll);
300b062d639SMichael Jones ASSERT_EQ(errno, 0);
3011d02a8bcSMichael Jones EXPECT_EQ(str_end - yes_prefix, ptrdiff_t(8));
30237ce7349SMichael Jones
30337ce7349SMichael Jones const char *letter_after_prefix = "0xabc123";
30437ce7349SMichael Jones errno = 0;
30537ce7349SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(letter_after_prefix, &str_end, 16),
30637ce7349SMichael Jones 0xabc123ll);
30737ce7349SMichael Jones ASSERT_EQ(errno, 0);
30837ce7349SMichael Jones EXPECT_EQ(str_end - letter_after_prefix, ptrdiff_t(8));
30937ce7349SMichael Jones }
31037ce7349SMichael Jones
TEST(LlvmLibcStrToLLTest,MessyBaseSixteenDecode)31137ce7349SMichael Jones TEST(LlvmLibcStrToLLTest, MessyBaseSixteenDecode) {
31237ce7349SMichael Jones char *str_end = nullptr;
31337ce7349SMichael Jones
31437ce7349SMichael Jones const char *just_prefix = "0x";
31537ce7349SMichael Jones errno = 0;
31637ce7349SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_prefix, &str_end, 16), 0ll);
31737ce7349SMichael Jones ASSERT_EQ(errno, 0);
31837ce7349SMichael Jones EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1));
31937ce7349SMichael Jones
32037ce7349SMichael Jones errno = 0;
32137ce7349SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_prefix, &str_end, 0), 0ll);
32237ce7349SMichael Jones ASSERT_EQ(errno, 0);
32337ce7349SMichael Jones EXPECT_EQ(str_end - just_prefix, ptrdiff_t(1));
32437ce7349SMichael Jones
32537ce7349SMichael Jones const char *prefix_with_x_after = "0xx";
32637ce7349SMichael Jones errno = 0;
32737ce7349SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(prefix_with_x_after, &str_end, 16), 0ll);
32837ce7349SMichael Jones ASSERT_EQ(errno, 0);
32937ce7349SMichael Jones EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1));
33037ce7349SMichael Jones
33137ce7349SMichael Jones errno = 0;
33237ce7349SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(prefix_with_x_after, &str_end, 0), 0ll);
33337ce7349SMichael Jones ASSERT_EQ(errno, 0);
33437ce7349SMichael Jones EXPECT_EQ(str_end - prefix_with_x_after, ptrdiff_t(1));
335b062d639SMichael Jones }
336b062d639SMichael Jones
TEST(LlvmLibcStrToLLTest,AutomaticBaseSelection)337b062d639SMichael Jones TEST(LlvmLibcStrToLLTest, AutomaticBaseSelection) {
338b062d639SMichael Jones char *str_end = nullptr;
339b062d639SMichael Jones
340b062d639SMichael Jones const char *base_ten = "12345";
341b062d639SMichael Jones errno = 0;
342b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(base_ten, &str_end, 0), 12345ll);
343b062d639SMichael Jones ASSERT_EQ(errno, 0);
3441d02a8bcSMichael Jones EXPECT_EQ(str_end - base_ten, ptrdiff_t(5));
345b062d639SMichael Jones
346b062d639SMichael Jones const char *base_sixteen_no_prefix = "123abc";
347b062d639SMichael Jones errno = 0;
348b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(base_sixteen_no_prefix, &str_end, 0), 123ll);
349b062d639SMichael Jones ASSERT_EQ(errno, 0);
3501d02a8bcSMichael Jones EXPECT_EQ(str_end - base_sixteen_no_prefix, ptrdiff_t(3));
351b062d639SMichael Jones
352b062d639SMichael Jones const char *base_sixteen_with_prefix = "0x456def";
353b062d639SMichael Jones errno = 0;
354b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(base_sixteen_with_prefix, &str_end, 0),
355b062d639SMichael Jones 0x456defll);
356b062d639SMichael Jones ASSERT_EQ(errno, 0);
3571d02a8bcSMichael Jones EXPECT_EQ(str_end - base_sixteen_with_prefix, ptrdiff_t(8));
358b062d639SMichael Jones
359b062d639SMichael Jones const char *base_eight_with_prefix = "012345";
360b062d639SMichael Jones errno = 0;
361b062d639SMichael Jones ASSERT_EQ(__llvm_libc::strtoll(base_eight_with_prefix, &str_end, 0),
362b062d639SMichael Jones 012345ll);
363b062d639SMichael Jones ASSERT_EQ(errno, 0);
3641d02a8bcSMichael Jones EXPECT_EQ(str_end - base_eight_with_prefix, ptrdiff_t(6));
365*53804d4eSMichael Jones
366*53804d4eSMichael Jones const char *just_zero = "0";
367*53804d4eSMichael Jones errno = 0;
368*53804d4eSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_zero, &str_end, 0), 0ll);
369*53804d4eSMichael Jones ASSERT_EQ(errno, 0);
370*53804d4eSMichael Jones EXPECT_EQ(str_end - just_zero, ptrdiff_t(1));
371*53804d4eSMichael Jones
372*53804d4eSMichael Jones const char *just_zero_x = "0x";
373*53804d4eSMichael Jones errno = 0;
374*53804d4eSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_zero_x, &str_end, 0), 0ll);
375*53804d4eSMichael Jones ASSERT_EQ(errno, 0);
376*53804d4eSMichael Jones EXPECT_EQ(str_end - just_zero_x, ptrdiff_t(1));
377*53804d4eSMichael Jones
378*53804d4eSMichael Jones const char *just_zero_eight = "08";
379*53804d4eSMichael Jones errno = 0;
380*53804d4eSMichael Jones ASSERT_EQ(__llvm_libc::strtoll(just_zero_eight, &str_end, 0), 0ll);
381*53804d4eSMichael Jones ASSERT_EQ(errno, 0);
382*53804d4eSMichael Jones EXPECT_EQ(str_end - just_zero_eight, ptrdiff_t(1));
383b062d639SMichael Jones }
384