valeri/test/test.hpp

83 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
2024-08-02 22:27:36 +00:00
#include <algorithm>
#include <cstdint>
2024-08-02 22:27:36 +00:00
#include <cstdio>
2024-08-03 17:53:14 +00:00
#include <type_traits>
2024-08-02 22:27:36 +00:00
#include "common.hpp"
#include "result.hpp"
const uint64_t MAX_TESTS = 1024 * 1024;
static void (*tests[MAX_TESTS])() = {0};
const char* add_test(void (*fun)(void)) {
for (uint64_t i = 0; i < MAX_TESTS; ++i) {
if (tests[i] == 0) {
tests[i] = fun;
break;
}
}
return 0;
}
bool run_tests() {
for (uint64_t i = 0; i < MAX_TESTS; ++i) {
if (tests[i] == 0) break;
tests[i]();
}
return true;
}
#define CONCAT_IMPL(x, y) x##y
#define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
#define TEST_CASE_IMPL(ID, ...) \
static void ID(); \
static const char* MACRO_CONCAT(test_id_, __COUNTER__) [[maybe_unused]] = \
add_test(&ID); \
void ID()
#define TEST_CASE(fun_) TEST_CASE_IMPL(MACRO_CONCAT(fun_, _COUNTER_))
2024-08-03 17:53:14 +00:00
Result<Value> to_value(Arena& arena, Value& val) { return val.copy(arena); }
Result<Value> to_value(Arena& arena, const Value& val) {
return val.copy(arena);
}
2024-08-02 22:27:36 +00:00
template <class T>
2024-08-03 17:53:14 +00:00
requires std::derived_from<T, Object>
Result<Value> to_value(Arena& arena, T& val) {
return val.copy(arena);
}
2024-08-02 22:27:36 +00:00
template <class T>
2024-08-03 17:53:14 +00:00
requires(!std::derived_from<T, Object> && !std::is_fundamental<T>::value)
Result<Value> to_value(Arena& arena, T& val) {
return Value::create(arena, val);
2024-08-02 22:27:36 +00:00
}
template <class T>
2024-08-03 17:53:14 +00:00
requires(!std::derived_from<T, Object> && std::is_fundamental<T>::value)
Result<Value> to_value(Arena& arena, T val) {
return Value::create(arena, val);
2024-08-02 22:27:36 +00:00
}
#define ASSERT_EQUALS(lhs, rhs) \
do { \
2024-08-03 17:53:14 +00:00
auto lv = DIEX(to_value(arena, lhs)); \
auto rv = DIEX(to_value(arena, rhs)); \
2024-08-03 12:43:59 +00:00
short c = DIEX(lv.cmp(arena, rv)); \
2024-08-02 22:27:36 +00:00
if (c != 0) { \
fprintf(stderr, "Values not equal at %s:%d\n", __FILE__, __LINE__); \
debug_print(arena, lv); \
debug_print(arena, rv); \
exit(EXIT_FAILURE); \
} \
} while (0)
int main() { return run_tests() ? 0 : 1; }