//
// $Id: HashMapTest.m,v 1.12 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 "HashMapTest.h"
#import <ObjectiveLib/HashMap.h>
#import <ObjectiveLib/Vector.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>
#else
#import <objc/objc.h>
#endif

@implementation HashMapTest

- (void) testAssignKey
{
    OLHashMap* m;
    OLHashIterator* b;

    m = [[OLHashMap alloc] init];
    [m assignKey: @"doggy" value: @"boy"];
    if ([m size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", [m size]];
    }
    b = [m begin];
    if (![[[b dereference] first] isEqual: @"doggy"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"doggy\", but got \"%s\"",
            [[[b dereference] first] cString]];
    }
    if (![[[b dereference] second] isEqual: @"boy"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"boy\", but got \"%s\"",
            [[[b dereference] second] cString]];
    }
    [m assignKey: @"doggy" value: @"chunk"];
    if ([m size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", [m size]];
    }
    b = [m begin];
    if (![[[b dereference] first] isEqual: @"doggy"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"doggy\", but got \"%s\"",
            [[[b dereference] first] cString]];
    }
    if (![[[b dereference] second] isEqual: @"chunk"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"chunk\", but got \"%s\"",
            [[[b dereference] second] cString]];
    }
    [m release];
}

- (void) testCoding
{
    NSMutableData* data;
    NSArchiver* archiver;
    NSData* archData;
    OLHashMap* m1;
    OLHashMap* m2;
    NSNumber* k;
    NSNumber* v;
    int i;


    m1 = [[OLHashMap alloc] init];
    [self logMessage: "Building hash map"];
    for (i = 0; i< 10000; i++)
    {
        k = [[NSNumber alloc] initWithInt: i];
        v = [[NSNumber alloc] initWithInt: lrand48() % 10000];
        [m1 assignKey: k value: v];
        [k release];
        [v release];
    }
    data = [[NSMutableData alloc] initWithCapacity: 10000];
    archiver = [[NSArchiver alloc] initForWritingWithMutableData: data];
    [self logMessage: "Encoding hash map"];
    [archiver encodeRootObject: m1];
    [archiver release];
    [self logMessage: "Decoding hash map"];
    m2 = [NSUnarchiver unarchiveObjectWithData: data];
    [data release];
    if (![m1 isEqual: m2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The hash maps should be equal"];
    }
#if defined(HAVE_KEYED_ARCHIVES)
    [self logMessage: "Encoding hash map with keys"];
    archData = [NSKeyedArchiver archivedDataWithRootObject: m1];
    [self logMessage: "Decoding hash map with keys"];
    m2 = [NSKeyedUnarchiver unarchiveObjectWithData: archData];
    if (![m1 isEqual: m2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The hash maps should be equal"];
    }
#endif
    [m1 release];
    [self logMessage: "Done"];
}

- (void) testConvenienceAllocators
{
    OLHashMap* m;
    OLHashMap* m2;
    NSNumber* num;

    m = [OLHashMap hashMap];
    if (![m isMemberOfClass: [OLHashMap class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLHashMap class]->name, [m class]->name];
    }
    if (![m empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The map should be empty"];
    }
    num = [[NSNumber alloc] initWithInt: 1];
    [m assignKey: num value: [num description]];
    [num release];
    num = [[NSNumber alloc] initWithInt: 2];
    [m assignKey: num value: [num description]];
    [num release];
    num = [[NSNumber alloc] initWithInt: 3];
    [m assignKey: num value: [num description]];
    [num release];
    m2 = [OLHashMap hashMapFrom: [m begin] to: [m end]];
    if (![m isEqual: m2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The maps should be equal"];
    }
    num = [[NSNumber alloc] initWithInt: 4];
    [m assignKey: num value: [num description]];
    [num release];
    num = [[NSNumber alloc] initWithInt: 5];
    [m assignKey: num value: [num description]];
    [num release];
    m2 = [OLHashMap hashMapWithHashMap: m];
    if (![m isEqual: m2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The maps should be equal"];
    }
}

- (void) testEqualRange
{
    OLHashMap* m;
    OLPair* p;
    unsigned dist;

    m = [[OLHashMap alloc] init];
    [m assignKey: @"doggy" value: @"boy"];
    p = [m equalRange: @"doggy"];
    dist = [OLIterator distanceFrom: [p first] to: [p second]];
    if (dist != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", dist];
    }
    if (![[[[p first] dereference] first] isEqual: @"doggy"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"doggy\", but got \"%s\"",
            [[[[p first] dereference] first] cString]];
    }
    if (![[[[p first] dereference] second] isEqual: @"boy"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"boy\", but got \"%s\"",
            [[[[p first] dereference] second] cString]];
    }
    p = [m equalRange: @"chunks"];
    dist = [OLIterator distanceFrom: [p first] to: [p second]];
    if (dist != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", dist];
    }
    if (![[p first] isEqual: [m end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The key should not have been found"];
    }
    if (![[p second] isEqual: [m end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The key should not have been found"];
    }
    [m release];
}

- (void) testErase
{
    OLHashMap* m;
    NSString* vals[] = { @"one", @"two", @"three", @"four", @"five" };
    NSNumber* key;
    OLHashIterator* b;
    unsigned count;
    int i;

    m = [[OLHashMap alloc] init];
    key = [[NSNumber alloc] initWithInt: 0];
    [m insertKey: key value: vals[0]];
    [key release];
    [m erase: [m begin]];
    if ([m size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [m size]];
    }
    for (i = 0; i < 5; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        [m insertKey: key value: vals[i]];
        [key release];
    }
    [m eraseFrom: [[m begin] advance] to: [m end]];
    if ([m size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", [m size]];
    }
    b = [m begin];
    if ([[[b dereference] first] intValue] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [[[b dereference] first] intValue]];
    }
    if (![[[b dereference] second] isEqual: vals[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"one\", but got \"%s\"",
            [[[b dereference] second] cString]];
    }
    count = [m eraseKey: [[b dereference] first]];
    if ([m size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [m size]];
    }
    if (count != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", count];
    }
    for (i = 0; i < 5; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        [m insertKey: key value: vals[i]];
        [key release];
    }
    [m clear];
    if ([m size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [m size]];
    }
    [m release];
}

- (void) testFind
{
    OLHashMap* m;
    NSString* vals[] = { @"one", @"two", @"three", @"four", @"five" };
    NSNumber* key;
    OLHashIterator* itor;
    id val;
    int i;
    unsigned count;

    m = [[OLHashMap alloc] init];
    for (i = 0; i < 5; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        [m assignKey: key value: vals[i]];
        [key release];
    }
    key = [[NSNumber alloc] initWithInt: 0];
    count = [m count: key];
    if (count != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", count];
    }
    itor = [m find: key];
    if ([[[itor dereference] first] intValue] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i",
            [[[itor dereference] first] intValue]];
    }
    if (![[[itor dereference] second] isEqual: vals[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [vals[0] cString], [[[itor dereference] second] cString]];
    }
    val = [m valueForKey: key];
    if (![val isEqual: vals[0]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [vals[0] cString], [val cString]];
    }
    [key release];
    key = [[NSNumber alloc] initWithInt: 700];
    count = [m count: key];
    if (count != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", count];
    }
    itor = [m find: key];
    if (![itor isEqual: [m end]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The key should not have been found"];
    }
    val = [m valueForKey: key];
    if (val != nil)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The key should not have been found"];
    }
    [key release];
    [m release];
}

- (void) testInsert
{
    OLHashMap* m;
    OLPair* p;
    OLPair* r;
    OLVector* v;
    NSString* vals[] = { @"one", @"two", @"three", @"four", @"five" };
    NSNumber* key;
    OLHashIterator* itor;
    int i;

    m = [[OLHashMap alloc] init];
    p = [[OLPair alloc] initWithFirst: @"my" second: @"dog"];
    r = [m insert: p];
    [p release];
    if ([m size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", [m size]];
    }
    if (![[r second] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    if (![[[[r first] dereference] first] isEqual: @"my"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"my\", but got \"%s\"",
            [[[[r first] dereference] first] cString]];
    }
    if (![[[[r first] dereference] second] isEqual: @"dog"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"dog\", but got \"%s\"",
            [[[[r first] dereference] second] cString]];
    }
    p = [[OLPair alloc] initWithFirst: @"my" second: @"flea"];
    r = [m insert: p];
    [p release];
    if ([m size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", [m size]];
    }
    if ([[r second] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected NO, but got YES"];
    }
    if (![[[[r first] dereference] first] isEqual: @"my"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"my\", but got \"%s\"",
            [[[[r first] dereference] first] cString]];
    }
    if (![[[[r first] dereference] second] isEqual: @"dog"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"dog\", but got \"%s\"",
            [[[[r first] dereference] second] cString]];
    }
    r = [m insertKey: @"your" value: @"cat"];
    if ([m size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %u", [m size]];
    }
    if (![[r second] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    if (![[[[r first] dereference] first] isEqual: @"your"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"your\", but got \"%s\"",
            [[[[r first] dereference] first] cString]];
    }
    if (![[[[r first] dereference] second] isEqual: @"cat"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"cat\", but got \"%s\"",
            [[[[r first] dereference] second] cString]];
    }
    r = [m insertKey: @"your" value: @"girlfriend"];
    if ([m size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %u", [m size]];
    }
    if ([[r second] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected NO, but got YES"];
    }
    if (![[[[r first] dereference] first] isEqual: @"your"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"your\", but got \"%s\"",
            [[[[r first] dereference] first] cString]];
    }
    if (![[[[r first] dereference] second] isEqual: @"cat"])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"cat\", but got \"%s\"",
            [[[[r first] dereference] second] cString]];
    }
    [m release];
    v = [[OLVector alloc] init];
    for (i = 0; i < 5; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        p = [[OLPair alloc] initWithFirst: key second: vals[i]];
        [key release];
        [v pushBack: p];
        [p release];
    }
    m = [[OLHashMap alloc] init];
    [m insertFrom: [v begin] to: [v end]];
    [v release];
    if ([m size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %u", [m size]];
    }
    for (itor = [m begin], i = 0; i < 5; [itor advance], i++)
    {
        if (![[[itor dereference] second] isEqual: vals[[[[itor dereference] first] intValue]]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [vals[i] cString], [[[itor dereference] second] cString]];
        }
    }
    [m release];
}

- (void) testInitializers
{
    OLHashMap* m;
    OLVector* v;
    NSString* vals[] = { @"one", @"two", @"three", @"four", @"five" };
    NSNumber* key;
    OLPair* p;
    int i;
    unsigned count;

    m = [[OLHashMap alloc] init];
    if ([m size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [m size]];
    }
    [m release];
    m = [[OLHashMap alloc] initWithTableSize: 7000];
    if ([m size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [m size]];
    }
    [m release];
    v = [[OLVector alloc] init];
    for (i = 0; i < 5; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        p = [[OLPair alloc] initWithFirst: key second: vals[i]];
        [key release];
        [v pushBack: p];
        [p release];
    }
    m = [[OLHashMap alloc] initFrom: [v begin] to: [v end]];
    if ([m size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %u", [m size]];
    }
    for (i = 0; i < 5; i++)
    {
        count = [m count: [[v at: i] first]];
        if (count != 1)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected 1, but got %u", count];
        }
        if (![[m valueForKey: [[v at: i] first]] isEqual: vals[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [vals[i] cString], [m valueForKey: [[v at: i] first]]];
        }
    }
    [v release];
    [m release];
}

- (void) testIterators
{
    OLHashMap* m;
    NSString* vals[] = { @"one", @"two", @"three", @"four", @"five" };
    NSNumber* key;
    OLHashIterator* itor;
    OLHashIterator* end;
    int i;

    m = [[OLHashMap alloc] init];
    for (i = 0; i < 5; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        [m insertKey: key value: vals[i]];
        [key release];
    }
    for (itor = [m begin], end = [m end]; ![itor isEqual: end]; [itor advance])
    {
        if (![[[itor dereference] second] isEqual: vals[[[[itor dereference] first] intValue]]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [vals[i] cString], [[[itor dereference] second] cString]];
        }
    }
    [m release];
}

- (void) testProperties
{
    OLHashMap* m;
    OLHashMap* m2;

    m = [[OLHashMap alloc] init];
    if ([m size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %u", [m size]];
    }
    if (![m empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The hash map is empty"];
    }
    [m assignKey: @"doggy" value: @"boy"];
    if ([m size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %u", [m size]];
    }
    if ([m empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The hash map is not empty"];
    }
    if (![[m allocator] isEqual: [OLAllocator defaultAllocator]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([m allocator] != [OLAllocator defaultAllocator])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    m2 = [[OLHashMap alloc] init];
    if ([m isEqual: m2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The maps are not equal"];
    }
    if (![m2 size] == 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", [m2 size]];
    }
    [m2 insertFrom: [m begin] to: [m end]];
    if (![m isEqual: m2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The maps are equal"];
    }
    if ([m maxSize] != [m2 maxSize])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "All hash maps must have the same max size"];
    }
    [self logMessage: "The max size of a hash map is %u", [m maxSize]];
    [m release];
    [m2 release];
}

- (void) testSwap
{
    OLHashMap* m;
    OLHashMap* m2;
    NSString* vals[] = { @"one", @"two" };
    NSString* vals2[] = { @"three", @"four" };
    NSNumber* key;
    OLHashIterator* itor;
    OLHashIterator* end;
    int i;

    m = [[OLHashMap alloc] init];
    m2 = [[OLHashMap alloc] init];
    for (i = 0; i < 2; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        [m assignKey: key value: vals[i]];
        [key release];
    }
    for ( ; i < 4; i++)
    {
        key = [[NSNumber alloc] initWithInt: i];
        [m2 assignKey: key value: vals2[i - 2]];
        [key release];
    }
    [m swap: m2];
    if ([m size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %u", [m size]];
    }
    if ([m2 size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %u", [m2 size]];
    }
    for (itor = [m2 begin], end = [m2 end], i = 0; ![itor isEqual: end]; [itor advance], i++)
    {
        if ([[[itor dereference] first] intValue] != i)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i",
                i, [[[itor dereference] first] intValue]];
        }
        if (![[[itor dereference] second] isEqual: vals[i]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [vals[i] cString], [[[itor dereference] second] cString]];
        }
    }
    for (itor = [m begin], end = [m end]; ![itor isEqual: end]; [itor advance], i++)
    {
        if ([[[itor dereference] first] intValue] != i)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i",
                i, [[[itor dereference] first] intValue]];
        }
        if (![[[itor dereference] second] isEqual: vals2[i - 2]])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected \"%s\", but got \"%s\"",
                [vals2[i - 2] cString], [[[itor dereference] second] cString]];
        }
    }
    [m release];
    [m2 release];
}

@end
