tests/test_mempool.c

Wed, 25 Dec 2024 19:33:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Dec 2024 19:33:04 +0100
changeset 1054
fb1076ead56f
parent 993
b642eca4b956
permissions
-rw-r--r--

add implementation for creating new json values

relates to #527

781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
a786b0a89b37 migrate mempool 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"
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a786b0a89b37 migrate mempool 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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/test.h"
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "util_allocator.h"
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include "cx/mempool.h"
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 CX_TEST(test_mempool_create) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 CxMempool *pool = cxBasicMempoolCreate(16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 CX_TEST_ASSERT(pool->auto_destr == NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 CX_TEST_ASSERT(pool->allocator != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 CX_TEST_ASSERT(pool->allocator->cl != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 CX_TEST_ASSERT(pool->allocator->data == pool);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 CX_TEST_ASSERT(pool->allocator->cl->malloc != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 CX_TEST_ASSERT(pool->allocator->cl->calloc != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 CX_TEST_ASSERT(pool->allocator->cl->realloc != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 CX_TEST_ASSERT(pool->allocator->cl->free != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 CX_TEST_ASSERT(pool->capacity == 16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 CX_TEST_ASSERT(pool->size == 0);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 CX_TEST_ASSERT(pool->data != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
49 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_TEST(test_mempool_malloc) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 CxMempool *pool = cxBasicMempoolCreate(4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_TEST_ASSERT(pool->size == 2);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 CX_TEST_ASSERT(pool->capacity == 4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 CX_TEST_ASSERT(pool->size == 4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 CX_TEST_ASSERT(pool->capacity == 4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 CX_TEST_ASSERT(pool->size == 6);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CX_TEST_ASSERT(pool->capacity >= 6);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
68 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 CX_TEST(test_mempool_calloc) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 CxMempool *pool = cxBasicMempoolCreate(4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 int *test = cxCalloc(pool->allocator, 2, sizeof(int));
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 CX_TEST_ASSERT(test != NULL);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 CX_TEST_ASSERT(test[0] == 0);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 CX_TEST_ASSERT(test[1] == 0);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
79 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 static unsigned test_mempool_destructor_called;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
84 static void test_mempool_destructor(cx_attr_unused void *mem) {
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 test_mempool_destructor_called++;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 CX_TEST(test_mempool_realloc) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 int *data = cxMalloc(pool->allocator, sizeof(int));
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 *data = 13;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 int *rdata = data;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 unsigned n = 1;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 while (rdata == data) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 n <<= 1;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 // eventually the memory should be moved elsewhere
785
bb18daa62d5f migrate map tests - relates to #342
Mike Becker <universe@uap-core.de>
parents: 781
diff changeset
100 CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable");
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t));
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 CX_TEST_ASSERT(*rdata == 13);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 // test if destructor is still intact
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 test_mempool_destructor_called = 0;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 cxFree(pool->allocator, rdata);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 CX_TEST_ASSERT(test_mempool_destructor_called == 1);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 }
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
110 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 CX_TEST(test_mempool_free) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 CxMempool *pool = cxBasicMempoolCreate(4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 void *mem1, *mem2;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 mem1 = cxMalloc(pool->allocator, 16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 cxFree(pool->allocator, mem1);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 CX_TEST_ASSERT(pool->size == 0);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
122 mem1 = cxMalloc(pool->allocator, 16);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 mem1 = cxMalloc(pool->allocator, 16);
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
124 mem1 = cxMalloc(pool->allocator, 16);
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 785
diff changeset
125 mem2 = cxMalloc(pool->allocator, 16);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 mem2 = cxMalloc(pool->allocator, 16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 CX_TEST_ASSERT(pool->size == 5);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 cxFree(pool->allocator, mem1);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 CX_TEST_ASSERT(pool->size == 4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 cxFree(pool->allocator, mem2);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 CX_TEST_ASSERT(pool->size == 3);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 }
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
134 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 CX_TEST(test_mempool_destroy) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 CxMempool *pool = cxBasicMempoolCreate(4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 int *data = cxMalloc(pool->allocator, sizeof(int));
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 *data = 13;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 cxMempoolSetDestructor(data, test_mempool_destructor);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 CX_TEST_ASSERT(*data == 13);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 test_mempool_destructor_called = 0;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 cxFree(pool->allocator, data);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 CX_TEST_ASSERT(test_mempool_destructor_called == 1);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 data = cxMalloc(pool->allocator, sizeof(int));
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 cxMempoolSetDestructor(data, test_mempool_destructor);
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
149 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 CX_TEST_ASSERT(test_mempool_destructor_called == 2);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 CX_TEST(test_mempool_register) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 CxMempool *pool = cxBasicMempoolCreate(4);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 CX_TEST_DO {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 int *data = cxMalloc(pool->allocator, sizeof(int));
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 test_mempool_destructor_called = 0;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 cxMempoolSetDestructor(data, test_mempool_destructor);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 int donotfree = 0;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 cxMempoolRegister(pool, &donotfree, test_mempool_destructor);
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
162 cxMempoolFree(pool);
781
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 CX_TEST_ASSERT(test_mempool_destructor_called == 2);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 CxTestSuite *cx_test_suite_mempool(void) {
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 CxTestSuite *suite = cx_test_suite_new("mempool");
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 cx_test_register(suite, test_mempool_create);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 cx_test_register(suite, test_mempool_malloc);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 cx_test_register(suite, test_mempool_calloc);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 cx_test_register(suite, test_mempool_realloc);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 cx_test_register(suite, test_mempool_free);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 cx_test_register(suite, test_mempool_destroy);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 cx_test_register(suite, test_mempool_register);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 return suite;
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 }

mercurial