tests/test_allocator.c

Tue, 22 Oct 2024 12:17:48 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 22 Oct 2024 12:17:48 +0200
changeset 942
8a5bbdb7f87f
parent 782
74d777455e96
child 963
2f601274bbac
permissions
-rw-r--r--

use json reader state macros everywhere

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

mercurial