From 6e0e6f743759f3d9ae20f653a50acbe94aa545a9 Mon Sep 17 00:00:00 2001 From: Ole Kristian Morud Date: Mon, 15 Jan 2024 14:54:06 +0100 Subject: [PATCH] Add rudimentary test --- .gitignore | 1 - test/CMakeLists.txt | 4 ++++ test/test_arena.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/test_arena.c diff --git a/.gitignore b/.gitignore index 9f534f1..ccac27a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ obj/ -test/ *.o *.a *.so diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..fd5e1e6 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_executable(arena_allocator_test test_arena.c ../src/arena.c) + +target_include_directories(arena_allocator_test PUBLIC ../include) diff --git a/test/test_arena.c b/test/test_arena.c new file mode 100644 index 0000000..ed07b63 --- /dev/null +++ b/test/test_arena.c @@ -0,0 +1,53 @@ + +#include "arena.h" +#include // exit(), EXIT_FAILURE +#include // memset +#include // fprintf +#include // sysconf + +int main() +{ + + printf("."); + + arena_t a = arena_new(); + if (arena_new_failed(&a)) { + fprintf(stderr, "\narena_new failed"); + exit(EXIT_FAILURE); + } + + size_t size = sysconf(_SC_PAGE_SIZE); + if (size == -1) { + perror("sysconf"); + exit(EXIT_FAILURE); + } + + // test many small allocations + for (int i = 0; i < size * 8; i++) { + char* s = arena_alloc(&a, 4 * sizeof *s); + if (!s) { + fprintf(stderr, "\narena_alloc failed"); + exit(EXIT_FAILURE); + } + memset(s, 'a', 4); + } + + // test a few allocations above the cap*2 + for (int i = 0; i < 2; i++) { + size_t n = a.cap * 3 + 123; + printf("allocating %d bytes\n", n); + volatile char* s = arena_alloc(&a, n); + if (!s) { + fprintf(stderr, "\narena_alloc failed"); + exit(EXIT_FAILURE); + } + } + + int ok = arena_delete(&a); + if (ok == -1) { + fprintf(stderr, "arena_delete failed"); + exit(EXIT_FAILURE); + } + + return EXIT_SUCCESS; +}