Update tests

Add test for arena_attach
This commit is contained in:
2024-01-15 16:33:17 +01:00
parent 0af6582a64
commit 6b46af52aa
4 changed files with 92 additions and 32 deletions

View File

@@ -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)

View File

@@ -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)

6
test/README Normal file
View File

@@ -0,0 +1,6 @@
run compiled test in conjunction with other tools, e.g.
/usr/bin/time strace -e trace=\!write ./test/test_arena

View File

@@ -1,53 +1,105 @@
#include "arena.h"
#include "knob.h"
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <string.h> // memset
#include <stdio.h> // fprintf
#include <unistd.h> // 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;
}