From 78ae00c22ea7a8fd9030207d11885c893ff54305 Mon Sep 17 00:00:00 2001 From: ccgargantua Date: Sat, 18 May 2024 12:51:19 -0400 Subject: [PATCH] Improved test code --- test.c | 29 +++++++++++++-------- test.h | 79 +++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 74 insertions(+), 34 deletions(-) diff --git a/test.c b/test.c index 600a973..ca26a11 100644 --- a/test.c +++ b/test.c @@ -30,6 +30,13 @@ #include "test.h" +#define ARENA_DEBUG +#define ARENA_IMPLEMENTATION +#define ARENA_SUPPRESS_MALLOC_WARN +#define ARENA_DEFAULT_ALIGNMENT 0 +#include "arena.h" + + void test_arena_create(void) { Arena *arena = arena_create(0); @@ -231,17 +238,17 @@ void test_arena_delete_allocation_list(void) int main(void) { - SUITE(test_arena_create, "Arena creation suite"); - SUITE(test_arena_expand, "Arena reallocation suite"); - SUITE(test_arena_alloc, "Arena unaligned allocation suite"); - SUITE(test_arena_alloc_aligned, "Arena aligned allocation suite"); - SUITE(test_arena_copy, "Arena copy suite"); - SUITE(test_arena_clear, "Arena clearing suite"); - SUITE(test_arena_get_allocation_struct, "Arena debug method 'arena_get_allocation_struct' suite"); - SUITE(test_arena_add_allocation, "Arena debug method 'arena_add_allocation' suite"); - SUITE(test_arena_delete_allocation_list, "Arena debug method 'arena_delete_allocation_list' suite"); - - fprintf(stderr, "\nFinished. Passed %d/%d tests.\n", passed_tests, total_tests); + SUITE(test_arena_create); + SUITE(test_arena_expand); + SUITE(test_arena_alloc); + SUITE(test_arena_alloc_aligned); + SUITE(test_arena_copy); + SUITE(test_arena_clear); + SUITE(test_arena_get_allocation_struct); + SUITE(test_arena_add_allocation); + SUITE(test_arena_delete_allocation_list); + + WRAP_UP(); return 0; } diff --git a/test.h b/test.h index f897417..713a733 100644 --- a/test.h +++ b/test.h @@ -1,3 +1,32 @@ +// Tests are contained in testing suite functions, the forware declarations +// of which are bundled together. A testing suite function should exist for +// each function within `arena.h`, and should be placed relative to where +// the function they are testing is placed in regard to the functions around +// it for both forward declarations, implementations, and `SUITE`s. +// +// After implementing a testing suite, you should use the `SUITE` in +// `main()`. +// +// Within the testing suite, the following macros should be used: +// +// TEST_FATAL(exp, desc) | TEST_FATAL should be used whenever a failure +// could cause future tests to crash the process. +// If exp evaluates to false (0), the test executable +// will abort completely and the desc string will be +// printed. +// +// EX/ An `arena_create` failing and returning NULL +// could cause an access within the test to +// segfault. +// +// TEST_EQUAL(a, b) | TEST_EQUAL will fail when a != b +// +// TEST_NULL(a) | TEST_NULL will fail when a != NULL +// +// TEST_NOT_NULL(a) | TEST_NOT_NULL will fail when a == NULL +// +// TEST_ARRAY_EQUAL(a, b, s) | TEST_ARRAY_EQUAL will fail if any elements differ + #ifndef TEST_H #define TEST_H @@ -35,7 +64,7 @@ int buffer_index = 0; #define TEST(exp, msg) \ do \ { \ - if(!(exp)) \ + if (!(exp)) \ { \ REPORT(msg); \ } \ @@ -44,10 +73,18 @@ int buffer_index = 0; passed_tests++; \ } \ total_tests++; \ - }while(0) + }while (0) -#define TEST_FATAL(exp, msg) do{if(!(exp)){REPORT_FATAL(msg);return;}}while(0) +#define TEST_FATAL(exp, msg) \ +do \ +{ \ + if (!(exp)) \ + { \ + REPORT_FATAL(msg); \ + return; \ + } \ +}while (0) #define TEST_NULL(a) TEST(a == NULL, #a " is not NULL") @@ -60,36 +97,32 @@ int buffer_index = 0; { \ int i; \ total_tests++; \ - for(i = 0; i < s; i++) \ + for (i = 0; i < s; i++) \ { \ - if(a[i] != b[i]) \ + if (a[i] != b[i]) \ { \ REPORT(#a " does not equal " #b); \ break; \ } \ } \ passed_tests++; \ - }while(0) - - -#define SUITE(suite, name) \ - do \ - { \ - temp_passed = passed_tests; \ - temp_total = total_tests; \ - suite(); \ - fprintf(stderr, "Passed %d/%d tests in '%s'\n", passed_tests-temp_passed, total_tests-temp_total, name); \ - fprintf(stderr, "%s", buffer); \ - buffer_index = 0; \ - buffer[0] = '\0'; \ + }while (0) + + +#define SUITE(suite) \ + do \ + { \ + temp_passed = passed_tests; \ + temp_total = total_tests; \ + suite(); \ + fprintf(stderr, "Passed %d/%d tests in '%s'\n", passed_tests-temp_passed, total_tests-temp_total, #suite); \ + fprintf(stderr, "%s", buffer); \ + buffer_index = 0; \ + buffer[0] = '\0'; \ } while(0) -#define ARENA_DEBUG -#define ARENA_IMPLEMENTATION -#define ARENA_SUPPRESS_MALLOC_WARN -#define ARENA_DEFAULT_ALIGNMENT 0 -#include "arena.h" +#define WRAP_UP() fprintf(stderr, "\nFinished. Passed %d/%d tests.\n", passed_tests, total_tests); #endif \ No newline at end of file