Skip to content

Commit d2be982

Browse files
authored
[libc] Clean up alternate test framework support (#89659)
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.
1 parent 59bf49a commit d2be982

File tree

10 files changed

+94
-50
lines changed

10 files changed

+94
-50
lines changed

libc/src/__support/OSUtil/fuchsia/io.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_FUCHSIA_IO_H
1010
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_FUCHSIA_IO_H
1111

12-
#ifndef LIBC_COPT_TEST_USE_FUCHSIA
13-
#error this file should only be used by tests
14-
#endif
15-
1612
#include "src/__support/CPP/string_view.h"
1713

14+
#include <iostream>
1815
#include <zircon/sanitizer.h>
1916

2017
namespace LIBC_NAMESPACE {
2118

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

2631
} // namespace LIBC_NAMESPACE

libc/test/UnitTest/FPExceptMatcher.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88

99
#include "FPExceptMatcher.h"
1010

11+
#include "test/UnitTest/Test.h"
12+
1113
#include "hdr/types/fenv_t.h"
1214
#include "src/__support/FPUtil/FEnvImpl.h"
1315
#include <memory>
1416
#include <setjmp.h>
1517
#include <signal.h>
1618

19+
#if LIBC_TEST_HAS_MATCHERS()
20+
1721
namespace LIBC_NAMESPACE {
1822
namespace testing {
1923

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

5054
} // namespace testing
5155
} // namespace LIBC_NAMESPACE
56+
57+
#endif // LIBC_TEST_HAS_MATCHERS()

libc/test/UnitTest/FPExceptMatcher.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#ifndef LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
1010
#define LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
1111

12-
#ifndef LIBC_COPT_TEST_USE_FUCHSIA
13-
1412
#include "test/UnitTest/Test.h"
13+
#include "test/UnitTest/TestLogger.h"
14+
15+
#if LIBC_TEST_HAS_MATCHERS()
1516

1617
namespace LIBC_NAMESPACE {
1718
namespace testing {
@@ -24,7 +25,7 @@ class FPExceptMatcher : public Matcher<bool> {
2425
public:
2526
class FunctionCaller {
2627
public:
27-
virtual ~FunctionCaller(){};
28+
virtual ~FunctionCaller() {}
2829
virtual void call() = 0;
2930
};
3031

@@ -57,8 +58,11 @@ class FPExceptMatcher : public Matcher<bool> {
5758
true, \
5859
LIBC_NAMESPACE::testing::FPExceptMatcher( \
5960
LIBC_NAMESPACE::testing::FPExceptMatcher::getFunctionCaller(func)))
60-
#else
61+
62+
#else // !LIBC_TEST_HAS_MATCHERS()
63+
6164
#define ASSERT_RAISES_FP_EXCEPT(func) ASSERT_DEATH(func, WITH_SIGNAL(SIGFPE))
62-
#endif // LIBC_COPT_TEST_USE_FUCHSIA
65+
66+
#endif // LIBC_TEST_HAS_MATCHERS()
6367

6468
#endif // LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H

libc/test/UnitTest/GTest.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Header for using the gtest framework -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_UTILS_UNITTEST_GTEST_H
10+
#define LLVM_LIBC_UTILS_UNITTEST_GTEST_H
11+
12+
#include <gtest/gtest.h>
13+
14+
namespace LIBC_NAMESPACE::testing {
15+
16+
using ::testing::Matcher;
17+
using ::testing::Test;
18+
19+
} // namespace LIBC_NAMESPACE::testing
20+
21+
#define LIBC_TEST_HAS_MATCHERS() (1)
22+
23+
#endif // LLVM_LIBC_UTILS_UNITTEST_GTEST_H

libc/test/UnitTest/LibcTest.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -446,16 +446,6 @@ CString libc_make_test_file_path_func(const char *file_name);
446446
#define EXPECT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, )
447447
#define ASSERT_STRNE(LHS, RHS) LIBC_TEST_STR_(testStrNe, LHS, RHS, return)
448448

449-
////////////////////////////////////////////////////////////////////////////////
450-
// Errno checks.
451-
452-
#define ASSERT_ERRNO_EQ(VAL) \
453-
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
454-
#define ASSERT_ERRNO_SUCCESS() \
455-
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
456-
#define ASSERT_ERRNO_FAILURE() \
457-
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
458-
459449
////////////////////////////////////////////////////////////////////////////////
460450
// Subprocess checks.
461451

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

495485
#define WITH_SIGNAL(X) X
496486

487+
#define LIBC_TEST_HAS_MATCHERS() (1)
488+
497489
#endif // LLVM_LIBC_TEST_UNITTEST_LIBCTEST_H

libc/test/UnitTest/MemoryMatcher.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "test/UnitTest/Test.h"
1212

13+
#if LIBC_TEST_HAS_MATCHERS()
14+
1315
using LIBC_NAMESPACE::testing::tlog;
1416

1517
namespace LIBC_NAMESPACE {
@@ -76,3 +78,5 @@ void MemoryMatcher::explainError() {
7678

7779
} // namespace testing
7880
} // namespace LIBC_NAMESPACE
81+
82+
#endif // LIBC_TEST_HAS_MATCHERS()

libc/test/UnitTest/MemoryMatcher.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using MemoryView = LIBC_NAMESPACE::cpp::span<const char>;
2121
} // namespace testing
2222
} // namespace LIBC_NAMESPACE
2323

24-
#ifdef LIBC_COPT_TEST_USE_FUCHSIA
24+
#if !LIBC_TEST_HAS_MATCHERS()
2525

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

42-
#else
42+
#else // LIBC_TEST_HAS_MATCHERS()
4343

4444
namespace LIBC_NAMESPACE::testing {
4545

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

67-
#endif
67+
#endif // !LIBC_TEST_HAS_MATCHERS()
6868

6969
#endif // LLVM_LIBC_TEST_UNITTEST_MEMORYMATCHER_H

libc/test/UnitTest/PigweedTest.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

libc/test/UnitTest/Test.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,35 @@
1616
// redefine it as necessary.
1717
#define libc_make_test_file_path(file_name) (file_name)
1818

19-
#if defined(LIBC_COPT_TEST_USE_FUCHSIA)
20-
#include "FuchsiaTest.h"
21-
#elif defined(LIBC_COPT_TEST_USE_PIGWEED)
22-
#include "PigweedTest.h"
19+
// The LIBC_COPT_TEST_USE_* macros can select either of two alternate test
20+
// frameworks:
21+
// * gtest, the well-known model for them all
22+
// * zxtest, the gtest workalike subset sometimes used in the Fuchsia build
23+
// The default is to use llvm-libc's own gtest workalike framework.
24+
//
25+
// All the frameworks provide the basic EXPECT_* and ASSERT_* macros that gtest
26+
// does. The wrapper headers below define LIBC_NAMESPACE::testing::Test as the
27+
// base class for test fixture classes. Each also provides a definition of the
28+
// macro LIBC_TEST_HAS_MATCHERS() for use in `#if` conditionals to guard use of
29+
// gmock-style matchers, which zxtest does not support.
30+
31+
#if defined(LIBC_COPT_TEST_USE_ZXTEST)
32+
#include "ZxTest.h"
33+
// TODO: Migrate Pigweed to setting LIBC_COPT_TEST_USE_GTEST instead.
34+
#elif defined(LIBC_COPT_TEST_USE_GTEST) || defined(LIBC_COPT_TEST_USE_PIGWEED)
35+
#include "GTest.h"
2336
#else
2437
#include "LibcTest.h"
2538
#endif
2639

40+
// These are defined the same way for each framework, in terms of the macros
41+
// they all provide.
42+
43+
#define ASSERT_ERRNO_EQ(VAL) \
44+
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
45+
#define ASSERT_ERRNO_SUCCESS() \
46+
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
47+
#define ASSERT_ERRNO_FAILURE() \
48+
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
49+
2750
#endif // LLVM_LIBC_TEST_UNITTEST_TEST_H

libc/test/UnitTest/FuchsiaTest.h renamed to libc/test/UnitTest/ZxTest.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===-- Header for setting up the Fuchsia tests -----------------*- C++ -*-===//
1+
//===-- Header for using Fuchsia's zxtest framework ------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===----------------------------------------------------------------------===//
7+
//===---------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_UTILS_UNITTEST_FUCHSIATEST_H
10-
#define LLVM_LIBC_UTILS_UNITTEST_FUCHSIATEST_H
9+
#ifndef LLVM_LIBC_UTILS_UNITTEST_ZXTEST_H
10+
#define LLVM_LIBC_UTILS_UNITTEST_ZXTEST_H
1111

1212
#include <zxtest/zxtest.h>
1313

@@ -29,7 +29,12 @@
2929
#endif
3030

3131
namespace LIBC_NAMESPACE::testing {
32+
3233
using Test = ::zxtest::Test;
33-
}
3434

35-
#endif // LLVM_LIBC_UTILS_UNITTEST_FUCHSIATEST_H
35+
} // namespace LIBC_NAMESPACE::testing
36+
37+
// zxtest does not have gmock-style matchers.
38+
#define LIBC_TEST_HAS_MATCHERS() (0)
39+
40+
#endif // LLVM_LIBC_UTILS_UNITTEST_ZXTEST_H

0 commit comments

Comments
 (0)