tests/test_allocator.c

Sat, 30 Dec 2023 14:32:42 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 30 Dec 2023 14:32:42 +0100
changeset 782
74d777455e96
permissions
-rw-r--r--

migrate allocator tests - relates to #342

universe@782 1 /*
universe@782 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@782 3 *
universe@782 4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
universe@782 5 *
universe@782 6 * Redistribution and use in source and binary forms, with or without
universe@782 7 * modification, are permitted provided that the following conditions are met:
universe@782 8 *
universe@782 9 * 1. Redistributions of source code must retain the above copyright
universe@782 10 * notice, this list of conditions and the following disclaimer.
universe@782 11 *
universe@782 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@782 13 * notice, this list of conditions and the following disclaimer in the
universe@782 14 * documentation and/or other materials provided with the distribution.
universe@782 15 *
universe@782 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@782 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@782 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@782 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@782 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@782 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@782 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@782 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@782 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@782 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@782 26 * POSSIBILITY OF SUCH DAMAGE.
universe@782 27 */
universe@782 28
universe@782 29 #include "cx/test.h"
universe@782 30
universe@782 31 #include "cx/allocator.h"
universe@782 32
universe@782 33 CX_TEST(test_allocator_default) {
universe@782 34 CX_TEST_DO {
universe@782 35 CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL);
universe@782 36 }
universe@782 37 }
universe@782 38
universe@782 39 CX_TEST(test_allocator_default_malloc) {
universe@782 40 void *test = cxMalloc(cxDefaultAllocator, 16);
universe@782 41 CX_TEST_DO {
universe@782 42 CX_TEST_ASSERT(test != NULL);
universe@782 43 // we cannot assert sth. but valgrind will detect an error
universe@782 44 memcpy(test, "0123456789ABCDEF", 16);
universe@782 45 }
universe@782 46 free(test);
universe@782 47 }
universe@782 48
universe@782 49 CX_TEST(test_allocator_default_calloc) {
universe@782 50 char *test = cxCalloc(cxDefaultAllocator, 8, 2);
universe@782 51 CX_TEST_DO {
universe@782 52 CX_TEST_ASSERT(test != NULL);
universe@782 53 for (int i = 0; i < 16; i++) {
universe@782 54 CX_TEST_ASSERT(test[i] == 0);
universe@782 55 }
universe@782 56 }
universe@782 57 free(test);
universe@782 58 }
universe@782 59
universe@782 60 CX_TEST(test_allocator_default_realloc) {
universe@782 61 char *test = calloc(8, 1);
universe@782 62 memcpy(test, "Test", 5);
universe@782 63 CX_TEST_DO {
universe@782 64 test = cxRealloc(cxDefaultAllocator, test, 16);
universe@782 65 CX_TEST_ASSERT(test != NULL);
universe@782 66 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
universe@782 67 }
universe@782 68 free(test);
universe@782 69 }
universe@782 70
universe@782 71 CX_TEST(test_allocator_default_free) {
universe@782 72 void *test = malloc(16);
universe@782 73 CX_TEST_DO {
universe@782 74 // we cannot assert sth. but valgrind will detect an error
universe@782 75 cxFree(cxDefaultAllocator, test);
universe@782 76 CX_TEST_ASSERT(true);
universe@782 77 }
universe@782 78 }
universe@782 79
universe@782 80 CX_TEST(test_allocator_reallocate) {
universe@782 81 void *test = calloc(8, 1);
universe@782 82 memcpy(test, "Test", 5);
universe@782 83 CX_TEST_DO {
universe@782 84 int ret = cxReallocate(cxDefaultAllocator, &test, 16);
universe@782 85 CX_TEST_ASSERT(ret == 0);
universe@782 86 CX_TEST_ASSERT(test != NULL);
universe@782 87 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
universe@782 88 }
universe@782 89 free(test);
universe@782 90 }
universe@782 91
universe@782 92 CX_TEST(test_allocator_reallocate_low_level) {
universe@782 93 void *test = calloc(8, 1);
universe@782 94 memcpy(test, "Test", 5);
universe@782 95 CX_TEST_DO {
universe@782 96 int ret = cx_reallocate(&test, 16);
universe@782 97 CX_TEST_ASSERT(ret == 0);
universe@782 98 CX_TEST_ASSERT(test != NULL);
universe@782 99 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
universe@782 100 }
universe@782 101 free(test);
universe@782 102 }
universe@782 103
universe@782 104 static void *test_allocator_mock_failing_realloc(
universe@782 105 __attribute__((__unused__))void *p,
universe@782 106 __attribute__((__unused__))void *d,
universe@782 107 __attribute__((__unused__))size_t n
universe@782 108 ) {
universe@782 109 return NULL;
universe@782 110 }
universe@782 111
universe@782 112 CX_TEST(test_allocator_reallocate_fails) {
universe@782 113 // Mock an allocator that always returns NULL on realloc
universe@782 114 cx_allocator_class mock_cl;
universe@782 115 mock_cl.realloc = test_allocator_mock_failing_realloc;
universe@782 116 CxAllocator mock = {&mock_cl, NULL};
universe@782 117
universe@782 118 void *test = calloc(8, 1);
universe@782 119 memcpy(test, "Test", 5);
universe@782 120 void *original = test;
universe@782 121 CX_TEST_DO {
universe@782 122 int ret = cxReallocate(&mock, &test, 16);
universe@782 123 // non-zero return code because of the failure
universe@782 124 CX_TEST_ASSERT(ret != 0);
universe@782 125 // the test pointer was not changed and still points to the same memory
universe@782 126 CX_TEST_ASSERT(test == original);
universe@782 127 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
universe@782 128 }
universe@782 129 free(test);
universe@782 130 }
universe@782 131
universe@782 132 CxTestSuite *cx_test_suite_allocator(void) {
universe@782 133 CxTestSuite *suite = cx_test_suite_new("allocator");
universe@782 134
universe@782 135 cx_test_register(suite, test_allocator_default);
universe@782 136 cx_test_register(suite, test_allocator_default_malloc);
universe@782 137 cx_test_register(suite, test_allocator_default_calloc);
universe@782 138 cx_test_register(suite, test_allocator_default_realloc);
universe@782 139 cx_test_register(suite, test_allocator_default_free);
universe@782 140 cx_test_register(suite, test_allocator_reallocate);
universe@782 141 cx_test_register(suite, test_allocator_reallocate_fails);
universe@782 142 cx_test_register(suite, test_allocator_reallocate_low_level);
universe@782 143
universe@782 144 return suite;
universe@782 145 }

mercurial