tests/test_buffer.c

changeset 1290
4ac889e14211
parent 1284
b2103354baed
child 1291
5942859fd76c
equal deleted inserted replaced
1289:2e8edba252a0 1290:4ac889e14211
179 buf.size = 8; 179 buf.size = 8;
180 cxBufferMinimumCapacity(&buf, 16); 180 cxBufferMinimumCapacity(&buf, 16);
181 CX_TEST_ASSERT(buf.capacity == 16); 181 CX_TEST_ASSERT(buf.capacity == 16);
182 CX_TEST_ASSERT(buf.size == 8); 182 CX_TEST_ASSERT(buf.size == 8);
183 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0); 183 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0);
184 cxBufferDestroy(&buf);
185 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
186 }
187 cx_testing_allocator_destroy(&talloc);
188 }
189
190 CX_TEST(test_buffer_shrink) {
191 CxTestingAllocator talloc;
192 cx_testing_allocator_init(&talloc);
193 CxAllocator *alloc = &talloc.base;
194 CX_TEST_DO {
195 CxBuffer buf;
196 cxBufferInit(&buf, NULL, 16, alloc, CX_BUFFER_FREE_CONTENTS);
197 cxBufferPutString(&buf, "Testing");
198 cxBufferTerminate(&buf);
199 CX_TEST_ASSERT(buf.capacity == 16);
200 CX_TEST_ASSERT(buf.size == 7);
201 cxBufferShrink(&buf, 4);
202 CX_TEST_ASSERT(buf.capacity == 11);
203 CX_TEST_ASSERT(buf.size == 7);
204 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0);
205 cxBufferDestroy(&buf);
206 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
207 }
208 cx_testing_allocator_destroy(&talloc);
209 }
210
211 CX_TEST(test_buffer_shrink_copy_on_write) {
212 CxTestingAllocator talloc;
213 cx_testing_allocator_init(&talloc);
214 CxAllocator *alloc = &talloc.base;
215 CX_TEST_DO {
216 CxBuffer buf;
217 const char* space = "Testing";
218 cxBufferInit(&buf, (void*)space, 16, alloc, CX_BUFFER_COPY_ON_WRITE);
219 buf.size = 8;
220 CX_TEST_ASSERT(buf.capacity == 16);
221 cxBufferShrink(&buf, 4);
222 CX_TEST_ASSERT(buf.capacity == 16);
223 CX_TEST_ASSERT(buf.size == 8);
224 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0);
225 CX_TEST_ASSERT(talloc.alloc_total == 0);
226 cxBufferDestroy(&buf);
227 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
228 }
229 cx_testing_allocator_destroy(&talloc);
230 }
231
232 CX_TEST(test_buffer_shrink_copy_on_extend) {
233 CxTestingAllocator talloc;
234 cx_testing_allocator_init(&talloc);
235 CxAllocator *alloc = &talloc.base;
236 CX_TEST_DO {
237 CxBuffer buf;
238 char space[16] = "Testing";
239 cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_COPY_ON_EXTEND);
240 buf.size = 8;
241 CX_TEST_ASSERT(buf.capacity == 16);
242 cxBufferShrink(&buf, 4);
243 CX_TEST_ASSERT(buf.capacity == 16);
244 CX_TEST_ASSERT(buf.size == 8);
245 CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0);
246 CX_TEST_ASSERT(talloc.alloc_total == 0);
184 cxBufferDestroy(&buf); 247 cxBufferDestroy(&buf);
185 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 248 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
186 } 249 }
187 cx_testing_allocator_destroy(&talloc); 250 cx_testing_allocator_destroy(&talloc);
188 } 251 }
1503 cx_test_register(suite, test_buffer_init_wrap_space_auto_free); 1566 cx_test_register(suite, test_buffer_init_wrap_space_auto_free);
1504 cx_test_register(suite, test_buffer_init_fresh_space); 1567 cx_test_register(suite, test_buffer_init_fresh_space);
1505 cx_test_register(suite, test_buffer_init_on_heap); 1568 cx_test_register(suite, test_buffer_init_on_heap);
1506 cx_test_register(suite, test_buffer_minimum_capacity_sufficient); 1569 cx_test_register(suite, test_buffer_minimum_capacity_sufficient);
1507 cx_test_register(suite, test_buffer_minimum_capacity_extend); 1570 cx_test_register(suite, test_buffer_minimum_capacity_extend);
1571 cx_test_register(suite, test_buffer_shrink);
1572 cx_test_register(suite, test_buffer_shrink_copy_on_write);
1573 cx_test_register(suite, test_buffer_shrink_copy_on_extend);
1508 cx_test_register(suite, test_buffer_clear); 1574 cx_test_register(suite, test_buffer_clear);
1509 cx_test_register(suite, test_buffer_clear_copy_on_write); 1575 cx_test_register(suite, test_buffer_clear_copy_on_write);
1510 cx_test_register(suite, test_buffer_reset); 1576 cx_test_register(suite, test_buffer_reset);
1511 cx_test_register(suite, test_buffer_seek_set_zero); 1577 cx_test_register(suite, test_buffer_seek_set_zero);
1512 cx_test_register(suite, test_buffer_seek_set_valid); 1578 cx_test_register(suite, test_buffer_seek_set_valid);

mercurial