//- Copyright (c) 2008-2020 James Grenning
//- All rights reserved
//- For use by participants in Wingman Software training courses.

#include "CircularBuffer.h"
#include "CppUTest/TestHarness.h"

using namespace std;

TEST_GROUP(CircularBufferException)
{
    CircularBuffer* buffer;

    void setup()
    {
        buffer = new CircularBuffer(10);
    }

    void teardown()
    {
        delete buffer;
    }

    void fillTheBuffer(int seed, int count)
    {
        for (int i = 0; i < count; i++)
            buffer->put(seed++);
    }

    void drainAndCheckBuffer(int seed, int capacity)
    {
        for (int j = 0; j < capacity; j++)
        {
            LONGS_EQUAL(j + seed, buffer->get());
            CHECK(!buffer->isFull());
        }
    }

};

TEST(CircularBufferException, holds_a_message)
{
    CircularBufferException e("the message");
    string expected = "the message";
    CHECK_EQUAL(expected, e.message());
}

TEST(CircularBufferException, put_to_full_throws)
{
    fillTheBuffer(900, buffer->capacity());
    try
    {
        buffer->put(9999);
        FAIL("Put to full circularBuffer should throw");
    }
    catch (CircularBufferException& e)
    {
        string expected = "Put to full circular buffer";
        CHECK_EQUAL(expected, e.message());
    }
}

TEST(CircularBufferException, put_to_full_does_no_harm)
{
    fillTheBuffer(900, buffer->capacity());

    try
    {
        buffer->put(9999);
    }
    catch (CircularBufferException&)
    {
    }

    drainAndCheckBuffer(900, buffer->capacity());

    CHECK_TRUE(buffer->isEmpty());
}

TEST(CircularBufferException, get_from_empty_throws)
{
    try
    {
        buffer->get();
        FAIL("Get from empty should throw");
    }
    catch (CircularBufferException& e)
    {
        string expected = "Get from empty circular buffer";
        CHECK_EQUAL(expected, e.message());
        CHECK_TRUE(buffer->isEmpty());
    }
}

