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_UTILS_UNITTEST_FPEXCEPTMATCHER_H
10 #define LLVM_LIBC_UTILS_UNITTEST_FPEXCEPTMATCHER_H
12 #ifndef LIBC_COPT_TEST_USE_FUCHSIA
14 #include "test/UnitTest/Test.h"
16 namespace LIBC_NAMESPACE
{
19 // TODO: Make the matcher match specific exceptions instead of just identifying
20 // that an exception was raised.
21 class FPExceptMatcher
: public Matcher
<bool> {
25 class FunctionCaller
{
27 virtual ~FunctionCaller(){};
28 virtual void call() = 0;
31 template <typename Func
> static FunctionCaller
*getFunctionCaller(Func func
) {
32 struct Callable
: public FunctionCaller
{
34 explicit Callable(Func theFunc
) : f(theFunc
) {}
35 void call() override
{ f(); }
38 return new Callable(func
);
41 // Takes ownership of func.
42 explicit FPExceptMatcher(FunctionCaller
*func
);
44 bool match(bool unused
) { return exceptionRaised
; }
46 void explainError() override
{
47 tlog
<< "A floating point exception should have been raised but it "
52 } // namespace testing
53 } // namespace LIBC_NAMESPACE
55 #define ASSERT_RAISES_FP_EXCEPT(func) \
58 LIBC_NAMESPACE::testing::FPExceptMatcher( \
59 LIBC_NAMESPACE::testing::FPExceptMatcher::getFunctionCaller(func)))
61 #define ASSERT_RAISES_FP_EXCEPT(func) ASSERT_DEATH(func, WITH_SIGNAL(SIGFPE))
62 #endif // LIBC_COPT_TEST_USE_FUCHSIA
64 #endif // LLVM_LIBC_UTILS_UNITTEST_FPEXCEPTMATCHER_H