[libc-commits] [libc] 722832e - [libc] Add strtoint32 and strtoint64 tests

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jun 1 15:06:52 PDT 2023


Author: Michael Jones
Date: 2023-06-01T15:06:46-07:00
New Revision: 722832e6d764748f8ee167c95370e043594ef995

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

LOG: [libc] Add strtoint32 and strtoint64 tests

There were regressions in the testing framework due to none of the
functioning buildbots having a 32 bit long. This allowed the 32 bit
version of the strtointeger function to go untested. This patch adds
tests for strtoint32 and strtoint64, which are internal testing
functions that use constant integer sizes. It also fixes the tests to
properly handle these situations.

Reviewed By: sivachandra

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

Added: 
    libc/test/src/stdlib/strtoint32_test.cpp
    libc/test/src/stdlib/strtoint64_test.cpp

Modified: 
    libc/test/src/stdlib/CMakeLists.txt
    libc/test/src/stdlib/StrtolTest.h
    libc/test/src/stdlib/atoi_test.cpp
    libc/test/src/stdlib/atol_test.cpp
    libc/test/src/stdlib/strtod_test.cpp
    libc/test/src/stdlib/strtof_test.cpp
    libc/test/src/stdlib/strtol_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index ad849af59050c..8a2107367ad7f 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -88,6 +88,30 @@ add_header_library(
     libc.src.errno.errno
 )
 
+add_libc_test(
+  strtoint32_test
+  SUITE
+    libc-stdlib-tests
+  SRCS
+    strtoint32_test.cpp
+  DEPENDS
+    libc.src.__support.str_to_integer
+    libc.src.errno.errno
+    .strtol_test_support
+)
+
+add_libc_test(
+  strtoint64_test
+  SUITE
+    libc-stdlib-tests
+  SRCS
+    strtoint64_test.cpp
+  DEPENDS
+    libc.src.__support.str_to_integer
+    libc.src.errno.errno
+    .strtol_test_support
+)
+
 add_libc_test(
   strtol_test
   SUITE

diff  --git a/libc/test/src/stdlib/StrtolTest.h b/libc/test/src/stdlib/StrtolTest.h
index 4b8e33e0563b9..0bc71615e4c3c 100644
--- a/libc/test/src/stdlib/StrtolTest.h
+++ b/libc/test/src/stdlib/StrtolTest.h
@@ -325,7 +325,8 @@ struct StrtoTest : public __llvm_libc::testing::Test {
               ((is_signed_v<ReturnT> && sizeof(ReturnT) == 4)
                    ? T_MAX
                    : ReturnT(0xFFFFFFFF)));
-    ASSERT_EQ(libc_errno, 0);
+    ASSERT_EQ(libc_errno,
+              is_signed_v<ReturnT> && sizeof(ReturnT) == 4 ? ERANGE : 0);
     EXPECT_EQ(str_end - max_32_bit_value, ptr
diff _t(10));
 
     const char *negative_max_32_bit_value = "-0xFFFFFFFF";
@@ -334,7 +335,8 @@ struct StrtoTest : public __llvm_libc::testing::Test {
               ((is_signed_v<ReturnT> && sizeof(ReturnT) == 4)
                    ? T_MIN
                    : -ReturnT(0xFFFFFFFF)));
-    ASSERT_EQ(libc_errno, 0);
+    ASSERT_EQ(libc_errno,
+              is_signed_v<ReturnT> && sizeof(ReturnT) == 4 ? ERANGE : 0);
     EXPECT_EQ(str_end - negative_max_32_bit_value, ptr
diff _t(11));
 
     // Max size for signed 32 bit numbers
@@ -357,30 +359,41 @@ struct StrtoTest : public __llvm_libc::testing::Test {
     const char *max_64_bit_value = "0xFFFFFFFFFFFFFFFF";
     libc_errno = 0;
     ASSERT_EQ(func(max_64_bit_value, &str_end, 0),
-              (is_signed_v<ReturnT> ? T_MAX : ReturnT(0xFFFFFFFFFFFFFFFF)));
-    ASSERT_EQ(libc_errno, (is_signed_v<ReturnT> ? ERANGE : 0));
+              (is_signed_v<ReturnT> || sizeof(ReturnT) < 8
+                   ? T_MAX
+                   : ReturnT(0xFFFFFFFFFFFFFFFF)));
+    ASSERT_EQ(libc_errno,
+              (is_signed_v<ReturnT> || sizeof(ReturnT) < 8 ? ERANGE : 0));
     EXPECT_EQ(str_end - max_64_bit_value, ptr
diff _t(18));
 
+    // See the end of CleanBase10Decode for an explanation of how this large
+    // negative number can end up as T_MAX.
     const char *negative_max_64_bit_value = "-0xFFFFFFFFFFFFFFFF";
     libc_errno = 0;
-    ASSERT_EQ(func(negative_max_64_bit_value, &str_end, 0),
-              (is_signed_v<ReturnT> ? T_MIN : -ReturnT(0xFFFFFFFFFFFFFFFF)));
-    ASSERT_EQ(libc_errno, (is_signed_v<ReturnT> ? ERANGE : 0));
+    ASSERT_EQ(
+        func(negative_max_64_bit_value, &str_end, 0),
+        (is_signed_v<ReturnT>
+             ? T_MIN
+             : (sizeof(ReturnT) < 8 ? T_MAX : -ReturnT(0xFFFFFFFFFFFFFFFF))));
+    ASSERT_EQ(libc_errno,
+              (is_signed_v<ReturnT> || sizeof(ReturnT) < 8 ? ERANGE : 0));
     EXPECT_EQ(str_end - negative_max_64_bit_value, ptr
diff _t(19));
 
     // Max size for signed 64 bit numbers
 
     const char *max_63_bit_value = "0x7FFFFFFFFFFFFFFF";
     libc_errno = 0;
-    ASSERT_EQ(func(max_63_bit_value, &str_end, 0), ReturnT(0x7FFFFFFFFFFFFFFF));
-    ASSERT_EQ(libc_errno, 0);
+    ASSERT_EQ(func(max_63_bit_value, &str_end, 0),
+              (sizeof(ReturnT) < 8 ? T_MAX : ReturnT(0x7FFFFFFFFFFFFFFF)));
+    ASSERT_EQ(libc_errno, sizeof(ReturnT) < 8 ? ERANGE : 0);
     EXPECT_EQ(str_end - max_63_bit_value, ptr
diff _t(18));
 
     const char *negative_max_63_bit_value = "-0x7FFFFFFFFFFFFFFF";
     libc_errno = 0;
     ASSERT_EQ(func(negative_max_63_bit_value, &str_end, 0),
-              -ReturnT(0x7FFFFFFFFFFFFFFF));
-    ASSERT_EQ(libc_errno, 0);
+              (sizeof(ReturnT) >= 8 ? -ReturnT(0x7FFFFFFFFFFFFFFF)
+                                    : (is_signed_v<ReturnT> ? T_MIN : T_MAX)));
+    ASSERT_EQ(libc_errno, sizeof(ReturnT) < 8 ? ERANGE : 0);
     EXPECT_EQ(str_end - negative_max_63_bit_value, ptr
diff _t(19));
   }
 

diff  --git a/libc/test/src/stdlib/atoi_test.cpp b/libc/test/src/stdlib/atoi_test.cpp
index 70c5b98733975..6fbde557e1f04 100644
--- a/libc/test/src/stdlib/atoi_test.cpp
+++ b/libc/test/src/stdlib/atoi_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for atoi -----------------------------------------------===//
+//===-- Unittests for atoi ------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

diff  --git a/libc/test/src/stdlib/atol_test.cpp b/libc/test/src/stdlib/atol_test.cpp
index 7b4dffd8d124d..ad1cbba946503 100644
--- a/libc/test/src/stdlib/atol_test.cpp
+++ b/libc/test/src/stdlib/atol_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for atol -----------------------------------------------===//
+//===-- Unittests for atol ------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

diff  --git a/libc/test/src/stdlib/strtod_test.cpp b/libc/test/src/stdlib/strtod_test.cpp
index b775c247f756e..70129ba0714d4 100644
--- a/libc/test/src/stdlib/strtod_test.cpp
+++ b/libc/test/src/stdlib/strtod_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for strtod ---------------------------------------------===//
+//===-- Unittests for strtod ----------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

diff  --git a/libc/test/src/stdlib/strtof_test.cpp b/libc/test/src/stdlib/strtof_test.cpp
index 45863f6ccea77..59bea795b93e0 100644
--- a/libc/test/src/stdlib/strtof_test.cpp
+++ b/libc/test/src/stdlib/strtof_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for strtof ---------------------------------------------===//
+//===-- Unittests for strtof ----------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

diff  --git a/libc/test/src/stdlib/strtoint32_test.cpp b/libc/test/src/stdlib/strtoint32_test.cpp
new file mode 100644
index 0000000000000..fa5e57145d24c
--- /dev/null
+++ b/libc/test/src/stdlib/strtoint32_test.cpp
@@ -0,0 +1,45 @@
+//===-- Unittests for strtoint32 ------------------------------------------===//
+//
+// 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 <stdint.h>
+
+#include "src/__support/str_to_integer.h"
+#include "src/errno/libc_errno.h"
+
+#include "StrtolTest.h"
+#include "test/UnitTest/Test.h"
+
+namespace __llvm_libc {
+
+int32_t strtoint32(const char *__restrict str, char **__restrict str_end,
+                   int base) {
+  auto result = internal::strtointeger<int32_t>(str, base);
+  if (result.has_error())
+    libc_errno = result.error;
+
+  if (str_end != nullptr)
+    *str_end = const_cast<char *>(str + result.parsed_len);
+
+  return result;
+}
+
+uint32_t strtouint32(const char *__restrict str, char **__restrict str_end,
+                     int base) {
+  auto result = internal::strtointeger<uint32_t>(str, base);
+  if (result.has_error())
+    libc_errno = result.error;
+
+  if (str_end != nullptr)
+    *str_end = const_cast<char *>(str + result.parsed_len);
+
+  return result;
+}
+} // namespace __llvm_libc
+
+STRTOL_TEST(Strtoint32, __llvm_libc::strtoint32)
+STRTOL_TEST(Strtouint32, __llvm_libc::strtouint32)

diff  --git a/libc/test/src/stdlib/strtoint64_test.cpp b/libc/test/src/stdlib/strtoint64_test.cpp
new file mode 100644
index 0000000000000..95e4761881f51
--- /dev/null
+++ b/libc/test/src/stdlib/strtoint64_test.cpp
@@ -0,0 +1,45 @@
+//===-- Unittests for strtoint64 ------------------------------------------===//
+//
+// 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 <stdint.h>
+
+#include "src/__support/str_to_integer.h"
+#include "src/errno/libc_errno.h"
+
+#include "StrtolTest.h"
+#include "test/UnitTest/Test.h"
+
+namespace __llvm_libc {
+
+int64_t strtoint64(const char *__restrict str, char **__restrict str_end,
+                   int base) {
+  auto result = internal::strtointeger<int64_t>(str, base);
+  if (result.has_error())
+    libc_errno = result.error;
+
+  if (str_end != nullptr)
+    *str_end = const_cast<char *>(str + result.parsed_len);
+
+  return result;
+}
+
+uint64_t strtouint64(const char *__restrict str, char **__restrict str_end,
+                     int base) {
+  auto result = internal::strtointeger<uint64_t>(str, base);
+  if (result.has_error())
+    libc_errno = result.error;
+
+  if (str_end != nullptr)
+    *str_end = const_cast<char *>(str + result.parsed_len);
+
+  return result;
+}
+} // namespace __llvm_libc
+
+STRTOL_TEST(Strtoint64, __llvm_libc::strtoint64)
+STRTOL_TEST(Strtouint64, __llvm_libc::strtouint64)

diff  --git a/libc/test/src/stdlib/strtol_test.cpp b/libc/test/src/stdlib/strtol_test.cpp
index f6cd120f92cf9..fb92ee6978d9b 100644
--- a/libc/test/src/stdlib/strtol_test.cpp
+++ b/libc/test/src/stdlib/strtol_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for strtol ---------------------------------------------===//
+//===-- Unittests for strtol ----------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.


        


More information about the libc-commits mailing list