Implement buffer interface and tests

- Implement buffer_open_file/string, buffer_read, buffer_close in v0/src/buffer.c

- Add tests in v0/tests/test_buffer.c and test_buffer.txt; register tests in v0/tests/test.c

- Update v0/include.mk and Makefile

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-24 08:07:38 +02:00
parent f6a1b290fc
commit 7ff3f76de5
7 changed files with 185 additions and 25 deletions
+53 -12
View File
@@ -1,24 +1,65 @@
#include "test.h"
#include "../src/buffer.h"
#include <setjmp.h>
#include <stdio.h>
// @copilot implement fail()
static jmp_buf s_testJmp;
static const char* s_failMsg;
void fail(const char* msg) {
s_failMsg = msg;
longjmp(s_testJmp, 1);
}
typedef struct {
const char* name;
Test func;
} TestCase;
#include "test_buffer.c"
static int s_totalTests;
static int s_greenTests;
static Test[] = {
// @copilot add all tests
static TestCase s_tests[] = {
{"buffer_string_reads_chars", test_buffer_string_reads_chars},
{"buffer_string_eof", test_buffer_string_eof},
{"buffer_string_eof_after_content", test_buffer_string_eof_after_content},
{"buffer_file_reads_chars", test_buffer_file_reads_chars},
{"buffer_file_open_fail", test_buffer_file_open_fail},
};
int main(int argc, char** argv) {
/*
@copilot loop over each test.
If the test is good, count the test as part of green test.
Otherwise, go to the next one.
(void)argc;
(void)argv;
The start of the test must log the name of the test method.
After the test, log either "[OK]" or "[FAIL]".
s_totalTests = sizeof(s_tests) / sizeof(s_tests[0]);
s_greenTests = 0;
At the very end, log the name of each failed test and the total
number of green tests compared to the total.
*/
const char* failedTests[s_totalTests + 1];
int failedCount = 0;
for (int i = 0; i < s_totalTests; i++) {
printf("%s...", s_tests[i].name);
s_failMsg = NULL;
if (setjmp(s_testJmp) == 0) {
s_tests[i].func();
printf(" [OK]\n");
s_greenTests++;
} else {
printf(" [FAIL]: %s\n", s_failMsg ? s_failMsg : "");
failedTests[failedCount++] = s_tests[i].name;
}
}
if (failedCount > 0) {
printf("\nFailed tests:\n");
for (int i = 0; i < failedCount; i++) {
printf(" - %s\n", failedTests[i]);
}
}
printf("\n%d/%d tests passed.\n", s_greenTests, s_totalTests);
return failedCount > 0 ? 1 : 0;
}