ntptun/tests/TestUtil.hpp
2026-07-14 20:46:28 +02:00

43 lines
2.1 KiB
C++

// Tiny assertion helpers for the dependency-free unit tests.
#ifndef NTPTUN_TEST_UTIL_HPP
#define NTPTUN_TEST_UTIL_HPP
#include <cstdio>
namespace test {
inline int& failures() {
static int count = 0;
return count;
}
} // namespace test
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::fprintf(stderr, "CHECK failed: %s (%s:%d)\n", #cond, \
__FILE__, __LINE__); \
++::test::failures(); \
} \
} while (0)
#define CHECK_EQ(a, b) \
do { \
if (!((a) == (b))) { \
std::fprintf(stderr, "CHECK_EQ failed: %s == %s (%s:%d)\n", #a, \
#b, __FILE__, __LINE__); \
++::test::failures(); \
} \
} while (0)
#define TEST_MAIN() \
int main() { \
run_tests(); \
if (::test::failures() != 0) { \
std::fprintf(stderr, "%d failure(s)\n", ::test::failures()); \
return 1; \
} \
std::printf("OK\n"); \
return 0; \
}
#endif // NTPTUN_TEST_UTIL_HPP