From 75d8f63842fbbf08cb0fe59605b72a9ce9c83149 Mon Sep 17 00:00:00 2001 From: "Justin R. Cutler" Date: Wed, 6 Apr 2016 02:08:25 -0400 Subject: [PATCH] Add allocation test --- CMakeLists.txt | 2 ++ test/allocation.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/allocation.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a95efa2..4e1f979 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,5 +29,7 @@ if(HAVE_MMAP) set(ALLOCATORS ${ALLOCATORS} mmap_allocator) endif() +add_executable(test-allocation test/allocation.c) +target_link_libraries(test-allocation LINK_PUBLIC allocation ${ALLOCATORS}) add_executable(test-coroutine test/coroutine.c) target_link_libraries(test-coroutine LINK_PUBLIC coroutine ${ALLOCATORS}) diff --git a/test/allocation.c b/test/allocation.c new file mode 100644 index 0000000..de07f17 --- /dev/null +++ b/test/allocation.c @@ -0,0 +1,85 @@ +/* threadless.io + * Copyright (c) 2016 Justin R. Cutler + * Licensed under the MIT License. See LICENSE file in the project root for + * full license information. + */ +/** @file + * allocation interface test + * @author Justin R. Cutler + */ + +/* HAVE_* */ +#include "config.h" + +/* printf, perror */ +#include +/* EXIT_SUCCESS, EXIT_FAILURE */ +#include + +/* default_allocator_get */ +#include +#ifdef HAVE_MMAP +/* mmap_allocator_get */ +# include +#endif +/* ... */ +#include + + +static int run(allocator_t *allocator) +{ + int error = 0; + allocation_t allocation; + size_t size; + + allocation_init(&allocation, allocator); + + for (size = 1; !error && size < (1 << 22); size = size << 1) { + error = allocation_realloc_array(&allocation, 1, size); + } + + if (!error) { + size_t big = 1UL << (sizeof(size_t) * 4); + error = !allocation_realloc_array(&allocation, big, big); + } + + for (size = 1 << 22; !error && size; size = size >> 1) { + error = allocation_realloc_array(&allocation, 1, size); + } + + if (!error) { + printf("OK\n"); + } else { + perror("allocation_realloc_array"); + } + + allocation_free(&allocation); + + return error; +} + + +int main(int argc, char *argv[]) +{ + int error; + allocator_t *allocator; + + (void) argc; + (void) argv; + + printf("default allocator:\n"); + allocator = default_allocator_get(); + error = run(allocator); + allocator_destroy(allocator); + +#ifdef HAVE_MMAP + if (!error) { + printf("mmap allocator:\n"); + allocator = mmap_allocator_get(); + error = run(allocator); + allocator_destroy(allocator); + } +#endif + + return !error ? EXIT_SUCCESS : EXIT_FAILURE; +}