colla/tests/runner.h
2025-09-28 16:31:22 +02:00

45 lines
1.1 KiB
C

#pragma once
#include "../colla.h"
typedef struct unit_test_t unit_test_t;
struct unit_test_t {
strview_t fname;
strview_t name;
void (*fn)(void);
unit_test_t *next;
};
extern unit_test_t *test_head;
extern unit_test_t *test_tail;
extern const char *last_fail_reason;
extern bool last_failed;
void ut_register(const char *file, const char *name, void (*fn)(void));
// #pragma data_seg(".CRT$XCU")
#if COLLA_WIN
#define INITIALIZER(f) \
static void f(void); \
__declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
__pragma(comment(linker,"/include:" #f "_")) \
static void f(void)
#else
#define INITIALIZER(f) \
__attribute__((constructor)) static void f(void)
#endif
#define UNIT_TEST(name) \
void ut__test_##name(void); \
INITIALIZER(ut__register_##name) { ut_register(__FILE__, #name, ut__test_##name); } \
void ut__test_##name(void)
#define ASSERT(cond) \
if (!(cond)) { \
last_fail_reason = "assert(" COLLA_STRINGIFY(cond) ") at " COLLA_STRINGIFY(__LINE__); \
last_failed = true; \
return; \
}