//
// $Id: LayeredStreamTest.m,v 1.3 2005/05/18 06:46:55 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "LayeredStreamTest.h"
#import <ObjectiveLib/DataInStream.h>
#import <ObjectiveLib/DataOutStream.h>
#import <ObjectiveLib/ObjectInStream.h>
#import <ObjectiveLib/ObjectOutStream.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>

@implementation LayeredStreamTest

#define LST_SUCCESS 1000000

- (int) readData: (OLInStream*)stream
{
    int i;

    if ([stream isMemberOfClass: [OLObjectInStream class]])
    {
        for (i = 0; i < 50000; i++)
        {
            if ([[(OLObjectInStream*)stream readObject] intValue] != i)
                return i;
        }
    }
    else
    {
        for (i = 0; i < 1000000; i++)
        {
            if ([stream readInt] != i)
                return i;
        }
    }
    return LST_SUCCESS;
}

- (void) runTestStreams: (NSString**)streams
{
    OLDataOutStream* dout;
    OLOutStream* topOut;
    OLInStream* topIn;
    int i;
    int rc;
    unsigned count;
    NSString* cur;

    for (count = 0, cur = streams[0]; cur != nil; count++, cur = streams[count]);

    dout = [OLDataOutStream stream];
    topOut = dout;
    for (i = 0; i < count; i++)
    {
        topOut = [NSClassFromString(
            [NSString stringWithFormat: @"OL%@OutStream", streams[i]])
            streamWithOutStream: topOut];
    }
    [self writeData: topOut];
    topIn = [OLDataInStream streamWithDataObject: [dout data]];
    for (i = 0; i < count; i++)
    {
        topIn = [NSClassFromString(
            [NSString stringWithFormat: @"OL%@InStream", streams[i]])
            streamWithInStream: topIn];
    }
    rc = [self readData: topIn];
    if (rc != LST_SUCCESS)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Error reading data at index %i", rc];
    }
}

- (void) testBufferingObject
{
    NSString* streams[] = { @"Buffering", @"Object", nil };
    [self runTestStreams: streams];
}

- (void) testObjectBuffering
{
    NSString* streams[] = { @"Object", @"Buffering", nil };
    [self runTestStreams: streams];
}

#if defined(HAVE_BZLIB)

- (void) testBufferingBzlib
{
    NSString* streams[] = { @"Buffering", @"Bzlib", nil };
    [self runTestStreams: streams];
}

- (void) testBzlibBuffering
{
    NSString* streams[] = { @"Bzlib", @"Buffering", nil };
    [self runTestStreams: streams];
}

- (void) testBzlibBufferingObject
{
    NSString* streams[] = { @"Bzlib", @"Buffering", @"Object", nil };
    [self runTestStreams: streams];
}

#endif

#if defined(HAVE_ZLIB)

- (void) testBufferingGzip
{
    NSString* streams[] = { @"Buffering", @"Gzip", nil };
    [self runTestStreams: streams];
}

- (void) testBufferingZlib
{
    NSString* streams[] = { @"Buffering", @"Zlib", nil };
    [self runTestStreams: streams];
}

- (void) testGzipBuffering
{
    NSString* streams[] = { @"Gzip", @"Buffering", nil };
    [self runTestStreams: streams];
}

- (void) testGzipBufferingObject
{
    NSString* streams[] = { @"Gzip", @"Buffering", @"Object", nil };
    [self runTestStreams: streams];
}

- (void) testZlibBuffering
{
    NSString* streams[] = { @"Zlib", @"Buffering", nil };
    [self runTestStreams: streams];
}

- (void) testZlibBufferingObject
{
    NSString* streams[] = { @"Zlib", @"Buffering", @"Object", nil };
    [self runTestStreams: streams];
}

#endif

- (void) writeData: (OLOutStream*)stream
{
    int i;

    if ([stream isMemberOfClass: [OLObjectOutStream class]])
    {
        for (i = 0; i < 50000; i++)
            [(OLObjectOutStream*)stream writeObject: [NSNumber numberWithInt: i]];
    }
    else
    {
        for (i = 0; i < 1000000; i++)
            [stream writeInt: i];
    }
    [stream close];
}

@end
