[libc-commits] [libc] [libc][test] Fix GoogleTest incompatibilities in unit tests (PR #239961)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 04:35:18 PST 2026


https://github.com/frobtech created https://github.com/llvm/llvm-project/pull/239961

llvm-libc unit tests are intended to target a source-compatible subset
of GoogleTest, dispatched through test/UnitTest/Test.h. Building and
running the unit test suite against real GoogleTest uncovered several
latent issues that LibcTest either did not exercise or silently
accepted:

- Several Linux sys/ tests included test/UnitTest/LibcTest.h directly
  instead of routing through test/UnitTest/Test.h.
- rename_test.cpp included the raw Linux generated macro header
  include/llvm-libc-macros/linux/unistd-macros.h directly, colliding
  with system <unistd.h> enum constants when system headers are also
  included; use the hdr/fcntl_macros.h and hdr/unistd_macros.h proxy
  headers instead.
- src/ctype/toascii.h did not #undef a pre-existing XSI toascii macro
  from system <ctype.h>, unlike src/ctype/isascii.h.
- Five test suites mixed TEST_F and TEST under the same suite name,
  which GoogleTest rejects at runtime.
- DivTest.h aliased LlvmLibcDivTest to distinct DivTest<IntType,
  ReturnType> template instantiations across div_test.cpp,
  ldiv_test.cpp, lldiv_test.cpp, and imaxdiv_test.cpp, which GoogleTest
  rejects when those translation units are linked into a single test
  executable; template the helper method instead of the fixture class.
- strsep_test.cpp asserted EXPECT_STREQ(..., nullptr) when calling
  strsep on an empty non-null string with empty delimiters. Per BSD
  strsep semantics, strsep sets *stringp to nullptr and returns the
  original pointer to "", matching the non-empty cases immediately
  below it; LibcTest's string_view wrapper previously masked this by
  conflating nullptr with "".

Part of #91630

Assisted-by: Gemini

>From 2b8cb95cd1d1536e6cb0c953ccbd0a77af9cddcd Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Sat, 7 Nov 2026 00:38:32 -0800
Subject: [PATCH] [libc][test] Fix GoogleTest incompatibilities in unit tests

llvm-libc unit tests are intended to target a source-compatible subset
of GoogleTest, dispatched through test/UnitTest/Test.h. Building and
running the unit test suite against real GoogleTest uncovered several
latent issues that LibcTest either did not exercise or silently
accepted:

- Several Linux sys/ tests included test/UnitTest/LibcTest.h directly
  instead of routing through test/UnitTest/Test.h.
- rename_test.cpp included the raw Linux generated macro header
  include/llvm-libc-macros/linux/unistd-macros.h directly, colliding
  with system <unistd.h> enum constants when system headers are also
  included; use the hdr/fcntl_macros.h and hdr/unistd_macros.h proxy
  headers instead.
- src/ctype/toascii.h did not #undef a pre-existing XSI toascii macro
  from system <ctype.h>, unlike src/ctype/isascii.h.
- Five test suites mixed TEST_F and TEST under the same suite name,
  which GoogleTest rejects at runtime.
- DivTest.h aliased LlvmLibcDivTest to distinct DivTest<IntType,
  ReturnType> template instantiations across div_test.cpp,
  ldiv_test.cpp, lldiv_test.cpp, and imaxdiv_test.cpp, which GoogleTest
  rejects when those translation units are linked into a single test
  executable; template the helper method instead of the fixture class.
- strsep_test.cpp asserted EXPECT_STREQ(..., nullptr) when calling
  strsep on an empty non-null string with empty delimiters. Per BSD
  strsep semantics, strsep sets *stringp to nullptr and returns the
  original pointer to "", matching the non-empty cases immediately
  below it; LibcTest's string_view wrapper previously masked this by
  conflating nullptr with "".

Part of #91630

Assisted-by: Gemini
---
 libc/src/ctype/toascii.h                            |  1 +
 libc/test/src/__support/str_to_double_test.cpp      |  2 +-
 libc/test/src/__support/str_to_float_test.cpp       |  2 +-
 libc/test/src/stdio/CMakeLists.txt                  |  2 ++
 libc/test/src/stdio/remove_test.cpp                 |  2 +-
 libc/test/src/stdio/rename_test.cpp                 |  3 ++-
 libc/test/src/stdlib/DivTest.h                      | 13 ++++++-------
 libc/test/src/string/strsep_test.cpp                |  4 ++--
 libc/test/src/sys/mman/linux/pkey_test.cpp          |  2 +-
 .../src/sys/socket/linux/sendmsg_recvmsg_test.cpp   |  1 -
 .../test/src/sys/socket/linux/sendrecvmmsg_test.cpp |  1 -
 .../src/sys/socket/linux/sockaddr_storage_test.cpp  |  2 +-
 .../test/src/sys/socket/linux/socket_test_support.h |  2 +-
 libc/test/src/sys/xattr/lxattr_test.cpp             |  2 +-
 libc/test/src/sys/xattr/xattr_test.cpp              |  2 +-
 15 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/libc/src/ctype/toascii.h b/libc/src/ctype/toascii.h
index d41e58521ee70a..131832edb6ae31 100644
--- a/libc/src/ctype/toascii.h
+++ b/libc/src/ctype/toascii.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC_CTYPE_TOASCII_H
 
 #include "src/__support/macros/config.h"
+#undef toascii
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/test/src/__support/str_to_double_test.cpp b/libc/test/src/__support/str_to_double_test.cpp
index be9aab62812070..0a5effe65167d0 100644
--- a/libc/test/src/__support/str_to_double_test.cpp
+++ b/libc/test/src/__support/str_to_double_test.cpp
@@ -120,7 +120,7 @@ TEST_F(LlvmLibcStrToDblTest, SimpleDecimalConversion64SubnormalRounding) {
                                  1);
 }
 
-TEST(LlvmLibcStrToDblTest, SimpleDecimalConversionExtraTypes) {
+TEST_F(LlvmLibcStrToDblTest, SimpleDecimalConversionExtraTypes) {
   uint64_t double_output_mantissa = 0;
   uint32_t output_exp2 = 0;
 
diff --git a/libc/test/src/__support/str_to_float_test.cpp b/libc/test/src/__support/str_to_float_test.cpp
index 7a52fb6a81d1ab..8205cc029a019c 100644
--- a/libc/test/src/__support/str_to_float_test.cpp
+++ b/libc/test/src/__support/str_to_float_test.cpp
@@ -65,7 +65,7 @@ TEST_F(LlvmLibcStrToFltTest, SimpleDecimalConversion32HugeExponents) {
   simple_decimal_conversion_test("1e-2147483647", 0, 0, ERANGE);
 }
 
-TEST(LlvmLibcStrToFltTest, SimpleDecimalConversionExtraTypes) {
+TEST_F(LlvmLibcStrToFltTest, SimpleDecimalConversionExtraTypes) {
   uint32_t float_output_mantissa = 0;
   uint32_t output_exp2 = 0;
 
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index c1c3bcd776c3a2..3109d436732bc1 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -545,7 +545,9 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
     SRCS
       rename_test.cpp
     DEPENDS
+      libc.hdr.fcntl_macros
       libc.hdr.sys_stat_macros
+      libc.hdr.unistd_macros
       libc.src.fcntl.open
       libc.src.stdio.rename
       libc.src.unistd.access
diff --git a/libc/test/src/stdio/remove_test.cpp b/libc/test/src/stdio/remove_test.cpp
index 46863e43f32f04..a9ac22a251a5d7 100644
--- a/libc/test/src/stdio/remove_test.cpp
+++ b/libc/test/src/stdio/remove_test.cpp
@@ -53,7 +53,7 @@ TEST_F(LlvmLibcRemoveTest, CreateAndRemoveDir) {
   ASSERT_THAT(LIBC_NAMESPACE::access(TEST_DIR, F_OK), Fails(ENOENT));
 }
 
-TEST(LlvmLibcRemoveTest, RemoveNonExistent) {
+TEST_F(LlvmLibcRemoveTest, RemoveNonExistent) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
   ASSERT_THAT(LIBC_NAMESPACE::remove("non-existent"), Fails(ENOENT));
 }
diff --git a/libc/test/src/stdio/rename_test.cpp b/libc/test/src/stdio/rename_test.cpp
index 2ad01b9d5aa646..93deeae3e97445 100644
--- a/libc/test/src/stdio/rename_test.cpp
+++ b/libc/test/src/stdio/rename_test.cpp
@@ -6,8 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/fcntl_macros.h"
 #include "hdr/sys_stat_macros.h"
-#include "include/llvm-libc-macros/linux/unistd-macros.h"
+#include "hdr/unistd_macros.h"
 #include "src/fcntl/open.h"
 #include "src/stdio/rename.h"
 #include "src/unistd/access.h"
diff --git a/libc/test/src/stdlib/DivTest.h b/libc/test/src/stdlib/DivTest.h
index 90fae6c4df44ff..cfde2f4e0e2ec4 100644
--- a/libc/test/src/stdlib/DivTest.h
+++ b/libc/test/src/stdlib/DivTest.h
@@ -8,12 +8,10 @@
 
 #include "test/UnitTest/Test.h"
 
-template <typename IntType, typename ReturnType>
-class DivTest : public LIBC_NAMESPACE::testing::Test {
+class LlvmLibcDivTest : public LIBC_NAMESPACE::testing::Test {
 public:
-  using DivFunc = ReturnType(IntType, IntType);
-
-  void simpleTest(DivFunc func) {
+  template <typename IntType, typename ReturnType>
+  void simpleTest(ReturnType (&func)(IntType, IntType)) {
     auto result = func(10, 3);
     EXPECT_EQ(result.quot, IntType(3));
     EXPECT_EQ(result.rem, IntType(1));
@@ -33,5 +31,6 @@ class DivTest : public LIBC_NAMESPACE::testing::Test {
 };
 
 #define LIST_DIV_TESTS(IntType, ReturnType, func)                              \
-  using LlvmLibcDivTest = DivTest<IntType, ReturnType>;                        \
-  TEST_F(LlvmLibcDivTest, SimpleTest##ReturnType) { simpleTest(func); }
+  TEST_F(LlvmLibcDivTest, SimpleTest##ReturnType) {                            \
+    simpleTest<IntType, ReturnType>(func);                                     \
+  }
diff --git a/libc/test/src/string/strsep_test.cpp b/libc/test/src/string/strsep_test.cpp
index 553edd99604eff..59e401c9903ecc 100644
--- a/libc/test/src/string/strsep_test.cpp
+++ b/libc/test/src/string/strsep_test.cpp
@@ -18,8 +18,8 @@ TEST(LlvmLibcStrsepTest, NullSrc) {
 TEST(LlvmLibcStrsepTest, NoTokenFound) {
   {
     char s[] = "";
-    char *string = s;
-    EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, ""), nullptr);
+    char *string = s, *orig = s;
+    EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, ""), orig);
     EXPECT_TRUE(string == nullptr);
   }
   {
diff --git a/libc/test/src/sys/mman/linux/pkey_test.cpp b/libc/test/src/sys/mman/linux/pkey_test.cpp
index d81df4057501f6..2e6e3bcf1ec071 100644
--- a/libc/test/src/sys/mman/linux/pkey_test.cpp
+++ b/libc/test/src/sys/mman/linux/pkey_test.cpp
@@ -19,7 +19,7 @@
 #include "src/sys/mman/pkey_set.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
-#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
 #include "test/UnitTest/TestLogger.h"
 
 #include <linux/param.h> // For EXEC_PAGESIZE.
diff --git a/libc/test/src/sys/socket/linux/sendmsg_recvmsg_test.cpp b/libc/test/src/sys/socket/linux/sendmsg_recvmsg_test.cpp
index 0941c8eac4570e..4b0393566cfc85 100644
--- a/libc/test/src/sys/socket/linux/sendmsg_recvmsg_test.cpp
+++ b/libc/test/src/sys/socket/linux/sendmsg_recvmsg_test.cpp
@@ -22,7 +22,6 @@
 #include "src/__support/CPP/scope.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
-#include "test/UnitTest/LibcTest.h"
 #include "test/UnitTest/Test.h"
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
diff --git a/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp b/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp
index 16900d6f01a26b..642f4156670192 100644
--- a/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp
+++ b/libc/test/src/sys/socket/linux/sendrecvmmsg_test.cpp
@@ -23,7 +23,6 @@
 #include "src/unistd/close.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
-#include "test/UnitTest/LibcTest.h"
 #include "test/UnitTest/Test.h"
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
diff --git a/libc/test/src/sys/socket/linux/sockaddr_storage_test.cpp b/libc/test/src/sys/socket/linux/sockaddr_storage_test.cpp
index 97cfcc9dde0dc3..57bcc50de95969 100644
--- a/libc/test/src/sys/socket/linux/sockaddr_storage_test.cpp
+++ b/libc/test/src/sys/socket/linux/sockaddr_storage_test.cpp
@@ -10,7 +10,7 @@
 #include "hdr/types/sa_family_t.h"
 #include "hdr/types/struct_sockaddr_storage.h"
 #include "hdr/types/struct_sockaddr_un.h"
-#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
 
 using LlvmLibcSockaddrStorageTest = LIBC_NAMESPACE::testing::Test;
 
diff --git a/libc/test/src/sys/socket/linux/socket_test_support.h b/libc/test/src/sys/socket/linux/socket_test_support.h
index b930f8723f0877..ac092f454f05e8 100644
--- a/libc/test/src/sys/socket/linux/socket_test_support.h
+++ b/libc/test/src/sys/socket/linux/socket_test_support.h
@@ -27,7 +27,7 @@
 #include "src/unistd/chdir.h"
 #include "src/unistd/close.h"
 #include "src/unistd/fchdir.h"
-#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace testing {
diff --git a/libc/test/src/sys/xattr/lxattr_test.cpp b/libc/test/src/sys/xattr/lxattr_test.cpp
index 2f7d2b630bf792..98fa81cea942d3 100644
--- a/libc/test/src/sys/xattr/lxattr_test.cpp
+++ b/libc/test/src/sys/xattr/lxattr_test.cpp
@@ -281,7 +281,7 @@ TEST_F(LlvmLibcLxattrTest, GetCrashesOnNullBufferNonZeroSize) {
       WITH_SIGNAL(-1));
 }
 
-TEST(LlvmLibcLxattrTest, ListCrashesOnNullPath) {
+TEST_F(LlvmLibcLxattrTest, ListCrashesOnNullPath) {
   EXPECT_DEATH(
       [] {
         constexpr size_t BUFFER_SIZE = 32;
diff --git a/libc/test/src/sys/xattr/xattr_test.cpp b/libc/test/src/sys/xattr/xattr_test.cpp
index 54a8004343054a..77d8c2ae80673a 100644
--- a/libc/test/src/sys/xattr/xattr_test.cpp
+++ b/libc/test/src/sys/xattr/xattr_test.cpp
@@ -315,7 +315,7 @@ TEST_F(LlvmLibcXattrTest, GetCrashesOnNullBufferNonZeroSize) {
       WITH_SIGNAL(-1));
 }
 
-TEST(LlvmLibcXattrTest, ListCrashesOnNullPath) {
+TEST_F(LlvmLibcXattrTest, ListCrashesOnNullPath) {
   EXPECT_DEATH(
       [] {
         constexpr size_t BUFFER_SIZE = 32;



More information about the libc-commits mailing list