1 //===-- FPExceptMatcher.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
10 #define LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
12 #include "src/__support/macros/config.h"
13 #include "test/UnitTest/Test.h"
14 #include "test/UnitTest/TestLogger.h"
16 #if LIBC_TEST_HAS_MATCHERS()
18 namespace LIBC_NAMESPACE_DECL
{
21 // TODO: Make the matcher match specific exceptions instead of just identifying
22 // that an exception was raised.
23 class FPExceptMatcher
: public Matcher
<bool> {
27 class FunctionCaller
{
29 virtual ~FunctionCaller() {}
30 virtual void call() = 0;
33 template <typename Func
> static FunctionCaller
*getFunctionCaller(Func func
) {
34 struct Callable
: public FunctionCaller
{
36 explicit Callable(Func theFunc
) : f(theFunc
) {}
37 void call() override
{ f(); }
40 return new Callable(func
);
43 // Takes ownership of func.
44 explicit FPExceptMatcher(FunctionCaller
*func
);
46 bool match(bool unused
) { return exceptionRaised
; }
48 void explainError() override
{
49 tlog
<< "A floating point exception should have been raised but it "
54 } // namespace testing
55 } // namespace LIBC_NAMESPACE_DECL
57 #define ASSERT_RAISES_FP_EXCEPT(func) \
60 LIBC_NAMESPACE::testing::FPExceptMatcher( \
61 LIBC_NAMESPACE::testing::FPExceptMatcher::getFunctionCaller(func)))
63 #else // !LIBC_TEST_HAS_MATCHERS()
65 #define ASSERT_RAISES_FP_EXCEPT(func) ASSERT_DEATH(func, WITH_SIGNAL(SIGFPE))
67 #endif // LIBC_TEST_HAS_MATCHERS()
69 #endif // LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H