/*
 * vdr-podcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2006 Tobias Grimm <vdr@e-tobi.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: circularpespacketbuffer_test.cc 3253 2006-04-23 13:35:28Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "circularpespacketbuffer.h"
#include "pesaudiopacket.h"
#include "stringmessagemock.h"

using namespace std;

class PesAudioPacketMock: public StringMessageMock, public PesAudioPacket
{
    public:
        PesAudioPacketMock(StringMessageMock& parent, string messagePrefix)
        :StringMessageMock(parent, messagePrefix), PesAudioPacket(20)
        {
        }

        ~PesAudioPacketMock()
        {
            AddMethod("dtor");
        }
};

class CircularPesPacketBufferTest: public CxxTest::TestSuite
{
    public:
        void TestEmptyBuffer()
        {
            CircularPesPacketBuffer buffer(10);

            TS_ASSERT(buffer.IsEmpty());
            TS_ASSERT(NULL == buffer.GetPesPacket().get());
        }

        void TestNotEmptyBuffer()
        {
            CircularPesPacketBuffer buffer(10);
            ref_ptr<PesAudioPacket> packet(new PesAudioPacket(20));

            buffer.PutPesPacket(packet);

            TS_ASSERT(!buffer.IsEmpty());
            TS_ASSERT(packet.get() == buffer.GetPesPacket().get());
        }

        void TestFifo()
        {
            CircularPesPacketBuffer buffer(10);
            ref_ptr<PesAudioPacket> packet1(new PesAudioPacket(20));
            ref_ptr<PesAudioPacket> packet2(new PesAudioPacket(20));

            buffer.PutPesPacket(packet1);
            buffer.PutPesPacket(packet2);

            TS_ASSERT_EQUALS(packet1.get(), buffer.GetPesPacket().get());
            TS_ASSERT_EQUALS(packet2.get(), buffer.GetPesPacket().get());
        }

        void TestDestroyContainingPackets()
        {
            CircularPesPacketBuffer* buffer = new CircularPesPacketBuffer(10);
            StringMessageMock mock;

            buffer->PutPesPacket(ref_ptr<PesAudioPacket>(
              new PesAudioPacketMock(mock, "packet1")));
            buffer->PutPesPacket(ref_ptr<PesAudioPacket>(
              new PesAudioPacketMock(mock, "packet2")));

            mock.ExpectMethod("packet2:dtor");
            mock.ExpectMethod("packet1:dtor");

            delete buffer;

            mock.Verify();
        }

        void TestRoundTrip()
        {
            CircularPesPacketBuffer buffer(3);

            ref_ptr<PesAudioPacket> packet1(new PesAudioPacket(20));
            ref_ptr<PesAudioPacket> packet2(new PesAudioPacket(20));
            ref_ptr<PesAudioPacket> packet3(new PesAudioPacket(20));
            ref_ptr<PesAudioPacket> packet4(new PesAudioPacket(20));

            TS_ASSERT(buffer.PutPesPacket(packet1));
            TS_ASSERT(buffer.PutPesPacket(packet2));
            TS_ASSERT(buffer.PutPesPacket(packet3));
            TS_ASSERT(!buffer.PutPesPacket(packet4));

            TS_ASSERT_EQUALS(packet1.get(), buffer.GetPesPacket().get());
            TS_ASSERT(buffer.PutPesPacket(packet4));

            TS_ASSERT_EQUALS(packet2.get(), buffer.GetPesPacket().get());
            TS_ASSERT_EQUALS(packet3.get(), buffer.GetPesPacket().get());
            TS_ASSERT_EQUALS(packet4.get(), buffer.GetPesPacket().get());
            TS_ASSERT(NULL == buffer.GetPesPacket().get());
        }

        void TestDefaultEofStatus()
        {
            CircularPesPacketBuffer buffer(3);
            TS_ASSERT(!buffer.Eof());
        }

        void TestSetEofStatus()
        {
            CircularPesPacketBuffer buffer(3);

            buffer.SetEof(true);
            TS_ASSERT(buffer.Eof());

            buffer.SetEof(false);
            TS_ASSERT(!buffer.Eof());
        }
};
