tests/test_buffer.c

changeset 1028
3e4905241838
parent 1026
ca20f9ffcb62
child 1030
06091e067bee
--- a/tests/test_buffer.c	Thu Dec 19 12:00:20 2024 +0100
+++ b/tests/test_buffer.c	Thu Dec 19 21:33:45 2024 +0100
@@ -933,7 +933,39 @@
     cxBufferDestroy(&buf);
 }
 
-CX_TEST(test_buffer_put_string_extend_copy_on_write) {
+CX_TEST(test_buffer_put_string_copy_on_extend) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    const CxAllocator *alloc = &talloc.base;
+    CxBuffer buf;
+    char original[16] = "preparedXXXXXXX\0";
+    CX_TEST_DO {
+        cxBufferInit(&buf, original, 16, alloc, CX_BUFFER_COPY_ON_EXTEND);
+        buf.capacity = 8;
+        buf.size = buf.pos = 4;
+        size_t written = cxBufferPutString(&buf, "test");
+        CX_TEST_ASSERT(written == 4);
+        CX_TEST_ASSERT(buf.size == 8);
+        CX_TEST_ASSERT(buf.pos == 8);
+        CX_TEST_ASSERT(buf.capacity == 8);
+        CX_TEST_ASSERT(0 == memcmp(buf.space, "preptest", 8));
+        CX_TEST_ASSERT(original == buf.space);
+        written = cxBufferPutString(&buf, "ing");
+        CX_TEST_ASSERT(written == 3);
+        CX_TEST_ASSERT(buf.size == 11);
+        CX_TEST_ASSERT(buf.pos == 11);
+        CX_TEST_ASSERT(buf.capacity >= 11);
+        CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11));
+        CX_TEST_ASSERT(original != buf.space);
+        CX_TEST_ASSERT(0 == memcmp(original, "preptestXXXXXXX\0", 16));
+        CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
+        cxBufferDestroy(&buf);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+        cx_testing_allocator_destroy(&talloc);
+    }
+}
+
+CX_TEST(test_buffer_put_string_copy_on_write) {
     CxBuffer buf;
     char original[16] = "preparedXXXXXXX\0";
     cxBufferInit(&buf, original, 16, cxDefaultAllocator, CX_BUFFER_COPY_ON_WRITE);
@@ -1289,7 +1321,8 @@
     cx_test_register(suite, test_buffer_put_string_fit);
     cx_test_register(suite, test_buffer_put_string_discard);
     cx_test_register(suite, test_buffer_put_string_extend);
-    cx_test_register(suite, test_buffer_put_string_extend_copy_on_write);
+    cx_test_register(suite, test_buffer_put_string_copy_on_extend);
+    cx_test_register(suite, test_buffer_put_string_copy_on_write);
     cx_test_register(suite, test_buffer_terminate);
     cx_test_register(suite, test_buffer_write_size_overflow);
     cx_test_register(suite, test_buffer_write_capacity_overflow);

mercurial