-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This catches missing corner cases from atol tests. Differential Revision: https://reviews.llvm.org/D140076
- Loading branch information
Showing
6 changed files
with
115 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//===-- 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 "utils/UnitTest/Test.h" | ||
|
||
#include <limits.h> | ||
|
||
template <typename ReturnT> struct AtoTest : public __llvm_libc::testing::Test { | ||
using FunctionT = ReturnT (*)(const char *); | ||
|
||
void validNumbers(FunctionT func) { | ||
const char *zero = "0"; | ||
ASSERT_EQ(func(zero), static_cast<ReturnT>(0)); | ||
|
||
const char *ten = "10"; | ||
ASSERT_EQ(func(ten), static_cast<ReturnT>(10)); | ||
|
||
const char *negative_hundred = "-100"; | ||
ASSERT_EQ(func(negative_hundred), static_cast<ReturnT>(-100)); | ||
|
||
const char *positive_thousand = "+1000"; | ||
ASSERT_EQ(func(positive_thousand), static_cast<ReturnT>(1000)); | ||
|
||
const char *spaces_before = " 12345"; | ||
ASSERT_EQ(func(spaces_before), static_cast<ReturnT>(12345)); | ||
|
||
const char *tabs_before = "\t\t\t\t67890"; | ||
ASSERT_EQ(func(tabs_before), static_cast<ReturnT>(67890)); | ||
|
||
const char *letters_after = "123abc"; | ||
ASSERT_EQ(func(letters_after), static_cast<ReturnT>(123)); | ||
|
||
const char *letters_between = "456def789"; | ||
ASSERT_EQ(func(letters_between), static_cast<ReturnT>(456)); | ||
|
||
const char *all_together = "\t 110 times 5 = 550"; | ||
ASSERT_EQ(func(all_together), static_cast<ReturnT>(110)); | ||
|
||
const char *biggest_int = "2147483647"; | ||
ASSERT_EQ(func(biggest_int), static_cast<ReturnT>(INT_MAX)); | ||
|
||
const char *smallest_int = "-2147483648"; | ||
ASSERT_EQ(func(smallest_int), static_cast<ReturnT>(INT_MIN)); | ||
|
||
if constexpr (sizeof(ReturnT) >= 8) { | ||
const char *biggest_long_long = "9223372036854775807"; | ||
ASSERT_EQ(func(biggest_long_long), static_cast<ReturnT>(LLONG_MAX)); | ||
|
||
const char *smallest_long_long = "-9223372036854775808"; | ||
ASSERT_EQ(func(smallest_long_long), static_cast<ReturnT>(LLONG_MIN)); | ||
} | ||
} | ||
|
||
void nonBaseTenWholeNumbers(FunctionT func) { | ||
const char *hexadecimal = "0x10"; | ||
ASSERT_EQ(func(hexadecimal), static_cast<ReturnT>(0)); | ||
|
||
const char *octal = "010"; | ||
ASSERT_EQ(func(octal), static_cast<ReturnT>(10)); | ||
|
||
const char *decimal_point = "5.9"; | ||
ASSERT_EQ(func(decimal_point), static_cast<ReturnT>(5)); | ||
} | ||
|
||
void notNumbers(FunctionT func) { | ||
const char *ten_as_word = "ten"; | ||
ASSERT_EQ(func(ten_as_word), static_cast<ReturnT>(0)); | ||
|
||
const char *lots_of_letters = | ||
"wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk"; | ||
ASSERT_EQ(func(lots_of_letters), static_cast<ReturnT>(0)); | ||
} | ||
}; | ||
|
||
template <typename ReturnType> | ||
AtoTest(ReturnType (*)(const char *)) -> AtoTest<ReturnType>; | ||
|
||
#define ATOI_TEST(name, func) \ | ||
using LlvmLibc##name##Test = AtoTest<decltype(func(""))>; \ | ||
TEST_F(LlvmLibc##name##Test, ValidNumbers) { validNumbers(func); } \ | ||
TEST_F(LlvmLibc##name##Test, NonBaseTenWholeNumbers) { \ | ||
nonBaseTenWholeNumbers(func); \ | ||
} \ | ||
TEST_F(LlvmLibc##name##Test, NotNumbers) { notNumbers(func); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters