tests/test_mempool.c

Sun, 29 Sep 2024 14:40:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 29 Sep 2024 14:40:43 +0200
changeset 896
7e09c76390c3
parent 785
bb18daa62d5f
child 985
68754c7de906
permissions
-rw-r--r--

allow find() member function to start in an arbitrary subtree

relates to #166

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "cx/test.h"
#include "util_allocator.h"

#include "cx/mempool.h"

CX_TEST(test_mempool_create) {
    CxMempool *pool = cxBasicMempoolCreate(16);
    CX_TEST_DO {
        CX_TEST_ASSERT(pool->auto_destr == NULL);
        CX_TEST_ASSERT(pool->allocator != NULL);
        CX_TEST_ASSERT(pool->allocator->cl != NULL);
        CX_TEST_ASSERT(pool->allocator->data == pool);
        CX_TEST_ASSERT(pool->allocator->cl->malloc != NULL);
        CX_TEST_ASSERT(pool->allocator->cl->calloc != NULL);
        CX_TEST_ASSERT(pool->allocator->cl->realloc != NULL);
        CX_TEST_ASSERT(pool->allocator->cl->free != NULL);
        CX_TEST_ASSERT(pool->capacity == 16);
        CX_TEST_ASSERT(pool->size == 0);
        CX_TEST_ASSERT(pool->data != NULL);
    }
    cxMempoolDestroy(pool);
}

CX_TEST(test_mempool_malloc) {
    CxMempool *pool = cxBasicMempoolCreate(4);
    CX_TEST_DO {
        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
        CX_TEST_ASSERT(pool->size == 2);
        CX_TEST_ASSERT(pool->capacity == 4);
        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
        CX_TEST_ASSERT(pool->size == 4);
        CX_TEST_ASSERT(pool->capacity == 4);
        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
        CX_TEST_ASSERT(pool->size == 6);
        CX_TEST_ASSERT(pool->capacity >= 6);
    }
    cxMempoolDestroy(pool);
}

CX_TEST(test_mempool_calloc) {
    CxMempool *pool = cxBasicMempoolCreate(4);
    CX_TEST_DO {
        int *test = cxCalloc(pool->allocator, 2, sizeof(int));
        CX_TEST_ASSERT(test != NULL);
        CX_TEST_ASSERT(test[0] == 0);
        CX_TEST_ASSERT(test[1] == 0);
    }
    cxMempoolDestroy(pool);
}

static unsigned test_mempool_destructor_called;

static void test_mempool_destructor(__attribute__((__unused__)) void *mem) {
    test_mempool_destructor_called++;
}

CX_TEST(test_mempool_realloc) {
    CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor);
    CX_TEST_DO {
        CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor);
        int *data = cxMalloc(pool->allocator, sizeof(int));
        *data = 13;

        int *rdata = data;
        unsigned n = 1;
        while (rdata == data) {
            n <<= 1;
            // eventually the memory should be moved elsewhere
            CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable");
            rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t));
        }

        CX_TEST_ASSERT(*rdata == 13);
        // test if destructor is still intact
        test_mempool_destructor_called = 0;
        cxFree(pool->allocator, rdata);
        CX_TEST_ASSERT(test_mempool_destructor_called == 1);
    }
    cxMempoolDestroy(pool);
}


CX_TEST(test_mempool_free) {
    CxMempool *pool = cxBasicMempoolCreate(4);
    void *mem1, *mem2;
    CX_TEST_DO {
        mem1 = cxMalloc(pool->allocator, 16);
        cxFree(pool->allocator, mem1);
        CX_TEST_ASSERT(pool->size == 0);

        cxMalloc(pool->allocator, 16);
        cxMalloc(pool->allocator, 16);
        mem1 = cxMalloc(pool->allocator, 16);
        cxMalloc(pool->allocator, 16);
        mem2 = cxMalloc(pool->allocator, 16);

        CX_TEST_ASSERT(pool->size == 5);
        cxFree(pool->allocator, mem1);
        CX_TEST_ASSERT(pool->size == 4);
        cxFree(pool->allocator, mem2);
        CX_TEST_ASSERT(pool->size == 3);
    }
    cxMempoolDestroy(pool);
}

CX_TEST(test_mempool_destroy) {
    CxMempool *pool = cxBasicMempoolCreate(4);
    CX_TEST_DO {
        int *data = cxMalloc(pool->allocator, sizeof(int));
        *data = 13;
        cxMempoolSetDestructor(data, test_mempool_destructor);
        CX_TEST_ASSERT(*data == 13);
        test_mempool_destructor_called = 0;
        cxFree(pool->allocator, data);
        CX_TEST_ASSERT(test_mempool_destructor_called == 1);
        data = cxMalloc(pool->allocator, sizeof(int));
        cxMempoolSetDestructor(data, test_mempool_destructor);
        cxMempoolDestroy(pool);
        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
    }
}

CX_TEST(test_mempool_register) {
    CxMempool *pool = cxBasicMempoolCreate(4);
    CX_TEST_DO {
        int *data = cxMalloc(pool->allocator, sizeof(int));
        test_mempool_destructor_called = 0;
        cxMempoolSetDestructor(data, test_mempool_destructor);
        int donotfree = 0;
        cxMempoolRegister(pool, &donotfree, test_mempool_destructor);
        cxMempoolDestroy(pool);
        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
    }
}


CxTestSuite *cx_test_suite_mempool(void) {
    CxTestSuite *suite = cx_test_suite_new("mempool");

    cx_test_register(suite, test_mempool_create);
    cx_test_register(suite, test_mempool_malloc);
    cx_test_register(suite, test_mempool_calloc);
    cx_test_register(suite, test_mempool_realloc);
    cx_test_register(suite, test_mempool_free);
    cx_test_register(suite, test_mempool_destroy);
    cx_test_register(suite, test_mempool_register);

    return suite;
}

mercurial