| 30 #include "util_allocator.h" |
30 #include "util_allocator.h" |
| 31 |
31 |
| 32 #include "cx/mempool.h" |
32 #include "cx/mempool.h" |
| 33 |
33 |
| 34 CX_TEST(test_mempool_create) { |
34 CX_TEST(test_mempool_create) { |
| 35 CxMempool *pool = cxBasicMempoolCreate(16); |
35 CxMempool *pool = cxMempoolCreateSimple(16); |
| 36 CX_TEST_DO { |
36 CX_TEST_DO { |
| 37 CX_TEST_ASSERT(pool->auto_destr == NULL); |
37 CX_TEST_ASSERT(pool->auto_destr == NULL); |
| 38 CX_TEST_ASSERT(pool->allocator != NULL); |
38 CX_TEST_ASSERT(pool->allocator != NULL); |
| 39 CX_TEST_ASSERT(pool->allocator->cl != NULL); |
39 CX_TEST_ASSERT(pool->allocator->cl != NULL); |
| 40 CX_TEST_ASSERT(pool->allocator->data == pool); |
40 CX_TEST_ASSERT(pool->allocator->data == pool); |
| 48 } |
48 } |
| 49 cxMempoolFree(pool); |
49 cxMempoolFree(pool); |
| 50 } |
50 } |
| 51 |
51 |
| 52 CX_TEST(test_mempool_malloc) { |
52 CX_TEST(test_mempool_malloc) { |
| 53 CxMempool *pool = cxBasicMempoolCreate(4); |
53 CxMempool *pool = cxMempoolCreateSimple(4); |
| 54 CX_TEST_DO { |
54 CX_TEST_DO { |
| 55 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
55 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
| 56 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
56 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
| 57 CX_TEST_ASSERT(pool->size == 2); |
57 CX_TEST_ASSERT(pool->size == 2); |
| 58 CX_TEST_ASSERT(pool->capacity == 4); |
58 CX_TEST_ASSERT(pool->capacity == 4); |
| 67 } |
67 } |
| 68 cxMempoolFree(pool); |
68 cxMempoolFree(pool); |
| 69 } |
69 } |
| 70 |
70 |
| 71 CX_TEST(test_mempool_calloc) { |
71 CX_TEST(test_mempool_calloc) { |
| 72 CxMempool *pool = cxBasicMempoolCreate(4); |
72 CxMempool *pool = cxMempoolCreateSimple(4); |
| 73 CX_TEST_DO { |
73 CX_TEST_DO { |
| 74 int *test = cxCalloc(pool->allocator, 2, sizeof(int)); |
74 int *test = cxCalloc(pool->allocator, 2, sizeof(int)); |
| 75 CX_TEST_ASSERT(test != NULL); |
75 CX_TEST_ASSERT(test != NULL); |
| 76 CX_TEST_ASSERT(test[0] == 0); |
76 CX_TEST_ASSERT(test[0] == 0); |
| 77 CX_TEST_ASSERT(test[1] == 0); |
77 CX_TEST_ASSERT(test[1] == 0); |
| 110 cxMempoolFree(pool); |
110 cxMempoolFree(pool); |
| 111 } |
111 } |
| 112 |
112 |
| 113 |
113 |
| 114 CX_TEST(test_mempool_free) { |
114 CX_TEST(test_mempool_free) { |
| 115 CxMempool *pool = cxBasicMempoolCreate(4); |
115 CxMempool *pool = cxMempoolCreateSimple(4); |
| 116 void *mem1, *mem2; |
116 void *mem1, *mem2; |
| 117 CX_TEST_DO { |
117 CX_TEST_DO { |
| 118 mem1 = cxMalloc(pool->allocator, 16); |
118 mem1 = cxMalloc(pool->allocator, 16); |
| 119 cxFree(pool->allocator, mem1); |
119 cxFree(pool->allocator, mem1); |
| 120 CX_TEST_ASSERT(pool->size == 0); |
120 CX_TEST_ASSERT(pool->size == 0); |
| 133 } |
133 } |
| 134 cxMempoolFree(pool); |
134 cxMempoolFree(pool); |
| 135 } |
135 } |
| 136 |
136 |
| 137 CX_TEST(test_mempool_destroy) { |
137 CX_TEST(test_mempool_destroy) { |
| 138 CxMempool *pool = cxBasicMempoolCreate(4); |
138 CxMempool *pool = cxMempoolCreateSimple(4); |
| 139 CX_TEST_DO { |
139 CX_TEST_DO { |
| 140 int *data = cxMalloc(pool->allocator, sizeof(int)); |
140 int *data = cxMalloc(pool->allocator, sizeof(int)); |
| 141 *data = 13; |
141 *data = 13; |
| 142 cxMempoolSetDestructor(data, test_mempool_destructor); |
142 cxMempoolSetDestructor(data, test_mempool_destructor); |
| 143 CX_TEST_ASSERT(*data == 13); |
143 CX_TEST_ASSERT(*data == 13); |
| 150 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
150 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
| 151 } |
151 } |
| 152 } |
152 } |
| 153 |
153 |
| 154 CX_TEST(test_mempool_register) { |
154 CX_TEST(test_mempool_register) { |
| 155 CxMempool *pool = cxBasicMempoolCreate(4); |
155 CxMempool *pool = cxMempoolCreateSimple(4); |
| 156 CX_TEST_DO { |
156 CX_TEST_DO { |
| 157 int *data = cxMalloc(pool->allocator, sizeof(int)); |
157 int *data = cxMalloc(pool->allocator, sizeof(int)); |
| 158 test_mempool_destructor_called = 0; |
158 test_mempool_destructor_called = 0; |
| 159 cxMempoolSetDestructor(data, test_mempool_destructor); |
159 cxMempoolSetDestructor(data, test_mempool_destructor); |
| 160 int donotfree = 0; |
160 int donotfree = 0; |