79 lines
3.5 KiB
C++
79 lines
3.5 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
#include <cmath>
|
||
|
|
#include <functional>
|
||
|
|
#include <iostream>
|
||
|
|
#include <sstream>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
namespace htab::test {
|
||
|
|
|
||
|
|
struct TestCase {
|
||
|
|
std::string name;
|
||
|
|
std::function<void()> fn;
|
||
|
|
};
|
||
|
|
|
||
|
|
inline std::vector<TestCase>& registry() {
|
||
|
|
static std::vector<TestCase> tests;
|
||
|
|
return tests;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline int& failures_in_current_test() {
|
||
|
|
static int count = 0;
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
struct Registrar {
|
||
|
|
Registrar(const char* name, std::function<void()> fn) {
|
||
|
|
registry().push_back({name, std::move(fn)});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
inline void report_fail(const char* file, int line, const std::string& msg) {
|
||
|
|
++failures_in_current_test();
|
||
|
|
std::cerr << " FAIL " << file << ":" << line << ": " << msg << "\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace htab::test
|
||
|
|
|
||
|
|
#define HTAB_CONCAT_INNER(a, b) a##b
|
||
|
|
#define HTAB_CONCAT(a, b) HTAB_CONCAT_INNER(a, b)
|
||
|
|
|
||
|
|
#define TEST_CASE(name) \
|
||
|
|
static void HTAB_CONCAT(htab_test_fn_, __LINE__)(); \
|
||
|
|
static ::htab::test::Registrar HTAB_CONCAT(htab_test_reg_, __LINE__)( \
|
||
|
|
name, HTAB_CONCAT(htab_test_fn_, __LINE__)); \
|
||
|
|
static void HTAB_CONCAT(htab_test_fn_, __LINE__)()
|
||
|
|
|
||
|
|
#define CHECK(cond) \
|
||
|
|
do { \
|
||
|
|
if (!(cond)) { \
|
||
|
|
::htab::test::report_fail(__FILE__, __LINE__, "Condition failed: " #cond); \
|
||
|
|
} \
|
||
|
|
} while (false)
|
||
|
|
|
||
|
|
#define CHECK_EQ(a, b) \
|
||
|
|
do { \
|
||
|
|
auto htab_a_ = (a); \
|
||
|
|
auto htab_b_ = (b); \
|
||
|
|
if (!(htab_a_ == htab_b_)) { \
|
||
|
|
std::ostringstream oss_; \
|
||
|
|
oss_ << "Expected " << #a << " == " << #b << " (got " << htab_a_ << " vs " \
|
||
|
|
<< htab_b_ << ")"; \
|
||
|
|
::htab::test::report_fail(__FILE__, __LINE__, oss_.str()); \
|
||
|
|
} \
|
||
|
|
} while (false)
|
||
|
|
|
||
|
|
#define CHECK_NEAR(a, b, eps) \
|
||
|
|
do { \
|
||
|
|
double htab_a_ = static_cast<double>(a); \
|
||
|
|
double htab_b_ = static_cast<double>(b); \
|
||
|
|
double htab_eps_ = static_cast<double>(eps); \
|
||
|
|
if (!(std::fabs(htab_a_ - htab_b_) <= htab_eps_)) { \
|
||
|
|
std::ostringstream oss_; \
|
||
|
|
oss_ << "Expected " << #a << " ~= " << #b << " (got " << htab_a_ << " vs " \
|
||
|
|
<< htab_b_ << ")"; \
|
||
|
|
::htab::test::report_fail(__FILE__, __LINE__, oss_.str()); \
|
||
|
|
} \
|
||
|
|
} while (false)
|