2012-03-20 20:17:45 +00:00
|
|
|
#import "Testing.h"
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
#import <Foundation/NSDate.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2012-03-27 17:05:19 +00:00
|
|
|
#import <Foundation/NSLock.h>
|
2012-03-20 20:17:45 +00:00
|
|
|
#import <Foundation/NSSet.h>
|
|
|
|
#if defined(GNUSTEP_BASE_LIBRARY)
|
|
|
|
#import <Foundation/NSSerialization.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static NSUInteger fooCount = 0;
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
START_SET("NSDictionary Blocks")
|
|
|
|
# ifndef __has_feature
|
|
|
|
# define __has_feature(x) 0
|
|
|
|
# endif
|
|
|
|
# if __has_feature(blocks)
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
2012-03-27 17:05:19 +00:00
|
|
|
__block NSLock *fooLock = [NSLock new];
|
2012-03-20 20:17:45 +00:00
|
|
|
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"foo",
|
|
|
|
@"key1", @"bar", @"key2", @"foo", @"key3", nil];
|
2012-03-27 17:05:19 +00:00
|
|
|
void(^enumBlock)(id,id,BOOL*) = ^(id key, id obj, BOOL *stop){
|
|
|
|
if ([obj isEqual: @"foo"]){ [fooLock lock]; fooCount++; [fooLock unlock];}};
|
|
|
|
[dict enumerateKeysAndObjectsUsingBlock: enumBlock];
|
2012-03-20 20:17:45 +00:00
|
|
|
PASS((2 == fooCount),
|
|
|
|
"Can enumerate dictionary using a block");
|
2012-03-27 17:05:19 +00:00
|
|
|
fooCount = 0;
|
|
|
|
[dict enumerateKeysAndObjectsWithOptions: NSEnumerationConcurrent
|
|
|
|
usingBlock: enumBlock];
|
|
|
|
PASS((2 == fooCount),
|
|
|
|
"Can enumerate dictionary concurrently using a block");
|
2012-03-20 20:17:45 +00:00
|
|
|
NSSet *fooKeys = [dict keysOfEntriesPassingTest: ^(id key, id obj, BOOL *stop){
|
|
|
|
return [obj isEqual: @"foo"];}];
|
|
|
|
PASS((([fooKeys count] == 2)
|
|
|
|
&& ([fooKeys containsObject: @"key1"])
|
|
|
|
&& ([fooKeys containsObject: @"key3"]))
|
|
|
|
, "Can use blocks as predicates.");
|
2012-03-27 17:05:19 +00:00
|
|
|
[fooLock release];
|
2012-03-20 20:17:45 +00:00
|
|
|
[arp release]; arp = nil;
|
|
|
|
# else
|
|
|
|
SKIP("No Blocks support in the compiler.")
|
|
|
|
# endif
|
|
|
|
END_SET("NSDictionary Blocks")
|
|
|
|
return 0;
|
|
|
|
}
|