test/test_buffer.cpp

changeset 547
9e933920dcf9
parent 546
900795d59d03
child 548
459bca1cdf8d
--- a/test/test_buffer.cpp	Sun May 01 16:48:26 2022 +0200
+++ b/test/test_buffer.cpp	Mon May 16 19:15:44 2022 +0200
@@ -145,6 +145,16 @@
     EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
 }
 
+TEST_F(BufferShiftLeft, ZeroOffsetInterface) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShift(&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);
@@ -198,6 +208,16 @@
     EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
 }
 
+TEST_F(BufferShiftRight, ZeroOffsetInterface) {
+    ASSERT_EQ(buf.pos, 4);
+    ASSERT_EQ(buf.size, 4);
+    int ret = cxBufferShift(&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);
@@ -559,3 +579,103 @@
     ASSERT_EQ(target.size, 15);
     EXPECT_EQ(memcmp(target.space, "prepfoobarhello", 15), 0);
 }
+
+class BufferSeek : public ::testing::Test {
+protected:
+    void SetUp() override {
+        cxBufferInit(&buf, nullptr, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT);
+        buf.size = 6;
+        buf.pos = 3;
+    }
+
+    void TearDown() override {
+        cxBufferDestroy(&buf);
+    }
+
+    CxBuffer buf{};
+};
+
+TEST_F(BufferSeek, SetZero) {
+    int result = cxBufferSeek(&buf, 0, SEEK_SET);
+    EXPECT_EQ(result, 0);
+    EXPECT_EQ(buf.pos, 0);
+}
+
+TEST_F(BufferSeek, SetValid) {
+    int result = cxBufferSeek(&buf, 5, SEEK_SET);
+    EXPECT_EQ(result, 0);
+    EXPECT_EQ(buf.pos, 5);
+}
+
+TEST_F(BufferSeek, SetInvalid) {
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, 6, SEEK_SET);
+    EXPECT_NE(result, 0);
+    EXPECT_EQ(buf.pos, 3);
+}
+
+TEST_F(BufferSeek, CurZero) {
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, 0, SEEK_CUR);
+    EXPECT_EQ(result, 0);
+    EXPECT_EQ(buf.pos, 3);
+}
+
+TEST_F(BufferSeek, CurValidPositive) {
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, 2, SEEK_CUR);
+    EXPECT_EQ(result, 0);
+    EXPECT_EQ(buf.pos, 5);
+}
+
+TEST_F(BufferSeek, CurValidNegative) {
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, -3, SEEK_CUR);
+    EXPECT_EQ(result, 0);
+    EXPECT_EQ(buf.pos, 0);
+}
+
+TEST_F(BufferSeek, CurInvalidPositive) {
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, 3, SEEK_CUR);
+    EXPECT_NE(result, 0);
+    EXPECT_EQ(buf.pos, 3);
+}
+
+TEST_F(BufferSeek, CurInvalidNegative) {
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, -4, SEEK_CUR);
+    EXPECT_NE(result, 0);
+    EXPECT_EQ(buf.pos, 3);
+}
+
+TEST_F(BufferSeek, EndZero) {
+    ASSERT_EQ(buf.size, 6);
+    int result = cxBufferSeek(&buf, 0, SEEK_END);
+    // the (past-the-)end position is always invalid
+    EXPECT_NE(result, 0);
+    EXPECT_EQ(buf.pos, 3);
+}
+
+TEST_F(BufferSeek, EndValid) {
+    ASSERT_EQ(buf.size, 6);
+    int result = cxBufferSeek(&buf, -6, SEEK_END);
+    EXPECT_EQ(result, 0);
+    EXPECT_EQ(buf.pos, 0);
+}
+
+TEST_F(BufferSeek, EndInvalid) {
+    ASSERT_EQ(buf.size, 6);
+    int result = cxBufferSeek(&buf, 1, SEEK_END);
+    EXPECT_NE(result, 0);
+    EXPECT_EQ(buf.pos, 3);
+}
+
+TEST_F(BufferSeek, WhenceInvalid) {
+    ASSERT_EQ(buf.size, 6);
+    ASSERT_EQ(buf.pos, 3);
+    int result = cxBufferSeek(&buf, 2, 9000);
+    EXPECT_NE(result, 0);
+    EXPECT_EQ(buf.size, 6);
+    EXPECT_EQ(buf.pos, 3);
+}
\ No newline at end of file

mercurial