benchmark 1.7.0
check.h
1#ifndef CHECK_H_
2#define CHECK_H_
3
4#include <cmath>
5#include <cstdlib>
6#include <ostream>
7
8#include "benchmark/export.h"
9#include "internal_macros.h"
10#include "log.h"
11
12namespace benchmark {
13namespace internal {
14
15typedef void(AbortHandlerT)();
16
17BENCHMARK_EXPORT
18AbortHandlerT*& GetAbortHandler();
19
20BENCHMARK_NORETURN inline void CallAbortHandler() {
21 GetAbortHandler()();
22 std::abort(); // fallback to enforce noreturn
23}
24
25// CheckHandler is the class constructed by failing BM_CHECK macros.
26// CheckHandler will log information about the failures and abort when it is
27// destructed.
29 public:
30 CheckHandler(const char* check, const char* file, const char* func, int line)
31 : log_(GetErrorLogInstance()) {
32 log_ << file << ":" << line << ": " << func << ": Check `" << check
33 << "' failed. ";
34 }
35
36 LogType& GetLog() { return log_; }
37
38#if defined(COMPILER_MSVC)
39#pragma warning(push)
40#pragma warning(disable : 4722)
41#endif
42 BENCHMARK_NORETURN ~CheckHandler() BENCHMARK_NOEXCEPT_OP(false) {
43 log_ << std::endl;
44 CallAbortHandler();
45 }
46#if defined(COMPILER_MSVC)
47#pragma warning(pop)
48#endif
49
50 CheckHandler& operator=(const CheckHandler&) = delete;
51 CheckHandler(const CheckHandler&) = delete;
52 CheckHandler() = delete;
53
54 private:
55 LogType& log_;
56};
57
58} // end namespace internal
59} // end namespace benchmark
60
61// The BM_CHECK macro returns a std::ostream object that can have extra
62// information written to it.
63#ifndef NDEBUG
64#define BM_CHECK(b) \
65 (b ? ::benchmark::internal::GetNullLogInstance() \
66 : ::benchmark::internal::CheckHandler(#b, __FILE__, __func__, __LINE__) \
67 .GetLog())
68#else
69#define BM_CHECK(b) ::benchmark::internal::GetNullLogInstance()
70#endif
71
72// clang-format off
73// preserve whitespacing between operators for alignment
74#define BM_CHECK_EQ(a, b) BM_CHECK((a) == (b))
75#define BM_CHECK_NE(a, b) BM_CHECK((a) != (b))
76#define BM_CHECK_GE(a, b) BM_CHECK((a) >= (b))
77#define BM_CHECK_LE(a, b) BM_CHECK((a) <= (b))
78#define BM_CHECK_GT(a, b) BM_CHECK((a) > (b))
79#define BM_CHECK_LT(a, b) BM_CHECK((a) < (b))
80
81#define BM_CHECK_FLOAT_EQ(a, b, eps) BM_CHECK(std::fabs((a) - (b)) < (eps))
82#define BM_CHECK_FLOAT_NE(a, b, eps) BM_CHECK(std::fabs((a) - (b)) >= (eps))
83#define BM_CHECK_FLOAT_GE(a, b, eps) BM_CHECK((a) - (b) > -(eps))
84#define BM_CHECK_FLOAT_LE(a, b, eps) BM_CHECK((b) - (a) > -(eps))
85#define BM_CHECK_FLOAT_GT(a, b, eps) BM_CHECK((a) - (b) > (eps))
86#define BM_CHECK_FLOAT_LT(a, b, eps) BM_CHECK((b) - (a) > (eps))
87//clang-format on
88
89#endif // CHECK_H_
Definition: check.h:28
Definition: log.h:14