[libc-commits] [libc] d07c54c - [libc] Templatize ato* tests

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Thu Dec 15 09:31:50 PST 2022


Author: Alex Brachet
Date: 2022-12-15T17:30:00Z
New Revision: d07c54c2a5d670293c507ce8822f629eed5eb372

URL: https://github.com/llvm/llvm-project/commit/d07c54c2a5d670293c507ce8822f629eed5eb372
DIFF: https://github.com/llvm/llvm-project/commit/d07c54c2a5d670293c507ce8822f629eed5eb372.diff

LOG: [libc] Templatize ato* tests

This catches missing corner cases from atol tests.

Differential Revision: https://reviews.llvm.org/D140076

Added: 
    libc/test/src/stdlib/AtoiTest.h

Modified: 
    libc/test/src/stdlib/CMakeLists.txt
    libc/test/src/stdlib/atoi_test.cpp
    libc/test/src/stdlib/atol_test.cpp
    libc/test/src/stdlib/atoll_test.cpp
    utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/test/src/stdlib/AtoiTest.h b/libc/test/src/stdlib/AtoiTest.h
new file mode 100644
index 0000000000000..4013800f42490
--- /dev/null
+++ b/libc/test/src/stdlib/AtoiTest.h
@@ -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); }

diff  --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index c05bb94d5043f..2f6f62bcc7eb3 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -10,6 +10,12 @@ add_libc_unittest(
     libc.src.stdlib.atof
 )
 
+add_header_library(
+  atoi_test_support
+  HDRS
+    AtoiTest.h
+)
+
 add_libc_unittest(
   atoi_test
   SUITE
@@ -17,6 +23,7 @@ add_libc_unittest(
   SRCS
     atoi_test.cpp
   DEPENDS
+    .atoi_test_support
     libc.src.stdlib.atoi
 )
 
@@ -27,6 +34,7 @@ add_libc_unittest(
   SRCS
     atol_test.cpp
   DEPENDS
+    .atoi_test_support
     libc.src.stdlib.atol
 )
 
@@ -37,6 +45,7 @@ add_libc_unittest(
   SRCS
     atoll_test.cpp
   DEPENDS
+    .atoi_test_support
     libc.src.stdlib.atoll
 )
 

diff  --git a/libc/test/src/stdlib/atoi_test.cpp b/libc/test/src/stdlib/atoi_test.cpp
index 892f93ac1b28b..7e605e479cc0f 100644
--- a/libc/test/src/stdlib/atoi_test.cpp
+++ b/libc/test/src/stdlib/atoi_test.cpp
@@ -6,63 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "AtoiTest.h"
+
 #include "src/stdlib/atoi.h"
 
 #include "utils/UnitTest/Test.h"
 
-#include <limits.h>
-
-TEST(LlvmLibcAToITest, ValidNumbers) {
-  const char *zero = "0";
-  ASSERT_EQ(__llvm_libc::atoi(zero), 0);
-
-  const char *ten = "10";
-  ASSERT_EQ(__llvm_libc::atoi(ten), 10);
-
-  const char *negative_hundred = "-100";
-  ASSERT_EQ(__llvm_libc::atoi(negative_hundred), -100);
-
-  const char *positive_thousand = "+1000";
-  ASSERT_EQ(__llvm_libc::atoi(positive_thousand), 1000);
-
-  const char *spaces_before = "     12345";
-  ASSERT_EQ(__llvm_libc::atoi(spaces_before), 12345);
-
-  const char *tabs_before = "\t\t\t\t67890";
-  ASSERT_EQ(__llvm_libc::atoi(tabs_before), 67890);
-
-  const char *letters_after = "123abc";
-  ASSERT_EQ(__llvm_libc::atoi(letters_after), 123);
-
-  const char *letters_between = "456def789";
-  ASSERT_EQ(__llvm_libc::atoi(letters_between), 456);
-
-  const char *all_together = "\t   110 times 5 = 550";
-  ASSERT_EQ(__llvm_libc::atoi(all_together), 110);
-
-  const char *biggest_int = "2147483647";
-  ASSERT_EQ(__llvm_libc::atoi(biggest_int), INT_MAX);
-
-  const char *smallest_int = "-2147483648";
-  ASSERT_EQ(__llvm_libc::atoi(smallest_int), INT_MIN);
-}
-
-TEST(LlvmLibcAToITest, NonBaseTenWholeNumbers) {
-  const char *hexadecimal = "0x10";
-  ASSERT_EQ(__llvm_libc::atoi(hexadecimal), 0);
-
-  const char *octal = "010";
-  ASSERT_EQ(__llvm_libc::atoi(octal), 10);
-
-  const char *decimal_point = "5.9";
-  ASSERT_EQ(__llvm_libc::atoi(decimal_point), 5);
-}
-
-TEST(LlvmLibcAToITest, NotNumbers) {
-  const char *ten_as_word = "ten";
-  ASSERT_EQ(__llvm_libc::atoi(ten_as_word), 0);
-
-  const char *lots_of_letters =
-      "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk";
-  ASSERT_EQ(__llvm_libc::atoi(lots_of_letters), 0);
-}
+ATOI_TEST(Atoi, __llvm_libc::atoi)

diff  --git a/libc/test/src/stdlib/atol_test.cpp b/libc/test/src/stdlib/atol_test.cpp
index 7290ef1191cbf..5c1f22b8dab49 100644
--- a/libc/test/src/stdlib/atol_test.cpp
+++ b/libc/test/src/stdlib/atol_test.cpp
@@ -6,57 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "AtoiTest.h"
+
 #include "src/stdlib/atol.h"
 
 #include "utils/UnitTest/Test.h"
 
-#include <limits.h>
-
-TEST(LlvmLibcAToLTest, ValidNumbers) {
-  const char *zero = "0";
-  ASSERT_EQ(__llvm_libc::atol(zero), 0l);
-
-  const char *ten = "10";
-  ASSERT_EQ(__llvm_libc::atol(ten), 10l);
-
-  const char *negative_hundred = "-100";
-  ASSERT_EQ(__llvm_libc::atol(negative_hundred), -100l);
-
-  const char *positive_thousand = "+1000";
-  ASSERT_EQ(__llvm_libc::atol(positive_thousand), 1000l);
-
-  const char *spaces_before = "     12345";
-  ASSERT_EQ(__llvm_libc::atol(spaces_before), 12345l);
-
-  const char *tabs_before = "\t\t\t\t67890";
-  ASSERT_EQ(__llvm_libc::atol(tabs_before), 67890l);
-
-  const char *letters_after = "123abc";
-  ASSERT_EQ(__llvm_libc::atol(letters_after), 123l);
-
-  const char *letters_between = "456def789";
-  ASSERT_EQ(__llvm_libc::atol(letters_between), 456l);
-
-  const char *all_together = "\t   110 times 5 = 550";
-  ASSERT_EQ(__llvm_libc::atol(all_together), 110l);
-}
-
-TEST(LlvmLibcAToLTest, NonBaseTenWholeNumbers) {
-  const char *hexadecimal = "0x10";
-  ASSERT_EQ(__llvm_libc::atol(hexadecimal), 0l);
-
-  const char *octal = "010";
-  ASSERT_EQ(__llvm_libc::atol(octal), 10l);
-
-  const char *decimal_point = "5.9";
-  ASSERT_EQ(__llvm_libc::atol(decimal_point), 5l);
-}
-
-TEST(LlvmLibcAToLTest, NotNumbers) {
-  const char *ten_as_word = "ten";
-  ASSERT_EQ(__llvm_libc::atol(ten_as_word), 0l);
-
-  const char *lots_of_letters =
-      "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk";
-  ASSERT_EQ(__llvm_libc::atol(lots_of_letters), 0l);
-}
+ATOI_TEST(Atol, __llvm_libc::atol)

diff  --git a/libc/test/src/stdlib/atoll_test.cpp b/libc/test/src/stdlib/atoll_test.cpp
index 74c86211303cc..c600058830e8a 100644
--- a/libc/test/src/stdlib/atoll_test.cpp
+++ b/libc/test/src/stdlib/atoll_test.cpp
@@ -6,63 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "AtoiTest.h"
+
 #include "src/stdlib/atoll.h"
 
 #include "utils/UnitTest/Test.h"
 
-#include <limits.h>
-
-TEST(LlvmLibcAToLLTest, ValidNumbers) {
-  const char *zero = "0";
-  ASSERT_EQ(__llvm_libc::atoll(zero), 0ll);
-
-  const char *ten = "10";
-  ASSERT_EQ(__llvm_libc::atoll(ten), 10ll);
-
-  const char *negative_hundred = "-100";
-  ASSERT_EQ(__llvm_libc::atoll(negative_hundred), -100ll);
-
-  const char *positive_thousand = "+1000";
-  ASSERT_EQ(__llvm_libc::atoll(positive_thousand), 1000ll);
-
-  const char *spaces_before = "     12345";
-  ASSERT_EQ(__llvm_libc::atoll(spaces_before), 12345ll);
-
-  const char *tabs_before = "\t\t\t\t67890";
-  ASSERT_EQ(__llvm_libc::atoll(tabs_before), 67890ll);
-
-  const char *letters_after = "123abc";
-  ASSERT_EQ(__llvm_libc::atoll(letters_after), 123ll);
-
-  const char *letters_between = "456def789";
-  ASSERT_EQ(__llvm_libc::atoll(letters_between), 456ll);
-
-  const char *all_together = "\t   110 times 5 = 550";
-  ASSERT_EQ(__llvm_libc::atoll(all_together), 110ll);
-
-  const char *biggest_long_long = "9223372036854775807";
-  ASSERT_EQ(__llvm_libc::atoll(biggest_long_long), LLONG_MAX);
-
-  const char *smallest_long_long = "-9223372036854775808";
-  ASSERT_EQ(__llvm_libc::atoll(smallest_long_long), LLONG_MIN);
-}
-
-TEST(LlvmLibcAToLLTest, NonBaseTenWholeNumbers) {
-  const char *hexadecimal = "0x10";
-  ASSERT_EQ(__llvm_libc::atoll(hexadecimal), 0ll);
-
-  const char *octal = "010";
-  ASSERT_EQ(__llvm_libc::atoll(octal), 10ll);
-
-  const char *decimal_point = "5.9";
-  ASSERT_EQ(__llvm_libc::atoll(decimal_point), 5ll);
-}
-
-TEST(LlvmLibcAToLLTest, NotNumbers) {
-  const char *ten_as_word = "ten";
-  ASSERT_EQ(__llvm_libc::atoll(ten_as_word), 0ll);
-
-  const char *lots_of_letters =
-      "wtragsdhfgjykutjdyfhgnchgmjhkyurktfgjhlu;po7urtdjyfhgklyk";
-  ASSERT_EQ(__llvm_libc::atoll(lots_of_letters), 0ll);
-}
+ATOI_TEST(Atoll, __llvm_libc::atoll)

diff  --git a/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
index bc353e9b1df96..e125b85435134 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
@@ -10,9 +10,15 @@ package(default_visibility = ["//visibility:public"])
 
 licenses(["notice"])
 
+cc_library(
+    name = "atoi_test_helper",
+    hdrs = ["AtoiTest.h"],
+)
+
 libc_test(
     name = "atoi_test",
     srcs = ["atoi_test.cpp"],
+    deps = [":atoi_test_helper"],
     libc_function_deps = [
         "//libc:atoi",
     ],
@@ -21,6 +27,7 @@ libc_test(
 libc_test(
     name = "atol_test",
     srcs = ["atol_test.cpp"],
+    deps = [":atoi_test_helper"],
     libc_function_deps = [
         "//libc:atol",
     ],
@@ -29,6 +36,7 @@ libc_test(
 libc_test(
     name = "atoll_test",
     srcs = ["atoll_test.cpp"],
+    deps = [":atoi_test_helper"],
     libc_function_deps = [
         "//libc:atoll",
     ],


        


More information about the libc-commits mailing list