Permalink
Browse files

Some tests and fixes for equality matchers.

  • Loading branch information...
Jim Dovey
Jim Dovey committed Jun 29, 2011
1 parent 9032503 commit 9acf5c6a24e153532b30abc30ce201c841f845b8
@@ -277,6 +277,20 @@ typedef CFBit AQBit;
*/
- (AQBitfield *) bitfieldUsingMask: (AQBitfield *) mask;
+/**
+ Obtain a bitfield matching the receiver with all bits shifted to the left.
+ @param bits The number of places to shift.
+ @return A new bitfield instance.
+ */
+- (AQBitfield *) bitfieldFromLeftShiftingBy: (NSUInteger) bits;
+
+/**
+ Obtain a bitfield matching the receiver with all bits shifted to the roght.
+ @param bits The number of places to shift.
+ @return A new bitfield instance.
+ */
+- (AQBitfield *) bitfieldFromRightShiftingBy: (NSUInteger) bits;
+
@end
/**
@@ -501,6 +501,28 @@ - (AQBitfield *) bitfieldUsingMask: (AQBitfield *) mask
#endif
}
+- (AQBitfield *) bitfieldFromLeftShiftingBy: (NSUInteger) bits
+{
+ AQBitfield * result = [self copy];
+ [result shiftBitsLeftBy: bits];
+#if USING_ARC
+ return ( result );
+#else
+ return ( [result autorelease] );
+#endif
+}
+
+- (AQBitfield *) bitfieldFromRightShiftingBy: (NSUInteger) bits
+{
+ AQBitfield * result = [self copy];
+ [result shiftBitsRightBy: bits];
+#if USING_ARC
+ return ( result );
+#else
+ return ( [result autorelease] );
+#endif
+}
+
@end
@implementation NSIndexSet (AQBitfieldCreation)
@@ -76,14 +76,15 @@ - (id)initWithRanges: (NSArray *) ranges masks: (NSArray *) masks matchingValues
if ( [masks count] > idx )
mask = [masks objectAtIndex: idx];
- if ( mask == nil )
- {
- [_value unionWithBitfield: obj];
- }
- else
- {
- [_value unionWithBitfield: [obj bitfieldUsingMask: mask]];
- }
+ AQBitfield * modified = [obj copy];
+ if ( mask != nil )
+ [modified maskWithBits: mask];
+
+ NSRange range = [[ranges objectAtIndex: idx] range];
+ if ( range.location != 0 )
+ [modified shiftBitsRightBy: range.location];
+
+ [_value unionWithBitfield: modified];
}];
}
@@ -35,6 +35,7 @@
#import "AQStateMatchingDescriptorTests.h"
#import "AQStateMaskMatchingDescriptor.h"
+#import "AQStateMaskedEqualityMatchingDescriptor.h"
#import "AQBitfield.h"
#import "AQRange.h"
@@ -109,4 +110,18 @@ - (void) testFullRange
STAssertTrue(NSEqualRanges(totalRange, desc.fullRange), @"Expected total range of %@ to be %@, got %@", desc, NSStringFromRange(totalRange), NSStringFromRange(desc.fullRange));
}
+- (void) testSimpleEquality
+{
+ AQStateMaskedEqualityMatchingDescriptor * desc = [[AQStateMaskedEqualityMatchingDescriptor alloc] initWith32BitValue: 0xFF700055 forRange: NSMakeRange(32, 32)];
+ AQBitfield * bitfield = [[AQBitfield alloc] initWith64BitField: 0xFF700055FF00FF00];
+
+ STAssertTrue([desc matchesBitfield: bitfield], @"Expected equality descriptor %@ to match bitfield %@", desc, bitfield);
+
+ [bitfield flipBitAtIndex: 20];
+ STAssertTrue([desc matchesBitfield: bitfield], @"Expected equality descriptor %@ to match bitfield %@", desc, bitfield);
+
+ [bitfield flipBitAtIndex: 40];
+ STAssertFalse([desc matchesBitfield: bitfield], @"Expected equality descriptor %@ NOT to match bitfield %@", desc, bitfield);
+}
+
@end

0 comments on commit 9acf5c6

Please sign in to comment.