--- 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{};