1 // TEST_CFLAGS -framework Foundation
3 #import <Foundation/Foundation.h>
4 #import <Foundation/NSDictionary.h>
5 #import <objc/runtime.h>
6 #import <objc/objc-abi.h>
9 @interface TestIndexed : NSObject <NSFastEnumeration> {
10 NSMutableArray *indexedValues;
12 @property(readonly) NSUInteger count;
13 - (id)objectAtIndexedSubscript:(NSUInteger)index;
14 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
17 @implementation TestIndexed
20 if ((self = [super init])) {
21 indexedValues = [NSMutableArray new];
26 #if !__has_feature(objc_arc)
28 [indexedValues release];
33 - (NSUInteger)count { return [indexedValues count]; }
34 - (id)objectAtIndexedSubscript:(NSUInteger)index { return [indexedValues objectAtIndex:index]; }
35 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index {
36 if (index == NSNotFound)
37 [indexedValues addObject:object];
39 [indexedValues replaceObjectAtIndex:index withObject:object];
42 - (NSString *)description {
43 return [NSString stringWithFormat:@"indexedValues = %@", indexedValues];
46 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
47 return [indexedValues countByEnumeratingWithState:state objects:buffer count:len];
53 @interface TestKeyed : NSObject <NSFastEnumeration> {
54 NSMutableDictionary *keyedValues;
56 @property(readonly) NSUInteger count;
57 - (id)objectForKeyedSubscript:(id)key;
58 - (void)setObject:(id)object forKeyedSubscript:(id)key;
61 @implementation TestKeyed
64 if ((self = [super init])) {
65 keyedValues = [NSMutableDictionary new];
70 #if !__has_feature(objc_arc)
72 [keyedValues release];
77 - (NSUInteger)count { return [keyedValues count]; }
78 - (id)objectForKeyedSubscript:(id)key { return [keyedValues objectForKey:key]; }
79 - (void)setObject:(id)object forKeyedSubscript:(id)key {
80 [keyedValues setObject:object forKey:key];
83 - (NSString *)description {
84 return [NSString stringWithFormat:@"keyedValues = %@", keyedValues];
87 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
88 return [keyedValues countByEnumeratingWithState:state objects:buffer count:len];
96 #if __has_feature(objc_bool) // placeholder until we get a more precise macro.
97 TestIndexed *testIndexed = [TestIndexed new];
98 id objects[] = { @1, @2, @3, @4, @5 };
99 size_t i, count = sizeof(objects) / sizeof(id);
100 for (i = 0; i < count; ++i) {
101 testIndexed[NSNotFound] = objects[i];
103 for (i = 0; i < count; ++i) {
104 id object = testIndexed[i];
105 testassert(object == objects[i]);
109 for (id object in testIndexed) {
110 NSString *message = [NSString stringWithFormat:@"testIndexed[%zu] = %@\n", i++, object];
111 testprintf([message UTF8String]);
115 TestKeyed *testKeyed = [TestKeyed new];
116 id keys[] = { @"One", @"Two", @"Three", @"Four", @"Five" };
117 for (i = 0; i < count; ++i) {
119 testKeyed[key] = objects[i];
121 for (i = 0; i < count; ++i) {
123 id object = testKeyed[key];
124 testassert(object == objects[i]);
127 for (id key in testKeyed) {
128 NSString *message = [NSString stringWithFormat:@"testKeyed[@\"%@\"] = %@\n", key, testKeyed[key]];
129 testprintf([message UTF8String]);