/** \file
 *
 *  Contains unit tests for the MappedIOStream class
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef _MAPPEDIOSTREAM_TEST_H_
#define _MAPPEDIOSTREAM_TEST_H_

#include "MappedIOStream.h"

#include <sys/stat.h>

#include <cxxtest/TestSuite.h>

/**
 *  Unit tests for MappedIOStream class
 *
 *  \ingroup io
 */
class MappedIOStream_Test : public CxxTest::TestSuite
{
public:

    void test1Write( void )
    {
        MappedIOStream stream( "MappedIOStream_Test.log", true, 8 );
        stream << "Hello";
        TS_ASSERT_EQUALS( stream.bytesWritten(), 5U );
        stream.write( " Worldly", 6 );
        TS_ASSERT_EQUALS( stream.bytesWritten(), 6U );

        const struct stat statBuf( stream.getStat() );
        TS_ASSERT_EQUALS( statBuf.st_size, 16 );
    }

    void test2Read( void )
    {
        MappedIOStream stream( "MappedIOStream_Test.log", false, 8 );
        Str str;
        char buffer[16];
        stream.read( buffer, 16 );
        TS_ASSERT_EQUALS( stream.bytesRead(), 16U );
        TS_ASSERT_SAME_DATA( buffer, "Hello World", 12 );

        stream.seek( 4 );
        char buf[4];
        buf[3] = 0;
        stream.read( buf, 3 );
        TS_ASSERT_EQUALS( stream.bytesRead(), 3U );
        TS_ASSERT_SAME_DATA( buf, "o W", 4 );

        remove( "MappedIOStream_Test.log" );
    }

};

#endif // _MAPPEDIOSTREAM_TEST_H
