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