//- Copyright (c) 2008-2009 James Grenning --- All rights reserved
//- For exclusive use by participants in Wingman Software training courses.
//- Cannot be used by attendees to train others without written permission.
//- www.wingman-sw.com james@wingman-sw.com

#include "CppUTest/TestHarness.h"

extern "C"
{
#include "CircularBuffer.h"
#include "FormatOutputSpy.h"
}

TEST_GROUP(CircularBufferPrint)
{
    CircularBuffer * buffer;
    const char * expectedOutput;
    const int default_value = -1;

    void setup()
    {
        UT_PTR_SET(FormatOutput, FormatOutputSpy);
        FormatOutputSpy_Create(80*20);

        buffer = CircularBuffer_Create(10, default_value);
        expectedOutput = "";
    }

    void teardown()
    {
         CircularBuffer_Destroy(buffer);
         FormatOutputSpy_Destroy();
    }

    void fillTheQueue(int seed, int howMany)
    {
        for (int i = 0; i < howMany; i++)
            CircularBuffer_Put(buffer, i+seed);
    }
};

TEST(CircularBufferPrint, PrintEmpty)
{
    expectedOutput = "Circular buffer content:\n<>\n";

    CircularBuffer_Print(buffer);

    STRCMP_EQUAL(expectedOutput, FormatOutputSpy_GetOutput());
}

TEST(CircularBufferPrint, PrintAfterOneIsPut)
{
    expectedOutput = "Circular buffer content:\n<1>\n";

    CircularBuffer_Put(buffer, 1);
    CircularBuffer_Print(buffer);

    STRCMP_EQUAL(expectedOutput, FormatOutputSpy_GetOutput());
}

TEST(CircularBufferPrint, PrintNotYetWrappedOrFull)
{
    expectedOutput = "Circular buffer content:\n<1, 2, 3>\n";

    CircularBuffer_Put(buffer, 1);
    CircularBuffer_Put(buffer, 2);
    CircularBuffer_Put(buffer, 3);
    CircularBuffer_Print(buffer);

    STRCMP_EQUAL(expectedOutput, FormatOutputSpy_GetOutput());
}


TEST(CircularBufferPrint, PrintNotYetWrappedAndIsFull)
{
    const char* expectedOutput =
        "Circular buffer content:\n"
        "<200, 201, 202, 203, 204>\n";

    CircularBuffer * b = CircularBuffer_Create(5, default_value);
    CircularBuffer_Put(b, 200);
    CircularBuffer_Put(b, 201);
    CircularBuffer_Put(b, 202);
    CircularBuffer_Put(b, 203);
    CircularBuffer_Put(b, 204);

    CircularBuffer_Print(b);


    STRCMP_EQUAL(expectedOutput, FormatOutputSpy_GetOutput());
    CircularBuffer_Destroy(b);
}

TEST(CircularBufferPrint, PrintWrappedAndIsFullOldestToNewest)
{
    const char* expectedOutput =
        "Circular buffer content:\n"
        "<201, 202, 203, 204, 999>\n";

    CircularBuffer * b = CircularBuffer_Create(5, default_value);
    CircularBuffer_Put(b, 200);
    CircularBuffer_Put(b, 201);
    CircularBuffer_Put(b, 202);
    CircularBuffer_Put(b, 203);
    CircularBuffer_Put(b, 204);
    CircularBuffer_Get(b);
    CircularBuffer_Put(b, 999);

    CircularBuffer_Print(b);

    STRCMP_EQUAL(expectedOutput, FormatOutputSpy_GetOutput());
    CircularBuffer_Destroy(b);
}


