1//===-- FPExceptMatcher.h ---------------------------------------*- 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_TEST_UNITTEST_FPEXCEPTMATCHER_H
10#define LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
11
12#include "test/UnitTest/Test.h"
13#include "test/UnitTest/TestLogger.h"
14
15#if LIBC_TEST_HAS_MATCHERS()
16
17namespace LIBC_NAMESPACE {
18namespace testing {
19
20// TODO: Make the matcher match specific exceptions instead of just identifying
21// that an exception was raised.
22class FPExceptMatcher : public Matcher<bool> {
23 bool exceptionRaised;
24
25public:
26 class FunctionCaller {
27 public:
28 virtual ~FunctionCaller() {}
29 virtual void call() = 0;
30 };
31
32 template <typename Func> static FunctionCaller *getFunctionCaller(Func func) {
33 struct Callable : public FunctionCaller {
34 Func f;
35 explicit Callable(Func theFunc) : f(theFunc) {}
36 void call() override { f(); }
37 };
38
39 return new Callable(func);
40 }
41
42 // Takes ownership of func.
43 explicit FPExceptMatcher(FunctionCaller *func);
44
45 bool match(bool unused) { return exceptionRaised; }
46
47 void explainError() override {
48 tlog << "A floating point exception should have been raised but it "
49 << "wasn't\n";
50 }
51};
52
53} // namespace testing
54} // namespace LIBC_NAMESPACE
55
56#define ASSERT_RAISES_FP_EXCEPT(func) \
57 ASSERT_THAT( \
58 true, \
59 LIBC_NAMESPACE::testing::FPExceptMatcher( \
60 LIBC_NAMESPACE::testing::FPExceptMatcher::getFunctionCaller(func)))
61
62#else // !LIBC_TEST_HAS_MATCHERS()
63
64#define ASSERT_RAISES_FP_EXCEPT(func) ASSERT_DEATH(func, WITH_SIGNAL(SIGFPE))
65
66#endif // LIBC_TEST_HAS_MATCHERS()
67
68#endif // LLVM_LIBC_TEST_UNITTEST_FPEXCEPTMATCHER_H
69

source code of libc/test/UnitTest/FPExceptMatcher.h