test/mpool_tests.c

Fri, 11 Mar 2016 18:06:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 11 Mar 2016 18:06:27 +0100
changeset 219
2df780a4482b
parent 192
1e51558b9d09
child 225
a1a068c2c4ef
permissions
-rw-r--r--

fixed misaligned memory access in test_ucx_buffer_write

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2015 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 == 17, "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     UCX_TEST_END
    79     ucx_mempool_destroy(pool);
    80 }
    82 UCX_TEST(test_ucx_mempool_calloc) {
    84     UcxMempool *pool = ucx_mempool_new(1);
    85     UCX_TEST_BEGIN
    87     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    89     UCX_TEST_ASSERT(test != NULL, "no memory for test data");
    90     UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
    92     UCX_TEST_END
    93     ucx_mempool_destroy(pool);
    94 }
    96 UCX_TEST(test_ucx_mempool_free) {
    97     UcxMempool *pool = ucx_mempool_new(16);
    98     void *mem1;
    99     void *mem2;
   101     UCX_TEST_BEGIN
   103     mem1 = ucx_mempool_malloc(pool, 16);
   104     ucx_mempool_free(pool, mem1);
   106     UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
   108     ucx_mempool_malloc(pool, 16);
   109     ucx_mempool_malloc(pool, 16);
   110     mem1 = ucx_mempool_malloc(pool, 16);
   111     ucx_mempool_malloc(pool, 16);
   112     mem2 = ucx_mempool_malloc(pool, 16);
   114     ucx_mempool_free(pool, mem1);
   116     UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
   118     ucx_mempool_free(pool, mem2);
   120     UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
   122     UCX_TEST_END
   123     ucx_mempool_destroy(pool);
   124 }
   126 UCX_EXTERN void test_setdestr(void* elem) {
   127     intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
   128     *cb = 42;
   129 }
   131 UCX_TEST(test_ucx_mempool_set_destr) {
   133     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   134     UCX_TEST_BEGIN
   135     UcxMempool *pool = ucx_mempool_new(2);
   137     ucx_mempool_malloc(pool, sizeof(intptr_t));
   138     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   140     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   142     test[0] = 5; test[1] = (intptr_t) cb;
   143     *cb = 13;
   145     ucx_mempool_set_destr(test, test_setdestr);
   146     UCX_TEST_ASSERT(
   147             *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
   148     UCX_TEST_ASSERT(
   149             test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
   151     ucx_mempool_destroy(pool);
   153     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   155     UCX_TEST_END
   156     if (cb != NULL) free(cb);
   157 }
   160 UCX_TEST(test_ucx_mempool_reg_destr) {
   162     intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   163     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   164     UCX_TEST_BEGIN
   165     UcxMempool *pool = ucx_mempool_new(1);
   167     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   169     test[0] = 5; test[1] = (intptr_t) cb;
   170     *cb = 13;
   172     ucx_mempool_reg_destr(pool, test, test_setdestr);
   174     ucx_destructor *pooladdr = (ucx_destructor*)
   175             ((char*)pool->data[0] + sizeof(ucx_destructor));
   177     UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
   179     ucx_mempool_destroy(pool);
   180     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   181     UCX_TEST_END
   183     if (test != NULL) free(test);
   184     if (cb != NULL) free(cb);
   185 }
   187 UCX_TEST(test_ucx_mempool_realloc) {
   189     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   190     UCX_TEST_BEGIN
   191     UcxMempool *pool = ucx_mempool_new(2);
   193     ucx_mempool_malloc(pool, sizeof(intptr_t));
   194     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   196     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   198     test[0] = 5; test[1] = (intptr_t) cb;
   199     *cb = 13;
   201     ucx_mempool_set_destr(test, test_setdestr);
   203     intptr_t *rtest, n = 2;
   204     do {
   205         n *= 2;
   206         UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
   207         rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
   208     } while (rtest == test);
   209     test = rtest;
   211     UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   212             "realloc killed destructor")
   213     UCX_TEST_ASSERT(
   214             test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
   216     ucx_mempool_destroy(pool);
   218     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   220     UCX_TEST_END
   221     if (cb != NULL) free(cb);
   222 }

mercurial