tests/test_mempool.c

Fri, 26 Jan 2024 19:17:46 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 26 Jan 2024 19:17:46 +0100
changeset 820
8b86ee2e09bb
parent 785
bb18daa62d5f
child 985
68754c7de906
permissions
-rw-r--r--

remove check that is always true in cx_arl_remove()

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 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 cxMempoolDestroy(pool);
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 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 cxMempoolDestroy(pool);
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 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 cxMempoolDestroy(pool);
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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 static void test_mempool_destructor(__attribute__((__unused__)) void *mem) {
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 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 cxMempoolDestroy(pool);
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
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 cxMalloc(pool->allocator, 16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 cxMalloc(pool->allocator, 16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 mem1 = cxMalloc(pool->allocator, 16);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 cxMalloc(pool->allocator, 16);
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 }
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 cxMempoolDestroy(pool);
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);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 cxMempoolDestroy(pool);
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);
a786b0a89b37 migrate mempool tests - relates to #342
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 cxMempoolDestroy(pool);
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