Debian Codegraph @

gcc/testsuite/objc.dg/foreach-4.m

raw
1 /* Test basic Objective-C foreach syntax. This tests iterations, with
2 the declaration syntax 'for (id object in array) statements'
3 */
4 /* { dg-do run } */
5 /* { dg-skip-if "No NeXT fast enum. pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
6 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
7 /* { dg-options "-mno-constant-cfstrings" { target *-*-darwin* } } */
8 /* { dg-additional-sources "../objc-obj-c++-shared/nsconstantstring-class-impl.m" } */
9 /* { dg-additional-options "-Wno-objc-root-class" } */
10
11 #import "../objc-obj-c++-shared/TestsuiteObject.m"
12 #ifndef __NEXT_RUNTIME__
13 #include <objc/NXConstStr.h>
14 #else
15 #include "../objc-obj-c++-shared/nsconstantstring-class.h"
16 #endif
17
18 extern int printf (const char *, ...);
19 #include <stdlib.h>
20
21 /*
22 struct __objcFastEnumerationState
23 {
24 unsigned long state;
25 id *itemsPtr;
26 unsigned long *mutationsPtr;
27 unsigned long extra[5];
28 };
29 */
30
31 /* A mini-array implementation that can be used to test fast
32 enumeration. You create the array with some objects; you can
33 mutate the array, and you can fast-enumerate it.
34 */
35 @interface MyArray : TestsuiteObject
36 {
37 unsigned int length;
38 id *objects;
39 unsigned long mutated;
40 }
41 - (id) initWithLength: (unsigned int)l objects: (id *)o;
42 - (void) mutate;
43 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
44 objects:(id *)stackbuf
45 count:(unsigned long)len;
46 @end
47
48 @implementation MyArray : TestsuiteObject
49 - (id) initWithLength: (unsigned int)l
50 objects: (id *)o
51 {
52 length = l;
53 objects = o;
54 mutated = 0;
55 return self;
56 }
57 - (void) mutate
58 {
59 mutated = 1;
60 }
61 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state
62 objects: (id*)stackbuf
63 count: (unsigned long)len
64 {
65 unsigned long i, batch_size;
66
67 /* We keep how many objects we served in the state->state counter. So the next batch
68 will contain up to length - state->state objects. */
69 batch_size = length - state->state;
70
71 /* Make obvious adjustments. */
72 if (batch_size < 0)
73 batch_size = 0;
74
75 if (batch_size > len)
76 batch_size = len;
77
78 /* Copy the objects. */
79 for (i = 0; i < batch_size; i++)
80 stackbuf[i] = objects[i];
81
82 state->state += batch_size;
83 state->itemsPtr = stackbuf;
84 state->mutationsPtr = &mutated;
85
86 return batch_size;
87 }
88 @end
89
90 int main (void)
91 {
92 MyArray *array;
93 int test_variable, counter, i;
94 id *objects;
95
96 array = [[MyArray alloc] initWithLength: 0
97 objects: NULL];
98
99 /* Test that an empty array does nothing. */
100 for (id object in array)
101 abort ();
102
103 /* Test iterating over 1 object. */
104 objects = malloc (sizeof (id) * 1);
105 objects[0] = @"One Object";
106
107 array = [[MyArray alloc] initWithLength: 1
108 objects: objects];
109
110 for (id object in array)
111 printf ("%p\n", object);
112
113 /* Test iterating over 20 objects. */
114 objects = malloc (sizeof (id) * 20);
115 for (i = 0; i < 20; i++)
116 objects[i] = @"object";
117
118 array = [[MyArray alloc] initWithLength: 20
119 objects: objects];
120
121 for (id object in array)
122 printf ("%p\n", object);
123
124 /* Test iterating over 200 objects. */
125 objects = malloc (sizeof (id) * 200);
126 for (i = 0; i < 200; i++)
127 objects[i] = @"object";
128
129 array = [[MyArray alloc] initWithLength: 200
130 objects: objects];
131
132 counter = 0;
133 for (id object in array)
134 {
135 if (object != nil)
136 counter++;
137 }
138
139 if (counter != 200)
140 abort ();
141
142 printf ("Counter was %d (should be 200)\n", counter);
143
144 /* Test iterating again over the same array. */
145 counter = 0;
146 for (id object in array)
147 {
148 if (object != nil)
149 counter++;
150 }
151
152 if (counter != 200)
153 abort ();
154
155 printf ("Counter was %d (should be 200)\n", counter);
156
157 /* Test nested iterations. */
158 objects = malloc (sizeof (id) * 20);
159 for (i = 0; i < 20; i++)
160 objects[i] = @"object";
161
162 array = [[MyArray alloc] initWithLength: 20
163 objects: objects];
164 counter = 0;
165 for (id object in array)
166 {
167 for (id another_object in array)
168 if (another_object != nil)
169 counter++;
170 }
171
172 printf ("Counter was %d (should be 400)\n", counter);
173
174 if (counter != 400)
175 abort ();
176
177 /* Test 'continue'. */
178 objects = malloc (sizeof (id) * 20);
179 for (i = 0; i < 20; i++)
180 objects[i] = @"object";
181
182 array = [[MyArray alloc] initWithLength: 20
183 objects: objects];
184 counter = 0;
185 for (id object in array)
186 {
187 if (counter == 15)
188 continue;
189
190 counter++;
191 }
192
193 printf ("Counter was %d (should be 15)\n", counter);
194
195 if (counter != 15)
196 abort ();
197
198 /* Test 'break'. */
199 objects = malloc (sizeof (id) * 20);
200 for (i = 0; i < 20; i++)
201 objects[i] = @"object";
202
203 array = [[MyArray alloc] initWithLength: 20
204 objects: objects];
205 counter = 0;
206 for (id object in array)
207 {
208 counter++;
209
210 if (counter == 15)
211 break;
212 }
213
214 printf ("Counter was %d (should be 15)\n", counter);
215
216 if (counter != 15)
217 abort ();
218
219 /* Test 'break' and 'continue' in nested iterations. */
220 objects = malloc (sizeof (id) * 20);
221 for (i = 0; i < 20; i++)
222 objects[i] = @"object";
223
224 array = [[MyArray alloc] initWithLength: 20
225 objects: objects];
226 counter = 0;
227 for (id object in array)
228 {
229 int local_counter = 0;
230
231 /* Each internal loop should increase counter by 24. */
232 for (id another_object in array)
233 {
234 local_counter++;
235
236 if (local_counter == 10)
237 {
238 counter = counter + 20;
239 break;
240 }
241
242 if (local_counter >= 5)
243 continue;
244
245 counter++;
246 }
247
248 /* Exit after 4 iterations. */
249 if (counter == 96)
250 break;
251 }
252
253 printf ("Counter was %d (should be 96)\n", counter);
254
255 if (counter != 96)
256 abort ();
257
258 /* Test that C for loops still work. */
259 test_variable = 0;
260
261 for (counter = 0; counter < 4; counter++)
262 test_variable++;
263
264 if (test_variable != 4)
265 abort ();
266
267 return 0;
268 }