[libc-commits] [libc] [libc][test] Support GoogleTest in LibcCTest (PR #247019)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Sun Nov 29 14:27:24 PST 2026


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

C header tests could not run under GoogleTest because `LibcCTest.cpp` included `LibcTest.h` directly and used `LibcTest`-private APIs.

Switch `LibcCTest.cpp` to include `test/UnitTest/Test.h` and provide a GoogleTest implementation (matching `Test.h`'s `LIBC_COPT_TEST_USE_GTEST` and `LIBC_COPT_TEST_USE_PIGWEED` selection, with an explicit `#error` for `LIBC_COPT_TEST_USE_ZXTEST`) using `::testing::RegisterTest` and `ADD_FAILURE_AT` so each C test registers under its `LIBC_C_TEST_NAME` suite/test name and preserves failure file/line reporting. Hoist the identical `libc_c_test_anchor()` definition outside the preprocessor guard.

Part of #91630

Assisted-by: Gemini

>From 58909b0e3c58a51ecde3919b282b18ce4fff4799 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Sun, 29 Nov 2026 14:15:01 -0800
Subject: [PATCH] [libc][test] Support GoogleTest in LibcCTest

C header tests could not run under GoogleTest because LibcCTest.cpp
included LibcTest.h directly and used LibcTest-private APIs.

Switch LibcCTest.cpp to include test/UnitTest/Test.h and provide a
GoogleTest implementation (matching Test.h's LIBC_COPT_TEST_USE_GTEST
and LIBC_COPT_TEST_USE_PIGWEED selection, with an explicit #error for
LIBC_COPT_TEST_USE_ZXTEST) using ::testing::RegisterTest and
ADD_FAILURE_AT so each C test registers under its LIBC_C_TEST_NAME
suite/test name and preserves failure file/line reporting. Hoist the
identical libc_c_test_anchor() definition outside the preprocessor
guard.

Part of #91630

Assisted-by: Gemini
---
 libc/test/UnitTest/LibcCTest.cpp | 46 +++++++++++++++++++++++++++++---
 1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/libc/test/UnitTest/LibcCTest.cpp b/libc/test/UnitTest/LibcCTest.cpp
index 8e19f7dd302271..af340ac1e2c26b 100644
--- a/libc/test/UnitTest/LibcCTest.cpp
+++ b/libc/test/UnitTest/LibcCTest.cpp
@@ -12,7 +12,45 @@
 //===----------------------------------------------------------------------===//
 
 #include "test/UnitTest/LibcCTest.h"
-#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
+
+#if defined(LIBC_COPT_TEST_USE_GTEST) || defined(LIBC_COPT_TEST_USE_PIGWEED)
+
+#include <string>
+#include <string_view>
+
+namespace {
+
+class CTest : public LIBC_NAMESPACE::testing::Test {
+  void TestBody() override { libc_c_test_run(); }
+};
+
+[[maybe_unused]] ::testing::TestInfo *const c_test_info = [] {
+  std::string_view full_name(LIBC_C_TEST_NAME);
+  size_t dot = full_name.find('.');
+  std::string suite = dot == std::string_view::npos
+                          ? "LlvmLibcCTest"
+                          : std::string(full_name.substr(0, dot));
+  const char *name = dot == std::string_view::npos ? LIBC_C_TEST_NAME
+                                                   : LIBC_C_TEST_NAME + dot + 1;
+  return ::testing::RegisterTest(
+      suite.c_str(), name, nullptr, nullptr, __FILE__, __LINE__,
+      []() -> LIBC_NAMESPACE::testing::Test * { return new CTest(); });
+}();
+
+} // namespace
+
+extern "C" void libc_c_test_fail(const char *cond, int expected,
+                                 const char *file, int line) {
+  ADD_FAILURE_AT(file, line)
+      << "Expected (" << cond << ") to be " << (expected ? "true" : "false");
+}
+
+#elif defined(LIBC_COPT_TEST_USE_ZXTEST)
+
+#error "LibcCTest does not support LIBC_COPT_TEST_USE_ZXTEST"
+
+#else
 
 namespace {
 class CTest : public LIBC_NAMESPACE::testing::Test {
@@ -25,8 +63,6 @@ class CTest : public LIBC_NAMESPACE::testing::Test {
 
 static CTest c_test_instance;
 
-extern "C" void libc_c_test_anchor() {}
-
 extern "C" void libc_c_test_fail(const char *cond, int expected,
                                  const char *file, int line) {
   LIBC_NAMESPACE::testing::internal::test(
@@ -34,3 +70,7 @@ extern "C" void libc_c_test_fail(const char *cond, int expected,
       static_cast<bool>(expected), cond, expected ? "true" : "false",
       LIBC_NAMESPACE::testing::internal::Location(file, line));
 }
+
+#endif
+
+extern "C" void libc_c_test_anchor() {}



More information about the libc-commits mailing list