migrate more buffer tests - relates to #342

Wed, 03 Jan 2024 22:17:40 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 03 Jan 2024 22:17:40 +0100
changeset 792
3ca984931e1d
parent 791
945ee9bf2dd1
child 793
db1c8dfe403a

migrate more buffer tests - relates to #342

only read and write tests are remaining now

tests/test_buffer.c file | annotate | diff | comparison | revisions
tests/test_buffer.cpp file | annotate | diff | comparison | revisions
--- a/tests/test_buffer.c	Tue Jan 02 23:31:00 2024 +0100
+++ b/tests/test_buffer.c	Wed Jan 03 22:17:40 2024 +0100
@@ -389,6 +389,202 @@
     cxBufferDestroy(&buf);
 }
 
+CX_TEST(test_buffer_eof_reached) {
+    CxBuffer buf;
+    cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
+    buf.size = buf.pos = 3;
+    CX_TEST_DO {
+        CX_TEST_ASSERT(cxBufferEof(&buf));
+        buf.pos = buf.size - 1;
+        CX_TEST_ASSERT(!cxBufferEof(&buf));
+        cxBufferPut(&buf, 'a');
+        CX_TEST_ASSERT(cxBufferEof(&buf));
+    }
+    cxBufferDestroy(&buf);
+}
+
+CX_TEST(test_buffer_eof_not_reached) {
+    CxBuffer buf;
+    cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
+    buf.size = 6;
+    CX_TEST_DO {
+        buf.pos = buf.size - 1;
+        CX_TEST_ASSERT(!cxBufferEof(&buf));
+        buf.pos = 0;
+        cxBufferWrite("test", 1, 5, &buf);
+        CX_TEST_ASSERT(!cxBufferEof(&buf));
+    }
+    cxBufferDestroy(&buf);
+}
+
+#define TEST_BUFFER_SHIFT_SETUP(buf) \
+    CxTestingAllocator talloc; \
+    cx_testing_allocator_init(&talloc); \
+    CxAllocator *alloc = &talloc.base; \
+    CxBuffer buf; \
+    cxBufferInit(&buf, NULL, 16, alloc, CX_BUFFER_DEFAULT); \
+    memcpy(buf.space, "test____XXXXXXXX", 16); \
+    buf.capacity = 8; \
+    buf.pos = 4; \
+    buf.size = 4
+#define TEST_BUFFER_SHIFT_TEARDOWN(buf) \
+    cxBufferDestroy(&buf); \
+    CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); \
+    cx_testing_allocator_destroy(&talloc);
+
+
+CX_TEST(test_buffer_shift_left_zero) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShiftLeft(&buf, 0);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 4);
+        CX_TEST_ASSERT(buf.size == 4);
+        CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_left_zero_offset_interface) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShift(&buf, -0);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 4);
+        CX_TEST_ASSERT(buf.size == 4);
+        CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_left_standard) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShiftLeft(&buf, 2);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 2);
+        CX_TEST_ASSERT(buf.size == 2);
+        CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_left_overshift) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShiftLeft(&buf, 6);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 0);
+        CX_TEST_ASSERT(buf.size == 0);
+        CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_left_overshift_pos_only) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    buf.pos = 2;
+    CX_TEST_DO {
+        int ret = cxBufferShiftLeft(&buf, 3);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 0);
+        CX_TEST_ASSERT(buf.size == 1);
+        CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_left_offset_interface) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    buf.pos = 3;
+    CX_TEST_DO {
+        int ret = cxBufferShift(&buf, -2);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 1);
+        CX_TEST_ASSERT(buf.size == 2);
+        CX_TEST_ASSERT(memcmp(buf.space, "stst____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_right_zero) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShiftRight(&buf, 0);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 4);
+        CX_TEST_ASSERT(buf.size == 4);
+        CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_right_zero_offset_interface) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShift(&buf, +0);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 4);
+        CX_TEST_ASSERT(buf.size == 4);
+        CX_TEST_ASSERT(memcmp(buf.space, "test____XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_right_standard) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShiftRight(&buf, 3);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 7);
+        CX_TEST_ASSERT(buf.size == 7);
+        CX_TEST_ASSERT(memcmp(buf.space, "testest_XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_right_overshift_discard) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    CX_TEST_DO {
+        int ret = cxBufferShiftRight(&buf, 6);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 8);
+        CX_TEST_ASSERT(buf.size == 8);
+        CX_TEST_ASSERT(buf.capacity == 8);
+        CX_TEST_ASSERT(memcmp(buf.space, "test__teXXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_right_overshift_extend) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    buf.flags |= CX_BUFFER_AUTO_EXTEND;
+    CX_TEST_DO {
+        int ret = cxBufferShiftRight(&buf, 6);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 10);
+        CX_TEST_ASSERT(buf.size == 10);
+        CX_TEST_ASSERT(buf.capacity >= 10);
+        // cannot assert more than 10 bytes because
+        // the buffer was required to reallocate the space
+        CX_TEST_ASSERT(memcmp(buf.space, "test__test", 10) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
+CX_TEST(test_buffer_shift_right_offset_interface) {
+    TEST_BUFFER_SHIFT_SETUP(buf);
+    buf.pos = 3;
+    CX_TEST_DO {
+        int ret = cxBufferShift(&buf, 2);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(buf.pos == 5);
+        CX_TEST_ASSERT(buf.size == 6);
+        CX_TEST_ASSERT(memcmp(buf.space, "tetest__XXXXXXXX", 16) == 0);
+        TEST_BUFFER_SHIFT_TEARDOWN(buf);
+    }
+}
+
 CxTestSuite *cx_test_suite_buffer(void) {
     CxTestSuite *suite = cx_test_suite_new("buffer");
 
@@ -413,6 +609,20 @@
     cx_test_register(suite, test_buffer_seek_end_valid);
     cx_test_register(suite, test_buffer_seek_end_invalid);
     cx_test_register(suite, test_buffer_seek_whence_invalid);
+    cx_test_register(suite, test_buffer_eof_reached);
+    cx_test_register(suite, test_buffer_eof_not_reached);
+    cx_test_register(suite, test_buffer_shift_left_zero);
+    cx_test_register(suite, test_buffer_shift_left_zero_offset_interface);
+    cx_test_register(suite, test_buffer_shift_left_standard);
+    cx_test_register(suite, test_buffer_shift_left_overshift);
+    cx_test_register(suite, test_buffer_shift_left_overshift_pos_only);
+    cx_test_register(suite, test_buffer_shift_left_offset_interface);
+    cx_test_register(suite, test_buffer_shift_right_zero);
+    cx_test_register(suite, test_buffer_shift_right_zero_offset_interface);
+    cx_test_register(suite, test_buffer_shift_right_standard);
+    cx_test_register(suite, test_buffer_shift_right_overshift_discard);
+    cx_test_register(suite, test_buffer_shift_right_overshift_extend);
+    cx_test_register(suite, test_buffer_shift_right_offset_interface);
     
     return suite;
 }
--- a/tests/test_buffer.cpp	Tue Jan 02 23:31:00 2024 +0100
+++ b/tests/test_buffer.cpp	Wed Jan 03 22:17:40 2024 +0100
@@ -31,169 +31,6 @@
 #include <gtest/gtest.h>
 #include "util_allocator.h"
 
-class BufferFixture : public ::testing::Test {
-protected:
-    void SetUp() override {
-        cxBufferInit(&buf, NULL, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
-        buf.size = 6;
-        buf.pos = 3;
-    }
-
-    void TearDown() override {
-        cxBufferDestroy(&buf);
-    }
-
-    CxBuffer buf{};
-};
-
-class BufferShiftFixture : public ::testing::Test {
-protected:
-    void SetUp() override {
-        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
-        cxBufferInit(&buf, NULL, 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);
-        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
-    }
-
-    CxTestingAllocator alloc;
-    CxBuffer buf{};
-};
-
-class BufferShiftLeft : public BufferShiftFixture {
-};
-
-TEST_F(BufferShiftLeft, Zero) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShiftLeft(&buf, 0);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
-}
-
-TEST_F(BufferShiftLeft, ZeroOffsetInterface) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShift(&buf, -0);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
-}
-
-TEST_F(BufferShiftLeft, Standard) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShiftLeft(&buf, 2);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 2);
-    CX_TEST_ASSERT(buf.size == 2);
-    CX_TEST_ASSERT(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);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 0);
-    CX_TEST_ASSERT(buf.size == 0);
-    CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
-}
-
-TEST_F(BufferShiftLeft, OvershiftPosOnly) {
-    buf.pos = 2;
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShiftLeft(&buf, 3);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 0);
-    CX_TEST_ASSERT(buf.size == 1);
-    CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
-}
-
-TEST_F(BufferShiftLeft, OffsetInterface) {
-    buf.pos = 3;
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShift(&buf, -2);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 1);
-    CX_TEST_ASSERT(buf.size == 2);
-    CX_TEST_ASSERT(memcmp(buf.space, "stst________", 8) == 0);
-}
-
-TEST_F(BufferShiftRight, Zero) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShiftRight(&buf, 0);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
-}
-
-TEST_F(BufferShiftRight, ZeroOffsetInterface) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShift(&buf, +0);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    CX_TEST_ASSERT(memcmp(buf.space, "test________", 8) == 0);
-}
-
-TEST_F(BufferShiftRight, Standard) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShiftRight(&buf, 3);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 7);
-    CX_TEST_ASSERT(buf.size == 7);
-    CX_TEST_ASSERT(memcmp(buf.space, "testest_____", 8) == 0);
-}
-
-TEST_F(BufferShiftRight, OvershiftDiscard) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    CX_TEST_ASSERT(buf.capacity == 8);
-    int ret = cxBufferShiftRight(&buf, 6);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 8);
-    CX_TEST_ASSERT(buf.size == 8);
-    CX_TEST_ASSERT(buf.capacity == 8);
-    CX_TEST_ASSERT(memcmp(buf.space, "test__te____", 8) == 0);
-}
-
-TEST_F(BufferShiftRight, OvershiftExtend) {
-    CX_TEST_ASSERT(buf.pos == 4);
-    CX_TEST_ASSERT(buf.size == 4);
-    CX_TEST_ASSERT(buf.capacity == 8);
-    buf.flags |= CX_BUFFER_AUTO_EXTEND;
-    int ret = cxBufferShiftRight(&buf, 6);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 10);
-    CX_TEST_ASSERT(buf.size == 10);
-    EXPECT_GE(buf.capacity, 10);
-    CX_TEST_ASSERT(memcmp(buf.space, "test__test__", 8) == 0);
-}
-
-TEST_F(BufferShiftRight, OffsetInterface) {
-    buf.pos = 3;
-    CX_TEST_ASSERT(buf.size == 4);
-    int ret = cxBufferShift(&buf, 2);
-    CX_TEST_ASSERT(ret == 0);
-    CX_TEST_ASSERT(buf.pos == 5);
-    CX_TEST_ASSERT(buf.size == 6);
-    CX_TEST_ASSERT(memcmp(buf.space, "tetest______", 8) == 0);
-}
-
 class BufferWrite : public ::testing::Test {
 protected:
     CxBuffer buf{}, target{};
@@ -507,24 +344,6 @@
     EXPECT_EQ(memcmp(target.space, "prepfoohello,", 13), 0);
 }
 
-
-TEST_F(BufferEof, Reached) {
-    buf.pos = buf.size;
-    CX_TEST_ASSERT(cxBufferEof(&buf));
-    buf.pos = buf.size - 1;
-    CX_TEST_ASSERT(!cxBufferEof(&buf));
-    cxBufferPut(&buf, 'a');
-    CX_TEST_ASSERT(cxBufferEof(&buf));
-}
-
-TEST_F(BufferEof, NotReached) {
-    buf.pos = buf.size - 1;
-    CX_TEST_ASSERT(!cxBufferEof(&buf));
-    buf.pos = 0;
-    cxBufferWrite("test", 1, 5, &buf);
-    CX_TEST_ASSERT(!cxBufferEof(&buf));
-}
-
 class BufferRead : public ::testing::Test {
 protected:
     CxBuffer buf{};

mercurial