2014-11-01 04:54:55 +00:00
|
|
|
#ifndef CHECK_H_
|
|
|
|
#define CHECK_H_
|
|
|
|
|
2018-06-01 10:14:19 +00:00
|
|
|
#include <cmath>
|
2015-03-06 19:07:24 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
#include "internal_macros.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
namespace benchmark {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-05-23 21:05:55 +00:00
|
|
|
typedef void(AbortHandlerT)();
|
|
|
|
|
2016-05-23 21:27:43 +00:00
|
|
|
inline AbortHandlerT*& GetAbortHandler() {
|
2016-10-07 18:35:03 +00:00
|
|
|
static AbortHandlerT* handler = &std::abort;
|
|
|
|
return handler;
|
2016-05-23 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:27:43 +00:00
|
|
|
BENCHMARK_NORETURN inline void CallAbortHandler() {
|
2016-10-07 18:35:03 +00:00
|
|
|
GetAbortHandler()();
|
|
|
|
std::abort(); // fallback to enforce noreturn
|
2016-05-23 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 19:07:24 +00:00
|
|
|
// CheckHandler is the class constructed by failing CHECK macros. CheckHandler
|
|
|
|
// will log information about the failures and abort when it is destructed.
|
|
|
|
class CheckHandler {
|
2016-10-07 18:35:03 +00:00
|
|
|
public:
|
2015-03-06 19:07:24 +00:00
|
|
|
CheckHandler(const char* check, const char* file, const char* func, int line)
|
2016-10-07 18:35:03 +00:00
|
|
|
: log_(GetErrorLogInstance()) {
|
|
|
|
log_ << file << ":" << line << ": " << func << ": Check `" << check
|
|
|
|
<< "' failed. ";
|
2015-03-06 19:07:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 03:34:34 +00:00
|
|
|
LogType& GetLog() { return log_; }
|
2015-03-06 19:07:24 +00:00
|
|
|
|
2016-05-31 21:52:26 +00:00
|
|
|
BENCHMARK_NORETURN ~CheckHandler() BENCHMARK_NOEXCEPT_OP(false) {
|
2016-10-07 18:35:03 +00:00
|
|
|
log_ << std::endl;
|
|
|
|
CallAbortHandler();
|
2015-03-06 19:07:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 18:35:03 +00:00
|
|
|
CheckHandler& operator=(const CheckHandler&) = delete;
|
2016-05-23 21:05:55 +00:00
|
|
|
CheckHandler(const CheckHandler&) = delete;
|
2015-10-05 12:05:06 +00:00
|
|
|
CheckHandler() = delete;
|
2016-10-07 18:35:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
LogType& log_;
|
2015-03-06 19:07:24 +00:00
|
|
|
};
|
|
|
|
|
2016-10-07 18:35:03 +00:00
|
|
|
} // end namespace internal
|
|
|
|
} // end namespace benchmark
|
2015-03-06 19:07:24 +00:00
|
|
|
|
|
|
|
// The CHECK macro returns a std::ostream object that can have extra information
|
|
|
|
// written to it.
|
|
|
|
#ifndef NDEBUG
|
2016-10-07 18:35:03 +00:00
|
|
|
#define CHECK(b) \
|
|
|
|
(b ? ::benchmark::internal::GetNullLogInstance() \
|
|
|
|
: ::benchmark::internal::CheckHandler(#b, __FILE__, __func__, __LINE__) \
|
|
|
|
.GetLog())
|
2015-03-06 19:07:24 +00:00
|
|
|
#else
|
2016-10-07 18:35:03 +00:00
|
|
|
#define CHECK(b) ::benchmark::internal::GetNullLogInstance()
|
2015-03-06 19:07:24 +00:00
|
|
|
#endif
|
2014-11-01 04:54:55 +00:00
|
|
|
|
2018-06-01 10:14:19 +00:00
|
|
|
// clang-format off
|
|
|
|
// preserve whitespacing between operators for alignment
|
2014-11-01 04:54:55 +00:00
|
|
|
#define CHECK_EQ(a, b) CHECK((a) == (b))
|
|
|
|
#define CHECK_NE(a, b) CHECK((a) != (b))
|
|
|
|
#define CHECK_GE(a, b) CHECK((a) >= (b))
|
|
|
|
#define CHECK_LE(a, b) CHECK((a) <= (b))
|
|
|
|
#define CHECK_GT(a, b) CHECK((a) > (b))
|
|
|
|
#define CHECK_LT(a, b) CHECK((a) < (b))
|
|
|
|
|
2017-05-01 20:48:13 +00:00
|
|
|
#define CHECK_FLOAT_EQ(a, b, eps) CHECK(std::fabs((a) - (b)) < (eps))
|
|
|
|
#define CHECK_FLOAT_NE(a, b, eps) CHECK(std::fabs((a) - (b)) >= (eps))
|
|
|
|
#define CHECK_FLOAT_GE(a, b, eps) CHECK((a) - (b) > -(eps))
|
|
|
|
#define CHECK_FLOAT_LE(a, b, eps) CHECK((b) - (a) > -(eps))
|
|
|
|
#define CHECK_FLOAT_GT(a, b, eps) CHECK((a) - (b) > (eps))
|
|
|
|
#define CHECK_FLOAT_LT(a, b, eps) CHECK((b) - (a) > (eps))
|
2018-06-01 10:14:19 +00:00
|
|
|
//clang-format on
|
2017-04-28 19:40:21 +00:00
|
|
|
|
2014-11-01 04:54:55 +00:00
|
|
|
#endif // CHECK_H_
|