Пример #1
0
bool 
DataBuffer::Test( )
{
    bool ok = true;
    cout << "Testing DataBuffer" << endl;

    DataBuffer buff;
    TESTCHECK( buff.Data() == 0, true, &ok );
    TESTCHECK( buff.Size(), 0, &ok );
    char sIn[ 10 ] = "123456789";
    Foo fIn = { 127, 2.5f };
    cout << "Add( char *, 10 )" << endl;
    buff.Add( sIn, 10 );
    TESTCHECK( *buff.Data(), '1', &ok );
    TESTCHECK( buff.Size(), 10, &ok );
    cout << "Add( Foo )" << endl;
    buff.Add( fIn );
    cout << "Read( 10 )" << endl;
    const char * sOut = buff.Read( 10 );
    TESTCHECK( (string( sIn ) == string( sOut )), true, &ok );
    cout << "Read< Foo >()" << endl;
    const Foo * pFoo = buff.Read< Foo >( );
    TESTCHECK( pFoo->i, 127, &ok );
    TESTCHECK( pFoo->f, 2.5f, &ok );
    
    if ( ok )
        cout << "DataBuffer PASSED." << endl << endl;
    else
        cout << "DataBuffer FAILED." << endl << endl;
    return ok;
}
Пример #2
0
bool
File::Test( )
{
    bool ok = true;
    cout << "Testing File" << endl;

    try
    {
        const string testDataFileName = "FileTest.dat";
        char testData[ 266 ]
                = { 0, '\r', '\n', '\r', '\n', '\n', '\r', '\r', 0, '\n' };
        for ( int i = 0; i < 256; ++i )
            testData[ i + 10 ] = static_cast< char >( i );
        DataBuffer buff;
        buff.Add( testData, 266 );
        {
            cout << "FileWriter( string ) constructor" << endl;
            FileWriter writer( testDataFileName );
            cout << "writer.Save( DataBuffer )" << endl;
            writer.Save( buff );
        }
        TESTCHECK( FileExists( testDataFileName ), true, &ok );
        TESTCHECK( FileSize( testDataFileName ),
                   (int) (ARRAY_LENGTH( testData )), &ok );
        DateTime now( true );
        DateTime then = now;
        then.Increment( 0, 0, 0, 0, -1 );   //1 minute ago
        TESTCHECK( (now < FileModDate( testDataFileName )), false, &ok );
        TESTCHECK( (then < FileModDate( testDataFileName )), true, &ok );
        buff.Clear( );
        {
            cout << "FileReader( string ) constructor" << endl;
            FileReader reader( testDataFileName );
            cout << "writer.Load( DataBuffer * )" << endl;
            reader.Load( &buff );
        }
        const vector< char > & testData1 = buff.Buffer();
        TESTCHECK( testData1.size(), ARRAY_LENGTH( testData ), &ok );
        TESTCHECK( memcmp( &testData1[0], testData, ARRAY_LENGTH( testData ) ),
                   0, &ok );
        cout << "DeleteFile( string )" << endl;
        DeleteFile( testDataFileName );
        TESTCHECK( FileExists( testDataFileName ), false, &ok );

        const string testTextFileName = "FileTest.txt";
        const string testText = "Four score and seven years ago\n"
                "our fathers set forth upon this continent\n\r"
                "a new nation,\r\n"
                "conceived in liberty\r"
                "and dedicated to the proposition\x0"
                "that all men are created equal.";
        {
            cout << "FileWriter( string, File::Text ) constructor" << endl;
            FileWriter writer( testTextFileName, File::Text );
            cout << "writer.Save( string )" << endl;
            writer.Save( testText );
        }
        TESTCHECK( FileExists( testTextFileName ), true, &ok );
        string testText1;
        {
            cout << "FileReader( string, File::Text ) constructor" << endl;
            FileReader reader( testTextFileName, File::Text );
            cout << "reader.Load( string * )" << endl;
            reader.Load( &testText1 );
        }
        TESTCHECK( (testText1 == testText), true, &ok );
        cout << "DeleteFile( string )" << endl;
        DeleteFile( testTextFileName );
        TESTCHECK( FileExists( testTextFileName ), false, &ok );
    }
    catch ( FileException & except )
    {
        cout << except.Description() << endl;
        ok = false;
    }
    
    if ( ok )
        cout << "File PASSED." << endl << endl;
    else
        cout << "File FAILED." << endl << endl;
    return ok;
}