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