test/test_basic_mempool.cpp

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

mercurial