threadless.io/test/allocation.c

107 lines
2.2 KiB
C

/* 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"
/* errno, EINVAL */
#include <errno.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 test_allocation(allocation_t *allocation, size_t size)
{
int error = allocation_realloc_array(allocation, 1, size);
if (!error && allocation->size != size) {
/* bad size */
errno = EINVAL;
error = -1;
}
return error;
}
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 = test_allocation(&allocation, size);
}
if (!error) {
size_t big = 1UL << (sizeof(size_t) * 4);
size = allocation.size;
error = !allocation_realloc_array(&allocation, big, big);
if (!error && allocation.size != size) {
/* bad size */
errno = EINVAL;
error = -1;
}
}
for (size = 1 << 22; !error && size; size = size >> 1) {
error = test_allocation(&allocation, 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;
}