//
// $Id: QueueTest.m,v 1.11 2005/05/18 06:46:56 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 "QueueTest.h"
#import <ObjectiveLib/Queue.h>
#import <Foundation/NSString.h>
#import <Foundation/NSData.h>
#import <Foundation/NSArchiver.h>
#if defined(HAVE_KEYED_ARCHIVES)
#import <Foundation/NSKeyedArchiver.h>
#endif
#import <Foundation/NSValue.h>
#import <stdlib.h>
#if defined(__NEXT_RUNTIME__)
#import <objc/objc-class.h>
#endif

@implementation QueueTest

- (void) testAccessors
{
    OLQueue* q;
    NSString* strs[] = { @"one", @"two", @"three" };
    int i;

    q = [[OLQueue alloc] init];
    for (i = 0; i < 3; i++)
        [q push: strs[i]];
    if (![[q front] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [strs[0] cString], [[q front] cString]];
    }
    if (![[q back] isEqual: strs[2]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [strs[2] cString], [[q front] cString]];
    }
    [q release];
}

- (void) testCoding
{
    NSMutableData* data;
    NSArchiver* archiver;
    NSData* archData;
    OLQueue* q1;
    OLQueue* q2;
    NSNumber* num;
    int i;


    q1 = [[OLQueue alloc] init];
    for (i = 0; i< 5000; i++)
    {
        num = [[NSNumber alloc] initWithInt: lrand48() % 10000];
        [q1 push: num];
        [num release];
    }
    data = [[NSMutableData alloc] initWithCapacity: 5000];
    archiver = [[NSArchiver alloc] initForWritingWithMutableData: data];
    [archiver encodeRootObject: q1];
    [archiver release];
    q2 = [NSUnarchiver unarchiveObjectWithData: data];
    [data release];
    if (![q1 isEqual: q2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The deques should be equal"];
    }
#if defined(HAVE_KEYED_ARCHIVES)
    archData = [NSKeyedArchiver archivedDataWithRootObject: q1];
    q2 = [NSKeyedUnarchiver unarchiveObjectWithData: archData];
    if (![q1 isEqual: q2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The deques should be equal"];
    }
#endif
    [q1 release];
}

- (void) testConvenienceAllocators
{
    OLQueue* q;
    OLQueue* q2;
    NSNumber* num;

    q = [OLQueue queue];
    if (![q isMemberOfClass: [OLQueue class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLQueue class]->name, [q class]->name];
    }
    if (![q empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The queue should be empty"];
    }
    num = [[NSNumber alloc] initWithInt: 1];
    [q push: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 2];
    [q push: num];
    [num release];
    q2 = [OLQueue queueWithQueue: q];
    if (![q isEqual: q2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The queues should be equal"];
    }
}

- (void) testInitializers
{
    OLQueue* q;
    OLQueue* q2;
    NSString* strs[] = { @"one", @"two", @"three" };
    int i;

    q = [[OLQueue alloc] init];
    if ([q size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [q size]];
    }
    for (i = 0; i < 3; i++)
        [q push: strs[i]];
    q2 = [[OLQueue alloc] initWithQueue: q];
    if ([q2 size] != 3)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 3, but got %i", [q2 size]];
    }
    if (![q isEqual: q2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The queues are equal"];
    }
    [q release];
    [q2 release];
}

- (void) testPop
{
    OLQueue* q;
    NSString* strs[] = { @"one", @"two", @"three" };
    int i;

    q = [[OLQueue alloc] init];
    for (i = 0; i < 3; i++)
        [q push: strs[i]];
    for (i = 0; i < 3; i++)
    {
        if (![[q front] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[q front] cString]];
        }
        [q pop];
    }
    if ([q size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [q size]];
    }
    [q release];
}

- (void) testProperties
{
    OLQueue* q;
    OLQueue* q2;

    q = [[OLQueue alloc] init];
    if ([q size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [q size]];
    }
    if (![q empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The queue is empty"];
    }
    [q push: @"one"];
    q2 = [[OLQueue alloc] init];
    if ([q isEqual: q2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The queues are not equal"];
    }
    [q2 push: @"one"];
    if (![q isEqual: q2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The queues are equal"];
    }
    if (![[q allocator] isEqual: [OLAllocator defaultAllocator]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([q allocator] != [OLAllocator defaultAllocator])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    [q release];
    [q2 release];
}

- (void) testPush
{
    OLQueue* q;
    NSString* strs[] = { @"one", @"two", @"three" };
    int i;

    q = [[OLQueue alloc] init];
    for (i = 0; i < 3; i++)
    {
        [q push: strs[i]];
        if (![[q back] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[q back] cString]];
        }
    }
    [q release];
}

@end
