fix a concurrency issue in NSArray enumeration test

This commit is contained in:
Niels Grewe 2019-12-02 23:25:35 +01:00
parent 07d87a0c1b
commit b39c9e499c
No known key found for this signature in database
GPG key ID: 99779549298DB83E
2 changed files with 17 additions and 4 deletions

View file

@ -23,7 +23,6 @@ matrix:
env: LIBRARY_COMBO=gnu-gnu-gnu
sudo: required
before_install:
- ls /usr/local/bin
- sudo apt-get -qq update
- sudo apt-get install -y cmake pkg-config libgnutls28-dev libgmp-dev libffi-dev libicu-dev libxml2-dev libxslt1-dev libssl-dev libavahi-client-dev zlib1g-dev libblocksruntime-dev
- >

View file

@ -4,10 +4,12 @@
#import <Foundation/NSIndexSet.h>
#import <Foundation/NSString.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSValue.h>
static NSUInteger fooCount = 0;
static NSUInteger lastIndex = NSNotFound;
static BOOL reverse = NO;
int main()
{
START_SET("NSArray Blocks")
@ -16,10 +18,20 @@ int main()
# endif
# if __has_feature(blocks)
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSLock *lock = [[[NSLock alloc] init] autorelease];
NSArray *array = [NSArray arrayWithObjects: @"foo", @"bar", @"foo", nil];
void(^enumBlock)(id,NSUInteger,BOOL*) = ^(id obj, NSUInteger index, BOOL *stop){
if ([obj isEqual: @"foo"]){ fooCount++;} lastIndex = index;};
void(^enumBlock)(id,NSUInteger,BOOL*) = ^(id obj, NSUInteger index, BOOL *stop) {
[lock lock];
if ([obj isEqual: @"foo"]) {
fooCount++;
}
if (lastIndex == NSNotFound) {
lastIndex = index;
} else {
lastIndex = reverse ? MIN(lastIndex, index) : MAX(lastIndex, index);
}
[lock unlock];
};
[array enumerateObjectsUsingBlock: enumBlock];
PASS((2 == fooCount) && (lastIndex == 2),
"Can forward enumerate array using a block");
@ -31,9 +43,11 @@ int main()
"Can forward enumerate array concurrently using a block");
fooCount = 0;
lastIndex = NSNotFound;
reverse = YES;
[array enumerateObjectsWithOptions: NSEnumerationReverse
usingBlock: enumBlock];
PASS((0 == lastIndex), "Can enumerate array in reverse using a block");
reverse = NO;
fooCount = 0;
lastIndex = NSNotFound;
enumBlock = ^(id obj, NSUInteger index, BOOL *stop){if ([obj isEqual: @"foo"]){