test/test_basic_mempool.cpp

changeset 571
f83583a0bbac
child 572
f0f99dd06d9f
equal deleted inserted replaced
570:7edce1b5a798 571:f83583a0bbac
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/basic_mempool.h"
30 #include "util_allocator.h"
31 #include <gtest/gtest.h>
32
33 class CxBasicMempool : public ::testing::Test {
34 protected:
35 CxTestingAllocator testingAllocator;
36 CxMempool *pool = nullptr;
37
38 void TearDown() override {
39 if (pool != nullptr) {
40 cxMempoolDestroy(pool);
41 }
42 EXPECT_TRUE(testingAllocator.verify());
43 }
44 };
45
46 TEST_F(CxBasicMempool, Create) {
47 pool = cxBasicMempoolCreateSimple(16);
48 ASSERT_NE(pool->allocator, nullptr);
49 ASSERT_NE(pool->cl, nullptr);
50 EXPECT_NE(pool->cl->destroy, nullptr);
51 ASSERT_NE(pool->allocator->cl, nullptr);
52 EXPECT_EQ(pool->allocator->data, pool);
53 EXPECT_NE(pool->allocator->cl->malloc, nullptr);
54 EXPECT_NE(pool->allocator->cl->calloc, nullptr);
55 EXPECT_NE(pool->allocator->cl->realloc, nullptr);
56 EXPECT_NE(pool->allocator->cl->free, nullptr);
57
58 auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
59 EXPECT_EQ(basic_pool->allocator, cxDefaultAllocator);
60 EXPECT_EQ(basic_pool->size, 16);
61 EXPECT_EQ(basic_pool->ndata, 0);
62 EXPECT_NE(basic_pool->data, nullptr);
63 }
64
65 TEST_F(CxBasicMempool, malloc) {
66 pool = cxBasicMempoolCreate(4, &testingAllocator);
67 auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
68 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
69 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
70 EXPECT_EQ(testingAllocator.alloc_total, 2);
71 EXPECT_EQ(basic_pool->ndata, 2);
72 EXPECT_EQ(basic_pool->size, 4);
73 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
74 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
75 EXPECT_EQ(testingAllocator.alloc_total, 4);
76 EXPECT_EQ(basic_pool->ndata, 4);
77 EXPECT_EQ(basic_pool->size, 4);
78 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
79 EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
80 EXPECT_EQ(testingAllocator.alloc_total, 6);
81 EXPECT_EQ(basic_pool->ndata, 6);
82 EXPECT_GE(basic_pool->size, 6);
83 EXPECT_TRUE(testingAllocator.used());
84 }
85
86 TEST_F(CxBasicMempool, calloc) {
87 pool = cxBasicMempoolCreate(4, &testingAllocator);
88
89 auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int));
90 ASSERT_NE(test, nullptr);
91 EXPECT_EQ(test[0], 0);
92 EXPECT_EQ(test[1], 0);
93 EXPECT_TRUE(testingAllocator.used());
94 }
95
96 static unsigned test_destructor_called = 0;
97
98 static void test_destructor([[maybe_unused]] void *mem) {
99 test_destructor_called++;
100 }
101
102 TEST_F(CxBasicMempool, destructor) {
103 pool = cxBasicMempoolCreate(4, &testingAllocator);
104 auto data = cxMalloc(pool->allocator, sizeof(int));
105 *((int *) data) = 13;
106 cxMempoolSetDestructor(pool, data, test_destructor);
107 EXPECT_EQ(*((int *) data), 13);
108 test_destructor_called = 0;
109 cxFree(pool->allocator, data);
110 EXPECT_EQ(test_destructor_called, 1);
111 data = cxMalloc(pool->allocator, sizeof(int));
112 cxMempoolSetDestructor(pool, data, test_destructor);
113 cxMempoolDestroy(pool);
114 pool = nullptr;
115 EXPECT_EQ(test_destructor_called, 2);
116 }
117
118 TEST_F(CxBasicMempool, realloc) {
119 pool = cxBasicMempoolCreate(4, &testingAllocator);
120 auto data = cxMalloc(pool->allocator, sizeof(int));
121 *((int *) data) = 13;
122 cxMempoolSetDestructor(pool, data, test_destructor);
123
124 void *rdata = data;
125 unsigned n = 1;
126 while (rdata == data) {
127 n <<= 1;
128 ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere
129 rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t));
130 }
131
132 EXPECT_EQ(*((int *) rdata), 13);
133 // test if destructor is still intact
134 test_destructor_called = 0;
135 cxFree(pool->allocator, rdata);
136 EXPECT_EQ(test_destructor_called, 1);
137 }
138
139
140 TEST_F(CxBasicMempool, free) {
141 pool = cxBasicMempoolCreate(4, &testingAllocator);
142 auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
143
144 void *mem1;
145 void *mem2;
146
147 mem1 = cxMalloc(pool->allocator, 16);
148 cxFree(pool->allocator, mem1);
149 EXPECT_EQ(basic_pool->ndata, 0);
150
151 cxMalloc(pool->allocator, 16);
152 cxMalloc(pool->allocator, 16);
153 mem1 = cxMalloc(pool->allocator, 16);
154 cxMalloc(pool->allocator, 16);
155 mem2 = cxMalloc(pool->allocator, 16);
156
157 EXPECT_EQ(basic_pool->ndata, 5);
158 cxFree(pool->allocator, mem1);
159 EXPECT_EQ(basic_pool->ndata, 4);
160 cxFree(pool->allocator, mem2);
161 EXPECT_EQ(basic_pool->ndata, 3);
162 }

mercurial