1 //===-- Unittests for atoi -----------------------------------------------===//
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/atoi.h"
10 
11 #include "utils/UnitTest/Test.h"
12 
13 #include <limits.h>
14 
TEST(LlvmLibcAToITest,ValidNumbers)15 TEST(LlvmLibcAToITest, ValidNumbers) {
16   const char *zero = "0";
17   ASSERT_EQ(__llvm_libc::atoi(zero), 0);
18 
19   const char *ten = "10";
20   ASSERT_EQ(__llvm_libc::atoi(ten), 10);
21 
22   const char *negative_hundred = "-100";
23   ASSERT_EQ(__llvm_libc::atoi(negative_hundred), -100);
24 
25   const char *positive_thousand = "+1000";
26   ASSERT_EQ(__llvm_libc::atoi(positive_thousand), 1000);
27 
28   const char *spaces_before = "     12345";
29   ASSERT_EQ(__llvm_libc::atoi(spaces_before), 12345);
30 
31   const char *tabs_before = "\t\t\t\t67890";
32   ASSERT_EQ(__llvm_libc::atoi(tabs_before), 67890);
33 
34   const char *letters_after = "123abc";
35   ASSERT_EQ(__llvm_libc::atoi(letters_after), 123);
36 
37   const char *letters_between = "456def789";
38   ASSERT_EQ(__llvm_libc::atoi(letters_between), 456);
39 
40   const char *all_together = "\t   110 times 5 = 550";
41   ASSERT_EQ(__llvm_libc::atoi(all_together), 110);
42 
43   const char *biggest_int = "2147483647";
44   ASSERT_EQ(__llvm_libc::atoi(biggest_int), INT_MAX);
45 
46   const char *smallest_int = "-2147483648";
47   ASSERT_EQ(__llvm_libc::atoi(smallest_int), INT_MIN);
48 }
49 
TEST(LlvmLibcAToITest,NonBaseTenWholeNumbers)50 TEST(LlvmLibcAToITest, NonBaseTenWholeNumbers) {
51   const char *hexadecimal = "0x10";
52   ASSERT_EQ(__llvm_libc::atoi(hexadecimal), 0);
53 
54   const char *octal = "010";
55   ASSERT_EQ(__llvm_libc::atoi(octal), 10);
56 
57   const char *decimal_point = "5.9";
58   ASSERT_EQ(__llvm_libc::atoi(decimal_point), 5);
59 }
60 
TEST(LlvmLibcAToITest,NotNumbers)61 TEST(LlvmLibcAToITest, NotNumbers) {
62   const char *ten_as_word = "ten";
63   ASSERT_EQ(__llvm_libc::atoi(ten_as_word), 0);
64 
65   const char *lots_of_letters =
66       "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk";
67   ASSERT_EQ(__llvm_libc::atoi(lots_of_letters), 0);
68 }
69