#170 add several more buffer tests

Sun, 24 Apr 2022 17:45:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 24 Apr 2022 17:45:09 +0200
changeset 535
2ff6e9184468
parent 534
0e893f84a7dd
child 536
cb9b9739055e

#170 add several more buffer tests

test/test_buffer.cpp file | annotate | diff | comparison | revisions
--- a/test/test_buffer.cpp	Sun Apr 24 17:44:52 2022 +0200
+++ b/test/test_buffer.cpp	Sun Apr 24 17:45:09 2022 +0200
@@ -31,44 +31,214 @@
 #include <gtest/gtest.h>
 #include "util_allocator.h"
 
-class Buffer : public ::testing::Test {
-protected:
-    void TearDown() override {
-        EXPECT_TRUE(testingAllocator.verify());
-    }
-
-    CxTestingAllocator testingAllocator;
-};
-
-TEST_F(Buffer, WrapSpace) {
+TEST(BufferInit, WrapSpace) {
+    CxTestingAllocator alloc;
     CxBuffer buf;
-    void *space = cxMalloc(&testingAllocator, 16);
-    cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_DEFAULT);
+    void *space = cxMalloc(&alloc, 16);
+    cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_DEFAULT);
     EXPECT_EQ(buf.space, space);
     EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
     EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
     EXPECT_EQ(buf.pos, 0);
     EXPECT_EQ(buf.size, 0);
     EXPECT_EQ(buf.capacity, 16);
-    EXPECT_EQ(buf.allocator, &testingAllocator);
+    EXPECT_EQ(buf.allocator, &alloc);
     cxBufferDestroy(&buf);
-    EXPECT_FALSE(testingAllocator.verify());
-    cxFree(&testingAllocator, space);
-    EXPECT_TRUE(testingAllocator.verify());
+    EXPECT_FALSE(alloc.verify());
+    cxFree(&alloc, space);
+    EXPECT_TRUE(alloc.verify());
 }
 
-TEST_F(Buffer, WrapSpaceAutoFree) {
+TEST(BufferInit, WrapSpaceAutoFree) {
+    CxTestingAllocator alloc;
     CxBuffer buf;
-    void *space = cxMalloc(&testingAllocator, 16);
-    cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_FREE_CONTENTS);
+    void *space = cxMalloc(&alloc, 16);
+    cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
     EXPECT_EQ(buf.space, space);
     EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
     EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
     EXPECT_EQ(buf.pos, 0);
     EXPECT_EQ(buf.size, 0);
     EXPECT_EQ(buf.capacity, 16);
-    EXPECT_EQ(buf.allocator, &testingAllocator);
-    EXPECT_FALSE(testingAllocator.verify());
+    EXPECT_EQ(buf.allocator, &alloc);
+    EXPECT_FALSE(alloc.verify());
+    cxBufferDestroy(&buf);
+    EXPECT_TRUE(alloc.verify());
+}
+
+TEST(BufferInit, FreshSpace) {
+    CxTestingAllocator alloc;
+    CxBuffer buf;
+    cxBufferInit(&buf, nullptr, 8, &alloc, CX_BUFFER_DEFAULT);
+    EXPECT_NE(buf.space, nullptr);
+    EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
+    EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
+    EXPECT_EQ(buf.pos, 0);
+    EXPECT_EQ(buf.size, 0);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(buf.allocator, &alloc);
+    EXPECT_FALSE(alloc.verify()); // space is still allocated
     cxBufferDestroy(&buf);
-    EXPECT_TRUE(testingAllocator.verify());
+    EXPECT_TRUE(alloc.verify());
+}
+
+class BufferShiftFixture : public ::testing::Test {
+protected:
+    void SetUp() override {
+        ASSERT_TRUE(alloc.verify());
+        cxBufferInit(&buf, nullptr, 16, &alloc, CX_BUFFER_DEFAULT);
+        memcpy(buf.space, "test____________", 16);
+        buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range
+        buf.pos = 4;
+        buf.size = 4;
+    }
+
+    void TearDown() override {
+        cxBufferDestroy(&buf);
+        EXPECT_TRUE(alloc.verify());
+    }
+
+    CxTestingAllocator alloc;
+    CxBuffer buf{};
+};
+
+class BufferShiftLeft : public BufferShiftFixture {
+};
+
+TEST_F(BufferShiftLeft, Zero) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShiftLeft(&buf, 0);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 4);
+    EXPECT_EQ(buf.size, 4);
+    EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
+}
+
+TEST_F(BufferShiftLeft, Standard) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShiftLeft(&buf, 2);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 2);
+    EXPECT_EQ(buf.size, 2);
+    EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
+}
+
+TEST_F(BufferShiftLeft, Overshift) {
+    ASSERT_LT(buf.pos, 6);
+    ASSERT_LT(buf.size, 6);
+    int ret = cxBufferShiftLeft(&buf, 6);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 0);
+    EXPECT_EQ(buf.size, 0);
+    EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
+}
+
+TEST_F(BufferShiftLeft, OvershiftPosOnly) {
+    buf.pos = 2;
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShiftLeft(&buf, 3);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 0);
+    EXPECT_EQ(buf.size, 1);
+    EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
+}
+
+TEST_F(BufferShiftLeft, OffsetInterface) {
+    buf.pos = 3;
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShift(&buf, -2);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 1);
+    EXPECT_EQ(buf.size, 2);
+    EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
 }
+
+class BufferShiftRight : public BufferShiftFixture {
+};
+
+TEST_F(BufferShiftRight, Zero) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShiftRight(&buf, 0);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 4);
+    EXPECT_EQ(buf.size, 4);
+    EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
+}
+
+TEST_F(BufferShiftRight, Standard) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShiftRight(&buf, 3);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 7);
+    EXPECT_EQ(buf.size, 7);
+    EXPECT_TRUE(memcmp(buf.space, "testest_____", 8) == 0);
+}
+
+TEST_F(BufferShiftRight, OvershiftDiscard) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    ASSERT_EQ(buf.capacity, 8);
+    int ret = cxBufferShiftRight(&buf, 6);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 8);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_TRUE(memcmp(buf.space, "test__te____", 8) == 0);
+}
+
+TEST_F(BufferShiftRight, OvershiftExtend) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    ASSERT_EQ(buf.capacity, 8);
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    int ret = cxBufferShiftRight(&buf, 6);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 10);
+    EXPECT_EQ(buf.size, 10);
+    EXPECT_GE(buf.capacity, 10);
+    EXPECT_TRUE(memcmp(buf.space, "test__test__", 8) == 0);
+}
+
+TEST_F(BufferShiftRight, OffsetInterface) {
+    buf.pos = 3;
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShift(&buf, 2);
+    EXPECT_EQ(ret, 0);
+    EXPECT_EQ(buf.pos, 5);
+    EXPECT_EQ(buf.size, 6);
+    EXPECT_TRUE(memcmp(buf.space, "tetest______", 8) == 0);
+}
+
+TEST(BufferMinimumCapacity, Sufficient) {
+    CxTestingAllocator alloc;
+    auto space = cxMalloc(&alloc, 8);
+    CxBuffer buf;
+    cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS);
+    memcpy(space, "Testing", 8);
+    buf.size = 8;
+    cxBufferMinimumCapacity(&buf, 6);
+    EXPECT_EQ(buf.capacity, 8);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
+    cxBufferDestroy(&buf);
+    EXPECT_TRUE(alloc.verify());
+}
+
+TEST(BufferMinimumCapacity, Extend) {
+    CxTestingAllocator alloc;
+    auto space = cxMalloc(&alloc, 8);
+    CxBuffer buf;
+    cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend!
+    memcpy(space, "Testing", 8);
+    buf.size = 8;
+    cxBufferMinimumCapacity(&buf, 16);
+    EXPECT_EQ(buf.capacity, 16);
+    EXPECT_EQ(buf.size, 8);
+    EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
+    cxBufferDestroy(&buf);
+    EXPECT_TRUE(alloc.verify());
+}

mercurial