<commit_before>#include <Utilities/Memory/CircleBuffer/ArbitrarySizeCirclebufferTest.hpp>
#include <Utility/Utilities/Include/IO/FileMap/FileMap.hpp>
#include <memory>

TEST_F(ArbitrarySizeCirclebufferTest, basicInit)
{
    m_circleBuffer->Initialize(104);
    SUCCEED();
}

/*
TEST_F(ArbitrarySizeCirclebufferTest, emptyConsume)
{
    m_circleBuffer->Initialize(104);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();
    const bool res = m_circleBuffer->Consume(returnHeader, returnData);

    ASSERT_FALSE(res);
    delete returnHeader;
    delete returnData;
}

TEST_F(ArbitrarySizeCirclebufferTest, toManyProduce)
{
    m_circleBuffer->Initialize(104);

    CircleBufferHeader sendHeader = CircleBufferHeader();
    TestStruct64 sendData = TestStruct64();
    bool res = m_circleBuffer->Produce(sendHeader, &sendData);
    ASSERT_TRUE(res);
    res = m_circleBuffer->Produce(sendHeader, &sendData);
    ASSERT_FALSE(res);
}

TEST_F(ArbitrarySizeCirclebufferTest, basicProduceAndConsume)
{
    m_circleBuffer->Initialize(300);

    CircleBufferHeader sendHeader = CircleBufferHeader();
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData = TestStruct64();
    sendData.f1 = 4;
    m_circleBuffer->Produce(sendHeader, &sendData);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();
    m_circleBuffer->Consume(returnHeader, returnData);

    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::TEXT).typeValue, CircleBufferType(returnHeader->packageType).typeValue);
    delete returnHeader;
    delete returnData;
}

TEST_F(ArbitrarySizeCirclebufferTest, twoProduceAndConsume)
{
    m_circleBuffer->Initialize(300);

    CircleBufferHeader sendHeader = CircleBufferHeader();
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);

    TestStruct64 sendData = TestStruct64();
    sendData.f1 = 4;

    // Produce
    m_circleBuffer->Produce(sendHeader, &sendData);
    sendData.f2 = 19;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::COMMAND);
    m_circleBuffer->Produce(sendHeader, &sendData);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();

    m_circleBuffer->Consume(returnHeader, returnData);
    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::TEXT).typeValue, CircleBufferType(returnHeader->packageType).typeValue);

    m_circleBuffer->Consume(returnHeader, returnData);
    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(19, returnData->f2);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::COMMAND).typeValue, CircleBufferType(returnHeader->packageType).typeValue);
    delete returnHeader;
    delete returnData;
}


TEST_F(ArbitrarySizeCirclebufferTest, twoBuffersUsingDifferentPartOfSameExternalMemory)
{
    using namespace Doremi::Utilities;
    void* memory = malloc(400);
    m_circleBuffer->Initialize(memory, 200);

    CircleBuffer<TestStruct64>* otherBuffer = new CircleBuffer<TestStruct64>();
    otherBuffer->Initialize(PointerArithmetic::Addition(memory, 200), 200);

    // Build package
    CircleBufferHeader sendHeader = CircleBufferHeader();
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData = TestStruct64();
    bool res;

    //// Produce
    // Produce first buffer
    sendData.f1 = 4;
    res = m_circleBuffer->Produce(sendHeader, &sendData);
    ASSERT_TRUE(res);

    sendData.f1 = 19;
    res = m_circleBuffer->Produce(sendHeader, &sendData);
    ASSERT_TRUE(res);

    sendData.f1 = 78;
    res = m_circleBuffer->Produce(sendHeader, &sendData);
    ASSERT_FALSE(res);

    // Produce second buffer
    sendData.f1 = 62;
    res = otherBuffer->Produce(sendHeader, &sendData);
    ASSERT_TRUE(res);

    sendData.f1 = 775;
    res = otherBuffer->Produce(sendHeader, &sendData);
    ASSERT_TRUE(res);

    sendData.f1 = 95;
    res = otherBuffer->Produce(sendHeader, &sendData);
    ASSERT_FALSE(res);


    //// Consume
    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();

    // Consume first buffer
    res = m_circleBuffer->Consume(returnHeader, returnData);
    ASSERT_TRUE(res);
    ASSERT_EQ(4, returnData->f1);

    res = m_circleBuffer->Consume(returnHeader, returnData);
    ASSERT_TRUE(res);
    ASSERT_EQ(19, returnData->f1);

    res = m_circleBuffer->Consume(returnHeader, returnData);
    ASSERT_FALSE(res);

    // Consume second buffer
    res = otherBuffer->Consume(returnHeader, returnData);
    ASSERT_TRUE(res);
    ASSERT_EQ(62, returnData->f1);

    res = otherBuffer->Consume(returnHeader, returnData);
    ASSERT_TRUE(res);
    ASSERT_EQ(775, returnData->f1);

    res = otherBuffer->Consume(returnHeader, returnData);
    ASSERT_FALSE(res);

    free(memory);
    delete otherBuffer;
    delete returnHeader;
    delete returnData;
}


TEST_F(ArbitrarySizeCirclebufferTest, twoBuffersPointingOnSameMemory)
{
    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();

    using namespace Doremi::Utilities;
    void* memory = malloc(200);
    m_circleBuffer->Initialize(memory, 200);

    CircleBuffer<TestStruct64>* otherBuffer = new CircleBuffer<TestStruct64>();
    otherBuffer->Initialize(memory, 200);

    // Build package<commit_msg>Unit tests for new circlebuffer<commit_after>#include <Utilities/Memory/CircleBuffer/ArbitrarySizeCirclebufferTest.hpp>
#include <Utility/Utilities/Include/IO/FileMap/FileMap.hpp>
#include <memory>

TEST_F(ArbitrarySizeCirclebufferTest, basicInit)
{
    m_circleBuffer->Initialize(104);
    SUCCEED();
}

TEST_F(ArbitrarySizeCirclebufferTest, singleProduce)
{
    m_circleBuffer->Initialize(104);

    CircleBufferHeader sendHeader;
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData = TestStruct64();

    m_circleBuffer->Produce(sendHeader, &sendData);
    SUCCEED();
}

TEST_F(ArbitrarySizeCirclebufferTest, toManyProduce)
{
    m_circleBuffer->Initialize(104);

    CircleBufferHeader sendHeader = CircleBufferHeader();
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData = TestStruct64();

    m_circleBuffer->Produce(sendHeader, &sendData);

    EXPECT_THROW(m_circleBuffer->Produce(sendHeader, &sendData), std::runtime_error);
}


TEST_F(ArbitrarySizeCirclebufferTest, emptyConsume)
{
    m_circleBuffer->Initialize(104);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();
    const bool res = m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));

    ASSERT_FALSE(res);
    delete returnHeader;
    delete returnData;
}

TEST_F(ArbitrarySizeCirclebufferTest, basicProduceAndConsume)
{
    m_circleBuffer->Initialize(300);

    CircleBufferHeader sendHeader;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData;
    sendData.f1 = 4;
    m_circleBuffer->Produce(sendHeader, &sendData);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();
    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));

    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::TEXT).typeValue, CircleBufferType(returnHeader->packageType).typeValue);
    delete returnHeader;
    delete returnData;
}

TEST_F(ArbitrarySizeCirclebufferTest, produceHalfInEndHalfInFront)
{
    m_circleBuffer->Initialize(151);

    CircleBufferHeader sendHeader;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData;
    sendData.f1 = 4;
    m_circleBuffer->Produce(sendHeader, &sendData);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();
    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));


    m_circleBuffer->Produce(sendHeader, &sendData);
    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::TEXT).typeValue, CircleBufferType(returnHeader->packageType).typeValue);

    delete returnHeader;
    delete returnData;
}

TEST_F(ArbitrarySizeCirclebufferTest, produceAllInFront)
{
    // (64+8)*1 + 32 = 104
    // 64 data, 8 metaheader, 32 buffer statatic data
    m_circleBuffer->Initialize(104);

    CircleBufferHeader sendHeader;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData;
    sendData.f1 = 4;
    m_circleBuffer->Produce(sendHeader, &sendData);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();
    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));


    m_circleBuffer->Produce(sendHeader, &sendData);
    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::TEXT).typeValue, CircleBufferType(returnHeader->packageType).typeValue);

    delete returnHeader;
    delete returnData;
}


TEST_F(ArbitrarySizeCirclebufferTest, twoProduceAndConsume)
{
    m_circleBuffer->Initialize(300);

    CircleBufferHeader sendHeader;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);

    TestStruct64 sendData;
    sendData.f1 = 4;

    // Produce
    m_circleBuffer->Produce(sendHeader, &sendData);
    sendData.f2 = 19;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::COMMAND);
    m_circleBuffer->Produce(sendHeader, &sendData);

    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();

    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::TEXT).typeValue, CircleBufferType(returnHeader->packageType).typeValue);

    m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_EQ(4, returnData->f1);
    ASSERT_EQ(19, returnData->f2);
    ASSERT_EQ(64, returnHeader->packageSize);
    ASSERT_EQ(CircleBufferType(CircleBufferTypeEnum::COMMAND).typeValue, CircleBufferType(returnHeader->packageType).typeValue);
    delete returnHeader;
    delete returnData;
}


TEST_F(ArbitrarySizeCirclebufferTest, twoBuffersUsingDifferentPartOfSameExternalMemory)
{
    using namespace Doremi::Utilities;
    void* memory = malloc(400);
    m_circleBuffer->Initialize(memory, 200);

    ArbitrarySizeCirclebuffer* otherBuffer = new ArbitrarySizeCirclebuffer();
    otherBuffer->Initialize(PointerArithmetic::Addition(memory, 200), 200);

    // Build package
    CircleBufferHeader sendHeader;
    sendHeader.packageType = CircleBufferType(CircleBufferTypeEnum::TEXT);
    sendHeader.packageSize = sizeof(TestStruct64);
    TestStruct64 sendData;

    //// Produce
    // Produce first buffer
    sendData.f1 = 4;
    m_circleBuffer->Produce(sendHeader, &sendData);

    sendData.f1 = 19;
    m_circleBuffer->Produce(sendHeader, &sendData);

    sendData.f1 = 78;

    ASSERT_THROW(m_circleBuffer->Produce(sendHeader, &sendData), std::runtime_error);

    // Produce second buffer
    sendData.f1 = 62;
    otherBuffer->Produce(sendHeader, &sendData);

    sendData.f1 = 775;
    otherBuffer->Produce(sendHeader, &sendData);

    sendData.f1 = 95;
    ASSERT_THROW(otherBuffer->Produce(sendHeader, &sendData), std::runtime_error);

    bool res;
    //// Consume
    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();

    // Consume first buffer
    res = m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_TRUE(res);
    ASSERT_EQ(4, returnData->f1);

    res = m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_TRUE(res);
    ASSERT_EQ(19, returnData->f1);

    res = m_circleBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_FALSE(res);

    // Consume second buffer
    res = otherBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_TRUE(res);
    ASSERT_EQ(62, returnData->f1);

    res = otherBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_TRUE(res);
    ASSERT_EQ(775, returnData->f1);

    res = otherBuffer->Consume(returnHeader, returnData, sizeof(TestStruct64));
    ASSERT_FALSE(res);

    free(memory);
    delete otherBuffer;
    delete returnHeader;
    delete returnData;
}

/*
TEST_F(ArbitrarySizeCirclebufferTest, twoBuffersPointingOnSameMemory)
{
    CircleBufferHeader* returnHeader = new CircleBufferHeader();
    TestStruct64* returnData = new TestStruct64();

    using namespace Doremi::Utilities;
    void* memory = malloc(200);
    m_circleBuffer->Initialize(memory, 200);

    CircleBuffer<TestStruct64>* otherBuffer = new CircleBuffer<TestStruct64>();
    otherBuffer->Initialize(memory, 200);

    // Build package
{'fraction_non_alphanumeric': 0.10842942061161641, 'fraction_numerical': 0.025281100050132493, 'mean_word_length': 5.1191936897458366, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6349117', 'n_tokens_mistral': 4477, 'n_tokens_neox': 4276, 'n_words': 689}