Files
faumaray 38e86d4a07 New arch
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 15:06:22 +03:00

83 lines
2.5 KiB
C++

#pragma once
// Minimal dependency-free test harness. Tests register cases with TEST_CASE and
// assert with CHECK / CHECK_EQ; main() runs them and reports a non-zero exit on
// any failure so ctest treats it as a failing test.
#include <cstdio>
#include <functional>
#include <string>
#include <vector>
namespace echotest
{
struct Case {
std::string name;
std::function<void()> fn;
};
inline std::vector<Case>& registry()
{
static std::vector<Case> cases;
return cases;
}
inline int& failures()
{
static int f = 0;
return f;
}
struct Registrar {
Registrar(const std::string& name, std::function<void()> fn)
{
registry().push_back({name, std::move(fn)});
}
};
inline int run()
{
int failed = 0;
for (auto& c : registry()) {
int before = failures();
try {
c.fn();
} catch (const std::exception& e) {
std::printf(" [THROW] %s: %s\n", c.name.c_str(), e.what());
++failures();
}
if (failures() > before) {
std::printf("[FAIL] %s\n", c.name.c_str());
++failed;
} else {
std::printf("[ok] %s\n", c.name.c_str());
}
}
std::printf("\n%zu cases, %d failed\n", registry().size(), failed);
return failed == 0 ? 0 : 1;
}
} // namespace echotest
#define TEST_CASE(name) \
static void name(); \
static echotest::Registrar reg_##name(#name, name); \
static void name()
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::printf(" CHECK failed: %s (%s:%d)\n", #cond, __FILE__, \
__LINE__); \
++echotest::failures(); \
} \
} while (0)
#define CHECK_EQ(a, b) \
do { \
if (!((a) == (b))) { \
std::printf(" CHECK_EQ failed: %s == %s (%s:%d)\n", #a, #b, \
__FILE__, __LINE__); \
++echotest::failures(); \
} \
} while (0)