Files
Structive/tests/test.hpp
T
2026-08-09 22:47:13 +08:00

31 lines
891 B
C++

#pragma once
#include <cstdio>
#include <cstdlib>
#include <utility>
namespace structive::test {
inline void check(bool condition, const char* expression, const char* file, int line) {
if (condition) {
return;
}
std::fprintf(stderr, "%s:%d: check failed: %s\n", file, line, expression);
std::abort();
}
template <class Exception, class Function>
bool throws_as(Function&& function) {
try {
std::forward<Function>(function)();
} catch (const Exception&) {
return true;
}
return false;
}
}
#define STRUCTIVE_CHECK(expression) ::structive::test::check(static_cast<bool>(expression), #expression, __FILE__, __LINE__)
#define STRUCTIVE_CHECK_THROWS_AS(expression, exception) \
STRUCTIVE_CHECK((::structive::test::throws_as<exception>([&] { static_cast<void>(expression); })))
#define REQUIRE(expression) STRUCTIVE_CHECK(expression)