Add allocation test

This commit is contained in:
Justin R. Cutler 2016-04-06 02:08:25 -04:00
parent 31633b0b49
commit 75d8f63842
2 changed files with 87 additions and 0 deletions

View File

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

85
test/allocation.c Normal file
View File

@ -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 <justin.r.cutler@gmail.com>
*/
/* HAVE_* */
#include "config.h"
/* printf, perror */
#include <stdio.h>
/* EXIT_SUCCESS, EXIT_FAILURE */
#include <stdlib.h>
/* default_allocator_get */
#include <threadless/default_allocator.h>
#ifdef HAVE_MMAP
/* mmap_allocator_get */
# include <threadless/mmap_allocator.h>
#endif
/* ... */
#include <threadless/allocation.h>
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;
}