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

@implementation HashSetTest

- (void) testCoding
{
    NSMutableData* data;
    NSArchiver* archiver;
    NSData* archData;
    OLHashSet* s1;
    OLHashSet* s2;
    NSNumber* v;
    int i;


    s1 = [[OLHashSet alloc] init];
    for (i = 0; i< 1000; i++)
    {
        v = [[NSNumber alloc] initWithInt: i];
        [s1 insert: v];
        [v release];
    }
    data = [[NSMutableData alloc] initWithCapacity: 1000];
    archiver = [[NSArchiver alloc] initForWritingWithMutableData: data];
    [archiver encodeRootObject: s1];
    [archiver release];
    s2 = [NSUnarchiver unarchiveObjectWithData: data];
    [data release];
    if (![s1 isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The hash sets should be equal"];
    }
#if defined(HAVE_KEYED_ARCHIVES)
    archData = [NSKeyedArchiver archivedDataWithRootObject: s1];
    s2 = [NSKeyedUnarchiver unarchiveObjectWithData: archData];
    if (![s1 isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The hash sets should be equal"];
    }
#endif
    [s1 release];
}

- (void) testConvenienceAllocators
{
    OLHashSet* s;
    OLHashSet* s2;
    NSNumber* num;

    s = [OLHashSet hashSet];
    if (![s isMemberOfClass: [OLHashSet class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLHashSet class]->name, [s class]->name];
    }
    if (![s empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The map should be empty"];
    }
    num = [[NSNumber alloc] initWithInt: 1];
    [s insert: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 2];
    [s insert: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 3];
    [s insert: num];
    [num release];
    s2 = [OLHashSet hashSetFrom: [s begin] to: [s end]];
    if (![s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The maps should be equal"];
    }
    num = [[NSNumber alloc] initWithInt: 4];
    [s insert: num];
    [num release];
    num = [[NSNumber alloc] initWithInt: 5];
    [s insert: num];
    [num release];
    s2 = [OLHashSet hashSetWithHashSet: s];
    if (![s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The maps should be equal"];
    }
}

- (void) testEqualRange
{
    OLHashSet* s;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    OLPair* p;
    OLHashIterator* end;
    int i;

    p = [OLArrayIterator pairWithPointer: strs andPointer: strs + 5];
    s = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    end = [s end];
    p = [s equalRange: strs[1]];
    if ([[p first] isEqual: end])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should have been found", [strs[1] cString]];
    }
    if (![[[p first] dereference] isEqual: strs[1]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got\"%s\"", [strs[1] cString]];
    }
    i = [OLIterator distanceFrom: [p first] to: [p second]];
    if (i != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", i];
    }
    p = [s equalRange: @"doggy"];
    if (![[p first] isEqual: end])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"joe\" should have been found"];
    }
    if (![[p second] isEqual: end])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"joe\" should have been found"];
    }
    [s release];
}

- (void) testErase
{
    OLHashSet* s;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    OLPair* p;
    OLHashIterator* end;
    OLHashIterator* itor;
    int i;
    unsigned erased;

    p = [OLArrayIterator pairWithPointer: strs andPointer: strs + 5];
    s = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    end = [s end];
    for (i = 0; i < 5; i++)
    {
        itor = [s find: strs[i]];
        if ([itor isEqual: end])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should have been found", [strs[i] cString]];
        }
        [s erase: itor];
        itor = [s find: strs[i]];
        if (![itor isEqual: end])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should not have been found", [strs[i] cString]];
        }
        if ([s size] != 4 - i)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i", 4 - i, [s size]];
        }
    }
    [s insertFrom: [p first] to: [p second]];
    erased = [s eraseValue: strs[0]];
    if (erased != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", erased];
    }
    erased = [s eraseValue: strs[0]];
    if (erased != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", erased];
    }
    [s clear];
    if ([s size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [s size]];
    }
    [s release];
}

- (void) testFind
{
    OLHashSet* s;
    NSNumber* nums[10000];
    OLPair* p;
    OLHashIterator* itor;
    unsigned count;
    int i;

    for (i = 0; i < 10000; i++)
        nums[i] = [[NSNumber alloc] initWithInt: i];
    p = [OLArrayIterator pairWithPointer: nums andPointer: nums + 10000];
    s = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    itor = [s find: nums[7574]];
    if ([itor isEqual: [s end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "7574 should have been found"];
    }
    count = [s count: nums[9935]];
    if (count != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", count];
    }
    [s erase: itor];
    itor = [s find: nums[7574]];
    if (![itor isEqual: [s end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "7574 should not have been found"];
    }
    count = [s count: nums[7574]];
    if (count != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", count];
    }
    [s release];
    for (i = 0; i < 10000; i++)
        [nums[i] release];
}

- (void) testInsert
{
    OLHashSet* s;
    OLPair* p;
    NSNumber* num;
    NSString* strs[5000];
    int i;

    s = [[OLHashSet alloc] init];
    p = [s insert: @"one"];
    if (![[p second] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The insert should have been successful"];
    }
    if (![[[p first] dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The expected \"one\", but got \"%s\"",
            [[[p first] dereference] cString]];
    }
    p = [s insert: @"one"];
    if ([[p second] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The insert should not have been successful"];
    }
    if (![[[p first] dereference] isEqual: @"one"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The expected \"one\", but got \"%s\"",
            [[[p first] dereference] cString]];
    }
    for (i = 0; i < 5000; i++)
    {
        num = [[NSNumber alloc] initWithInt: i];
        strs[i] = [num description];
        [num release];
    }
    p = [OLArrayIterator pairWithPointer: strs andPointer: strs + 5000];
    [s insertFrom: [p first] to: [p second]];
    if ([s size] != 5001)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5001, but got %u", [s size]];
    }
    for (i = 0; i < 5000; i++)
    {
        if ([s count: strs[i]] != 1)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but didn't find it",
                [strs[i] cString]];
        }
    }
    [s release];
}

- (void) testInitializers
{
    OLHashSet* s;
    OLHashSet* s2;
    OLVector* v;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    OLHashIterator* begin;
    OLHashIterator* end;
    int i;

    s = [[OLHashSet alloc] init];
    if ([s size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The expected 0, but got %u", [s size]];
    }
    [s release];
    v = [[OLVector alloc] init];
    for (i = 0; i < 5; i++)
        [v pushBack: strs[i]];
    s = [[OLHashSet alloc] initFrom: [v begin] to: [v end]];
    [v release];
    [self logMessage: "The set contains:"];
    for (begin = [s begin], end = [s end]; ![begin isEqual: end]; [begin advance])
        [self logMessage: "%s", [[begin dereference] cString]];
    for (i = 0; i < 5; i++)
    {
        if ([s count: strs[i]] != 1)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" was not found", [strs[i] cString]];
        }
    }
    s2 = [[OLHashSet alloc] initWithHashSet: s];
    if ([s2 size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The expected 5, but got %u", [s2 size]];
    }
    for (i = 0; i < 5; i++)
    {
        if ([s2 count: strs[i]] != 1)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" was not found", [strs[i] cString]];
        }
    }
    [s release];
    [s2 release];
}

- (void) testIsEqual
{
    OLHashSet* s;
    OLHashSet* s2;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    OLPair* p;

    p = [OLArrayIterator pairWithPointer: strs andPointer: strs + 5];
    s = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    s2 = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    if (![s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should be equal"];
    }
    [s2 release];
    s2 = [s copy];
    if (![s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should be equal"];
    }
    [s2 erase: [s2 find: strs[0]]];
    if ([s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should not be equal"];
    }
    p = [s2 insert: @"joe"];
    if ([s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should be equal"];
    }
    [s2 erase: [p first]];
    [s2 insert: strs[0]];
    if (![s isEqual: s2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should be equal"];
    }
    [s release];
    [s2 release];
}

- (void) testIterators
{
    OLHashSet* s;
    NSNumber* nums[10000];
    OLPair* p;
    OLHashIterator* begin;
    OLHashIterator* end;
    int foundNums[10000];
    int found;
    int i;

    s = [[OLHashSet alloc] init];
    [self logMessage: "Allocating numbers"];
    for (i = 0; i < 10000; i++)
        nums[i] = [[NSNumber alloc] initWithInt: i];
    [self logMessage: "Done"];
    p = [OLArrayIterator pairWithPointer: nums andPointer: nums + 10000];
    [self logMessage: "Inserting"];
    [s insertFrom: [p first] to: [p second]];
    [self logMessage: "Done"];
    [self logMessage: "Releasing numbers"];
    for (i = 0; i < 10000; i++)
    {
        [nums[i] release];
        foundNums[i] = -1;
    }
    [self logMessage: "Done"];
    [self logMessage: "Traversing set"];
    for (begin = [s begin], end = [s end]; ![begin isEqual: end]; [begin advance])
    {
        found = [[begin dereference] intValue];
        if (found < 0 || found >= 10000)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected a number from 0 to 9999, but got %i", found];
        }
        if (foundNums[found] != -1)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Found %i more than once in the collection", found];
        }
        foundNums[found] = found;
    }
    [self logMessage: "Done"];
    [self logMessage: "Verifying results"];
    for (i = 0; i < 10000; i++)
    {
        if (foundNums[i] == -1)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "The number %i was not iterated over", foundNums[i]];
        }
    }
    [self logMessage: "Done"];
    [self logMessage: "Releasing set"];
    [s release];
    [self logMessage: "Done"];
}

- (void) testProperties
{
    OLHashSet* s;
    OLHashSet* s2;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    OLPair* p;

    s = [[OLHashSet alloc] init];
    if (![s empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The set is empty"];
    }
    p = [OLArrayIterator pairWithPointer: strs andPointer: strs + 5];
    [s insertFrom: [p first] to: [p second]];
    if ([s empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The set is not empty"];
    }
    if (![[s allocator] isEqual: [OLAllocator defaultAllocator]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([s allocator] != [OLAllocator defaultAllocator])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([[s keyEqual] class] != [OLEqualTo class])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"OLEqualTo\" but got \"%s\"", [[s keyEqual] class]->name];
    }
    s2 = [[OLHashSet alloc] init];
    if ([s maxSize] != [s2 maxSize])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "All hash sets must have the same max size"];
    }
    [self logMessage: "The max size of a hash set is %u", [s maxSize]];
    [s release];
    [s2 release];
}

- (void) testSwap
{
    OLHashSet* s;
    OLHashSet* s2;
    OLHashSet* s3;
    OLHashSet* s4;
    NSString* strs[] = { @"one", @"two", @"three", @"four", @"five" };
    NSString* strs2[] = { @"six", @"seven" };
    OLPair* p;

    p = [OLArrayIterator pairWithPointer: strs andPointer: strs + 5];
    s = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    p = [OLArrayIterator pairWithPointer: strs2 andPointer: strs2 + 2];
    s2 = [[OLHashSet alloc] initFrom: [p first] to: [p second]];
    s3 = [s copy];
    s4 = [s2 copy];
    [s swap: s2];
    if (![s isEqual: s4])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should be equal"];
    }
    if (![s2 isEqual: s3])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The sets should be equal"];
    }
    [s release];
    [s2 release];
    [s3 release];
    [s4 release];
}

@end
