//
//  UnitTestBitSet.m
//  BitSet
//
//  Created by frankcastellucci on 1/3/11.
//  Copyright 2011 Axiom:1, Inc. All rights reserved.
//

#import "UnitTestBitArrayBase.h"


@implementation UnitTestBitArrayBase

- (void) setUp {
	[super setUp];
	bit0 = [[A1BitArray alloc]init];
}

- (void) tearDown {
	[bit0 release];

	[super tearDown];
}

/*!
 @function: testInit0
 Test initialization of default BitArray object
 */
- (void) testInit0 {
	STAssertNotNil(bit0,@"Could not create basic BitArray");
	STAssertTrue([bit0 dirty],@"Dirty flag not set on creation");
	STAssertTrue([bit0 bytesInBase] == 128,@"Number of bytes in base should be 128");
	STAssertTrue([bit0 bitsInBase] == 1024,@"Number if bits in base should be 1024");
	STAssertFalse([bit0 extentsAllowed],@"Extents should not be allowed for basic initialization");
	STAssertTrue([bit0 extentCount]==0,@"Should be ZERO extents allocated on init");
	STAssertTrue([bit0 bytesInExtent] == 0,@"Number of bytes in extent should be 0");
	STAssertTrue([bit0 bitsInExtent] == 0,@"Number if bits in extent should be 0");
}

/*!
 @function: testBase0
 Tests for default BitArray object
 */
- (void) testBase0 {
	NSError	*err=nil;
	STAssertFalse([bit0 bit:121 error:&err],@"Expected FALSE after initialization");
	
	[bit0 setBit:121 error:&err];
	STAssertTrue([bit0 bit:121 error:&err],@"Expected TRUE after setting");
	
	[bit0 clearBit:121 error:&err];
	STAssertFalse([bit0 bit:121 error:&err],@"Expected FALSE after clearing");
}

#define	TEST_BIT_1023	1023
- (void) testBase0Boundary {
	NSError	*err=nil;
	STAssertFalse([bit0 bit:TEST_BIT_1023 error:&err],@"Expected FALSE after initialization");
	[bit0 setBit:TEST_BIT_1023 error:&err];
	STAssertTrue([bit0 bit:TEST_BIT_1023 error:&err],@"Expected TRUE after setting");
	[bit0 clearBit:TEST_BIT_1023 error:&err];
	STAssertFalse([bit0 bit:TEST_BIT_1023 error:&err],@"Expected FALSE after clearing");	
}

- (void) testBase0Errors {
	NSError	*err=nil;
	[bit0 bit:1201 error:&err];
	STAssertNotNil(err,@"Should have extent error");
	err=nil;
	[bit0 setBit:1201 error:&err];
	STAssertNotNil(err,@"Should have extent error");
	err=nil;
	[bit0 clearBit:1201 error:&err];
	STAssertNotNil(err,@"Should have extent error");	
}

- (void) testBase0Persist {
	NSError	*err=nil;
	STAssertFalse([bit0 bit:121 error:&err],@"Expected FALSE after initialization");
	[bit0 setBit:121 error:&err];
	STAssertTrue([bit0 bit:121 error:&err],@"Expected TRUE after setting");
	[NSKeyedArchiver archiveRootObject:bit0 toFile:@"testBase0Persist.archive"];
	STAssertFalse([bit0 dirty],@"Dirty flag should be false after persist");
	[bit0 clearBit:121 error:&err];
	STAssertFalse([bit0 bit:121 error:&err],@"Expected FALSE after clearing");
	A1BitArray	*bit01 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"testBase0Persist.archive"];
	STAssertTrue([bit01 bit:121 error:&err],@"Expected bit 121 to be TRUE after file read");
}

- (void) testBase0Performance {
	NSError		*err=nil;
	NSUInteger	bits = [bit0 bitsInBase];
	for(NSUInteger x=0;x<bits;x++) {
		[bit0 bit:x error:&err];
		[bit0 setBit:x error:&err];
		[bit0 clearBit:x error:&err];
	}
	for(NSInteger x=bits-1;x>=0;--x) {
		[bit0 bit:x error:&err];
		[bit0 setBit:x error:&err];
		[bit0 clearBit:x error:&err];		
	}		
}

@end
