I was looking to see why two windows systems might have started failing on this, without spotting any reason. I noticed that there were a couple of classes untested, so I added them.

This commit is contained in:
rfm 2025-01-18 18:02:39 +00:00
parent d746ad53d7
commit 39b1b7fd01

View file

@ -6,32 +6,46 @@
#import "ObjectTesting.h" #import "ObjectTesting.h"
#import "../../../Source/GSFastEnumeration.h" #import "../../../Source/GSFastEnumeration.h"
static SEL add;
static SEL set;
static SEL key;
void fast_enumeration_mutation_add(id mutableCollection) void fast_enumeration_mutation_add(id mutableCollection)
{ {
NSUInteger i = 0; NSUInteger i = 0;
FOR_IN(id, o, mutableCollection) FOR_IN(id, o, mutableCollection)
if (i == [mutableCollection count]/2) { if (i == [mutableCollection count]/2)
if ([mutableCollection isKindOfClass: [NSMutableDictionary class]]) { {
[mutableCollection setObject: @"boom" forKey: @"boom"]; if ([mutableCollection respondsToSelector: set])
} else { {
[mutableCollection addObject: @"boom"]; [mutableCollection setObject: @"boom" forKey: @"boom"];
}
else
{
[mutableCollection addObject: @"boom"];
}
} }
}
i++; i++;
END_FOR_IN(mutableCollection) END_FOR_IN(mutableCollection)
} }
void fast_enumeration_mutation_remove(id mutableCollection) void fast_enumeration_mutation_remove(id mutableCollection)
{ {
NSUInteger i = 0; NSUInteger i = 0;
FOR_IN(id, o, mutableCollection) FOR_IN(id, o, mutableCollection)
if (i == [mutableCollection count]/2) { if (i == [mutableCollection count]/2)
if ([mutableCollection isKindOfClass: [NSMutableDictionary class]]) { {
[mutableCollection removeObjectForKey: o]; if ([mutableCollection respondsToSelector: key])
} else { {
[mutableCollection removeObject: o]; [mutableCollection removeObjectForKey: o];
}
else
{
[mutableCollection removeObject: o];
}
} }
}
i++; i++;
END_FOR_IN(mutableCollection) END_FOR_IN(mutableCollection)
} }
@ -50,7 +64,20 @@ void test_fast_enumeration(id collection, NSArray *objects)
} }
PASS_EQUAL(returnedObjects, objects, "fast enumeration returns all objects") PASS_EQUAL(returnedObjects, objects, "fast enumeration returns all objects")
id mutableCollection = [collection mutableCopy]; id mutableCollection;
if ([collection respondsToSelector: @selector(mutableCopyWithZone:)])
{
mutableCollection = AUTORELEASE([collection mutableCopy]);
}
else if ([collection respondsToSelector: add]
|| [collection respondsToSelector: set])
{
mutableCollection = collection; // It has a method to mutate it
}
else
{
return; // No mutable version
}
PASS_EXCEPTION( PASS_EXCEPTION(
fast_enumeration_mutation_add(mutableCollection), fast_enumeration_mutation_add(mutableCollection),
NSGenericException, NSGenericException,
@ -59,18 +86,22 @@ void test_fast_enumeration(id collection, NSArray *objects)
fast_enumeration_mutation_remove(mutableCollection), fast_enumeration_mutation_remove(mutableCollection),
NSGenericException, NSGenericException,
"Fast enumeration mutation remove properly calls @\"NSGenericException\"") "Fast enumeration mutation remove properly calls @\"NSGenericException\"")
[mutableCollection release];
} }
int main() int main()
{ {
NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSMutableArray *objects = [NSMutableArray array];
NSMutableArray *objects = [NSMutableArray array]; int i;
int i;
for (i = 0; i < 10000; i++) { add = @selector(addObject:);
[objects addObject: [NSString stringWithFormat: @"%.4d", i]]; set = @selector(setObject:forKey:);
} key = @selector(removeObjectForKey:);
for (i = 0; i < 10000; i++)
{
[objects addObject: [NSString stringWithFormat: @"%.4d", i]];
}
START_SET("NSArray") START_SET("NSArray")
id array = [NSArray arrayWithArray: objects]; id array = [NSArray arrayWithArray: objects];
@ -92,6 +123,22 @@ int main()
test_fast_enumeration(dict, objects); test_fast_enumeration(dict, objects);
END_SET("NSDictionary") END_SET("NSDictionary")
START_SET("NSMapTable")
id map = [NSMapTable strongToStrongObjectsMapTable];
FOR_IN(id, o, objects)
[map setObject: o forKey: o];
END_FOR_IN(objects)
test_fast_enumeration(map, objects);
END_SET("NSMapTable")
START_SET("NSHashTable")
id table = [NSHashTable weakObjectsHashTable];
FOR_IN(id, o, objects)
[table addObject: o];
END_FOR_IN(objects)
test_fast_enumeration(table, objects);
END_SET("NSHashTable")
[arp release]; arp = nil; [arp release]; arp = nil;
return 0; return 0;
} }