test/test_allocator.c

changeset 391
f094a53c1178
child 392
c195c33de85d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/test_allocator.c	Sun Feb 07 12:20:07 2021 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2021 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 "cx/allocator.h"
    1.33 +
    1.34 +#include <CUnit/Basic.h>
    1.35 +
    1.36 +void test_default_allocator_available(void) {
    1.37 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->malloc, cx_malloc_stdlib)
    1.38 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->realloc, cx_realloc_stdlib)
    1.39 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->calloc, cx_calloc_stdlib)
    1.40 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->free, cx_free_stdlib)
    1.41 +}
    1.42 +
    1.43 +void test_default_malloc(void) {
    1.44 +    void* test = cxMalloc(cxDefaultAllocator, 16);
    1.45 +    CU_ASSERT_PTR_NOT_NULL(test);
    1.46 +    free(test);
    1.47 +}
    1.48 +
    1.49 +void test_default_realloc(void) {
    1.50 +    void* test = calloc(8, 1);
    1.51 +    memcpy(test, "Test", 4);
    1.52 +    test = cxRealloc(cxDefaultAllocator, test, 16);
    1.53 +    CU_ASSERT_PTR_NOT_NULL(test);
    1.54 +    CU_ASSERT_STRING_EQUAL("Test", test)
    1.55 +    free(test);
    1.56 +}
    1.57 +
    1.58 +void test_default_calloc(void) {
    1.59 +    void* test = cxCalloc(cxDefaultAllocator, 8, 2);
    1.60 +    CU_ASSERT_PTR_NOT_NULL(test);
    1.61 +    free(test);
    1.62 +}
    1.63 +
    1.64 +void test_default_free(void) {
    1.65 +    CU_PASS("Testing standard free is not possible.")
    1.66 +}
    1.67 +
    1.68 +int main() {
    1.69 +    CU_pSuite suite = NULL;
    1.70 +
    1.71 +    if (CUE_SUCCESS != CU_initialize_registry()) {
    1.72 +        return CU_get_error();
    1.73 +    }
    1.74 +
    1.75 +    suite = CU_add_suite("stdlib allocator", NULL, NULL);
    1.76 +    if (NULL == suite) {
    1.77 +        CU_cleanup_registry();
    1.78 +        return CU_get_error();
    1.79 +    }
    1.80 +
    1.81 +    if (
    1.82 +            (NULL == CU_add_test(suite, "default allocator available", test_default_allocator_available)) ||
    1.83 +            (NULL == CU_add_test(suite, "test of malloc()", test_default_malloc)) ||
    1.84 +            (NULL == CU_add_test(suite, "test of realloc()", test_default_realloc)) ||
    1.85 +            (NULL == CU_add_test(suite, "test of realloc()", test_default_calloc)) ||
    1.86 +            (NULL == CU_add_test(suite, "test of free()", test_default_free))
    1.87 +        ) {
    1.88 +        CU_cleanup_registry();
    1.89 +        return CU_get_error();
    1.90 +    }
    1.91 +
    1.92 +    CU_basic_set_mode(CU_BRM_NORMAL);
    1.93 +    CU_basic_run_tests();
    1.94 +    CU_cleanup_registry();
    1.95 +
    1.96 +    return CU_get_error();
    1.97 +}

mercurial