tests/test_buffer.c

changeset 1542
197450c2b0b3
parent 1539
2cbdb482d325
--- a/tests/test_buffer.c	Fri Dec 05 16:36:10 2025 +0100
+++ b/tests/test_buffer.c	Fri Dec 05 16:38:17 2025 +0100
@@ -277,6 +277,41 @@
     cx_testing_allocator_destroy(&talloc);
 }
 
+CX_TEST(test_buffer_reserve) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    CxAllocator *alloc = &talloc.base;
+    CX_TEST_DO {
+        CxBuffer buf;
+        char space[16] = "Testing";
+        cxBufferInit(&buf, space, 16, alloc, CX_BUFFER_COPY_ON_EXTEND);
+        buf.size = 8;
+        CX_TEST_ASSERT(buf.capacity == 16);
+        CX_TEST_ASSERT(talloc.alloc_total == 0);
+        // reserve to grow
+        cxBufferReserve(&buf, 32);
+        CX_TEST_ASSERT(buf.capacity == 32);
+        CX_TEST_ASSERT(buf.size == 8);
+        CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0);
+        CX_TEST_ASSERT(talloc.alloc_total > 0);
+        CX_TEST_ASSERT((buf.flags & CX_BUFFER_COPY_ON_EXTEND) == 0);
+        // reserve to shrink
+        buf.size = 24;
+        cxBufferReserve(&buf, 16);
+        CX_TEST_ASSERT(buf.capacity == 16);
+        CX_TEST_ASSERT(buf.size == 16);
+        CX_TEST_ASSERT(memcmp(buf.space, "Testing", 8) == 0);
+        // reserve to free
+        cxBufferReserve(&buf, 0);
+        CX_TEST_ASSERT(buf.capacity == 0);
+        CX_TEST_ASSERT(buf.size == 0);
+        CX_TEST_ASSERT(buf.space == NULL);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+        cxBufferDestroy(&buf);
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CX_TEST(test_buffer_clear) {
     char space[16];
     strcpy(space, "clear test");
@@ -1769,6 +1804,7 @@
     cx_test_register(suite, test_buffer_shrink);
     cx_test_register(suite, test_buffer_shrink_copy_on_write);
     cx_test_register(suite, test_buffer_shrink_copy_on_extend);
+    cx_test_register(suite, test_buffer_reserve);
     cx_test_register(suite, test_buffer_clear);
     cx_test_register(suite, test_buffer_clear_copy_on_write);
     cx_test_register(suite, test_buffer_reset);

mercurial