Skip to content

Commit

Permalink
Implemented a bunch of test cases for AQBitfield.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Dovey committed Jun 20, 2011
1 parent 2fc148e commit 805a1de
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 9 deletions.
6 changes: 6 additions & 0 deletions AQAppStateMachine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3821C49D13AFC43D00175CEE /* AQBitfieldTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3821C49C13AFC43D00175CEE /* AQBitfieldTests.m */; };
38431B5813A7C26800178A7E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 38431B5713A7C26800178A7E /* Foundation.framework */; };
38431B6313A7C26800178A7E /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 38431B6213A7C26800178A7E /* SenTestingKit.framework */; };
38431B6613A7C26900178A7E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 38431B5713A7C26800178A7E /* Foundation.framework */; };
Expand Down Expand Up @@ -102,6 +103,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3821C49B13AFC43D00175CEE /* AQBitfieldTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AQBitfieldTests.h; sourceTree = "<group>"; };
3821C49C13AFC43D00175CEE /* AQBitfieldTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AQBitfieldTests.m; sourceTree = "<group>"; };
38431B5413A7C26800178A7E /* libAQAppStateMachine.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libAQAppStateMachine.a; sourceTree = BUILT_PRODUCTS_DIR; };
38431B5713A7C26800178A7E /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
38431B5B13A7C26800178A7E /* AQAppStateMachine-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AQAppStateMachine-Prefix.pch"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -262,6 +265,8 @@
children = (
38431B7213A7C26900178A7E /* AQAppStateMachineTests.h */,
38431B7413A7C26900178A7E /* AQAppStateMachineTests.m */,
3821C49B13AFC43D00175CEE /* AQBitfieldTests.h */,
3821C49C13AFC43D00175CEE /* AQBitfieldTests.m */,
386693EF13AF8A1500268560 /* SortedDictionary */,
38431B6D13A7C26900178A7E /* Supporting Files */,
);
Expand Down Expand Up @@ -561,6 +566,7 @@
3866945713AF8A1F00268560 /* MutableSortedDictionary.m in Sources */,
3866945A13AF8A1F00268560 /* SortedDictionary.m in Sources */,
3866946013AFB77500268560 /* AQAdditions.m in Sources */,
3821C49D13AFC43D00175CEE /* AQBitfieldTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 5 additions & 0 deletions AQAppStateMachine/AQBitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ typedef CFBit AQBit;
- (BOOL) bitsInRange: (NSRange) range maskedWith: (NSUInteger) mask matchBits: (NSUInteger) bits;
- (BOOL) bitsInRange: (NSRange) range maskedWith: (AQBitfield *) mask equalToBitfield: (AQBitfield *) bitfield;

- (void) shiftBitsLeftBy: (NSUInteger) bits;
- (void) shiftBitsRightBy: (NSUInteger) bits;

- (void) maskWithBits: (AQBitfield *) mask;

@end
83 changes: 74 additions & 9 deletions AQAppStateMachine/AQBitfield.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ - (NSUInteger) count

- (NSUInteger) countOfBit: (AQBit) bit inRange: (NSRange) range
{
return ( [_storage countOfIndexesInRange: range] );
if ( bit )
return ( [_storage countOfIndexesInRange: range] );
else
return ( range.length - [_storage countOfIndexesInRange: range] );
}

- (BOOL) containsBit: (AQBit) bit inRange: (NSRange) range
Expand Down Expand Up @@ -112,8 +115,31 @@ - (NSUInteger) firstIndexOfBit: (AQBit) bit
if ( bit )
return ( [_storage firstIndex] );

// TODO: Search for negative space
return ( 0 );
__block NSUInteger result = 0;

if ( [_storage respondsToSelector: @selector(enumerateRangesUsingBlock:)] )
{
[_storage enumerateRangesUsingBlock: ^(NSRange range, BOOL *stop) {
if ( range.location == 0 )
result = NSMaxRange(range)+1;
*stop = YES;
}];
}
else
{
__block NSUInteger lastHighest = 0;
[_storage enumerateIndexesUsingBlock: ^(NSUInteger idx, BOOL *stop) {
if ( idx - lastHighest > 1 )
{
result = lastHighest+1;
*stop = YES;
}

lastHighest = idx;
}];
}

return ( result );
}

- (NSUInteger) lastIndexOfBit: (AQBit) bit
Expand Down Expand Up @@ -174,7 +200,7 @@ - (NSMutableIndexSet *) _zeroBasedIndexSetForIndexesInRange: (NSRange) range

- (BOOL) bitsInRange: (NSRange) range matchBits: (NSUInteger) bits
{
NSParameterAssert(range.length <= sizeof(NSUInteger));
NSParameterAssert(range.length <= sizeof(NSUInteger)*8);
if ( range.length == 0 )
return ( NO );

Expand All @@ -194,7 +220,7 @@ - (BOOL) bitsInRange: (NSRange) range matchBits: (NSUInteger) bits

- (BOOL) bitsInRange: (NSRange) range equalToBitfield: (AQBitfield *) bitfield
{
NSParameterAssert(range.length == bitfield.count);
NSParameterAssert(range.length <= bitfield.count);
if ( range.length == 0 )
return ( NO );

Expand All @@ -204,7 +230,7 @@ - (BOOL) bitsInRange: (NSRange) range equalToBitfield: (AQBitfield *) bitfield

- (BOOL) bitsInRange: (NSRange) range maskedWith: (NSUInteger) mask matchBits: (NSUInteger) bits
{
NSParameterAssert(range.length <= sizeof(NSUInteger));
NSParameterAssert(range.length <= sizeof(NSUInteger)*8);
if ( range.length == 0 )
return ( NO );

Expand All @@ -228,18 +254,57 @@ - (BOOL) bitsInRange: (NSRange) range maskedWith: (NSUInteger) mask matchBits: (

- (BOOL) bitsInRange: (NSRange) range maskedWith: (AQBitfield *) mask equalToBitfield: (AQBitfield *) bitfield
{
NSParameterAssert(range.length == bitfield.count);
NSParameterAssert(range.length == mask.count);
NSParameterAssert(range.length <= bitfield.count);
NSParameterAssert(range.length <= mask.count);
if ( range.length == 0 )
return ( NO );

NSMutableIndexSet * tmp = [self _zeroBasedIndexSetForIndexesInRange: range];
NSMutableIndexSet * test = [bitfield mutableCopy];
NSMutableIndexSet * test = [bitfield->_storage mutableCopy];

[tmp removeIndexes: mask->_storage];
[test removeIndexes: mask->_storage];

return ( [tmp isEqualToIndexSet: test] );
}

- (void) shiftBitsLeftBy: (NSUInteger) bits
{
NSInteger shift = 0 - (NSInteger)bits;
[_storage shiftIndexesStartingAtIndex: bits by: shift];
}

- (void) shiftBitsRightBy: (NSUInteger) bits
{
[_storage shiftIndexesStartingAtIndex: 0 by: (NSInteger)bits];
}

- (void) maskWithBits: (AQBitfield *) mask
{
NSRange range = NSMakeRange(0, MIN(self.count, mask.count));

if ( [mask->_storage respondsToSelector: @selector(enumerateRangesUsingBlock:)] )
{
__block NSUInteger negativeRangeLocation = 0;
[mask->_storage enumerateRangesInRange: range options: 0 usingBlock: ^(NSRange range, BOOL *stop) {
if ( range.location > negativeRangeLocation )
{
[_storage removeIndexesInRange: NSMakeRange(negativeRangeLocation, range.location - negativeRangeLocation)];
}
negativeRangeLocation = NSMaxRange(range);
}];

if ( negativeRangeLocation < [_storage lastIndex] )
{
[_storage removeIndexesInRange: NSMakeRange(negativeRangeLocation, NSUIntegerMax-negativeRangeLocation)];
}
}
else
{
[mask->_storage enumerateIndexesInRange: range options: 0 usingBlock: ^(NSUInteger idx, BOOL *stop) {
// TODO: Implement
}];
}
}

@end
21 changes: 21 additions & 0 deletions AQAppStateMachineTests/AQBitfieldTests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// AQBitfieldTests.h
// AQAppStateMachine
//
// Created by Jim Dovey on 11-06-20.
// Copyright 2011 Jim Dovey. All rights reserved.
//
// See Also: http://developer.apple.com/iphone/library/documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html

// Application unit tests contain unit test code that must be injected into an application to run correctly.
// Define USE_APPLICATION_UNIT_TEST to 0 if the unit test code is designed to be linked into an independent test executable.

#define USE_APPLICATION_UNIT_TEST 0

#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
//#import "application_headers" as required

@interface AQBitfieldTests : SenTestCase

@end
Loading

0 comments on commit 805a1de

Please sign in to comment.