| 44 CX_TEST_ASSERT(pool->allocator->cl->free != NULL); |
44 CX_TEST_ASSERT(pool->allocator->cl->free != NULL); |
| 45 CX_TEST_ASSERT(pool->capacity == 16); |
45 CX_TEST_ASSERT(pool->capacity == 16); |
| 46 CX_TEST_ASSERT(pool->size == 0); |
46 CX_TEST_ASSERT(pool->size == 0); |
| 47 CX_TEST_ASSERT(pool->data != NULL); |
47 CX_TEST_ASSERT(pool->data != NULL); |
| 48 } |
48 } |
| 49 cxMempoolDestroy(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 = cxBasicMempoolCreate(4); |
| 54 CX_TEST_DO { |
54 CX_TEST_DO { |
| 63 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
63 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
| 64 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
64 CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); |
| 65 CX_TEST_ASSERT(pool->size == 6); |
65 CX_TEST_ASSERT(pool->size == 6); |
| 66 CX_TEST_ASSERT(pool->capacity >= 6); |
66 CX_TEST_ASSERT(pool->capacity >= 6); |
| 67 } |
67 } |
| 68 cxMempoolDestroy(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 = cxBasicMempoolCreate(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); |
| 78 } |
78 } |
| 79 cxMempoolDestroy(pool); |
79 cxMempoolFree(pool); |
| 80 } |
80 } |
| 81 |
81 |
| 82 static unsigned test_mempool_destructor_called; |
82 static unsigned test_mempool_destructor_called; |
| 83 |
83 |
| 84 static void test_mempool_destructor(cx_attr_unused void *mem) { |
84 static void test_mempool_destructor(cx_attr_unused void *mem) { |
| 105 // test if destructor is still intact |
105 // test if destructor is still intact |
| 106 test_mempool_destructor_called = 0; |
106 test_mempool_destructor_called = 0; |
| 107 cxFree(pool->allocator, rdata); |
107 cxFree(pool->allocator, rdata); |
| 108 CX_TEST_ASSERT(test_mempool_destructor_called == 1); |
108 CX_TEST_ASSERT(test_mempool_destructor_called == 1); |
| 109 } |
109 } |
| 110 cxMempoolDestroy(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 = cxBasicMempoolCreate(4); |
| 129 cxFree(pool->allocator, mem1); |
129 cxFree(pool->allocator, mem1); |
| 130 CX_TEST_ASSERT(pool->size == 4); |
130 CX_TEST_ASSERT(pool->size == 4); |
| 131 cxFree(pool->allocator, mem2); |
131 cxFree(pool->allocator, mem2); |
| 132 CX_TEST_ASSERT(pool->size == 3); |
132 CX_TEST_ASSERT(pool->size == 3); |
| 133 } |
133 } |
| 134 cxMempoolDestroy(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 = cxBasicMempoolCreate(4); |
| 139 CX_TEST_DO { |
139 CX_TEST_DO { |
| 144 test_mempool_destructor_called = 0; |
144 test_mempool_destructor_called = 0; |
| 145 cxFree(pool->allocator, data); |
145 cxFree(pool->allocator, data); |
| 146 CX_TEST_ASSERT(test_mempool_destructor_called == 1); |
146 CX_TEST_ASSERT(test_mempool_destructor_called == 1); |
| 147 data = cxMalloc(pool->allocator, sizeof(int)); |
147 data = cxMalloc(pool->allocator, sizeof(int)); |
| 148 cxMempoolSetDestructor(data, test_mempool_destructor); |
148 cxMempoolSetDestructor(data, test_mempool_destructor); |
| 149 cxMempoolDestroy(pool); |
149 cxMempoolFree(pool); |
| 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) { |
| 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; |
| 161 cxMempoolRegister(pool, &donotfree, test_mempool_destructor); |
161 cxMempoolRegister(pool, &donotfree, test_mempool_destructor); |
| 162 cxMempoolDestroy(pool); |
162 cxMempoolFree(pool); |
| 163 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
163 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
| 164 } |
164 } |
| 165 } |
165 } |
| 166 |
166 |
| 167 |
167 |