sots-engine/tests/shim_trace/check.h

37 lines
1.4 KiB
C++

// Minimal test helpers for the shim trace tests (no dependency on other modules).
#pragma once
#include <cstdio>
#include <string>
namespace tracetest {
inline int& failures() { static int n = 0; return n; }
inline int& checks() { static int n = 0; return n; }
inline void report(bool ok, const char* expr, const char* file, int line, const std::string& detail) {
++checks();
if (ok) return;
++failures();
std::fprintf(stderr, "FAIL %s:%d %s%s%s\n", file, line, expr, detail.empty() ? "" : " -- ", detail.c_str());
}
inline void check_str(const std::string& got, const std::string& want, const char* expr, const char* file, int line) {
report(got == want, expr, file, line, got == want ? "" : "\n got: " + got + "\n want: " + want);
}
template <class A, class B>
inline void check_eq(const A& a, const B& b, const char* expr, const char* file, int line) {
report(a == b, expr, file, line, a == b ? "" : "got " + std::to_string(a) + ", expected " + std::to_string(b));
}
inline int finish(const char* name) {
std::printf("%s: %d checks, %d failures\n", name, checks(), failures());
return failures() == 0 ? 0 : 1;
}
} // namespace tracetest
#define CHECK(expr) ::tracetest::report((expr), #expr, __FILE__, __LINE__, "")
#define CHECK_EQ(a, b) ::tracetest::check_eq((a), (b), #a " == " #b, __FILE__, __LINE__)
#define CHECK_STR(got, want) ::tracetest::check_str((got), (want), #got, __FILE__, __LINE__)