test/test_buffer.cpp

changeset 538
2cfbcbe86a7c
parent 536
cb9b9739055e
child 539
9cd98da9ee17
--- a/test/test_buffer.cpp	Sat Apr 30 08:40:40 2022 +0200
+++ b/test/test_buffer.cpp	Sat Apr 30 09:03:17 2022 +0200
@@ -258,4 +258,127 @@
     EXPECT_EQ(memcmp(space, "\0\0\0\0\0 test", 10), 0);
     EXPECT_EQ(buf.size, 0);
     EXPECT_EQ(buf.pos, 0);
+    cxBufferDestroy(&buf);
 }
+
+class BufferWrite : public ::testing::Test {
+protected:
+    CxBuffer buf{};
+
+    void SetUp() override {
+        cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
+        buf.capacity = 8; // artificially reduce capacity to check OOB writes
+        memset(buf.space, 0, 16);
+        memcpy(buf.space, "prep", 4);
+        buf.size = buf.pos = 4;
+    }
+
+    void TearDown() override {
+        cxBufferDestroy(&buf);
+    }
+};
+
+TEST_F(BufferWrite, SizeOneFit) {
+    const char *data = "test";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    size_t written = cxBufferWrite(data, 1, 4, &buf);
+    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, SizeOneOverwrite) {
+
+}
+
+TEST_F(BufferWrite, SizeOnePartialOverwrite) {
+
+}
+
+TEST_F(BufferWrite, SizeOneDiscard) {
+    const char *data = "testing";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    size_t written = cxBufferWrite(data, 1, 7, &buf);
+    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, SizeOneExtend) {
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    const char *data = "testing";
+    ASSERT_EQ(buf.capacity, 8);
+    ASSERT_EQ(buf.pos, 4);
+    size_t written = cxBufferWrite(data, 1, 7, &buf);
+    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, MultibyteOverwrite) {
+
+}
+
+TEST_F(BufferWrite, MultibytePartialOverwrite) {
+
+}
+
+TEST_F(BufferWrite, MultibyteFit) {
+
+}
+
+TEST_F(BufferWrite, MultibyteDiscard) {
+    // remember: whole elements are discarded if they do not fit
+}
+
+TEST_F(BufferWrite, MultibyteExtend) {
+
+}
+
+TEST_F(BufferWrite, PutcWrapperFit) {
+
+}
+
+TEST_F(BufferWrite, PutcWrapperOverwrite) {
+
+}
+
+TEST_F(BufferWrite, PutcWrapperPartialOverwrite) {
+
+}
+
+TEST_F(BufferWrite, PutcWrapperDiscard) {
+
+}
+
+TEST_F(BufferWrite, PutcWrapperExtend) {
+
+}
+
+TEST_F(BufferWrite, PutStringWrapperFit) {
+
+}
+
+TEST_F(BufferWrite, PutStringWrapperOverwrite) {
+
+}
+
+TEST_F(BufferWrite, PutStringWrapperPartialOverwrite) {
+
+}
+
+TEST_F(BufferWrite, PutStringWrapperDiscard) {
+
+}
+
+TEST_F(BufferWrite, PutStringWrapperExtend) {
+
+}

mercurial