During test, log to in-memory log

This commit is contained in:
2026-04-24 20:03:56 +02:00
parent 0e826e05e1
commit e021a2d63e
+26
View File
@@ -2,9 +2,11 @@
#include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static jmp_buf s_testJmp;
static const char* s_failMsg;
static char* s_logOutput = NULL;
void fail(const char* msg) {
s_failMsg = msg;
@@ -23,6 +25,27 @@ void assert_str(const char* expected, const char* actual, const char* msg) {
}
}
static void log_append(const char* msg) {
size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0;
size_t newLen = oldLen + strlen(msg) + 1;
char* newOutput = malloc(newLen);
if (newOutput) {
if (s_logOutput) {
strcpy(newOutput, s_logOutput);
free(s_logOutput);
} else {
newOutput[0] = '\0';
}
strcat(newOutput, msg);
s_logOutput = newOutput;
}
}
static void log_clear() {
free(s_logOutput);
s_logOutput = NULL;
}
typedef struct {
const char* name;
Test func;
@@ -59,11 +82,14 @@ int main(int argc, char** argv) {
const char* failedTests[s_totalTests + 1];
int failedCount = 0;
log_set_output(log_append);
for (int i = 0; i < s_totalTests; i++) {
printf("%s...", s_tests[i].name);
s_failMsg = NULL;
if (setjmp(s_testJmp) == 0) {
log_clear();
s_tests[i].func();
printf(" [OK]\n");
s_greenTests++;