]> git.saurik.com Git - apple/objc4.git/blob - test/subscripting.m
e05a370c58c8b735c984b8081c446eab8ccf67cb
[apple/objc4.git] / test / subscripting.m
1 // TEST_CONFIG MEM=arc,mrc CC=clang LANGUAGE=objc,objc++
2 // TEST_CFLAGS -framework Foundation
3
4 #if !__OBJC2__
5
6 #include "test.h"
7
8 int main()
9 {
10 succeed(__FILE__);
11 }
12
13 #else
14
15 #import <Foundation/Foundation.h>
16 #import <Foundation/NSDictionary.h>
17 #import <objc/runtime.h>
18 #import <objc/objc-abi.h>
19 #include "test.h"
20
21 @interface TestIndexed : NSObject <NSFastEnumeration> {
22 NSMutableArray *indexedValues;
23 }
24 @property(readonly) NSUInteger count;
25 - (id)objectAtIndexedSubscript:(NSUInteger)index;
26 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
27 @end
28
29 @implementation TestIndexed
30
31 - (id)init {
32 if ((self = [super init])) {
33 indexedValues = [NSMutableArray new];
34 }
35 return self;
36 }
37
38 #if !__has_feature(objc_arc)
39 - (void)dealloc {
40 [indexedValues release];
41 [super dealloc];
42 }
43 #endif
44
45 - (NSUInteger)count { return [indexedValues count]; }
46 - (id)objectAtIndexedSubscript:(NSUInteger)index { return [indexedValues objectAtIndex:index]; }
47 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index {
48 if (index == NSNotFound)
49 [indexedValues addObject:object];
50 else
51 [indexedValues replaceObjectAtIndex:index withObject:object];
52 }
53
54 - (NSString *)description {
55 return [NSString stringWithFormat:@"indexedValues = %@", indexedValues];
56 }
57
58 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
59 return [indexedValues countByEnumeratingWithState:state objects:buffer count:len];
60 }
61
62
63 @end
64
65 @interface TestKeyed : NSObject <NSFastEnumeration> {
66 NSMutableDictionary *keyedValues;
67 }
68 @property(readonly) NSUInteger count;
69 - (id)objectForKeyedSubscript:(id)key;
70 - (void)setObject:(id)object forKeyedSubscript:(id)key;
71 @end
72
73 @implementation TestKeyed
74
75 - (id)init {
76 if ((self = [super init])) {
77 keyedValues = [NSMutableDictionary new];
78 }
79 return self;
80 }
81
82 #if !__has_feature(objc_arc)
83 - (void)dealloc {
84 [keyedValues release];
85 [super dealloc];
86 }
87 #endif
88
89 - (NSUInteger)count { return [keyedValues count]; }
90 - (id)objectForKeyedSubscript:(id)key { return [keyedValues objectForKey:key]; }
91 - (void)setObject:(id)object forKeyedSubscript:(id)key {
92 [keyedValues setObject:object forKey:key];
93 }
94
95 - (NSString *)description {
96 return [NSString stringWithFormat:@"keyedValues = %@", keyedValues];
97 }
98
99 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
100 return [keyedValues countByEnumeratingWithState:state objects:buffer count:len];
101 }
102
103 @end
104
105 int main() {
106 PUSH_POOL {
107
108 #if __has_feature(objc_bool) // placeholder until we get a more precise macro.
109 TestIndexed *testIndexed = [TestIndexed new];
110 id objects[] = { @1, @2, @3, @4, @5 };
111 size_t i, count = sizeof(objects) / sizeof(id);
112 for (i = 0; i < count; ++i) {
113 testIndexed[NSNotFound] = objects[i];
114 }
115 for (i = 0; i < count; ++i) {
116 id object = testIndexed[i];
117 testassert(object == objects[i]);
118 }
119 if (getenv("VERBOSE")) {
120 i = 0;
121 for (id object in testIndexed) {
122 NSString *message = [NSString stringWithFormat:@"testIndexed[%zu] = %@\n", i++, object];
123 testprintf([message UTF8String]);
124 }
125 }
126
127 TestKeyed *testKeyed = [TestKeyed new];
128 id keys[] = { @"One", @"Two", @"Three", @"Four", @"Five" };
129 for (i = 0; i < count; ++i) {
130 id key = keys[i];
131 testKeyed[key] = objects[i];
132 }
133 for (i = 0; i < count; ++i) {
134 id key = keys[i];
135 id object = testKeyed[key];
136 testassert(object == objects[i]);
137 }
138 if (getenv("VERBOSE")) {
139 for (id key in testKeyed) {
140 NSString *message = [NSString stringWithFormat:@"testKeyed[@\"%@\"] = %@\n", key, testKeyed[key]];
141 testprintf([message UTF8String]);
142 }
143 }
144 #endif
145
146 } POP_POOL;
147
148 succeed(__FILE__);
149
150 return 0;
151 }
152
153 // __OBJC2__
154 #endif