| //===----------------------------------------------------------------------===// | |
| // | |
| // 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 | |
| // | |
| //===----------------------------------------------------------------------===// | |
| /// | |
| /// \file | |
| /// Helpers for libc C unittests. | |
| /// | |
| //===----------------------------------------------------------------------===// | |
| #include "test/UnitTest/LibcCTest.h" | |
| #include "test/UnitTest/Test.h" | |
| #if defined(LIBC_COPT_TEST_USE_ZXTEST) | |
| // With zxtest, LibcCTest.h uses zxtest's native C test frontend directly and | |
| // does not need C++ registration glue here. | |
| #elif 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"); | |
| } | |
| #else | |
| namespace { | |
| class CTest : public LIBC_NAMESPACE::testing::Test { | |
| public: | |
| CTest() { addTest(this); } | |
| void Run() override { libc_c_test_run(); } | |
| const char *getName() const override { return LIBC_C_TEST_NAME; } | |
| }; | |
| } // namespace | |
| static CTest c_test_instance; | |
| extern "C" void libc_c_test_fail(const char *cond, int expected, | |
| const char *file, int line) { | |
| LIBC_NAMESPACE::testing::internal::test( | |
| LIBC_NAMESPACE::testing::TestCond::EQ, !expected, | |
| static_cast<bool>(expected), cond, expected ? "true" : "false", | |
| LIBC_NAMESPACE::testing::internal::Location(file, line)); | |
| } | |
| #endif | |
| #ifndef LIBC_COPT_TEST_USE_ZXTEST | |
| extern "C" void libc_c_test_anchor() {} | |
| #endif |