Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / stdlib / AtoiTest.h
blob85701828c0f6b7cc645b65ad6868c4ebbc6cdad0
1 //===-- A template class for testing ato* functions -------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/type_traits.h"
10 #include "test/UnitTest/Test.h"
12 #include <limits.h>
14 using LIBC_NAMESPACE::cpp::is_same_v;
16 template <typename ReturnT>
17 struct AtoTest : public LIBC_NAMESPACE::testing::Test {
18 using FunctionT = ReturnT (*)(const char *);
20 void validNumbers(FunctionT func) {
21 const char *zero = "0";
22 ASSERT_EQ(func(zero), static_cast<ReturnT>(0));
24 const char *ten = "10";
25 ASSERT_EQ(func(ten), static_cast<ReturnT>(10));
27 const char *negative_hundred = "-100";
28 ASSERT_EQ(func(negative_hundred), static_cast<ReturnT>(-100));
30 const char *positive_thousand = "+1000";
31 ASSERT_EQ(func(positive_thousand), static_cast<ReturnT>(1000));
33 const char *spaces_before = " 12345";
34 ASSERT_EQ(func(spaces_before), static_cast<ReturnT>(12345));
36 const char *tabs_before = "\t\t\t\t67890";
37 ASSERT_EQ(func(tabs_before), static_cast<ReturnT>(67890));
39 const char *letters_after = "123abc";
40 ASSERT_EQ(func(letters_after), static_cast<ReturnT>(123));
42 const char *letters_between = "456def789";
43 ASSERT_EQ(func(letters_between), static_cast<ReturnT>(456));
45 const char *all_together = "\t 110 times 5 = 550";
46 ASSERT_EQ(func(all_together), static_cast<ReturnT>(110));
48 const char *biggest_int = "2147483647";
49 ASSERT_EQ(func(biggest_int), static_cast<ReturnT>(INT_MAX));
51 const char *smallest_int = "-2147483648";
52 ASSERT_EQ(func(smallest_int), static_cast<ReturnT>(INT_MIN));
54 if constexpr (sizeof(ReturnT) >= 8) {
55 const char *biggest_long_long = "9223372036854775807";
56 ASSERT_EQ(func(biggest_long_long), static_cast<ReturnT>(LLONG_MAX));
58 const char *smallest_long_long = "-9223372036854775808";
59 ASSERT_EQ(func(smallest_long_long), static_cast<ReturnT>(LLONG_MIN));
62 // If this is atoi and the size of int is less than the size of long, then
63 // we parse as long and cast to int to match existing behavior. This only
64 // matters for cases where the result would be outside of the int range, and
65 // those cases are undefined, so we can choose whatever output value we
66 // want. In this case we have chosen to cast since that matches existing
67 // implementations and makes differential fuzzing easier, but no user should
68 // rely on this behavior.
69 if constexpr (is_same_v<ReturnT, int> && sizeof(ReturnT) < sizeof(long)) {
71 static_assert(sizeof(int) == 4);
73 const char *bigger_than_biggest_int = "2147483649";
74 ASSERT_EQ(func(bigger_than_biggest_int),
75 static_cast<ReturnT>(2147483649));
77 const char *smaller_than_smallest_int = "-2147483649";
78 ASSERT_EQ(func(smaller_than_smallest_int),
79 static_cast<ReturnT>(-2147483649));
83 void nonBaseTenWholeNumbers(FunctionT func) {
84 const char *hexadecimal = "0x10";
85 ASSERT_EQ(func(hexadecimal), static_cast<ReturnT>(0));
87 const char *octal = "010";
88 ASSERT_EQ(func(octal), static_cast<ReturnT>(10));
90 const char *decimal_point = "5.9";
91 ASSERT_EQ(func(decimal_point), static_cast<ReturnT>(5));
94 void notNumbers(FunctionT func) {
95 const char *ten_as_word = "ten";
96 ASSERT_EQ(func(ten_as_word), static_cast<ReturnT>(0));
98 const char *lots_of_letters =
99 "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk";
100 ASSERT_EQ(func(lots_of_letters), static_cast<ReturnT>(0));
104 template <typename ReturnType>
105 AtoTest(ReturnType (*)(const char *)) -> AtoTest<ReturnType>;
107 #define ATOI_TEST(name, func) \
108 using LlvmLibc##name##Test = AtoTest<decltype(func(""))>; \
109 TEST_F(LlvmLibc##name##Test, ValidNumbers) { validNumbers(func); } \
110 TEST_F(LlvmLibc##name##Test, NonBaseTenWholeNumbers) { \
111 nonBaseTenWholeNumbers(func); \
113 TEST_F(LlvmLibc##name##Test, NotNumbers) { notNumbers(func); }