/*
 * 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: mpaframereader_test.cc 3211 2006-04-14 21:01:37Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "mpaframereader.h"
#include <sstream>

using namespace std;

// MPEG 1, Layer III, 32000 Hz, 48 kBit, mono, no padding
// 1111 1111 1111 1010 0011 1000 1100 0100
#define MPEG1_HEADER     "\xFF\xFA\x38\xC4"
#define MPEG1_FRAME_SIZE 216

// MPEG 2, Layer III, 22050 Hz, 32 kBit, mono, no padding
// 1111 1111 1111 0011 0100 0000 1100 0100
#define MPEG2_HEADER     "\xFF\xF3\x40\xC4"
#define MPEG2_FRAME_SIZE 104

class MpaFrameReaderTest: public CxxTest::TestSuite
{
    private:
        void WriteFrame(ostream& stream, char header[MPA_HEADER_SIZE],
          int frameSize)
        {
            stream.write(header, MPA_HEADER_SIZE);
            for (int i=0; i < frameSize - MPA_HEADER_SIZE; i++)
            {
                stream << "x";
            }
        }

    public:
        void TestEmptyStream()
        {
            MpaFrameReader frameReader;
            stringstream stream;
            TS_ASSERT(!frameReader.SeekNextFrame(stream).get());
        }

        void TestFailIfNoConsecutiveFrame()
        {
            MpaFrameReader frameReader;
            stringstream stream;
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            stream << "the next frame does not directly follow the previous one";
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            TS_ASSERT(!frameReader.SeekNextFrame(stream).get());
        }

        void TestTwoFramesStartingAtTheBeginning()
        {
            stringstream stream;
            MpaFrameReader frameReader;
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            TS_ASSERT(frameReader.SeekNextFrame(stream).get());
            TS_ASSERT_EQUALS(0 + MPA_HEADER_SIZE, stream.tellg());
        }

        void TestTwoFramesStartingAfterSomeNoneMpegData()
        {
            stringstream stream;
            MpaFrameReader frameReader;
            stream << "25 bytes of non-MP3-stuff";
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            TS_ASSERT(frameReader.SeekNextFrame(stream).get());
            TS_ASSERT_EQUALS(25 + MPA_HEADER_SIZE, stream.tellg());
        }

        void TestTwoFramesAfterAFrameWithoutSuccessor()
        {
            stringstream stream;
            MpaFrameReader frameReader;
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            stream << "25 bytes of non-MP3-stuff";
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            WriteFrame(stream, MPEG1_HEADER, MPEG1_FRAME_SIZE);
            TS_ASSERT(frameReader.SeekNextFrame(stream).get());
            TS_ASSERT_EQUALS(MPEG1_FRAME_SIZE + 25 + MPA_HEADER_SIZE,
              stream.tellg());
        }

        void TestMpeg2()
        {
            stringstream stream;
            MpaFrameReader frameReader;
            WriteFrame(stream, MPEG2_HEADER, MPEG2_FRAME_SIZE);
            WriteFrame(stream, MPEG2_HEADER, MPEG2_FRAME_SIZE);
            TS_ASSERT(frameReader.SeekNextFrame(stream).get());
            TS_ASSERT_EQUALS(0 + MPA_HEADER_SIZE, stream.tellg());
        }
};
