diff options
| author | physick <mynameisgennadiy@vk.com> | 2026-08-12 18:33:52 +0500 |
|---|---|---|
| committer | physick <mynameisgennadiy@vk.com> | 2026-08-29 14:17:03 +0500 |
| commit | d16a1385efe911b1683e390f672cadee78b842be (patch) | |
| tree | 9c4a5e8a01952befa6b48c8fee4bd7471dbf421f /src | |
| parent | b1b2586afa06105291355a19f581016c347463bc (diff) | |
test system
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 3 | ||||
| -rw-r--r-- | src/tests/arena_tests.c | 39 | ||||
| -rw-r--r-- | src/tests/runner.c | 37 | ||||
| -rw-r--r-- | src/tests/test.h | 16 |
4 files changed, 92 insertions, 3 deletions
@@ -26,9 +26,6 @@ int main(void) { return 1; } - DeleteSceneFromStack(); - return 0; - // if (!AddScene(CreatePuzzleScene(puzzle))) { // printf("Failed to create a puzzle menu\n"); // return 1; diff --git a/src/tests/arena_tests.c b/src/tests/arena_tests.c new file mode 100644 index 0000000..b1c8aa8 --- /dev/null +++ b/src/tests/arena_tests.c @@ -0,0 +1,39 @@ +#include "test.h" +#include "../utils/arena.h" +#include <stdlib.h> +#include <string.h> + +char *ArenaAllocation() { + Arena arena = {0}; + ArenaInit(&arena, 1024); + + // Init + size_t elements = 10; + int *arr = ArenaAlloc(&arena, sizeof(int) * elements); + if (arr == NULL) return "The int array allocation failed"; + for (int i = 0; i < elements; ++i) { + arr[i] = i; + } + char *duped = ArenaStrdup(&arena, "Test string"); + if (arr == NULL) return "The string array allocation failed"; + + // Access + for (int i = 0; i < elements; ++i) { + if (arr[i] != i) return "Arena memory corruption in the int array"; + } + if (strcmp(duped, "Test string") != 0) { + return "Arena memory corruption in the string duping"; + } + + free(arena.mem); + return NULL; +} + +char *ArenaOutOfMemory() { + Arena arena = {0}; + ArenaInit(&arena, sizeof(int) * 2); + int *arr = ArenaAlloc(&arena, sizeof(int) * 3); + free(arena.mem); + if (arr != NULL) return "The int array allocation did not fail"; + return NULL; +} diff --git a/src/tests/runner.c b/src/tests/runner.c new file mode 100644 index 0000000..68c17bf --- /dev/null +++ b/src/tests/runner.c @@ -0,0 +1,37 @@ +#include "test.h" +#include <stdio.h> + +const UnitTest TESTS[] = { + (UnitTest) { .Name = "Arena out of mem", .Function = ArenaOutOfMemory }, + (UnitTest) { .Name = "Arena allocation", .Function = ArenaAllocation }, +}; + +void SetGreenColor() { + printf("\x1b[32m"); +} + +void SetRedColor() { + printf("\x1b[31m"); +} + +void ResetColor() { + printf("\x1b[39m"); +} + +int main(void) { + size_t length = sizeof(TESTS) / sizeof(TESTS[0]); + SetGreenColor(); + for (size_t i = 0; i < length; ++i) { + const UnitTest *test = &TESTS[i]; + char *outp = test->Function(); + if (outp == NULL) { + printf("Test '%s' passed\n", test->Name); + } else { + SetRedColor(); + printf("test '%s' failed: %s\n", test->Name, outp); + return 1; + } + } + ResetColor(); + return 0; +} diff --git a/src/tests/test.h b/src/tests/test.h new file mode 100644 index 0000000..d2c4aeb --- /dev/null +++ b/src/tests/test.h @@ -0,0 +1,16 @@ +#ifndef TESTS_H +#define TESTS_H + +typedef struct { + char *Name; + // The test function. If it returns an error message + // the test is concidered as failed + char *(*Function)(); +} UnitTest; + +// ========== TESTS ========== + +char *ArenaAllocation(); +char *ArenaOutOfMemory(); + +#endif // TESTS_H |
