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 "ACNumber.h" 12#import <CoreFoundation/CoreFoundation.h> 13#import <CoreFoundation/CFBitVector.h> 14 15@implementation ANTLRBitSetTest 16 17-(void) testWithBitData 18{ 19 static const unsigned long long bitData[] = {3LL, 1LL}; 20 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSetWithBits:bitData Count:2]; 21 CFIndex actual = (CFIndex)[bitSet numBits]; 22 CFIndex expected = 3; 23 24 STAssertEquals(actual, expected, @"There should be three bits set in bitvector. But I have %d", actual); 25 [bitSet release]; 26} 27 28-(void) testWithBitArray 29{ 30 AMutableArray *bits = [AMutableArray arrayWithCapacity:10]; 31 [bits addObject:[ACNumber numberWithBool:YES]]; 32 [bits addObject:[ACNumber numberWithBool:YES]]; 33 [bits addObject:[ACNumber numberWithBool:NO]]; 34 [bits addObject:[ACNumber numberWithBool:YES]]; 35 [bits addObject:[ACNumber numberWithBool:NO]]; 36 [bits addObject:[ACNumber numberWithBool:YES]]; 37 STAssertTrue([[bits objectAtIndex:0] boolValue], @"Value at index 0 was not true"); 38 STAssertTrue([[bits objectAtIndex:1] boolValue], @"Value at index 1 was not true"); 39 STAssertFalse([[bits objectAtIndex:2] boolValue], @"Value at index 2 was not false"); 40 STAssertTrue([[bits objectAtIndex:3] boolValue], @"Value at index 3 was not true"); 41 STAssertFalse([[bits objectAtIndex:4] boolValue], @"Value at index 4 was not false"); 42 STAssertTrue([[bits objectAtIndex:5] boolValue], @"Value at index 5 was not true"); 43 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSetWithArray:bits]; 44 CFIndex actual = (CFIndex)[bitSet numBits]; 45 CFIndex expected = 4; 46 STAssertEquals(actual, expected, @"There should be four bits set in bitvector. But I have %d", actual); 47 [bitSet release]; 48} 49 50-(void) testAdd 51{ 52 53 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSet]; 54 [bitSet add:1]; 55 [bitSet add:2]; 56 [bitSet add:3]; 57 CFIndex actual = (CFIndex)[bitSet numBits]; 58 CFIndex expected = 3; 59 STAssertEquals(actual, expected, @"There should be three bits set in bitvector. But I have %d", actual); 60 [bitSet release]; 61} 62 63-(void) testRemove 64{ 65 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSet]; 66 [bitSet add:1]; 67 CFIndex actual = (CFIndex)[bitSet numBits]; 68 CFIndex expected = 1; 69 STAssertTrue(actual == expected, @"Bitset was not of size 1"); 70 STAssertTrue([bitSet member:1], @"Bit at index 1 is not a member..."); 71 [bitSet remove:1]; 72 actual = [bitSet numBits]; 73 STAssertTrue(actual == 0, @"Bitset was not empty"); 74 STAssertFalse([bitSet member:1], @"Bit at index 1 is a member..."); 75 STAssertTrue([bitSet isNil], @"There was at least one bit on..."); 76} 77 78-(void) testCopyBitSet 79{ 80 static const unsigned long long bitData[] = {3LL, 1LL}; 81 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSetWithBits:bitData Count:2]; 82 ANTLRBitSet *copy = [bitSet mutableCopyWithZone:nil]; 83 CFIndex actual = (CFIndex)[copy numBits]; 84 STAssertEquals(actual, (CFIndex)[bitSet numBits], @"There should be three bits set in bitvector. But I have %d", [copy numBits]); 85 [bitSet release]; 86} 87 88-(void) testOr 89{ 90 static const unsigned long long bitData[] = {3LL, 1LL}; 91 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSetWithBits:bitData Count:2]; 92 93 static const unsigned long long otherData[] = {5LL, 3LL, 1LL}; 94 ANTLRBitSet *otherBitSet = [ANTLRBitSet newBitSetWithBits:otherData Count:3]; 95 96 ANTLRBitSet *c = [bitSet or:otherBitSet]; 97 STAssertTrue([c size] == [otherBitSet size], @"c should be the same as otherBitSet"); 98} 99 100-(void) testOrInPlace 101{ 102 103 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSet]; 104 [bitSet add:1]; 105 [bitSet add:2]; 106 [bitSet add:16]; 107 CFIndex actual = (CFIndex)[bitSet numBits]; 108 CFIndex expected = 3; 109 STAssertEquals(actual, expected, @"There should be three bits set in bitvector. But I have %d", actual); 110 ANTLRBitSet *followSet = [ANTLRBitSet newBitSet]; 111 [followSet orInPlace:bitSet]; 112 actual = (CFIndex)[followSet numBits]; 113 expected = 3; 114 NSLog( @"%@\n", [followSet description] ); 115 STAssertEquals(actual, expected, @"There should be three bits set in bitvector. But I have %d", actual); 116 [bitSet release]; 117 [followSet release]; 118} 119 120-(void) testDescription 121{ 122 ANTLRBitSet *bitSet = [ANTLRBitSet newBitSet]; 123 [bitSet add:1]; 124 [bitSet add:2]; 125 NSMutableString *aDescription = (NSMutableString *)[bitSet description]; 126 STAssertTrue([aDescription isEqualToString:@"{1,2}"], @"Description was not right, expected '{1,2}' got: %@", aDescription); 127} 128 129@end 130