From 3bdccf200094ca6baf54a60ea5184d9be044d905 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Thu, 30 Apr 2026 22:21:08 +0200 Subject: [PATCH] Add integration test framework --- Makefile | 8 +++- README.md | 16 +++++++ actual.c | 1 + v0/integration_tests/simple/expected.c | 1 + v0/integration_tests/simple/input.c2 | 2 + v0/test_integration.c | 63 ++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 actual.c create mode 100644 v0/integration_tests/simple/expected.c create mode 100644 v0/integration_tests/simple/input.c2 create mode 100644 v0/test_integration.c diff --git a/Makefile b/Makefile index 479bc6c..cc6ba5e 100644 --- a/Makefile +++ b/Makefile @@ -12,4 +12,10 @@ generate_golden:: clean:: rm -f c2 -include v0/include.mk \ No newline at end of file +include v0/include.mk + +integration-test: v0/bin/c2 v0/bin/test_integration + ./v0/bin/test_integration + +v0/bin/test_integration: v0/test_integration.c + $(CC) $(CFLAGS) -o $@ $< \ No newline at end of file diff --git a/README.md b/README.md index 7f442ee..24ffa8c 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,21 @@ In order to run the tests, run `make test`. ## Versioning The current version is v0. Its source code lives in the `v0` directory. +## Testing + +### Unit Tests +Run unit tests with: +```bash +make test +``` + +### Integration Tests +Integration tests compare the compiler output with expected C files. +To add a new integration test, create a new directory under `v0/integration_tests/` with `input.c2` and `expected.c` files. +Run integration tests with: +```bash +make integration-test +``` + ## Languages Specifications See the specs directory for information on the actual language syntax. diff --git a/actual.c b/actual.c new file mode 100644 index 0000000..a5c1966 --- /dev/null +++ b/actual.c @@ -0,0 +1 @@ +Hello, world diff --git a/v0/integration_tests/simple/expected.c b/v0/integration_tests/simple/expected.c new file mode 100644 index 0000000..a5c1966 --- /dev/null +++ b/v0/integration_tests/simple/expected.c @@ -0,0 +1 @@ +Hello, world diff --git a/v0/integration_tests/simple/input.c2 b/v0/integration_tests/simple/input.c2 new file mode 100644 index 0000000..7770f3b --- /dev/null +++ b/v0/integration_tests/simple/input.c2 @@ -0,0 +1,2 @@ +module simple; +u32 x = 123; \ No newline at end of file diff --git a/v0/test_integration.c b/v0/test_integration.c new file mode 100644 index 0000000..13423e8 --- /dev/null +++ b/v0/test_integration.c @@ -0,0 +1,63 @@ +#define _DEFAULT_SOURCE +#include +#include +#include +#include + +int run_test(const char* dir_name) { + char cmd[2048]; + char input_path[1024]; + char expected_path[1024]; + + snprintf(input_path, sizeof(input_path), "v0/integration_tests/%s/input.c2", dir_name); + snprintf(expected_path, sizeof(expected_path), "v0/integration_tests/%s/expected.c", dir_name); + + if (snprintf(cmd, sizeof(cmd), "./v0/bin/c2 %s > actual.c", input_path) >= sizeof(cmd)) { + printf("Command buffer too small for %s\n", dir_name); + return 1; + } + + if (system(cmd) != 0) { + printf("Failed to run compiler for %s\n", dir_name); + return 1; + } + + if (snprintf(cmd, sizeof(cmd), "diff -u %s actual.c", expected_path) >= sizeof(cmd)) { + printf("Command buffer too small for %s\n", dir_name); + return 1; + } + + if (system(cmd) != 0) { + printf("Test %s failed: Output mismatch\n", dir_name); + return 1; + } + + printf("Test %s passed\n", dir_name); + return 0; +} + +int main() { + DIR* d = opendir("v0/integration_tests"); + if (!d) { + perror("opendir"); + return 1; + } + + struct dirent* dir; + int passed = 0; + int failed = 0; + + while ((dir = readdir(d)) != NULL) { + if (dir->d_type == DT_DIR && strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) { + if (run_test(dir->d_name) == 0) { + passed++; + } else { + failed++; + } + } + } + closedir(d); + + printf("\nTotal tests: %d, Passed: %d, Failed: %d\n", passed + failed, passed, failed); + return failed > 0 ? 1 : 0; +}