#170 complete write tests for status quo

Sun, 01 May 2022 11:44:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 May 2022 11:44:23 +0200
changeset 541
67e078518935
parent 540
47e0f2237a94
child 542
45bcfd152f85

#170 complete write tests for status quo

src/buffer.c file | annotate | diff | comparison | revisions
src/cx/buffer.h file | annotate | diff | comparison | revisions
test/test_buffer.cpp file | annotate | diff | comparison | revisions
--- a/src/buffer.c	Sun May 01 10:39:13 2022 +0200
+++ b/src/buffer.c	Sun May 01 11:44:23 2022 +0200
@@ -57,6 +57,7 @@
     buffer->pos = 0;
 
     buffer->flush_func = NULL;
+    buffer->flush_target = NULL;
     buffer->flush_blkmax = 0;
     buffer->flush_blksize = 4096;
     buffer->flush_threshold = SIZE_MAX;
--- a/src/cx/buffer.h	Sun May 01 10:39:13 2022 +0200
+++ b/src/cx/buffer.h	Sun May 01 11:44:23 2022 +0200
@@ -125,6 +125,11 @@
     );
 
     /**
+     * The target for \c flush_func.
+     */
+    void *flush_target;
+
+    /**
      * Flag register for buffer features.
      * @see #CX_BUFFER_DEFAULT
      * @see #CX_BUFFER_FREE_CONTENTS
--- a/test/test_buffer.cpp	Sun May 01 10:39:13 2022 +0200
+++ b/test/test_buffer.cpp	Sun May 01 11:44:23 2022 +0200
@@ -36,6 +36,7 @@
     EXPECT_EQ(buf->flush_blksize, 4096);
     EXPECT_EQ(buf->flush_threshold, SIZE_MAX);
     EXPECT_EQ(buf->flush_func, nullptr);
+    EXPECT_EQ(buf->flush_target, nullptr);
 }
 
 TEST(BufferInit, WrapSpace) {
@@ -311,6 +312,7 @@
     const char *data = "test";
     ASSERT_EQ(buf.capacity, 8);
     ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
     size_t written = cxBufferWrite(data, 1, 4, &buf);
     EXPECT_EQ(written, 4);
     EXPECT_EQ(buf.size, 8);
@@ -319,18 +321,11 @@
     EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
 }
 
-TEST_F(BufferWrite, SizeOneOverwrite) {
-
-}
-
-TEST_F(BufferWrite, SizeOnePartialOverwrite) {
-
-}
-
 TEST_F(BufferWrite, SizeOneDiscard) {
     const char *data = "testing";
     ASSERT_EQ(buf.capacity, 8);
     ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
     size_t written = cxBufferWrite(data, 1, 7, &buf);
     EXPECT_EQ(written, 4);
     EXPECT_EQ(buf.size, 8);
@@ -344,6 +339,7 @@
     const char *data = "testing";
     ASSERT_EQ(buf.capacity, 8);
     ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
     size_t written = cxBufferWrite(data, 1, 7, &buf);
     EXPECT_EQ(written, 7);
     EXPECT_EQ(buf.size, 11);
@@ -352,62 +348,162 @@
     EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
 }
 
-TEST_F(BufferWrite, MultibyteOverwrite) {
-
-}
-
-TEST_F(BufferWrite, MultibytePartialOverwrite) {
-
-}
-
 TEST_F(BufferWrite, MultibyteFit) {
-
+    const char *data = "test";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    size_t written = cxBufferWrite(data, 2, 2, &buf);
+    EXPECT_EQ(written, 2);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_EQ(buf.pos, 8);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
 }
 
 TEST_F(BufferWrite, MultibyteDiscard) {
+    const char *data = "tester";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.size, 4);
+    buf.pos = 3;
+    size_t written = cxBufferWrite(data, 2, 3, &buf);
     // remember: whole elements are discarded if they do not fit
+    EXPECT_EQ(written, 2);
+    EXPECT_EQ(buf.size, 7);
+    EXPECT_EQ(buf.pos, 7);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(memcmp(buf.space, "pretest\0", 8), 0);
 }
 
 TEST_F(BufferWrite, MultibyteExtend) {
-
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    const char *data = "tester";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.size, 4);
+    buf.pos = 3;
+    size_t written = cxBufferWrite(data, 2, 3, &buf);
+    // remember: whole elements are discarded if they do not fit
+    EXPECT_EQ(written, 3);
+    EXPECT_EQ(buf.size, 9);
+    EXPECT_EQ(buf.pos, 9);
+    EXPECT_GE(buf.capacity, 9);
+    EXPECT_EQ(memcmp(buf.space, "pretester\0", 10), 0);
 }
 
 TEST_F(BufferWrite, PutcWrapperFit) {
-
-}
-
-TEST_F(BufferWrite, PutcWrapperOverwrite) {
-
-}
-
-TEST_F(BufferWrite, PutcWrapperPartialOverwrite) {
-
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int c = cxBufferPut(&buf, 0x20 | 'a');
+    EXPECT_EQ(c, 'a');
+    EXPECT_EQ(buf.size, 5);
+    EXPECT_EQ(buf.pos, 5);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(memcmp(buf.space, "prepa\0", 6), 0);
 }
 
 TEST_F(BufferWrite, PutcWrapperDiscard) {
-
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.size, 4);
+    buf.pos = 8;
+    int c = cxBufferPut(&buf, 0x20 | 'a');
+    EXPECT_EQ(c, EOF);
+    EXPECT_EQ(buf.size, 4);
+    EXPECT_EQ(buf.pos, 8);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0\0", 9), 0);
 }
 
 TEST_F(BufferWrite, PutcWrapperExtend) {
-
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.size, 4);
+    buf.pos = 8;
+    int c = cxBufferPut(&buf, 0x20 | 'a');
+    EXPECT_EQ(c, 'a');
+    EXPECT_EQ(buf.size, 9);
+    EXPECT_EQ(buf.pos, 9);
+    EXPECT_GE(buf.capacity, 9);
+    EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0a\0", 10), 0);
 }
 
 TEST_F(BufferWrite, PutStringWrapperFit) {
-
-}
-
-TEST_F(BufferWrite, PutStringWrapperOverwrite) {
-
-}
-
-TEST_F(BufferWrite, PutStringWrapperPartialOverwrite) {
-
+    const char *data = "test";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    size_t written = cxBufferPutString(&buf, data);
+    EXPECT_EQ(written, 4);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_EQ(buf.pos, 8);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
 }
 
 TEST_F(BufferWrite, PutStringWrapperDiscard) {
-
+    const char *data = "testing";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    size_t written = cxBufferPutString(&buf, data);
+    EXPECT_EQ(written, 4);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_EQ(buf.pos, 8);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0);
 }
 
 TEST_F(BufferWrite, PutStringWrapperExtend) {
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    const char *data = "testing";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    size_t written = cxBufferPutString(&buf, data);
+    EXPECT_EQ(written, 7);
+    EXPECT_EQ(buf.size, 11);
+    EXPECT_EQ(buf.pos, 11);
+    EXPECT_GE(buf.capacity, 11);
+    EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
+}
 
+TEST_F(BufferWrite, MultOverflow) {
+    const char *data = "testing";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    size_t written = cxBufferWrite(data, 8, SIZE_MAX / 4, &buf);
+    EXPECT_EQ(written, 0);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(buf.pos, 4);
+    EXPECT_EQ(buf.size, 4);
+    EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
 }
+
+TEST_F(BufferWrite, MaxCapaOverflow) {
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    const char *data = "testing";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    size_t written = cxBufferWrite(data, 1, SIZE_MAX - 2, &buf);
+    EXPECT_EQ(written, 0);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(buf.pos, 4);
+    EXPECT_EQ(buf.size, 4);
+    EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
+}
+
+TEST_F(BufferWrite, OnlyOverwrite) {
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    ASSERT_EQ(buf.capacity, 8);
+    memcpy(buf.space, "preptest", 8);
+    buf.pos = 3;
+    buf.size = 8;
+    size_t written = cxBufferWrite("XXX", 2, 2, &buf);
+    EXPECT_EQ(written, 2);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_EQ(buf.pos, 7);
+    EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0);
+}

mercurial