test/mpool_tests.c

changeset 390
d345541018fa
parent 389
92e482410453
child 391
f094a53c1178
     1.1 --- a/test/mpool_tests.c	Mon Dec 30 09:54:10 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,239 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - */
    1.31 -
    1.32 -#include <inttypes.h>
    1.33 -
    1.34 -#include "mpool_tests.h"
    1.35 -
    1.36 -UCX_TEST(test_ucx_mempool_new) {
    1.37 -    UcxMempool *pool = ucx_mempool_new(16);
    1.38 -    UCX_TEST_BEGIN
    1.39 -    UCX_TEST_ASSERT(pool->size == 16, "wrong size");
    1.40 -    UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
    1.41 -    UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
    1.42 -    UCX_TEST_END
    1.43 -    ucx_mempool_destroy(pool);
    1.44 -}
    1.45 -
    1.46 -UCX_TEST(test_ucx_mempool_malloc) {
    1.47 -    
    1.48 -    UcxMempool *pool = ucx_mempool_new(1);
    1.49 -    UCX_TEST_BEGIN
    1.50 -    intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    1.51 -    
    1.52 -    UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
    1.53 -    UCX_TEST_ASSERT(pool->size == 1, "chcap called");
    1.54 -    
    1.55 -    intptr_t *pooladdr =
    1.56 -            (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
    1.57 -    *pooladdr = 5;
    1.58 -    
    1.59 -    UCX_TEST_ASSERT(*test == 5, "wrong pointer");
    1.60 -    
    1.61 -    UCX_TEST_END
    1.62 -    ucx_mempool_destroy(pool);
    1.63 -}
    1.64 -
    1.65 -UCX_TEST(test_ucx_mempool_malloc_with_chcap) {
    1.66 -    
    1.67 -    UcxMempool *pool = ucx_mempool_new(1);
    1.68 -    UCX_TEST_BEGIN
    1.69 -    ucx_mempool_malloc(pool, sizeof(int));
    1.70 -    intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    1.71 -    
    1.72 -    UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
    1.73 -    UCX_TEST_ASSERT(pool->size == 2, "chcap not called");
    1.74 -    
    1.75 -    intptr_t *pooladdr =
    1.76 -            (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
    1.77 -    *pooladdr = 5;
    1.78 -    
    1.79 -    UCX_TEST_ASSERT(*test == 5, "wrong pointer");
    1.80 -    
    1.81 -    // overflow test
    1.82 -    void *n0 = ucx_mempool_malloc(pool, (size_t)-1);
    1.83 -    void *n1 = ucx_mempool_malloc(pool, ((size_t)-1) - sizeof(void*)/2);
    1.84 -    
    1.85 -    UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
    1.86 -    UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
    1.87 -    
    1.88 -    UCX_TEST_END
    1.89 -    ucx_mempool_destroy(pool);
    1.90 -}
    1.91 -
    1.92 -UCX_TEST(test_ucx_mempool_calloc) {
    1.93 -    
    1.94 -    UcxMempool *pool = ucx_mempool_new(1);
    1.95 -    UCX_TEST_BEGIN
    1.96 -    
    1.97 -    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    1.98 -    
    1.99 -    UCX_TEST_ASSERT(test != NULL, "no memory for test data");
   1.100 -    UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
   1.101 -    
   1.102 -    // overflow test
   1.103 -    void *n0 = ucx_mempool_calloc(pool, (size_t)-1, 1);
   1.104 -    void *n1 = ucx_mempool_calloc(pool, ((size_t)-1)/2, 3);
   1.105 -    
   1.106 -    UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
   1.107 -    UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
   1.108 -    
   1.109 -    UCX_TEST_END
   1.110 -    ucx_mempool_destroy(pool);
   1.111 -}
   1.112 -
   1.113 -UCX_TEST(test_ucx_mempool_free) {
   1.114 -    UcxMempool *pool = ucx_mempool_new(16);
   1.115 -    void *mem1;
   1.116 -    void *mem2;
   1.117 -    
   1.118 -    UCX_TEST_BEGIN
   1.119 -    
   1.120 -    mem1 = ucx_mempool_malloc(pool, 16);
   1.121 -    ucx_mempool_free(pool, mem1);
   1.122 -    
   1.123 -    UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
   1.124 -    
   1.125 -    ucx_mempool_malloc(pool, 16);
   1.126 -    ucx_mempool_malloc(pool, 16);
   1.127 -    mem1 = ucx_mempool_malloc(pool, 16);
   1.128 -    ucx_mempool_malloc(pool, 16);
   1.129 -    mem2 = ucx_mempool_malloc(pool, 16);
   1.130 -    
   1.131 -    ucx_mempool_free(pool, mem1);
   1.132 -    
   1.133 -    UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
   1.134 -    
   1.135 -    ucx_mempool_free(pool, mem2);
   1.136 -    
   1.137 -    UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
   1.138 -    
   1.139 -    UCX_TEST_END
   1.140 -    ucx_mempool_destroy(pool);
   1.141 -}
   1.142 -
   1.143 -#ifdef __cplusplus
   1.144 -extern "C"
   1.145 -#endif
   1.146 -void test_setdestr(void* elem) {
   1.147 -    intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
   1.148 -    *cb = 42;
   1.149 -}
   1.150 -
   1.151 -UCX_TEST(test_ucx_mempool_set_destr) {
   1.152 -    
   1.153 -    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   1.154 -    UCX_TEST_BEGIN
   1.155 -    UcxMempool *pool = ucx_mempool_new(2);
   1.156 -    
   1.157 -    ucx_mempool_malloc(pool, sizeof(intptr_t));
   1.158 -    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   1.159 -    
   1.160 -    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   1.161 -    
   1.162 -    test[0] = 5; test[1] = (intptr_t) cb;
   1.163 -    *cb = 13;
   1.164 -    
   1.165 -    ucx_mempool_set_destr(test, test_setdestr);
   1.166 -    UCX_TEST_ASSERT(
   1.167 -            *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
   1.168 -    UCX_TEST_ASSERT(
   1.169 -            test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
   1.170 -    
   1.171 -    ucx_mempool_destroy(pool);
   1.172 -    
   1.173 -    UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   1.174 -    
   1.175 -    UCX_TEST_END
   1.176 -    if (cb != NULL) free(cb);
   1.177 -}
   1.178 -
   1.179 -
   1.180 -UCX_TEST(test_ucx_mempool_reg_destr) {
   1.181 -    
   1.182 -    intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   1.183 -    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   1.184 -    UCX_TEST_BEGIN
   1.185 -    UcxMempool *pool = ucx_mempool_new(1);
   1.186 -    
   1.187 -    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   1.188 -    
   1.189 -    test[0] = 5; test[1] = (intptr_t) cb;
   1.190 -    *cb = 13;
   1.191 -    
   1.192 -    ucx_mempool_reg_destr(pool, test, test_setdestr);
   1.193 -    
   1.194 -    ucx_destructor *pooladdr = (ucx_destructor*)
   1.195 -            ((char*)pool->data[0] + sizeof(ucx_destructor));
   1.196 -    
   1.197 -    UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
   1.198 -    
   1.199 -    ucx_mempool_destroy(pool);
   1.200 -    UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   1.201 -    UCX_TEST_END
   1.202 -
   1.203 -    if (test != NULL) free(test);
   1.204 -    if (cb != NULL) free(cb);
   1.205 -}
   1.206 -
   1.207 -UCX_TEST(test_ucx_mempool_realloc) {
   1.208 -
   1.209 -    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   1.210 -    UCX_TEST_BEGIN
   1.211 -    UcxMempool *pool = ucx_mempool_new(2);
   1.212 -    
   1.213 -    ucx_mempool_malloc(pool, sizeof(intptr_t));
   1.214 -    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   1.215 -
   1.216 -    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   1.217 -    
   1.218 -    test[0] = 5; test[1] = (intptr_t) cb;
   1.219 -    *cb = 13;
   1.220 -    
   1.221 -    ucx_mempool_set_destr(test, test_setdestr);
   1.222 -    
   1.223 -    intptr_t *rtest, n = 2;
   1.224 -    do {
   1.225 -        n *= 2;
   1.226 -        UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
   1.227 -        rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
   1.228 -    } while (rtest == test);
   1.229 -    test = rtest;
   1.230 -    
   1.231 -    UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   1.232 -            "realloc killed destructor")
   1.233 -    UCX_TEST_ASSERT(
   1.234 -            test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
   1.235 -    
   1.236 -    ucx_mempool_destroy(pool);
   1.237 -    
   1.238 -    UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   1.239 -    
   1.240 -    UCX_TEST_END
   1.241 -    if (cb != NULL) free(cb);
   1.242 -}

mercurial