//
// $Id: ListTest.m,v 1.17 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 "ListTest.h"
#import <ObjectiveLib/List.h>
#import <ObjectiveLib/Vector.h>
#import <ObjectiveLib/Memory.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSData.h>
#import <Foundation/NSArchiver.h>
#if defined(HAVE_KEYED_ARCHIVES)
#import <Foundation/NSKeyedArchiver.h>
#endif
#import <stdlib.h>
#if defined(__NEXT_RUNTIME__)
#import <objc/objc-class.h>
#endif

@interface FirstLetterSame : NSObject <OLBoolBinaryFunction>
{
}

- (BOOL) performBinaryFunctionWithArg: (id)arg1 andArg: (id)arg2;

@end

@implementation ListTest

- (void) testAccessors
{
    OLList* l;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    int i;

    l = [[OLList alloc] init];
    for (i = 0; i < 5; i++)
        [l pushBack: strs[i]];
    if (![[l back] isEqual: @"five"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"five\", but got \"%s\"", [[l back] cString]];
    }
    if (![[l front] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[l front] cString]];
    }
    [l release];
}

- (void) testAssign
{
    OLList* l;
    OLList* l2;
    OLListIterator* itor;
    OLListIterator* end;

    l = [[OLList alloc] init];
    [l pushBack: @"one"];
    [l pushBack: @"two"];
    [l pushBack: @"three"];
    l2 = [l copy];

    [l assign: 2 filledWith: @"four"];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    end = [l end];
    for (itor = [l begin]; ![itor isEqual: end]; [itor advance])
    {
        if (![[itor dereference] isEqual: @"four"])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"four\", but got \"%s\"", [[itor dereference] cString]];
        }
    }
    [l assignFrom: [l2 begin] to: [l2 end]];
    [l2 release];
    if ([l size] != 3)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 3, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"two"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"three"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l release];
}

- (void) testCoding
{
    NSMutableData* data;
    NSArchiver* archiver;
    NSData* archData;
    OLList* l1;
    OLList* l2;
    NSNumber* num;
    int i;


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

- (void) testConvenienceAllocators
{
    OLList* l;
    OLList* l2;
    NSNumber* num;

    l = [OLList list];
    if (![l isMemberOfClass: [OLList class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLList class]->name, [l class]->name];
    }
    if (![l empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list should be empty"];
    }
    num = [[NSNumber alloc] initWithInt: 5];
    [l pushBack: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 6];
    [l pushBack: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 7];
    [l pushBack: num];
    [num release];
    l2 = [OLList listFrom: [l begin] to: [l end]];
    if (![l isEqual: l2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The lists should be equal"];
    }
    num = [[NSNumber alloc] initWithInt: 8];
    [l pushBack: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 9];
    [l pushBack: num];
    [num release];
    l2 = [OLList listWithList: l];
    if (![l isEqual: l2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The lists should be equal"];
    }
    num = [[NSNumber alloc] initWithInt: 10];
    l = [OLList listWithSize: 666 filledWith: num];
    [num release];
    if (![l isMemberOfClass: [OLList class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLList class]->name, [l class]->name];
    }
    if ([l size] != 666)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 666, but got %u", [l size]];
    }
}

- (void) testErase
{
    NSString* strs[] = { @"one", @"two", @"three" };
    OLList* l;
    int i;
    OLListIterator* itor;
    OLGreater* greater;
    OLBoolBinder2nd* binder;

    l = [[OLList alloc] init];
    for (i = 0; i < 3; i++)
        [l pushBack: strs[i]];
    itor = [l begin];
    [itor advance];
    itor = [l erase: itor];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    if (![[itor dereference] isEqual: strs[2]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor reverse];
    if (![[itor dereference] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    itor = [l eraseFrom: itor to: [l end]];
    if ([l size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", [l size]];
    }
    if (![itor isEqual: [l end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The iterator should be at the end"];
    }
    [itor reverse];
    if (![[itor dereference] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l clear];
    if ([l size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [l size]];
    }
    for (i = 0; i < 3; i++)
        [l pushBack: strs[i]];
    [l popFront];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: strs[1]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: strs[2]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l popBack];
    if ([l size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", [l size]];
    }
    if (![[l front] isEqual: strs[1]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[l front] cString]];
    }
    [l clear];
    for (i = 0; i < 3; i++)
        [l pushBack: strs[i]];
    [l remove: @"two"];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: strs[2]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l clear];
    for (i = 0; i < 3; i++)
        [l pushBack: strs[i]];
    greater = [[OLGreater alloc] init];
    binder = [[OLBoolBinder2nd alloc] initWithBoolFunction: greater andRightArg: @"socks"];
    [greater release];
    [l removeIf: binder];
    [binder release];
    if ([l size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", [l size]];
    }
    if (![[l front] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[l front] cString]];
    }
    [l release];
}

- (void) testInitializers
{
    OLList* l;
    OLVector* v;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    int i;
    OLList* l2;

    l = [[OLList alloc] init];
    if ([l size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [l size]];
    }
    if (![l empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list must be empty"];
    }
    [l release];

    l = [[OLList alloc] initWithSize: 5 filledWith: @"one"];
    if ([l size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [l size]];
    }
    for (i = 0; i < 5; i++)
    {
        if (![[l front] isEqual: @"one"])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"one\", but got \"%s\"", [[l front] cString]];
        }
        [l popFront];
    }
    if (![l empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list should be empty"];
    }
    [l release];


    v = [[OLVector alloc] init];
    for (i = 0; i < 5; i++)
        [v pushBack: strs[i]];
    l = [[OLList alloc] initFrom: [v begin] to: [v end]];
    if ([l size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [l size]];
    }
    for (i = 0; i < 5; i ++)
    {
        if (![[l front] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Item at index %i is wrong", i];
        }
        [l popFront];
    }
    if (![l empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list should be empty"];
    }
    [l release];

    l = [[OLList alloc] initFrom: [v begin] to: [v end]];
    [v release];
    l2 = [[OLList alloc] initWithList: l];
    if ([l2 size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [l2 size]];
    }
    if (![l isEqual: l2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The lists should be equal"];
    }
    for (i = 0; i < 5; i++)
    {
        if (![[l front] isEqual: [l2 front]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "The elements at index %i should be equal", i];
        }
        [l popFront];
        [l2 popFront];
    }
    [l release];
    [l2 release];
}

- (void) testInsert
{
    OLList* l;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    int i;
    OLListIterator* itor;
    OLListIterator* where;
    OLListIterator* end;
    OLVector* v;

    l = [[OLList alloc] init];
    [l insertAt: [l begin] value: strs[0]];
    if ([l size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", [l size]];
    }
    if (![[l front] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got %s", [[l front] cString]];
    }
    itor = [l insertAt: [l end] value: strs[2]];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    if (![[l front] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got %i", [[l front] cString]];
    }
    if (![[l back] isEqual: strs[2]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got %s", [[l back] cString]];
    }
    where = [l insertAt: itor value: strs[1]];
    if ([l size] != 3)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 3, but got %i", [l size]];
    }
    end = [l end];
    for (itor = [l begin], i = 0; ![itor isEqual: end]; [itor advance], i++)
    {
        if (![[itor dereference] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
    }
    v = [[OLVector alloc] init];
    [v pushBack: [NSNumber numberWithInt: 1]];
    [v pushBack: [NSNumber numberWithInt: 2]];
    [v pushBack: [NSNumber numberWithInt: 3]];
    [l insertAt: where from: [v begin] to: [v end]];
    if ([l size] != 6)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 6, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: strs[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [strs[0] cString], [[itor dereference] cString]];
    }
    for (i = 0; i < 3; i++)
    {
        if (![[[itor advance] dereference] isEqual: [v at: i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [[v at: i] cString], [[itor dereference] cString]];
        }
    }
    [v release];
    for (i = 1; i < 3; i++)
    {
        if (![[[itor advance] dereference] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
    }
    [l insertAt: [l begin] count: 3 filledWith: @"doggy"];
    itor = [l begin];
    for (i = 0; i < 3; i++)
    {
        if (![[itor dereference] isEqual: @"doggy"])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"doggy\", but got \"%s\"", [[itor dereference] cString]];
        }
        [itor advance];
    }
    [l release];
}

- (void) testIterators
{
    OLList* l;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    int i;
    OLListIterator* itor;
    OLReverseBidiIterator* ritor;
    OLReverseBidiIterator* rend;

    l = [[OLList alloc] init];
    for (i = 0; i < 5; i++)
        [l pushBack: strs[i]];
    itor = [l begin];
    for (i = 0; i < 5; i++)
    {
        if (![[itor dereference] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
        [itor advance];
    }
    if (![itor isEqual: [l end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The iterator should be at the end"];
    }
    for (i = 4; i >= 0; i--)
    {
        [itor reverse];
        if (![[itor dereference] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
    }
    rend = [l rend];
    for (ritor = [l rbegin], i = 4; ![ritor isEqual: rend]; [ritor advance], i--)
    {
        if (![[ritor dereference] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
    }
    ritor = [ritor copy];
    if (![ritor isEqual: [l rend]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The reverse iterator should be at the reverse end"];
    }
    [ritor release];
    itor = [itor copy];
    if (![itor isEqual: [l begin]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The iterator should be at the beginning"];
    }
    [itor release];
    [l release];
}

- (void) testProperties
{
    OLList* l;
    OLList* l2;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    int i;

    l = [[OLList alloc] init];
    if (![l empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list must report that it is empty"];
    }
    l2 = [[OLList alloc] init];
    for (i = 0; i < 5; i++)
    {
        [l pushBack: strs[i]];
        [l2 pushBack: strs[i]];
    }
    if (![l isEqual: l2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The lists must be equal"];
    }
    if ([l size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [l size]];
    }
    if ([l empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list cannot report that it is empty"];
    }
    if (![[l allocator] isEqual: [OLAllocator defaultAllocator]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([l allocator] != [OLAllocator defaultAllocator])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    [l clear];
    if ([l isEqual: l2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The lists cannot be equal"];
    }
    if ([l maxSize] != [l2 maxSize])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "All lists must have the same max size"];
    }
    [self logMessage: "The max size of a list is %u", [l maxSize]];
    [l release];
    [l2 release];
}

- (void) testMerge
{
    NSString* strs1[] = { @"Daniela", @"Helena" };
    NSString* strs2[] = { @"Clara", @"Yehudi" };
    int i;
    OLList* l;
    OLList* l2;
    OLListIterator* itor;
    OLGreater* greater;

    l = [[OLList alloc] init];
    for (i = 0; i < 2; i++)
        [l pushBack: strs1[i]];
    l2 = [[OLList alloc] init];
    for (i = 0; i < 2; i++)
        [l2 pushBack: strs2[i]];
    [l merge: l2];
    if ([l size] != 4)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 4, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"Clara"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Clara\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Daniela"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Daniela\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Helena"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Helena\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Yehudi"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Yehudi\", but got \"%s\"", [[itor dereference] cString]];
    }
    if (![l2 empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list must be empty"];
    }
    [l clear];
    for (i = 1; i >= 0; i--)
        [l pushBack: strs1[i]];
    for (i = 1; i >= 0; i--)
        [l2 pushBack: strs2[i]];
    greater = [[OLGreater alloc] init];
    [l merge: l2 withOrder: greater];
    [greater release];
    if ([l size] != 4)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 4, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"Yehudi"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Yehudi\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Helena"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Helena\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Daniela"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Daniela\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Clara"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Clara\", but got \"%s\"", [[itor dereference] cString]];
    }
    if (![l2 empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list must be empty"];
    }
    [l release];
    [l2 release];
}

- (void) testResize
{
    NSString* strs[] = { @"one", @"two", @"three" };
    OLList* l;
    OLListIterator* itor;
    int i;

    l = [[OLList alloc] init];
    for (i = 0; i < 3; i++)
        [l pushBack: strs[i]];
    [l resize: 2 filledWith: @"doggy"];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    if (![[l front] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[l front] cString]];
    }
    if (![[l back] isEqual: @"two"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[l back] cString]];
    }
    [l resize: 4 filledWith: @"doggy"];
    if ([l size] != 4)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 4, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"two"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[itor dereference] cString]];
    }
    for (i = 0; i < 2; i++)
    {
        if (![[[itor advance] dereference] isEqual: @"doggy"])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"doggy\", but got \"%s\"", [[itor dereference] cString]];
        }
    }
    [l release];
}

- (void) testReverse
{
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    OLList* l;
    int i;
    OLListIterator* itor;
    OLListIterator* end;

    l = [[OLList alloc] init];
    for (i = 0; i < 5; i++)
        [l pushBack: strs[i]];
    [l reverse];
    end = [l end];
    for (itor = [l begin], i = 4; ![itor isEqual: end]; [itor advance], i--)
    {
        if (![[itor dereference] isEqual: strs[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
    }
    [l clear];
    [l reverse];
    if ([l size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list must be empty"];
    }
    [l release];
}

- (void) testSort
{
    NSString* strs[] = { @"Yehudi", @"Helena", @"Paula", @"Clara", @"Daniela" };
    OLList* l;
    OLListIterator* itor;
    int i;

    l = [[OLList alloc] init];
    for (i = 0; i < 5; i++)
        [l pushBack: strs[i]];
    [l sort];
    itor = [l begin];
    if (![[itor dereference] isEqual: @"Clara"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Clara\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Daniela"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Daniela\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Helena"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Helena\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Paula"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Paula\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Yehudi"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Yehudi\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l release];
}

- (void) testSplice
{
    OLList* l;
    OLList* l2;
    NSString* strs1[] = { @"one", @"five" };
    NSString* strs2[] = { @"two", @"three", @"four" };
    OLListIterator* itor;
    int i;

    l = [[OLList alloc] init];
    for (i = 0; i < 2; i++)
        [l pushBack: strs1[i]];
    l2 = [[OLList alloc] init];
    for (i = 0; i < 3; i++)
        [l2 pushBack: strs2[i]];
    [l spliceAt: [[l begin] advance] list: l2 from: [[l2 begin] advance] to: [[[l2 begin] advance] advance]];
    if ([l size] != 3)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 3, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"three"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"five"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"five\", but got \"%s\"", [[itor dereference] cString]];
    }
    if ([l2 size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l2 size]];
    }
    if (![[l2 front] isEqual: @"two"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[itor dereference] cString]];
    }
    if (![[l2 back] isEqual: @"four"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"four\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l clear];
    for (i = 0; i < 2; i++)
        [l pushBack: strs1[i]];
    [l2 clear];
    for (i = 0; i < 3; i++)
        [l2 pushBack: strs2[i]];
    [l spliceAt: [[l begin] advance] list: l2 from: [[l2 begin] advance]];
    if ([l size] != 3)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 3, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"three"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"five"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"five\", but got \"%s\"", [[itor dereference] cString]];
    }
    if ([l2 size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l2 size]];
    }
    if (![[l2 front] isEqual: @"two"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[l2 front] cString]];
    }
    if (![[l2 back] isEqual: @"four"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"four\", but got \"%s\"", [[l2 back] cString]];
    }
    [l clear];
    for (i = 0; i < 2; i++)
        [l pushBack: strs1[i]];
    [l2 clear];
    for (i = 0; i < 3; i++)
        [l2 pushBack: strs2[i]];
    [l spliceAt: [[l begin] advance] list: l2];
    if ([l size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"two"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"two\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"three"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"three\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"four"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"four\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"five"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"five\", but got \"%s\"", [[itor dereference] cString]];
    }
    if (![l2 empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The list must be empty"];
    }
    [l release];
    [l2 release];
}

- (void) testSwap
{
    OLList* l;
    OLList* l2;
    NSString* strs1[] = { @"one", @"two", @"three" };
    NSString* strs2[] = { @"four", @"five" };
    int i;
    OLListIterator* itor;
    OLListIterator* end;

    l = [[OLList alloc] init];
    for (i = 0; i < 3; i++)
        [l pushBack: strs1[i]];
    l2 = [[OLList alloc] init];
    for (i = 0; i < 2; i++)
        [l2 pushBack: strs2[i]];
    [l swap: l2];
    if ([l size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [l size]];
    }
    if ([l2 size] != 3)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 3, but got %i", [l2 size]];
    }
    end = [l end];
    for (itor = [l begin], i = 0; ![itor isEqual: end]; [itor advance], i++)
    {
        if (![[itor dereference] isEqual: strs2[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs2[i] cString], [[itor dereference] cString]];
        }
    }
    [l release];
    end = [l2 end];
    for (itor = [l2 begin], i = 0; ![itor isEqual: end]; [itor advance], i++)
    {
        if (![[itor dereference] isEqual: strs1[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs1[i] cString], [[itor dereference] cString]];
        }
    }
    [l2 release];
}

- (void) testUnique
{
    NSString* strs[] = { @"Clara", @"Clara", @"Daniela", @"Danny", @"Helena", @"Helena", @"Helena", @"Yehudi" };
    NSString* strs2[] = { @"Clara", @"Daniela", @"Danny", @"Helena", @"Yehudi" };
    OLList* l;
    OLListIterator* itor;
    OLListIterator* end;
    int i;
    FirstLetterSame* pred;

    l = [[OLList alloc] init];
    for (i = 0; i < 8; i++)
        [l pushBack: strs[i]];
    [l unique];
    if ([l size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [l size]];
    }
    end = [l end];
    for (itor = [l begin], i = 0; ![itor isEqual: end]; [itor advance], i++)
    {
        if (![[itor dereference] isEqual: strs2[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [strs[i] cString], [[itor dereference] cString]];
        }
    }
    [l clear];
    for (i = 0; i < 8; i++)
        [l pushBack: strs[i]];
    pred = [[FirstLetterSame alloc] init];
    [l uniqueWith: pred];
    [pred release];
    if ([l size] != 4)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 4, but got %i", [l size]];
    }
    itor = [l begin];
    if (![[itor dereference] isEqual: @"Clara"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Clara\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Daniela"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Daniela\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Helena"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Helena\", but got \"%s\"", [[itor dereference] cString]];
    }
    [itor advance];
    if (![[itor dereference] isEqual: @"Yehudi"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"Yehudi\", but got \"%s\"", [[itor dereference] cString]];
    }
    [l release];
}

@end

@implementation FirstLetterSame

- (BOOL) performBinaryFunctionWithArg: (id)arg1 andArg: (id)arg2
{
    return [arg1 characterAtIndex: 0] == [arg2 characterAtIndex: 0] ? YES : NO;
}

@end


