1 // 2 // ANTLRBitSetTest.m 3 // ANTLR 4 // 5 // Created by Ian Michell on 13/05/2010. 6 // Copyright 2010 Ian Michell and Alan Condit. All rights reserved. 7 // 8 9 #import "ANTLRBitSetTest.h" 10 #import "ANTLRBitSet.h" 11 #import <CoreFoundation/CoreFoundation.h> 12 #import <CoreFoundation/CFBitVector.h> 13 14 @implementation ANTLRBitSetTest 15 16 -(void) testWithBitData 17 { 18 static const unsigned long long bitData[] = {3LL, 1LL}; 19 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSetWithBits:bitData Count:2]; 20 CFIndex actual = (CFIndex)[bitSet numBits]; 21 CFIndex expected = 3; 22 23 STAssertEquals(actual, expected, @"There should be three bits set in bitvector. But I have %d", actual); 24 [bitSet release]; 25 } 26 27 -(void) testWithBitArray 28 { 29 AMutableArray *bits = [AMutableArray arrayWithCapacity:10]; 30 [bits addObject:[NSNumber numberWithBool:YES]]; 31 [bits addObject:[NSNumber numberWithBool:YES]]; 32 [bits addObject:[NSNumber numberWithBool:NO]]; 33 [bits addObject:[NSNumber numberWithBool:YES]]; 34 [bits addObject:[NSNumber numberWithBool:NO]]; 35 [bits addObject:[NSNumber numberWithBool:YES]]; 36 STAssertTrue([[bits objectAtIndex:0] boolValue], @"Value at index 0 was not true"); 37 STAssertTrue([[bits objectAtIndex:1] boolValue], @"Value at index 1 was not true"); 38 STAssertFalse([[bits objectAtIndex:2] boolValue], @"Value at index 2 was not false"); 39 STAssertTrue([[bits objectAtIndex:3] boolValue], @"Value at index 3 was not true"); 40 STAssertFalse([[bits objectAtIndex:4] boolValue], @"Value at index 4 was not false"); 41 STAssertTrue([[bits objectAtIndex:5] boolValue], @"Value at index 5 was not true"); 42 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSetWithArray:bits]; 43 CFIndex actual = (CFIndex)[bitSet numBits]; 44 CFIndex expected = 4; 45 STAssertEquals(actual, expected, @"There should be four bits set in bitvector. But I have %d", actual); 46 [bitSet release]; 47 } 48 49 -(void) testAdd 50 { 51 52 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSet]; 53 [bitSet add:1]; 54 [bitSet add:2]; 55 [bitSet add:3]; 56 CFIndex actual = (CFIndex)[bitSet numBits]; 57 CFIndex expected = 3; 58 STAssertEquals(actual, expected, @"There should be three bits set in bitvector. But I have %d", actual); 59 [bitSet release]; 60 } 61 62 -(void) testRemove 63 { 64 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSet]; 65 [bitSet add:1]; 66 CFIndex actual = (CFIndex)[bitSet numBits]; 67 CFIndex expected = 1; 68 STAssertTrue(actual == expected, @"Bitset was not of size 1"); 69 STAssertTrue([bitSet member:1], @"Bit at index 1 is not a member..."); 70 [bitSet remove:1]; 71 actual = [bitSet numBits]; 72 STAssertTrue(actual == 0, @"Bitset was not empty"); 73 STAssertFalse([bitSet member:1], @"Bit at index 1 is a member..."); 74 STAssertTrue([bitSet isNil], @"There was at least one bit on..."); 75 } 76 77 -(void) testCopyBitSet 78 { 79 static const unsigned long long bitData[] = {3LL, 1LL}; 80 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSetWithBits:bitData Count:2]; 81 ANTLRBitSet *copy = [bitSet mutableCopyWithZone:nil]; 82 CFIndex actual = (CFIndex)[copy numBits]; 83 STAssertEquals(actual, (CFIndex)[bitSet numBits], @"There should be three bits set in bitvector. But I have %d", [copy numBits]); 84 [bitSet release]; 85 } 86 87 -(void) testOr 88 { 89 static const unsigned long long bitData[] = {3LL, 1LL}; 90 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSetWithBits:bitData Count:2]; 91 92 static const unsigned long long otherData[] = {5LL, 3LL, 1LL}; 93 ANTLRBitSet *otherBitSet = [ANTLRBitSet newANTLRBitSetWithBits:otherData Count:3]; 94 95 ANTLRBitSet *c = [bitSet or:otherBitSet]; 96 STAssertTrue([c size] == [otherBitSet size], @"c should be the same as otherBitSet"); 97 } 98 99 -(void) testDescription 100 { 101 ANTLRBitSet *bitSet = [ANTLRBitSet newANTLRBitSet]; 102 [bitSet add:1]; 103 [bitSet add:2]; 104 NSMutableString *aDescription = (NSMutableString *)[bitSet description]; 105 STAssertTrue([aDescription isEqualToString:@"{1,2}"], @"Description was not right, expected '{1,2}' got: %@", aDescription); 106 } 107 108 @end 109