tests/test_mempool.cpp

changeset 727
d92a59f5d261
parent 653
e081643aae2a
equal deleted inserted replaced
726:44986c0e2b05 727:d92a59f5d261
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "cx/mempool.h"
30 #include "util_allocator.h"
31 #include <gtest/gtest.h>
32
33 TEST(Mempool, Create) {
34 auto pool = cxBasicMempoolCreate(16);
35 ASSERT_EQ(pool->auto_destr, nullptr);
36 ASSERT_NE(pool->allocator, nullptr);
37 ASSERT_NE(pool->allocator->cl, nullptr);
38 EXPECT_EQ(pool->allocator->data, pool);
39 EXPECT_NE(pool->allocator->cl->malloc, nullptr);
40 EXPECT_NE(pool->allocator->cl->calloc, nullptr);
41 EXPECT_NE(pool->allocator->cl->realloc, nullptr);
42 EXPECT_NE(pool->allocator->cl->free, nullptr);
43 EXPECT_EQ(pool->capacity, 16);
44 EXPECT_EQ(pool->size, 0);
45 EXPECT_NE(pool->data, nullptr);
46 cxMempoolDestroy(pool);
47 }
48
49 TEST(Mempool, malloc) {
50 auto pool = cxBasicMempoolCreate(4);
51 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
52 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
53 EXPECT_EQ(pool->size, 2);
54 EXPECT_EQ(pool->capacity, 4);
55 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
56 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
57 EXPECT_EQ(pool->size, 4);
58 EXPECT_EQ(pool->capacity, 4);
59 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
60 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
61 EXPECT_EQ(pool->size, 6);
62 EXPECT_GE(pool->capacity, 6);
63 cxMempoolDestroy(pool);
64 }
65
66 TEST(Mempool, calloc) {
67 auto pool = cxBasicMempoolCreate(4);
68
69 auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int));
70 ASSERT_NE(test, nullptr);
71 EXPECT_EQ(test[0], 0);
72 EXPECT_EQ(test[1], 0);
73 cxMempoolDestroy(pool);
74 }
75
76 static unsigned test_destructor_called;
77
78 static void test_destructor([[maybe_unused]] void *mem) {
79 test_destructor_called++;
80 }
81
82 TEST(Mempool, realloc) {
83 auto pool = cxMempoolCreate(4, test_destructor);
84 ASSERT_EQ(pool->auto_destr, test_destructor);
85 auto data = cxMalloc(pool->allocator, sizeof(int));
86 *((int *) data) = 13;
87
88 void *rdata = data;
89 unsigned n = 1;
90 while (rdata == data) {
91 n <<= 1;
92 ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere
93 rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t));
94 }
95
96 EXPECT_EQ(*((int *) rdata), 13);
97 // test if destructor is still intact
98 test_destructor_called = 0;
99 cxFree(pool->allocator, rdata);
100 EXPECT_EQ(test_destructor_called, 1);
101 cxMempoolDestroy(pool);
102 }
103
104
105 TEST(Mempool, free) {
106 auto pool = cxBasicMempoolCreate(4);
107
108 void *mem1;
109 void *mem2;
110
111 mem1 = cxMalloc(pool->allocator, 16);
112 cxFree(pool->allocator, mem1);
113 EXPECT_EQ(pool->size, 0);
114
115 cxMalloc(pool->allocator, 16);
116 cxMalloc(pool->allocator, 16);
117 mem1 = cxMalloc(pool->allocator, 16);
118 cxMalloc(pool->allocator, 16);
119 mem2 = cxMalloc(pool->allocator, 16);
120
121 EXPECT_EQ(pool->size, 5);
122 cxFree(pool->allocator, mem1);
123 EXPECT_EQ(pool->size, 4);
124 cxFree(pool->allocator, mem2);
125 EXPECT_EQ(pool->size, 3);
126 cxMempoolDestroy(pool);
127 }
128
129 TEST(Mempool, Destroy) {
130 auto pool = cxBasicMempoolCreate(4);
131 auto data = cxMalloc(pool->allocator, sizeof(int));
132 *((int *) data) = 13;
133 cxMempoolSetDestructor(data, test_destructor);
134 EXPECT_EQ(*((int *) data), 13);
135 test_destructor_called = 0;
136 cxFree(pool->allocator, data);
137 EXPECT_EQ(test_destructor_called, 1);
138 data = cxMalloc(pool->allocator, sizeof(int));
139 cxMempoolSetDestructor(data, test_destructor);
140 cxMempoolDestroy(pool);
141 EXPECT_EQ(test_destructor_called, 2);
142 }
143
144 TEST(Mempool, Register) {
145 auto pool = cxBasicMempoolCreate(4);
146 auto data = cxMalloc(pool->allocator, sizeof(int));
147 test_destructor_called = 0;
148 cxMempoolSetDestructor(data, test_destructor);
149 int donotfree = 0;
150 cxMempoolRegister(pool, &donotfree, test_destructor);
151 cxMempoolDestroy(pool);
152 EXPECT_EQ(test_destructor_called, 2);
153 }

mercurial