test/test_buffer.cpp

changeset 545
3700ac4bd9a3
parent 542
45bcfd152f85
child 546
900795d59d03
--- a/test/test_buffer.cpp	Sun May 01 13:14:31 2022 +0200
+++ b/test/test_buffer.cpp	Sun May 01 16:12:13 2022 +0200
@@ -293,9 +293,10 @@
 
 class BufferWrite : public ::testing::Test {
 protected:
-    CxBuffer buf{};
+    CxBuffer buf{}, target{};
 
     void SetUp() override {
+        cxBufferInit(&target, nullptr, 16, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
         cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
         buf.capacity = 8; // artificially reduce capacity to check OOB writes
         memset(buf.space, 0, 16);
@@ -305,6 +306,13 @@
 
     void TearDown() override {
         cxBufferDestroy(&buf);
+        cxBufferDestroy(&target);
+    }
+
+    void enableFlushing() {
+        buf.flush_target = ⌖
+        buf.flush_func = reinterpret_cast<cx_write_func>(cxBufferWrite);
+        buf.flush_blkmax = 1;
     }
 };
 
@@ -507,3 +515,23 @@
     EXPECT_EQ(buf.pos, 7);
     EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0);
 }
+
+TEST_F(BufferWrite, FlushAtCapacity) {
+    enableFlushing();
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    size_t written = cxBufferWrite("foo", 1, 3, &buf);
+    EXPECT_EQ(written, 3);
+    ASSERT_EQ(buf.pos, 7);
+    ASSERT_EQ(buf.size, 7);
+    ASSERT_EQ(target.pos, 0);
+    ASSERT_EQ(target.size, 0);
+    written = cxBufferWrite("hello", 1, 5, &buf);
+    EXPECT_EQ(written, 5);
+    EXPECT_EQ(buf.pos, 0);
+    EXPECT_EQ(buf.size, 0);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(target.pos, 12);
+    ASSERT_EQ(target.size, 12);
+    EXPECT_EQ(memcmp(target.space, "prepfoohello", 12), 0);
+}

mercurial