fix stupid initialization of target buffer in tests where no flush target is used

Tue, 10 Dec 2024 00:09:55 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Dec 2024 00:09:55 +0100
changeset 1006
8ee818fa29f7
parent 1005
67fa386ce600
child 1007
81b2986d2b04

fix stupid initialization of target buffer in tests where no flush target is used

tests/test_buffer.c file | annotate | diff | comparison | revisions
--- a/tests/test_buffer.c	Tue Dec 10 00:07:16 2024 +0100
+++ b/tests/test_buffer.c	Tue Dec 10 00:09:55 2024 +0100
@@ -603,8 +603,7 @@
 }
 
 CX_TEST(test_buffer_write_size_one_fit) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -620,12 +619,10 @@
     }
 
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_size_one_discard) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -640,12 +637,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptest\0", 9));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_size_one_extend) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -661,12 +656,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_multibyte_fit) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -681,12 +674,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptest", 8));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_multibyte_discard) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -703,12 +694,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "pretest\0", 8));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_multibyte_extend) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -726,12 +715,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "pretester", 9));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_put_fit) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -745,12 +732,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "prepa\0", 6));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_put_discard) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -765,12 +750,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0\0\0\0\0", 9));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_put_extend) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -786,12 +769,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0\0\0\0a", 9));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_put_string_fit) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -806,12 +787,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptest", 8));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_put_string_discard) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -826,12 +805,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptest\0", 9));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_put_string_extend) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -847,12 +824,35 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptesting", 11));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
+}
+
+CX_TEST(test_buffer_terminate) {
+    CxBuffer buf;
+    cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
+    memcpy(buf.space, "prepAAAAAA\0\0\0\0\0\0", 16);
+    buf.capacity = 8;
+    buf.size = buf.pos = 4;
+    const char *data = "test";
+    CX_TEST_DO {
+        size_t written = cxBufferPutString(&buf, data);
+        CX_TEST_ASSERT(0 != cxBufferTerminate(&buf));
+        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, "preptestAA", 10));
+        buf.flags |= CX_BUFFER_AUTO_EXTEND;
+        CX_TEST_ASSERT(0 == cxBufferTerminate(&buf));
+        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\0A", 10));
+    }
+    cxBufferDestroy(&buf);
 }
 
 CX_TEST(test_buffer_write_size_overflow) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -867,12 +867,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0", 5));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_capacity_overflow) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "prep\0\0\0\0\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -888,12 +886,10 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "prep\0", 5));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_only_overwrite) {
-    CxBuffer buf, target;
-    cxBufferInit(&target, NULL, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
+    CxBuffer buf;
     cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
     memcpy(buf.space, "preptest\0\0\0\0\0\0\0\0", 16);
     buf.capacity = 8;
@@ -909,7 +905,6 @@
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preXXX\0t", 8));
     }
     cxBufferDestroy(&buf);
-    cxBufferDestroy(&target);
 }
 
 CX_TEST(test_buffer_write_flush_at_capacity) {
@@ -1158,6 +1153,7 @@
     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_terminate);
     cx_test_register(suite, test_buffer_write_size_overflow);
     cx_test_register(suite, test_buffer_write_capacity_overflow);
     cx_test_register(suite, test_buffer_write_only_overwrite);

mercurial