[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / third-party / benchmark / src / check.h
blob0efd13ff4db6d9270b7c11aa7bd79c57cae87670
1 #ifndef CHECK_H_
2 #define CHECK_H_
4 #include <cmath>
5 #include <cstdlib>
6 #include <ostream>
8 #include "internal_macros.h"
9 #include "log.h"
11 namespace benchmark {
12 namespace internal {
14 typedef void(AbortHandlerT)();
16 inline AbortHandlerT*& GetAbortHandler() {
17 static AbortHandlerT* handler = &std::abort;
18 return handler;
21 BENCHMARK_NORETURN inline void CallAbortHandler() {
22 GetAbortHandler()();
23 std::abort(); // fallback to enforce noreturn
26 // CheckHandler is the class constructed by failing BM_CHECK macros.
27 // CheckHandler will log information about the failures and abort when it is
28 // destructed.
29 class CheckHandler {
30 public:
31 CheckHandler(const char* check, const char* file, const char* func, int line)
32 : log_(GetErrorLogInstance()) {
33 log_ << file << ":" << line << ": " << func << ": Check `" << check
34 << "' failed. ";
37 LogType& GetLog() { return log_; }
39 BENCHMARK_NORETURN ~CheckHandler() BENCHMARK_NOEXCEPT_OP(false) {
40 log_ << std::endl;
41 CallAbortHandler();
44 CheckHandler& operator=(const CheckHandler&) = delete;
45 CheckHandler(const CheckHandler&) = delete;
46 CheckHandler() = delete;
48 private:
49 LogType& log_;
52 } // end namespace internal
53 } // end namespace benchmark
55 // The BM_CHECK macro returns a std::ostream object that can have extra
56 // information written to it.
57 #ifndef NDEBUG
58 #define BM_CHECK(b) \
59 (b ? ::benchmark::internal::GetNullLogInstance() \
60 : ::benchmark::internal::CheckHandler(#b, __FILE__, __func__, __LINE__) \
61 .GetLog())
62 #else
63 #define BM_CHECK(b) ::benchmark::internal::GetNullLogInstance()
64 #endif
66 // clang-format off
67 // preserve whitespacing between operators for alignment
68 #define BM_CHECK_EQ(a, b) BM_CHECK((a) == (b))
69 #define BM_CHECK_NE(a, b) BM_CHECK((a) != (b))
70 #define BM_CHECK_GE(a, b) BM_CHECK((a) >= (b))
71 #define BM_CHECK_LE(a, b) BM_CHECK((a) <= (b))
72 #define BM_CHECK_GT(a, b) BM_CHECK((a) > (b))
73 #define BM_CHECK_LT(a, b) BM_CHECK((a) < (b))
75 #define BM_CHECK_FLOAT_EQ(a, b, eps) BM_CHECK(std::fabs((a) - (b)) < (eps))
76 #define BM_CHECK_FLOAT_NE(a, b, eps) BM_CHECK(std::fabs((a) - (b)) >= (eps))
77 #define BM_CHECK_FLOAT_GE(a, b, eps) BM_CHECK((a) - (b) > -(eps))
78 #define BM_CHECK_FLOAT_LE(a, b, eps) BM_CHECK((b) - (a) > -(eps))
79 #define BM_CHECK_FLOAT_GT(a, b, eps) BM_CHECK((a) - (b) > (eps))
80 #define BM_CHECK_FLOAT_LT(a, b, eps) BM_CHECK((b) - (a) > (eps))
81 //clang-format on
83 #endif // CHECK_H_