#include <gtest/gtest.h>
#include <zoost/zoost.h>

TEST(MutBufferTest, ConstructorAndGetSize)
{
    zoostMutBuffer buffer;
    EXPECT_EQ(buffer.GetSize(), 0);
}

TEST(MutBufferTest, PushAndGetSize)
{
    zoostMutBuffer buffer;
    const char data[] = "Hello";
    buffer.Push(data, 5);
    EXPECT_EQ(buffer.GetSize(), 5);
}

TEST(MutBufferTest, PushMultipleTimes)
{
    zoostMutBuffer buffer;
    const char data1[] = "Hello";
    const char data2[] = "World";

    buffer.Push(data1, 5);
    buffer.Push(data2, 5);

    EXPECT_EQ(buffer.GetSize(), 10);
}

TEST(MutBufferTest, GetBufferContent)
{
    zoostMutBuffer buffer;
    const char data[] = "Hello";
    buffer.Push(data, 5);

    const char* buffer_data = buffer.Data();
    EXPECT_EQ(std::memcmp(buffer_data, data, 5), 0);
}

TEST(MutBufferTest, IndexOf_Found)
{
    zoostMutBuffer buffer;
    const char data[] = "HelloWorld";
    buffer.Push(data, 10);

    const char search[] = "World";
    int index = buffer.IndexOf(search, 5, 0);
    EXPECT_EQ(index, 5);

    // 从指定位置开始搜索
    index = buffer.IndexOf(search, 5, 5);
    EXPECT_EQ(index, 5);
}

TEST(MutBufferTest, IndexOf_NotFound)
{
    zoostMutBuffer buffer;
    const char data[] = "Hello";
    buffer.Push(data, 5);

    const char search[] = "World";
    int index = buffer.IndexOf(search, 5, 0);
    EXPECT_EQ(index, -1);
}

TEST(MutBufferTest, IndexOf_InvalidParameters)
{
    zoostMutBuffer buffer;
    const char data[] = "Hello";
    buffer.Push(data, 5);

    const char search[] = "World";

    // start 为负
    int index = buffer.IndexOf(search, 5, -1);
    EXPECT_EQ(index, -1);

    // start 超过边界
    index = buffer.IndexOf(search, 5, 10);
    EXPECT_EQ(index, -1);

    // size 为0或负
    index = buffer.IndexOf(search, 0, 0);
    EXPECT_EQ(index, -1);

    index = buffer.IndexOf(search, -1, 0);
    EXPECT_EQ(index, -1);
}

TEST(MutBufferTest, IndexOf_EmptyBuffer)
{
    zoostMutBuffer buffer;
    const char search[] = "Hello";
    int index = buffer.IndexOf(search, 5, 0);
    EXPECT_EQ(index, -1);
}

TEST(MutBufferTest, RemoveOf_NormalCase)
{
    zoostMutBuffer buffer;
    const char data[] = "HelloWorld";
    buffer.Push(data, 10);

    // 移除 "World"
    buffer.RemoveOf(5, 5);
    EXPECT_EQ(buffer.GetSize(), 5);

    const char* buffer_data = buffer.Data();
    EXPECT_EQ(std::memcmp(buffer_data, "Hello", 5), 0);
}

TEST(MutBufferTest, RemoveOf_PartialAtEnd)
{
    zoostMutBuffer buffer;
    const char data[] = "HelloWorld";
    buffer.Push(data, 10);

    // 尝试移除超过末尾的部分
    buffer.RemoveOf(7, 5);
    EXPECT_EQ(buffer.GetSize(), 7);

    const char* buffer_data = buffer.Data();
    EXPECT_EQ(std::memcmp(buffer_data, "HelloWo", 7), 0);
}

TEST(MutBufferTest, RemoveOf_InvalidParameters)
{
    zoostMutBuffer buffer;
    const char data[] = "Hello";
    buffer.Push(data, 5);

    // start 为负 - 应该不修改
    buffer.RemoveOf(-1, 2);
    EXPECT_EQ(buffer.GetSize(), 5);

    // start 超过边界 - 应该不修改
    buffer.RemoveOf(10, 2);
    EXPECT_EQ(buffer.GetSize(), 5);

    // size 为0或负 - 应该不修改
    buffer.RemoveOf(0, 0);
    EXPECT_EQ(buffer.GetSize(), 5);

    buffer.RemoveOf(0, -1);
    EXPECT_EQ(buffer.GetSize(), 5);
}

TEST(MutBufferTest, RemoveOf_RemoveAll)
{
    zoostMutBuffer buffer;
    const char data[] = "HelloWorld";
    buffer.Push(data, 10);

    buffer.RemoveOf(0, 10);
    EXPECT_EQ(buffer.GetSize(), 0);
}

TEST(MutBufferTest, Clear)
{
    zoostMutBuffer buffer;
    const char data[] = "HelloWorld";
    buffer.Push(data, 10);

    EXPECT_EQ(buffer.GetSize(), 10);
    buffer.Clear();
    EXPECT_EQ(buffer.GetSize(), 0);
}

TEST(MutBufferTest, Clear_EmptyBuffer)
{
    zoostMutBuffer buffer;
    buffer.Clear();
    EXPECT_EQ(buffer.GetSize(), 0);
}

TEST(MutBufferTest, ThreadSafety_PushAndGet)
{
    zoostMutBuffer buffer;

    // 模拟多线程同时操作
    std::thread t1([&buffer]() {
        for (int i = 0; i < 1000; ++i) {
            const char data[] = "A";
            buffer.Push(data, 1);
        }
    });

    std::thread t2([&buffer]() {
        for (int i = 0; i < 1000; ++i) {
            const char data[] = "B";
            buffer.Push(data, 1);
        }
    });

    t1.join();
    t2.join();

    // 最终大小应该是2000
    EXPECT_EQ(buffer.GetSize(), 2000);
}

TEST(MutBufferTest, CombinedOperations)
{
    zoostMutBuffer buffer;

    // 1. 添加数据
    const char data1[] = "Hello ";
    const char data2[] = "World ";
    const char data3[] = "Test";

    buffer.Push(data1, 6);
    buffer.Push(data2, 6);
    buffer.Push(data3, 4);

    EXPECT_EQ(buffer.GetSize(), 16);

    // 2. 搜索
    const char search[] = "World";
    int index = buffer.IndexOf(search, 5, 0);
    EXPECT_EQ(index, 6);

    // 3. 移除
    buffer.RemoveOf(index, 5);
    EXPECT_EQ(buffer.GetSize(), 11);

    // 4. 验证剩余内容
    const char* buffer_data = buffer.Data();
    EXPECT_EQ(std::memcmp(buffer_data, "Hello  Test", 11), 0);

    // 5. 清除
    buffer.Clear();
    EXPECT_EQ(buffer.GetSize(), 0);
}

TEST(MutBufferTest, BinaryData)
{
    zoostMutBuffer buffer;

    // 测试二进制数据（包含空字符）
    const unsigned char binary_data[] = {0x00, 0x01, 0x02, 0x03, 0x00, 0xFF};
    buffer.Push(reinterpret_cast<const char*>(binary_data), 6);

    EXPECT_EQ(buffer.GetSize(), 6);

    // 搜索包含空字符的模式
    const unsigned char pattern[] = {0x02, 0x03, 0x00};
    int index = buffer.IndexOf(reinterpret_cast<const char*>(pattern), 3, 0);
    EXPECT_EQ(index, 2);
}

int main(int argc, char** argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}