//
// $Id: BoolVectorTest.m,v 1.10 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 "BoolVectorTest.h"
#import <ObjectiveLib/Vector.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 BoolVectorTest

- (void) testAccessors
{
    OLBoolVector* v;

    v = [[OLBoolVector alloc] init];
    [v pushBack: YES];
    [v pushBack: NO];

    if (![v at: 0])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    if ([v at: 1])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected NO, but got YES"];
    }
    if (![v front])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    if ([v back])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected NO, but got YES"];
    }
    [v release];
}

- (void) testAssign
{
    OLBoolVector* v;
    OLBoolVector* v2;
    int i;

    v = [[OLBoolVector alloc] initWithSize: 5 filledWithBool: NO];
    v2 = [v copy];
    [v assign: 2 filledWith: YES];
    if ([v size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 2, but got %i", [v size]];
    }
    if (![v at: 0])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    if (![v at: 1])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    [v assignFrom: [v2 begin] to: [v2 end]];
    [v2 release];
    if ([v size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5, but got %i", [v size]];
    }
    for (i = 0; i < 5; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected NO, but got YES"];
        }
    }
    [v assignAt: 2 value: YES];
    if (![v at: 2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    [v release];
}

- (void) testCoding
{
    OLBoolVector* v1;
    OLBoolVector* v2;
    NSMutableData* data;
    NSArchiver* archiver;
    NSData* archData;
    int i;

    v1 = [[OLBoolVector alloc] init];
    data = [[NSMutableData alloc] initWithCapacity: 1000];
    archiver = [[NSArchiver alloc] initForWritingWithMutableData: data];
    [archiver encodeObject: v1];
    [archiver release];
    [self logMessage: "A boolean vector of 0 elements creates an archive of %u bytes",
        [data length]];
    v2 = [NSUnarchiver unarchiveObjectWithData: data];
    [data release];
    if (![v1 isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
#if defined(HAVE_KEYED_ARCHIVES)
    archData = [NSKeyedArchiver archivedDataWithRootObject: v1];
    [self logMessage: "A boolean vector of 0 elements creates a keyed archive of %u bytes",
        [archData length]];
    v2 = [NSKeyedUnarchiver unarchiveObjectWithData: archData];
    if (![v1 isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
#endif
    for (i = 0; i < 10000; i++)
        [v1 pushBack: (lrand48() & 1) ? YES : NO];
    data = [[NSMutableData alloc] initWithCapacity: 1000];
    archiver = [[NSArchiver alloc] initForWritingWithMutableData: data];
    [archiver encodeObject: v1];
    [archiver release];
    [self logMessage: "A boolean vector of 10,000 elements creates an archive of %u bytes",
        [data length]];
    v2 = [NSUnarchiver unarchiveObjectWithData: data];
    [data release];
    if (![v1 isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
#if defined(HAVE_KEYED_ARCHIVES)
    archData = [NSKeyedArchiver archivedDataWithRootObject: v1];
    [self logMessage: "A boolean vector of 10,000 elements creates a keyed archive of %u bytes",
        [archData length]];
    v2 = [NSKeyedUnarchiver unarchiveObjectWithData: archData];
    if (![v1 isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
#endif
    [v1 release];
}

- (void) testConvenienceAllocators
{
    OLBoolVector* bv;
    OLBoolVector* bv2;

    bv = [OLBoolVector boolVector];
    if (![bv isMemberOfClass: [OLBoolVector class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLBoolVector class]->name, [bv class]->name];
    }
    if (![bv empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vector should be empty"];
    }
    [bv pushBack: YES];
    [bv pushBack: NO];
    [bv pushBack: YES];
    bv2 = [OLBoolVector boolVectorFrom: [bv begin] to: [bv end]];
    if (![bv isEqual: bv2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
    [bv pushBack: NO];
    [bv pushBack: YES];
    bv2 = [OLBoolVector boolVectorWithBoolVector: bv];
    if (![bv isEqual: bv2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
    bv = [OLBoolVector boolVectorWithCapacity: 7000];
    if (![bv isMemberOfClass: [OLBoolVector class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLBoolVector class]->name, [bv class]->name];
    }
    if ([bv capacity] < 7000)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected at least 7000, but got %u", [bv capacity]];
    }
    if (![bv empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vector should be empty"];
    }
    bv = [OLBoolVector boolVectorWithSize: 5234 filledWithBool: YES];
    if (![bv isMemberOfClass: [OLBoolVector class]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected \"%s\", but got \"%s\"",
            [OLBoolVector class]->name, [bv class]->name];
    }
    if ([bv size] != 5234)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 5234, but got %u", [bv size]];
    }
}

- (void) testErase
{
    OLBoolVector* v;
    OLBitIterator* itor;
    OLBitIterator* itor2;
    int i;

    v = [[OLBoolVector alloc] init];
    [v pushBack: YES];
    [v pushBack: NO];
    [v popBack];
    if ([v size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", [v size]];
    }
    if (![v at: 0])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    itor = [v begin];
    [v insertAt: itor count: 4 filledWith: NO];
    itor = [v end];
    [v insertAt: itor count: 5 filledWith: NO];
    itor = [v begin];
    [itor advanceBy: 4];
    itor = [v erase: itor];
    if ([v size] != 9)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 9, but got %i", [v size]];
    }
    for (i = 0; i < 9; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected NO, but got YES at index %i", i];
        }
    }
    [v insertAt: itor count: 4 filledWith: YES];
    itor = [v begin];
    [itor advanceBy: 4];
    itor2 = [itor copy];
    [itor2 advanceBy: 4];
    [v eraseFrom: itor to: itor2];
    [itor2 release];
    if ([v size] != 9)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 9, but got %i", [v size]];
    }
    for (i = 0; i < 9; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected NO, but got YES at index %i", i];
        }
    }
    [v release];
}

- (void) testInsert
{
    OLBoolVector* v;
    OLBoolVector* v2;
    int i;
    OLBitIterator* itor;

    v = [[OLBoolVector alloc] init];
    itor = [v insertAt: [v begin] value: YES];
    if ([v size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", [v size]];
    }
    if (![v at: 0])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Wrong value at index 0"];
    }
    if (![itor isEqual: [v begin]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The iterator should be equal to [v begin]"];
    }
    [v release];

    v = [[OLBoolVector alloc] init];
    [v insertAt: [v begin] count: 10000 filledWith: YES];
    if ([v size] != 10000)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 10000, got %u", [v size]];
    }
    for (i = 0; i < 10000; i++)
    {
        if (![v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Did not get the right value at index %i", i];
        }
    }
    itor = [v begin];
    [itor advanceBy: 5];
    itor = [v insertAt: itor value: NO];
    if ([v size] != 10001)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 10001, got %u", [v size]];
    }
    if ([[itor dereference] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected NO, but got YES"];
    }
    [itor reverse];
    if (![[itor dereference] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }
    [itor advanceBy: 2];
    if (![[itor dereference] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, but got NO"];
    }

    v2 = [[OLBoolVector alloc] init];
    [v2 pushBack: YES];
    [v2 pushBack: YES];
    if ([v2 size] != 2)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 4, got %u", [v2 size]];
    }
    if (![v2 at: 0])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, got NO"];
    }
    if (![v2 at: 1])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, got NO"];
    }

    [v insertAt: itor from: [v2 begin] to: [v2 end]];
    [v2 release];
    if ([v size] != 10003)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 10003, got %u", [v size]];
    }
    itor = [v begin];
    [itor advanceBy: 6];
    if (![[itor dereference] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, got NO"];
    }
    [itor advance];
    if (![[itor dereference] boolValue])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected YES, got NO"];
    }
    [v release];
}

- (void) testInitializers
{
    OLBoolVector* v;
    OLBoolVector* v2;
    int i;

    v = [[OLBoolVector alloc] init];
    if ([v size] != 0 || [v capacity] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size and capacity should be 0."];
    }
    [v release];

    v = [[OLBoolVector alloc] initWithCapacity: 10];
    if ([v size] != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 0."];
    }
    [v release];

    v = [[OLBoolVector alloc] initWithSize: 1 filledWithBool: YES];
    if ([v size] != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 1."];
    }
    if (![v at: 0])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Did not get the right value at index 0"];
    }
    [v release];

    v = [[OLBoolVector alloc] initWithSize: 100 filledWithBool: NO];
    if ([v size] != 100)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 100, got %u", [v size]];
    }
    for (i = 0; i < 100; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Did not get the right value at index %i", i];
        }
    }

    v2 = [[OLBoolVector alloc] initWithBoolVector: v];
    if ([v2 size] != 100)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 100, got %u", [v2 size]];
    }
    for (i = 0; i < 100; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Did not get the right value at index %i", i];
        }
    }
    if (![v isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
    [v2 release];

    v2 = [[OLBoolVector alloc] initFrom: [v begin] to: [v end]];
    if ([v2 size] != 100)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size should be 100, got %u", [v2 size]];
    }
    for (i = 0; i < 100; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Did not get the right value at index %i", i];
        }
    }
    if (![v isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors should be equal"];
    }
    [v2 release];
    [v release];
}

- (void) testIterators
{
    OLBoolVector* v;
    OLBitIterator* itor;
    OLBitIterator* begin;
    OLBitIterator* end;
    OLReverseRandomIterator* rbegin;
    OLReverseRandomIterator* rend;
    BOOL vals[] = { YES, NO, YES, NO, YES };
    int i;
    int diff;

    v = [[OLBoolVector alloc] init];
    for (i = 0; i < 5; i++)
        [v pushBack: vals[i]];
    for (itor = [v begin], end = [v end], i = 0; ![itor isEqual: end]; [itor advance], i++)
    {
        if ([[itor dereference] boolValue] != vals[i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i",
                vals[i], [[itor dereference] boolValue]];
        }
    }
    for ([itor reverse], i = 4; i >= 0; [itor reverse], i--)
    {
        if ([[itor dereference] boolValue] != vals[i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i",
                vals[i], [[itor dereference] boolValue]];
        }
    }
    [itor advanceBy: 2];
    begin = [v begin];
    diff = [itor difference: begin];
    if (diff != 1)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 1, but got %i", diff];
    }
    if ([[itor dereference] boolValue] != NO)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i",
            [[itor dereference] boolValue]];
    }
    [itor advanceBy: 2];
    [itor advanceBy: -3];
    diff = [itor difference: begin];
    if (diff != 0)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 0, but got %i", diff];
    }
    if ([[itor dereference] boolValue] != YES)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected %i, but got %i",
            YES, [[itor dereference] boolValue]];
    }
    if (![itor isEqual: begin])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The iterator should be equal to begin"];
    }
    for (rbegin = [v rbegin], rend = [v rend], i = 4;
         ![rbegin isEqual: rend];
         [rbegin advance], i--)
    {
        if ([[rbegin dereference] boolValue] != vals[i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i",
                vals[i], [[itor dereference] boolValue]];
        }
    }
    [v release];
}

- (void) testProperties
{
    OLBoolVector* v;
    OLBoolVector* v2;
    BOOL vals[] = { YES, NO, YES, NO, YES };
    int i;

    v = [[OLBoolVector alloc] init];
    if (![v empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vector must report that it is empty"];
    }
    for (i = 0; i < 5; i++)
        [v pushBack: vals[i]];
    v2 = [[OLBoolVector alloc] init];
    for (i = 0; i < 5; i++)
        [v2 pushBack: vals[i]];
    if (![v isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors must be equal"];
    }
    if ([v size] != 5)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The size must be exactly 5"];
    }
    if ([v empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vector cannot report that it is empty"];
    }
    [v clear];
    if (![v empty])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vector must report that it is empty"];
    }
    if (![[v allocator] isEqual: [OLAllocator defaultAllocator]])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([v allocator] != [OLAllocator defaultAllocator])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The allocator is incorrect"];
    }
    if ([v isEqual: v2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The vectors must not be equal"];
    }
    if ([v maxSize] != [v2 maxSize])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "All vectors must have the same max size"];
    }
    [self logMessage: "The max size of a boolean vector is %u", [v maxSize]];
    [v release];
    [v2 release];
}

- (void) testReserveResizeSwap
{
    OLBoolVector* v;
    OLBoolVector* v2;
    OLBoolVector* vCopy;
    OLBoolVector* v2Copy;
    int nums[] = { YES, NO, YES, NO, YES };
    int i;

    v = [[OLBoolVector alloc] init];
    for (i = 0; i < 5; i++)
        [v pushBack: nums[i]];
    [v reserve: 1000];
    if ([v capacity] < 1000)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "The capacity must be at least 1000, got %u", [v capacity]];
    }
    for (i = 0; i < 5; i++)
    {
        if ([v at: i] != nums[i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected %i, but got %i", nums[i], [v at: i]];
        }
    }
    v2 = [v copy];
    [v resize: 100 filledWith: NO];
    if ([v size] != 100)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 100, but got %i", [v size]];
    }
    for (i = 5; i < 100; i++)
    {
        if ([v at: i])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected NO, but got YES"];
        }
    }
    vCopy = [v copy];
    v2Copy = [v2 copy];
    [v swap: v2];
    if (![v isEqual: v2Copy])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected the vectors to be equal"];
    }
    if (![v2 isEqual: vCopy])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected the vectors to be equal"];
    }
    [v release];
    [v2 release];
    [vCopy release];
    [v2Copy release];
}

@end
