update torture

* factor test registration logic out of the macro to improve debuggability
* expand the test registration logic
* rename group -> layer for consistency
* fix indentation in the slow tests summary
This commit is contained in:
Nikita Smith
2026-06-19 20:12:22 -07:00
committed by Ryan Fleury
parent 0d62c49c52
commit 8ef58def99
4 changed files with 101 additions and 91 deletions
+41 -48
View File
@@ -17,20 +17,20 @@ TestStatus;
typedef struct TestResult TestResult;
struct TestResult
{
TestStatus status;
char *fail_file;
int fail_line;
char *fail_cond;
TestStatus status;
char *fail_file;
int fail_line;
char *fail_cond;
};
typedef struct TestCtx TestCtx;
struct TestCtx
{
CmdLine *cmdline;
String8 exemplars_path;
String8 artifacts_path;
String8 input_data_path;
TestResult *result_out;
CmdLine *cmdline;
String8 exemplars_path;
String8 artifacts_path;
String8 input_data_path;
TestResult *result_out;
String8List *test_out;
};
@@ -41,39 +41,33 @@ typedef TEST_FUNCTION_SIG(TestFunctionType);
typedef struct TestInfo TestInfo;
struct TestInfo
{
String8 layer;
String8 label;
S64 decl_line;
B32 skip;
String8 layer;
String8 label;
S64 decl_line;
B32 skip;
TestFunctionType *test_fn;
};
#if BUILD_TESTS
global U64 test_infos_count = 0;
// alloc storage for the tests
global U16 test_infos_count = 0;
global TestInfo test_infos[0xffff] = {0};
#define AddTest(name, file_name, line, skip_, ...) \
TEST_FUNCTION_DEF(name);\
__VA_ARGS__ void add_test__##name(void)\
{\
String8 file = str8_lit(file_name);\
U64 src_pos = str8_find_needle(file, 0, s("src/"), StringMatchFlag_SlashInsensitive);\
String8 layer_folder = str8_skip(file, src_pos+4);\
U64 layer_slash_pos = str8_find_needle(layer_folder, 0, s("/"), StringMatchFlag_SlashInsensitive);\
String8 layer_name = str8_prefix(layer_folder, layer_slash_pos);\
test_infos[test_infos_count].layer = layer_name;\
test_infos[test_infos_count].label = str8_lit(#name);\
test_infos[test_infos_count].decl_line = (line);\
test_infos[test_infos_count].skip = (skip_);\
test_infos[test_infos_count].test_fn = test__##name;\
test_infos_count += 1;\
}
internal void base_register_test(char *func_name, TestFunctionType *fn, char *file_path, int line, int skip);
#define AddTest(name, file_path, line, skip_, ...) \
TEST_FUNCTION_DEF(name); \
__VA_ARGS__ void add_test__##name(void) { base_register_test(Stringify(name), test__##name, file_path, line, skip_); }
# if COMPILER_MSVC
# pragma section(".CRT$XCU", read)
# define DeclareTest(name, skip) \
AddTest(name, __FILE__, __LINE__, (skip))\
__declspec(allocate(".CRT$XCU")) void (*add_test_ptr__##name)(void) = add_test__##name;\
__pragma(comment(linker, "/include:" Stringify(add_test_ptr__##name)))
# define DeclareTest(name, skip) \
/* register test */ AddTest(name, __FILE__, __LINE__, (skip)) \
/* alloc function pointer */ __declspec(allocate(".CRT$XCU")) void (*add_test_ptr__##name)(void) = add_test__##name; \
/* do not GC test caller */ __pragma(comment(linker, "/include:" Stringify(add_test_ptr__##name)))
# elif COMPILER_GCC || COMPILER_CLANG
// clang and gcc allocate memory for the function pointer automatically
# define DeclareTest(name, skip) AddTest(name, __FILE__, __LINE__, (skip), __attribute__((constructor)))
# else
# error DeclareTest not defined for this compiler.
@@ -82,23 +76,22 @@ __pragma(comment(linker, "/include:" Stringify(add_test_ptr__##name)))
# define DeclareTest(name, skip)
#endif
#define Test(name) DeclareTest(name, 0)\
TEST_FUNCTION_DEF(name)
#define SkippedTest(name) DeclareTest(name, 1)\
TEST_FUNCTION_DEF(name)
#define Test(name) DeclareTest(name, 0) TEST_FUNCTION_DEF(name)
#define SkippedTest(name) DeclareTest(name, 1) TEST_FUNCTION_DEF(name)
#define test_out(string) str8_list_push(arena, ctx->test_out, (string))
#define test_outf(...) str8_list_pushf(arena, ctx->test_out, __VA_ARGS__)
#define TestCheck(c) do { if (!(c)) {\
ctx->result_out[0] = (TestResult){ .fail_file = __FILE__, .fail_line = __LINE__, .fail_cond = Stringify(c) };\
if(debugger_is_attached()) { Trap(); }\
return;\
#define TestCheck(c) do { if (!(c)) { \
/* record failed check */ ctx->result_out[0] = (TestResult){ .fail_file = __FILE__, .fail_line = __LINE__, .fail_cond = Stringify(c) }; \
/* under debugger? -> trap */ if(debugger_is_attached()) { Trap(); } \
/* exit test */ return; \
} } while(0)
#define TestSkip() do {\
ctx->result_out[0] = (TestResult){ .status = TestStatus_Skip };\
return;\
#define TestSkip() do { \
ctx->result_out[0] = (TestResult){ .status = TestStatus_Skip }; \
return; \
} while(0)
// test log
#define test_out(string) str8_list_push(arena, ctx->test_out, (string))
#define test_outf(...) str8_list_pushf(arena, ctx->test_out, __VA_ARGS__)
#endif // BASE_TEST_H