Skip to content

Commit

Permalink
Improved test code
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgargantua committed May 18, 2024
1 parent 51ced78 commit 78ae00c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 34 deletions.
29 changes: 18 additions & 11 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
79 changes: 56 additions & 23 deletions test.h
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -35,7 +64,7 @@ int buffer_index = 0;
#define TEST(exp, msg) \
do \
{ \
if(!(exp)) \
if (!(exp)) \
{ \
REPORT(msg); \
} \
Expand All @@ -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")
Expand All @@ -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

0 comments on commit 78ae00c

Please sign in to comment.