2021-08-11 13:49:31 +00:00
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSSet.h>
|
|
|
|
#import <Foundation/NSOrderedSet.h>
|
|
|
|
#import "ObjectTesting.h"
|
|
|
|
#import "../../../Source/GSFastEnumeration.h"
|
|
|
|
|
|
|
|
void fast_enumeration_mutation_add(id mutableCollection)
|
|
|
|
{
|
|
|
|
NSUInteger i = 0;
|
|
|
|
FOR_IN(id, o, mutableCollection)
|
|
|
|
if (i == [mutableCollection count]/2) {
|
2024-11-15 20:48:09 +00:00
|
|
|
if ([mutableCollection isKindOfClass: [NSMutableDictionary class]]) {
|
|
|
|
[mutableCollection setObject: @"boom" forKey: @"boom"];
|
2021-08-11 13:49:31 +00:00
|
|
|
} else {
|
2024-11-15 20:48:09 +00:00
|
|
|
[mutableCollection addObject: @"boom"];
|
2021-08-11 13:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
END_FOR_IN(mutableCollection)
|
|
|
|
}
|
|
|
|
|
|
|
|
void fast_enumeration_mutation_remove(id mutableCollection)
|
|
|
|
{
|
|
|
|
NSUInteger i = 0;
|
|
|
|
FOR_IN(id, o, mutableCollection)
|
|
|
|
if (i == [mutableCollection count]/2) {
|
2024-11-15 20:48:09 +00:00
|
|
|
if ([mutableCollection isKindOfClass: [NSMutableDictionary class]]) {
|
|
|
|
[mutableCollection removeObjectForKey: o];
|
2021-08-11 13:49:31 +00:00
|
|
|
} else {
|
2024-11-15 20:48:09 +00:00
|
|
|
[mutableCollection removeObject: o];
|
2021-08-11 13:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
END_FOR_IN(mutableCollection)
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_fast_enumeration(id collection, NSArray *objects)
|
|
|
|
{
|
2024-11-15 20:48:09 +00:00
|
|
|
NSMutableArray *returnedObjects = [NSMutableArray array];
|
|
|
|
|
2021-08-11 13:49:31 +00:00
|
|
|
FOR_IN(id, o, collection)
|
2024-11-15 20:48:09 +00:00
|
|
|
[returnedObjects addObject: o];
|
2021-08-11 13:49:31 +00:00
|
|
|
END_FOR_IN(collection)
|
2024-11-15 20:48:09 +00:00
|
|
|
if (!([collection isKindOfClass: [NSArray class]]
|
|
|
|
|| [collection isKindOfClass: [NSOrderedSet class]]))
|
|
|
|
{
|
|
|
|
[returnedObjects sortUsingSelector: @selector(compare:)];
|
|
|
|
}
|
|
|
|
PASS_EQUAL(returnedObjects, objects, "fast enumeration returns all objects")
|
|
|
|
|
2021-08-11 13:49:31 +00:00
|
|
|
id mutableCollection = [collection mutableCopy];
|
|
|
|
PASS_EXCEPTION(
|
|
|
|
fast_enumeration_mutation_add(mutableCollection),
|
|
|
|
NSGenericException,
|
2024-11-15 20:48:09 +00:00
|
|
|
"Fast enumeration mutation add properly calls @\"NSGenericException\"")
|
2021-08-11 13:49:31 +00:00
|
|
|
PASS_EXCEPTION(
|
|
|
|
fast_enumeration_mutation_remove(mutableCollection),
|
|
|
|
NSGenericException,
|
2024-11-15 20:48:09 +00:00
|
|
|
"Fast enumeration mutation remove properly calls @\"NSGenericException\"")
|
2021-08-11 13:49:31 +00:00
|
|
|
[mutableCollection release];
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
|
|
|
|
NSMutableArray *objects = [NSMutableArray array];
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 10000; i++) {
|
2024-11-15 20:48:09 +00:00
|
|
|
[objects addObject: [NSString stringWithFormat: @"%.4d", i]];
|
2021-08-11 13:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
START_SET("NSArray")
|
2024-11-15 20:48:09 +00:00
|
|
|
id array = [NSArray arrayWithArray: objects];
|
2021-08-11 13:49:31 +00:00
|
|
|
test_fast_enumeration(array, objects);
|
|
|
|
END_SET("NSArray")
|
|
|
|
|
|
|
|
START_SET("NSSet")
|
2024-11-15 20:48:09 +00:00
|
|
|
id set = [NSSet setWithArray: objects];
|
2021-08-11 13:49:31 +00:00
|
|
|
test_fast_enumeration(set, objects);
|
|
|
|
END_SET("NSSet")
|
|
|
|
|
|
|
|
START_SET("NSOrderedSet")
|
2024-11-15 20:48:09 +00:00
|
|
|
id orderedSet = [NSOrderedSet orderedSetWithArray: objects];
|
2021-08-11 13:49:31 +00:00
|
|
|
test_fast_enumeration(orderedSet, objects);
|
|
|
|
END_SET("NSOrderedSet")
|
|
|
|
|
|
|
|
START_SET("NSDictionary")
|
2024-11-15 20:48:09 +00:00
|
|
|
id dict = [NSDictionary dictionaryWithObjects: objects forKeys: objects];
|
2021-08-11 13:49:31 +00:00
|
|
|
test_fast_enumeration(dict, objects);
|
|
|
|
END_SET("NSDictionary")
|
|
|
|
|
|
|
|
[arp release]; arp = nil;
|
|
|
|
return 0;
|
|
|
|
}
|