test/mpool_tests.c

Sun, 21 Jan 2018 14:10:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jan 2018 14:10:59 +0100
changeset 273
9c1591b3c4a4
parent 270
3d80d425543b
permissions
-rw-r--r--

fixes return value for multiplication with zero in ucx_szmul

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include <inttypes.h>
    31 #include "mpool_tests.h"
    33 UCX_TEST(test_ucx_mempool_new) {
    34     UcxMempool *pool = ucx_mempool_new(16);
    35     UCX_TEST_BEGIN
    36     UCX_TEST_ASSERT(pool->size == 16, "wrong size");
    37     UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
    38     UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
    39     UCX_TEST_END
    40     ucx_mempool_destroy(pool);
    41 }
    43 UCX_TEST(test_ucx_mempool_malloc) {
    45     UcxMempool *pool = ucx_mempool_new(1);
    46     UCX_TEST_BEGIN
    47     intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    49     UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
    50     UCX_TEST_ASSERT(pool->size == 1, "chcap called");
    52     intptr_t *pooladdr =
    53             (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
    54     *pooladdr = 5;
    56     UCX_TEST_ASSERT(*test == 5, "wrong pointer");
    58     UCX_TEST_END
    59     ucx_mempool_destroy(pool);
    60 }
    62 UCX_TEST(test_ucx_mempool_malloc_with_chcap) {
    64     UcxMempool *pool = ucx_mempool_new(1);
    65     UCX_TEST_BEGIN
    66     ucx_mempool_malloc(pool, sizeof(int));
    67     intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    69     UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
    70     UCX_TEST_ASSERT(pool->size == 2, "chcap not called");
    72     intptr_t *pooladdr =
    73             (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
    74     *pooladdr = 5;
    76     UCX_TEST_ASSERT(*test == 5, "wrong pointer");
    78     // overflow test
    79     void *n0 = ucx_mempool_malloc(pool, (size_t)-1);
    80     void *n1 = ucx_mempool_malloc(pool, ((size_t)-1) - sizeof(void*)/2);
    82     UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
    83     UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
    85     UCX_TEST_END
    86     ucx_mempool_destroy(pool);
    87 }
    89 UCX_TEST(test_ucx_mempool_calloc) {
    91     UcxMempool *pool = ucx_mempool_new(1);
    92     UCX_TEST_BEGIN
    94     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    96     UCX_TEST_ASSERT(test != NULL, "no memory for test data");
    97     UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
    99     // overflow test
   100     void *n0 = ucx_mempool_calloc(pool, (size_t)-1, 1);
   101     void *n1 = ucx_mempool_calloc(pool, ((size_t)-1)/2, 3);
   103     UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
   104     UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
   106     UCX_TEST_END
   107     ucx_mempool_destroy(pool);
   108 }
   110 UCX_TEST(test_ucx_mempool_free) {
   111     UcxMempool *pool = ucx_mempool_new(16);
   112     void *mem1;
   113     void *mem2;
   115     UCX_TEST_BEGIN
   117     mem1 = ucx_mempool_malloc(pool, 16);
   118     ucx_mempool_free(pool, mem1);
   120     UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
   122     ucx_mempool_malloc(pool, 16);
   123     ucx_mempool_malloc(pool, 16);
   124     mem1 = ucx_mempool_malloc(pool, 16);
   125     ucx_mempool_malloc(pool, 16);
   126     mem2 = ucx_mempool_malloc(pool, 16);
   128     ucx_mempool_free(pool, mem1);
   130     UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
   132     ucx_mempool_free(pool, mem2);
   134     UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
   136     UCX_TEST_END
   137     ucx_mempool_destroy(pool);
   138 }
   140 #ifdef __cplusplus
   141 extern "C"
   142 #endif
   143 void test_setdestr(void* elem) {
   144     intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
   145     *cb = 42;
   146 }
   148 UCX_TEST(test_ucx_mempool_set_destr) {
   150     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   151     UCX_TEST_BEGIN
   152     UcxMempool *pool = ucx_mempool_new(2);
   154     ucx_mempool_malloc(pool, sizeof(intptr_t));
   155     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   157     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   159     test[0] = 5; test[1] = (intptr_t) cb;
   160     *cb = 13;
   162     ucx_mempool_set_destr(test, test_setdestr);
   163     UCX_TEST_ASSERT(
   164             *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
   165     UCX_TEST_ASSERT(
   166             test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
   168     ucx_mempool_destroy(pool);
   170     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   172     UCX_TEST_END
   173     if (cb != NULL) free(cb);
   174 }
   177 UCX_TEST(test_ucx_mempool_reg_destr) {
   179     intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   180     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   181     UCX_TEST_BEGIN
   182     UcxMempool *pool = ucx_mempool_new(1);
   184     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   186     test[0] = 5; test[1] = (intptr_t) cb;
   187     *cb = 13;
   189     ucx_mempool_reg_destr(pool, test, test_setdestr);
   191     ucx_destructor *pooladdr = (ucx_destructor*)
   192             ((char*)pool->data[0] + sizeof(ucx_destructor));
   194     UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
   196     ucx_mempool_destroy(pool);
   197     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   198     UCX_TEST_END
   200     if (test != NULL) free(test);
   201     if (cb != NULL) free(cb);
   202 }
   204 UCX_TEST(test_ucx_mempool_realloc) {
   206     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   207     UCX_TEST_BEGIN
   208     UcxMempool *pool = ucx_mempool_new(2);
   210     ucx_mempool_malloc(pool, sizeof(intptr_t));
   211     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   213     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   215     test[0] = 5; test[1] = (intptr_t) cb;
   216     *cb = 13;
   218     ucx_mempool_set_destr(test, test_setdestr);
   220     intptr_t *rtest, n = 2;
   221     do {
   222         n *= 2;
   223         UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
   224         rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
   225     } while (rtest == test);
   226     test = rtest;
   228     UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   229             "realloc killed destructor")
   230     UCX_TEST_ASSERT(
   231             test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
   233     ucx_mempool_destroy(pool);
   235     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   237     UCX_TEST_END
   238     if (cb != NULL) free(cb);
   239 }

mercurial