Skip to content

Commit

Permalink
[libc] Clean up alternate test framework support (#89659)
Browse files Browse the repository at this point in the history
This replaces the old macros LIBC_COPT_TEST_USE_FUCHSIA and
LIBC_COPT_TEST_USE_PIGWEED with LIBC_COPT_TEST_ZXTEST and
LIBC_COPT_TEST_GTEST, respectively.  These are really not about
whether the code is in the Fuchsia build or in the Pigweed build,
but just about what test framework is being used.  The gtest
framework can be used in many contexts, and the zxtest framework
is not always what's used in the Fuchsia build.

The test/UnitTest/Test.h wrapper header now provides the macro
LIBC_TEST_HAS_MATCHERS() for use in `#if` conditionals on use of
gmock-style matchers, replacing `#if` conditionals that test the
framework selection macros directly.
  • Loading branch information
frobtech committed Apr 22, 2024
1 parent 59bf49a commit d2be982
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 50 deletions.
13 changes: 9 additions & 4 deletions libc/src/__support/OSUtil/fuchsia/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_FUCHSIA_IO_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_FUCHSIA_IO_H

#ifndef LIBC_COPT_TEST_USE_FUCHSIA
#error this file should only be used by tests
#endif

#include "src/__support/CPP/string_view.h"

#include <iostream>
#include <zircon/sanitizer.h>

namespace LIBC_NAMESPACE {

LIBC_INLINE void write_to_stderr(cpp::string_view msg) {
#if defined(LIBC_COPT_TEST_USE_ZXTEST)
// This is used in standalone context where there is nothing like POSIX I/O.
__sanitizer_log_write(msg.data(), msg.size());
#elif defined(LIBC_COPT_TEST_USE_GTEST)
// The gtest framework already relies on full standard C++ I/O via fdio.
std::cerr << std::string_view{msg.data(), msg.size()};
#else
#error this file should only be used by tests
#endif
}

} // namespace LIBC_NAMESPACE
Expand Down
6 changes: 6 additions & 0 deletions libc/test/UnitTest/FPExceptMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

#include "FPExceptMatcher.h"

#include "test/UnitTest/Test.h"

#include "hdr/types/fenv_t.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include <memory>
#include <setjmp.h>
#include <signal.h>

#if LIBC_TEST_HAS_MATCHERS()

namespace LIBC_NAMESPACE {
namespace testing {

Expand Down Expand Up @@ -49,3 +53,5 @@ FPExceptMatcher::FPExceptMatcher(FunctionCaller *func) {

} // namespace testing
} // namespace LIBC_NAMESPACE

#endif // LIBC_TEST_HAS_MATCHERS()
14 changes: 9 additions & 5 deletions libc/test/UnitTest/FPExceptMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#ifndef LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
#define LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H

#ifndef LIBC_COPT_TEST_USE_FUCHSIA

#include "test/UnitTest/Test.h"
#include "test/UnitTest/TestLogger.h"

#if LIBC_TEST_HAS_MATCHERS()

namespace LIBC_NAMESPACE {
namespace testing {
Expand All @@ -24,7 +25,7 @@ class FPExceptMatcher : public Matcher<bool> {
public:
class FunctionCaller {
public:
virtual ~FunctionCaller(){};
virtual ~FunctionCaller() {}
virtual void call() = 0;
};

Expand Down Expand Up @@ -57,8 +58,11 @@ class FPExceptMatcher : public Matcher<bool> {
true, \
LIBC_NAMESPACE::testing::FPExceptMatcher( \
LIBC_NAMESPACE::testing::FPExceptMatcher::getFunctionCaller(func)))
#else

#else // !LIBC_TEST_HAS_MATCHERS()

#define ASSERT_RAISES_FP_EXCEPT(func) ASSERT_DEATH(func, WITH_SIGNAL(SIGFPE))
#endif // LIBC_COPT_TEST_USE_FUCHSIA

#endif // LIBC_TEST_HAS_MATCHERS()

#endif // LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
23 changes: 23 additions & 0 deletions libc/test/UnitTest/GTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Header for using the gtest framework -------------------*- 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
//
//===---------------------------------------------------------------------===//

#ifndef LLVM_LIBC_UTILS_UNITTEST_GTEST_H
#define LLVM_LIBC_UTILS_UNITTEST_GTEST_H

#include <gtest/gtest.h>

namespace LIBC_NAMESPACE::testing {

using ::testing::Matcher;
using ::testing::Test;

} // namespace LIBC_NAMESPACE::testing

#define LIBC_TEST_HAS_MATCHERS() (1)

#endif // LLVM_LIBC_UTILS_UNITTEST_GTEST_H
12 changes: 2 additions & 10 deletions libc/test/UnitTest/LibcTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,16 +446,6 @@ CString libc_make_test_file_path_func(const char *file_name);
#define EXPECT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, )
#define ASSERT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, return)

////////////////////////////////////////////////////////////////////////////////
// Errno checks.

#define ASSERT_ERRNO_EQ(VAL) \
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
#define ASSERT_ERRNO_SUCCESS() \
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
#define ASSERT_ERRNO_FAILURE() \
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))

////////////////////////////////////////////////////////////////////////////////
// Subprocess checks.

Expand Down Expand Up @@ -494,4 +484,6 @@ CString libc_make_test_file_path_func(const char *file_name);

#define WITH_SIGNAL(X) X

#define LIBC_TEST_HAS_MATCHERS() (1)

#endif // LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H
4 changes: 4 additions & 0 deletions libc/test/UnitTest/MemoryMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "test/UnitTest/Test.h"

#if LIBC_TEST_HAS_MATCHERS()

using LIBC_NAMESPACE::testing::tlog;

namespace LIBC_NAMESPACE {
Expand Down Expand Up @@ -76,3 +78,5 @@ void MemoryMatcher::explainError() {

} // namespace testing
} // namespace LIBC_NAMESPACE

#endif // LIBC_TEST_HAS_MATCHERS()
6 changes: 3 additions & 3 deletions libc/test/UnitTest/MemoryMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using MemoryView = LIBC_NAMESPACE::cpp::span<const char>;
} // namespace testing
} // namespace LIBC_NAMESPACE

#ifdef LIBC_COPT_TEST_USE_FUCHSIA
#if !LIBC_TEST_HAS_MATCHERS()

#define EXPECT_MEM_EQ(expected, actual) \
do { \
Expand All @@ -39,7 +39,7 @@ using MemoryView = LIBC_NAMESPACE::cpp::span<const char>;
ASSERT_BYTES_EQ(e.data(), a.data(), e.size()); \
} while (0)

#else
#else // LIBC_TEST_HAS_MATCHERS()

namespace LIBC_NAMESPACE::testing {

Expand All @@ -64,6 +64,6 @@ class MemoryMatcher : public Matcher<MemoryView> {
#define ASSERT_MEM_EQ(expected, actual) \
ASSERT_THAT(actual, LIBC_NAMESPACE::testing::MemoryMatcher(expected))

#endif
#endif // !LIBC_TEST_HAS_MATCHERS()

#endif // LLVM_LIBC_TEST_UNITTEST_MEMORYMATCHER_H
18 changes: 0 additions & 18 deletions libc/test/UnitTest/PigweedTest.h

This file was deleted.

31 changes: 27 additions & 4 deletions libc/test/UnitTest/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@
// redefine it as necessary.
#define libc_make_test_file_path(file_name) (file_name)

#if defined(LIBC_COPT_TEST_USE_FUCHSIA)
#include "FuchsiaTest.h"
#elif defined(LIBC_COPT_TEST_USE_PIGWEED)
#include "PigweedTest.h"
// The LIBC_COPT_TEST_USE_* macros can select either of two alternate test
// frameworks:
// * gtest, the well-known model for them all
// * zxtest, the gtest workalike subset sometimes used in the Fuchsia build
// The default is to use llvm-libc's own gtest workalike framework.
//
// All the frameworks provide the basic EXPECT_* and ASSERT_* macros that gtest
// does. The wrapper headers below define LIBC_NAMESPACE::testing::Test as the
// base class for test fixture classes. Each also provides a definition of the
// macro LIBC_TEST_HAS_MATCHERS() for use in `#if` conditionals to guard use of
// gmock-style matchers, which zxtest does not support.

#if defined(LIBC_COPT_TEST_USE_ZXTEST)
#include "ZxTest.h"
// TODO: Migrate Pigweed to setting LIBC_COPT_TEST_USE_GTEST instead.
#elif defined(LIBC_COPT_TEST_USE_GTEST) || defined(LIBC_COPT_TEST_USE_PIGWEED)
#include "GTest.h"
#else
#include "LibcTest.h"
#endif

// These are defined the same way for each framework, in terms of the macros
// they all provide.

#define ASSERT_ERRNO_EQ(VAL) \
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
#define ASSERT_ERRNO_SUCCESS() \
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
#define ASSERT_ERRNO_FAILURE() \
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))

#endif // LLVM_LIBC_TEST_UNITTEST_TEST_H
17 changes: 11 additions & 6 deletions libc/test/UnitTest/FuchsiaTest.h → libc/test/UnitTest/ZxTest.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//===-- Header for setting up the Fuchsia tests -----------------*- C++ -*-===//
//===-- Header for using Fuchsia's zxtest framework ------------*- 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
//
//===----------------------------------------------------------------------===//
//===---------------------------------------------------------------------===//

#ifndef LLVM_LIBC_UTILS_UNITTEST_FUCHSIATEST_H
#define LLVM_LIBC_UTILS_UNITTEST_FUCHSIATEST_H
#ifndef LLVM_LIBC_UTILS_UNITTEST_ZXTEST_H
#define LLVM_LIBC_UTILS_UNITTEST_ZXTEST_H

#include <zxtest/zxtest.h>

Expand All @@ -29,7 +29,12 @@
#endif

namespace LIBC_NAMESPACE::testing {

using Test = ::zxtest::Test;
}

#endif // LLVM_LIBC_UTILS_UNITTEST_FUCHSIATEST_H
} // namespace LIBC_NAMESPACE::testing

// zxtest does not have gmock-style matchers.
#define LIBC_TEST_HAS_MATCHERS() (0)

#endif // LLVM_LIBC_UTILS_UNITTEST_ZXTEST_H

0 comments on commit d2be982

Please sign in to comment.