Add rudimentary test
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,4 @@
|
|||||||
obj/
|
obj/
|
||||||
test/
|
|
||||||
*.o
|
*.o
|
||||||
*.a
|
*.a
|
||||||
*.so
|
*.so
|
||||||
|
|||||||
4
test/CMakeLists.txt
Normal file
4
test/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
add_executable(arena_allocator_test test_arena.c ../src/arena.c)
|
||||||
|
|
||||||
|
target_include_directories(arena_allocator_test PUBLIC ../include)
|
||||||
53
test/test_arena.c
Normal file
53
test/test_arena.c
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
#include "arena.h"
|
||||||
|
#include <stdlib.h> // exit(), EXIT_FAILURE
|
||||||
|
#include <string.h> // memset
|
||||||
|
#include <stdio.h> // fprintf
|
||||||
|
#include <unistd.h> // 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user