//===-- A template class for testing ato* functions -------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN, LLONG_MAX, LLONG_MIN #include "src/__support/CPP/type_traits.h" #include "test/UnitTest/Test.h" using LIBC_NAMESPACE::cpp::is_same_v; template struct AtoTest : public LIBC_NAMESPACE::testing::Test { using FunctionT = ReturnT (*)(const char *); void validNumbers(FunctionT func) { const char *zero = "0"; ASSERT_EQ(func(zero), static_cast(0)); const char *ten = "10"; ASSERT_EQ(func(ten), static_cast(10)); const char *negative_hundred = "-100"; ASSERT_EQ(func(negative_hundred), static_cast(-100)); const char *positive_thousand = "+1000"; ASSERT_EQ(func(positive_thousand), static_cast(1000)); const char *spaces_before = " 12345"; ASSERT_EQ(func(spaces_before), static_cast(12345)); const char *tabs_before = "\t\t\t\t67890"; ASSERT_EQ(func(tabs_before), static_cast(67890)); const char *letters_after = "123abc"; ASSERT_EQ(func(letters_after), static_cast(123)); const char *letters_between = "456def789"; ASSERT_EQ(func(letters_between), static_cast(456)); const char *all_together = "\t 110 times 5 = 550"; ASSERT_EQ(func(all_together), static_cast(110)); const char *biggest_int = "2147483647"; ASSERT_EQ(func(biggest_int), static_cast(INT_MAX)); const char *smallest_int = "-2147483648"; ASSERT_EQ(func(smallest_int), static_cast(INT_MIN)); if constexpr (sizeof(ReturnT) >= 8) { const char *biggest_long_long = "9223372036854775807"; ASSERT_EQ(func(biggest_long_long), static_cast(LLONG_MAX)); const char *smallest_long_long = "-9223372036854775808"; ASSERT_EQ(func(smallest_long_long), static_cast(LLONG_MIN)); } // If this is atoi and the size of int is less than the size of long, then // we parse as long and cast to int to match existing behavior. This only // matters for cases where the result would be outside of the int range, and // those cases are undefined, so we can choose whatever output value we // want. In this case we have chosen to cast since that matches existing // implementations and makes differential fuzzing easier, but no user should // rely on this behavior. if constexpr (is_same_v && sizeof(ReturnT) < sizeof(long)) { static_assert(sizeof(int) == 4); const char *bigger_than_biggest_int = "2147483649"; ASSERT_EQ(func(bigger_than_biggest_int), static_cast(2147483649)); const char *smaller_than_smallest_int = "-2147483649"; ASSERT_EQ(func(smaller_than_smallest_int), static_cast(-2147483649)); } } void nonBaseTenWholeNumbers(FunctionT func) { const char *hexadecimal = "0x10"; ASSERT_EQ(func(hexadecimal), static_cast(0)); const char *octal = "010"; ASSERT_EQ(func(octal), static_cast(10)); const char *decimal_point = "5.9"; ASSERT_EQ(func(decimal_point), static_cast(5)); } void notNumbers(FunctionT func) { const char *ten_as_word = "ten"; ASSERT_EQ(func(ten_as_word), static_cast(0)); const char *lots_of_letters = "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk"; ASSERT_EQ(func(lots_of_letters), static_cast(0)); } }; template AtoTest(ReturnType (*)(const char *)) -> AtoTest; #define ATOI_TEST(name, func) \ using LlvmLibc##name##Test = AtoTest; \ TEST_F(LlvmLibc##name##Test, ValidNumbers) { validNumbers(func); } \ TEST_F(LlvmLibc##name##Test, NonBaseTenWholeNumbers) { \ nonBaseTenWholeNumbers(func); \ } \ TEST_F(LlvmLibc##name##Test, NotNumbers) { notNumbers(func); }