libs-base/Tests/base/NSMutableIndexSet/blocks.m
thebeing a5c6cfa9f7 Detect the presence of libdispatch and, if available, use it for collection
enumeration methods that take blocks as arguments. This allows us to implement
the NSEnumerationConcurrent option of those methods with minimal effort.

The searching methods on NSIndexSet and all the sorting methods are still
missing.



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@35010 72102866-910b-0410-8b05-ffd578937521
2012-03-27 17:05:19 +00:00

88 lines
2.8 KiB
Objective-C

#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSIndexSet.h>
#import <Foundation/NSLock.h>
#include <Testing.h>
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableIndexSet *set = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(1,5)];
[set addIndex:20];
[set addIndex:25];
START_SET("NSIndexSet Blocks")
# ifndef __has_feature
# define __has_feature(x) 0
# endif
# if __has_feature(blocks)
NSMutableIndexSet *referenceSet = nil;
__block NSMutableIndexSet *newSet = [NSMutableIndexSet indexSet];
__block BOOL didContainWrongIndex = NO;
__block NSUInteger lastIndex = NSNotFound;
NSLock *setLock = [NSLock new];
void(^enumBlock)(NSUInteger,BOOL*) = ^(NSUInteger idx, BOOL*shouldStop){
[setLock lock];
lastIndex = idx;
[newSet addIndex: idx];
[setLock unlock];
};
// Test forward enumeration:
[set enumerateIndexesUsingBlock: enumBlock];
PASS((YES == [set isEqual: newSet]),
"Can enumerate all indices in an index set using a block");
PASS((25 == lastIndex),
"Forward enumeration stops at the last index");
newSet = [NSMutableIndexSet indexSet];
didContainWrongIndex = NO;
lastIndex = NSNotFound;
// Test reverse enumeration:
[set enumerateIndexesWithOptions: NSEnumerationReverse
usingBlock: enumBlock];
PASS((YES == [set isEqual: newSet]),
"Can reverse enumerate all indices in an index set using a block");
PASS((1 == lastIndex),
"Reverse enumeration stops at the first index");
referenceSet = [NSMutableIndexSet indexSetWithIndexesInRange: (NSMakeRange(4,2))];
[referenceSet addIndex: 20];
newSet = [NSMutableIndexSet indexSet];
didContainWrongIndex = NO;
lastIndex = NSNotFound;
// Test subrange enumeration:
[set enumerateIndexesInRange: NSMakeRange(4,20)
options: 0
usingBlock: enumBlock];
PASS((YES == [referenceSet isEqual: newSet]), "Can enumerate subranges of an index set");
PASS((20 == lastIndex), "Subrange enumeration stops at the correct index");
newSet = [NSMutableIndexSet indexSet];
lastIndex = NSNotFound;
referenceSet = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(1,5)];
[referenceSet addIndex: 20];
enumBlock = ^(NSUInteger idx, BOOL*shouldStop){
[setLock lock];
[newSet addIndex: idx];
[setLock unlock];
if (20 == idx)
{
*shouldStop = YES;
}
};
// Test premature termination of enumeration:
[set enumerateIndexesUsingBlock: enumBlock];
PASS((YES == [referenceSet isEqual: newSet]), "block can prematurely terminate enumeration");
[setLock release];
# else
SKIP("No Blocks support in the compiler.")
# endif
END_SET("NSIndexSet Blocks")
[pool release];
return 0;
}