fix: replace unsafe fixed-size buffers with dynamic formatting helpers; add util format helpers; centralize log_on_line cleanup
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "util.h"
|
||||
|
||||
static jmp_buf s_testJmp;
|
||||
static const char* s_failMsg;
|
||||
@@ -49,8 +50,11 @@ char* read_file_content(const char* filepath) {
|
||||
}
|
||||
|
||||
void assert_log_file(const char* msg) {
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "v0/tests/%s.log", s_currentTestName);
|
||||
char* filepath = format_string("v0/tests/%s.log", s_currentTestName);
|
||||
if (!filepath) {
|
||||
fail("out of memory");
|
||||
return;
|
||||
}
|
||||
|
||||
const char* generate = getenv("GENERATE_GOLDEN");
|
||||
if (generate && strcmp(generate, "1") == 0) {
|
||||
@@ -67,18 +71,24 @@ void assert_log_file(const char* msg) {
|
||||
char* content = read_file_content(filepath);
|
||||
if (!content) {
|
||||
fail("could not open golden file for reading");
|
||||
free(filepath);
|
||||
return;
|
||||
}
|
||||
|
||||
assert_str(content, s_logOutput, msg);
|
||||
free(content);
|
||||
free(filepath);
|
||||
}
|
||||
|
||||
void assert_int(int expected, int actual, const char* msg) {
|
||||
if (expected != actual) {
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "%s (expected %d, got %d)", msg, expected, actual);
|
||||
fail(buf);
|
||||
char* buf = format_string("%s (expected %d, got %d)", msg, expected, actual);
|
||||
if (buf) {
|
||||
fail(buf);
|
||||
free(buf);
|
||||
} else {
|
||||
fail("out of memory");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,17 +105,22 @@ void assert_false(bool condition, const char* msg) {
|
||||
}
|
||||
|
||||
TokenStream* tokenstream_get_test(void) {
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "v0/tests/%s.c2", s_currentTestName);
|
||||
char* filepath = format_string("v0/tests/%s.c2", s_currentTestName);
|
||||
if (!filepath) {
|
||||
fail("out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (s_testSource) free(s_testSource);
|
||||
s_testSource = read_file_content(filepath);
|
||||
if (!s_testSource) {
|
||||
fail("could not read test source file");
|
||||
free(filepath);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return tokenstream_open(filepath, s_testSource);
|
||||
TokenStream* ts = tokenstream_open(filepath, s_testSource);
|
||||
free(filepath);
|
||||
return ts;
|
||||
}
|
||||
|
||||
static void log_append(const char* msg) {
|
||||
|
||||
Reference in New Issue
Block a user