1*bad3168bSMichael Jones //===-- Unittests for atoi -----------------------------------------------===//
2*bad3168bSMichael Jones //
3*bad3168bSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bad3168bSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5*bad3168bSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bad3168bSMichael Jones //
7*bad3168bSMichael Jones //===----------------------------------------------------------------------===//
8*bad3168bSMichael Jones
9*bad3168bSMichael Jones #include "src/stdlib/atoi.h"
10*bad3168bSMichael Jones
11*bad3168bSMichael Jones #include "utils/UnitTest/Test.h"
12*bad3168bSMichael Jones
13*bad3168bSMichael Jones #include <limits.h>
14*bad3168bSMichael Jones
TEST(LlvmLibcAToITest,ValidNumbers)15*bad3168bSMichael Jones TEST(LlvmLibcAToITest, ValidNumbers) {
16*bad3168bSMichael Jones const char *zero = "0";
17*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(zero), 0);
18*bad3168bSMichael Jones
19*bad3168bSMichael Jones const char *ten = "10";
20*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(ten), 10);
21*bad3168bSMichael Jones
22*bad3168bSMichael Jones const char *negative_hundred = "-100";
23*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(negative_hundred), -100);
24*bad3168bSMichael Jones
25*bad3168bSMichael Jones const char *positive_thousand = "+1000";
26*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(positive_thousand), 1000);
27*bad3168bSMichael Jones
28*bad3168bSMichael Jones const char *spaces_before = " 12345";
29*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(spaces_before), 12345);
30*bad3168bSMichael Jones
31*bad3168bSMichael Jones const char *tabs_before = "\t\t\t\t67890";
32*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(tabs_before), 67890);
33*bad3168bSMichael Jones
34*bad3168bSMichael Jones const char *letters_after = "123abc";
35*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(letters_after), 123);
36*bad3168bSMichael Jones
37*bad3168bSMichael Jones const char *letters_between = "456def789";
38*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(letters_between), 456);
39*bad3168bSMichael Jones
40*bad3168bSMichael Jones const char *all_together = "\t 110 times 5 = 550";
41*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(all_together), 110);
42*bad3168bSMichael Jones
43*bad3168bSMichael Jones const char *biggest_int = "2147483647";
44*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(biggest_int), INT_MAX);
45*bad3168bSMichael Jones
46*bad3168bSMichael Jones const char *smallest_int = "-2147483648";
47*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(smallest_int), INT_MIN);
48*bad3168bSMichael Jones }
49*bad3168bSMichael Jones
TEST(LlvmLibcAToITest,NonBaseTenWholeNumbers)50*bad3168bSMichael Jones TEST(LlvmLibcAToITest, NonBaseTenWholeNumbers) {
51*bad3168bSMichael Jones const char *hexadecimal = "0x10";
52*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(hexadecimal), 0);
53*bad3168bSMichael Jones
54*bad3168bSMichael Jones const char *octal = "010";
55*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(octal), 10);
56*bad3168bSMichael Jones
57*bad3168bSMichael Jones const char *decimal_point = "5.9";
58*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(decimal_point), 5);
59*bad3168bSMichael Jones }
60*bad3168bSMichael Jones
TEST(LlvmLibcAToITest,NotNumbers)61*bad3168bSMichael Jones TEST(LlvmLibcAToITest, NotNumbers) {
62*bad3168bSMichael Jones const char *ten_as_word = "ten";
63*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(ten_as_word), 0);
64*bad3168bSMichael Jones
65*bad3168bSMichael Jones const char *lots_of_letters =
66*bad3168bSMichael Jones "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk";
67*bad3168bSMichael Jones ASSERT_EQ(__llvm_libc::atoi(lots_of_letters), 0);
68*bad3168bSMichael Jones }
69