]> git.saurik.com Git - apple/objc4.git/blob - test/subscripting.m
objc4-756.2.tar.gz
[apple/objc4.git] / test / subscripting.m
1 // TEST_CFLAGS -framework Foundation
2
3 #import <Foundation/Foundation.h>
4 #import <Foundation/NSDictionary.h>
5 #import <objc/runtime.h>
6 #import <objc/objc-abi.h>
7 #include "test.h"
8
9 @interface TestIndexed : NSObject <NSFastEnumeration> {
10 NSMutableArray *indexedValues;
11 }
12 @property(readonly) NSUInteger count;
13 - (id)objectAtIndexedSubscript:(NSUInteger)index;
14 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
15 @end
16
17 @implementation TestIndexed
18
19 - (id)init {
20 if ((self = [super init])) {
21 indexedValues = [NSMutableArray new];
22 }
23 return self;
24 }
25
26 #if !__has_feature(objc_arc)
27 - (void)dealloc {
28 [indexedValues release];
29 [super dealloc];
30 }
31 #endif
32
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];
38 else
39 [indexedValues replaceObjectAtIndex:index withObject:object];
40 }
41
42 - (NSString *)description {
43 return [NSString stringWithFormat:@"indexedValues = %@", indexedValues];
44 }
45
46 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
47 return [indexedValues countByEnumeratingWithState:state objects:buffer count:len];
48 }
49
50
51 @end
52
53 @interface TestKeyed : NSObject <NSFastEnumeration> {
54 NSMutableDictionary *keyedValues;
55 }
56 @property(readonly) NSUInteger count;
57 - (id)objectForKeyedSubscript:(id)key;
58 - (void)setObject:(id)object forKeyedSubscript:(id)key;
59 @end
60
61 @implementation TestKeyed
62
63 - (id)init {
64 if ((self = [super init])) {
65 keyedValues = [NSMutableDictionary new];
66 }
67 return self;
68 }
69
70 #if !__has_feature(objc_arc)
71 - (void)dealloc {
72 [keyedValues release];
73 [super dealloc];
74 }
75 #endif
76
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];
81 }
82
83 - (NSString *)description {
84 return [NSString stringWithFormat:@"keyedValues = %@", keyedValues];
85 }
86
87 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
88 return [keyedValues countByEnumeratingWithState:state objects:buffer count:len];
89 }
90
91 @end
92
93 int main() {
94 PUSH_POOL {
95
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];
102 }
103 for (i = 0; i < count; ++i) {
104 id object = testIndexed[i];
105 testassert(object == objects[i]);
106 }
107 if (testverbose()) {
108 i = 0;
109 for (id object in testIndexed) {
110 NSString *message = [NSString stringWithFormat:@"testIndexed[%zu] = %@\n", i++, object];
111 testprintf([message UTF8String]);
112 }
113 }
114
115 TestKeyed *testKeyed = [TestKeyed new];
116 id keys[] = { @"One", @"Two", @"Three", @"Four", @"Five" };
117 for (i = 0; i < count; ++i) {
118 id key = keys[i];
119 testKeyed[key] = objects[i];
120 }
121 for (i = 0; i < count; ++i) {
122 id key = keys[i];
123 id object = testKeyed[key];
124 testassert(object == objects[i]);
125 }
126 if (testverbose()) {
127 for (id key in testKeyed) {
128 NSString *message = [NSString stringWithFormat:@"testKeyed[@\"%@\"] = %@\n", key, testKeyed[key]];
129 testprintf([message UTF8String]);
130 }
131 }
132 #endif
133
134 } POP_POOL;
135
136 succeed(__FILE__);
137
138 return 0;
139 }