From 6b46af52aa86780783e2dad5c41ae0f05124575c Mon Sep 17 00:00:00 2001 From: Ole Kristian Morud Date: Mon, 15 Jan 2024 16:33:17 +0100 Subject: [PATCH] Update tests Add test for arena_attach --- src/CMakeLists.txt | 4 +- test/CMakeLists.txt | 6 ++- test/README | 6 +++ test/test_arena.c | 108 ++++++++++++++++++++++++++++++++------------ 4 files changed, 92 insertions(+), 32 deletions(-) create mode 100644 test/README diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d9ca6e3..aaa5d09 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(arena_allocator arena.c) +add_library(arena arena.c) -target_include_directories(arena_allocator PUBLIC ../include) +target_include_directories(arena PUBLIC ../include) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd5e1e6..74f75c4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,6 @@ -add_executable(arena_allocator_test test_arena.c ../src/arena.c) +include_directories(${CMAKE_SOURCE_DIR}/include) -target_include_directories(arena_allocator_test PUBLIC ../include) +add_executable(test_arena test_arena.c) + +target_link_libraries(test_arena arena) diff --git a/test/README b/test/README new file mode 100644 index 0000000..5ee62ce --- /dev/null +++ b/test/README @@ -0,0 +1,6 @@ + +run compiled test in conjunction with other tools, e.g. + +/usr/bin/time strace -e trace=\!write ./test/test_arena + + diff --git a/test/test_arena.c b/test/test_arena.c index ed07b63..54e8ea3 100644 --- a/test/test_arena.c +++ b/test/test_arena.c @@ -1,53 +1,105 @@ #include "arena.h" +#include "knob.h" + #include // exit(), EXIT_FAILURE #include // memset #include // fprintf #include // sysconf + + int main() { + // calling print functions early makes my strace output more readable + printf("\n"); + fprintf(stderr, "\n"); - 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) { + size_t page_size = sysconf(_SC_PAGE_SIZE); + if (page_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"); + /* + * test arena from arena_new(); + * =============================== + */ + { + write(STDERR_FILENO, "\n\n", 2); + arena_t a = arena_new(); + if (arena_new_failed(&a)) { + fprintf(stderr, "arena_new failed\n"); 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"); + // test many small allocations + for (int i = 0; i < page_size * 8; i++) { + char* s = arena_alloc(&a, 4 * sizeof *s); + if (!s) { + fprintf(stderr, "arena_alloc failed\n"); + 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, "arena_alloc failed\n"); + exit(EXIT_FAILURE); + } + } + + int ok = arena_delete(&a); + if (ok == -1) { + fprintf(stderr, "arena_delete failed\n"); exit(EXIT_FAILURE); } } - int ok = arena_delete(&a); - if (ok == -1) { - fprintf(stderr, "arena_delete failed"); - exit(EXIT_FAILURE); + /* + * test arena made by arena_attach(); + * =============================== + */ + { + write(STDERR_FILENO, "\n\n", 2); + char* p = malloc(page_size); + arena_t a = arena_attach(p, page_size); + + bool failed = false; + + // attempt to grow more than the buffer size + int i; + for (i=0; i < page_size + 1; i++) { + char* s = arena_alloc(&a, 8); + if (!s) { + failed = true; + break; + } + } + + if (!failed) { + fprintf(stderr, "allocation was supposed to fail, but didn't\n"); + exit(EXIT_FAILURE); + } else if (i != page_size / KNOB_ALIGNMENT ) { + fprintf(stderr, "allocation failed after %d allocations, should instead fail after %d. Bad!\n", i, page_size/sizeof(void*)); + exit(EXIT_FAILURE); + } else { + fprintf(stderr, "arena from arena_attach failed after %d allocations, good!", i); + } + + // deleting an arena not made with arena_new should fail + int ok = arena_delete(&a); + if (ok != -1) { + fprintf(stderr, "arena_delete was supposed to fail, but didn't\n"); + exit(EXIT_FAILURE); + } } + return EXIT_SUCCESS; }