Convert codebase to C89 compatibility and update test scripts

This commit is contained in:
2026-04-29 10:20:30 +02:00
parent 189c21667b
commit 146aa4d9d1
14 changed files with 287 additions and 192 deletions
+30 -14
View File
@@ -33,12 +33,16 @@ void assert_log(const char* expected, const char* msg) {
}
char* read_file_content(const char* filepath) {
FILE* f = fopen(filepath, "r");
FILE* f;
long size;
char* content;
f = fopen(filepath, "r");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long size = ftell(f);
size = ftell(f);
fseek(f, 0, SEEK_SET);
char* content = malloc(size + 1);
content = malloc(size + 1);
if (!content) {
fclose(f);
return NULL;
@@ -51,12 +55,13 @@ char* read_file_content(const char* filepath) {
void assert_log_file(const char* msg) {
char* filepath = format_string("v0/tests/%s.log", s_currentTestName);
const char* generate;
char* content;
if (!filepath) {
fail("out of memory");
return;
}
const char* generate = getenv("GENERATE_GOLDEN");
generate = getenv("GENERATE_GOLDEN");
if (generate && strcmp(generate, "1") == 0) {
FILE* f = fopen(filepath, "w");
if (!f) {
@@ -68,7 +73,7 @@ void assert_log_file(const char* msg) {
return;
}
char* content = read_file_content(filepath);
content = read_file_content(filepath);
if (!content) {
fail("could not open golden file for reading");
free(filepath);
@@ -105,7 +110,10 @@ void assert_false(bool condition, const char* msg) {
}
TokenStream* tokenstream_get_test(void) {
char* filepath = format_string("v0/tests/%s.c2", s_currentTestName);
char* filepath = NULL;
TokenStream* ts = NULL;
filepath = format_string("v0/tests/%s.c2", s_currentTestName);
if (!filepath) {
fail("out of memory");
return NULL;
@@ -118,7 +126,7 @@ TokenStream* tokenstream_get_test(void) {
free(filepath);
return NULL;
}
TokenStream* ts = tokenstream_open(filepath, s_testSource);
ts = tokenstream_open(filepath, s_testSource);
free(filepath);
return ts;
}
@@ -139,7 +147,7 @@ static void log_append(const char* msg) {
}
}
static void log_clear() {
static void log_clear(void) {
free(s_logOutput);
s_logOutput = NULL;
}
@@ -180,16 +188,23 @@ static TestCase s_tests[] = {
int main(int argc, char** argv) {
/* Declarations first for C89 */
const char** failedTests;
int failedCount;
int i;
int j;
(void)argc;
(void)argv;
s_totalTests = sizeof(s_tests) / sizeof(s_tests[0]);
s_greenTests = 0;
const char* failedTests[s_totalTests + 1];
int failedCount = 0;
/* Allocate failed tests array dynamically to avoid VLAs */
failedTests = (const char**)malloc((s_totalTests + 1) * sizeof(const char*));
failedCount = 0;
for (int i = 0; i < s_totalTests; i++) {
for (i = 0; i < s_totalTests; i++) {
s_currentTestName = s_tests[i].name;
log_set_output(log_append);
printf("%s...", s_tests[i].name);
@@ -215,11 +230,12 @@ int main(int argc, char** argv) {
if (failedCount > 0) {
printf("\nFailed tests:\n");
for (int i = 0; i < failedCount; i++) {
printf(" - %s\n", failedTests[i]);
for (j = 0; j < failedCount; j++) {
printf(" - %s\n", failedTests[j]);
}
}
printf("\n%d/%d tests passed.\n", s_greenTests, s_totalTests);
free(failedTests);
return failedCount > 0 ? 1 : 0;
}