1 /* Test basic Objective-C foreach syntax. This tests iterations, with
2 the basic syntax 'for (object in array) statements'
5 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
6 /* { dg-additional-sources "../objc-obj-c++-shared/Object1.m" } */
8 #import "../objc-obj-c++-shared/Object1.h"
9 #import "../objc-obj-c++-shared/next-mapping.h"
10 #ifndef __NEXT_RUNTIME__
11 #include <objc/NXConstStr.h>
14 extern int printf (const char *, ...);
18 struct __objcFastEnumerationState
22 unsigned long *mutationsPtr;
23 unsigned long extra[5];
27 /* A mini-array implementation that can be used to test fast
28 enumeration. You create the array with some objects; you can
29 mutate the array, and you can fast-enumerate it.
31 @interface MyArray : Object
35 unsigned long mutated;
37 - (id) initWithLength: (unsigned int)l objects: (id *)o;
39 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
40 objects:(id *)stackbuf
41 count:(unsigned long)len;
44 @implementation MyArray : Object
45 - (id) initWithLength: (unsigned int)l
56 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state
57 objects: (id*)stackbuf
58 count: (unsigned long)len
60 unsigned long i, batch_size;
62 /* We keep how many objects we served in the state->state counter. So the next batch
63 will contain up to length - state->state objects. */
64 batch_size = length - state->state;
66 /* Make obvious adjustments. */
73 /* Copy the objects. */
74 for (i = 0; i < batch_size; i++)
75 stackbuf[i] = objects[i];
77 state->state += batch_size;
78 state->itemsPtr = stackbuf;
79 state->mutationsPtr = &mutated;
89 int test_variable, counter, i;
92 array = [[MyArray alloc] initWithLength: 0
95 /* Test that an empty array does nothing. */
102 /* Test iterating over 1 object. */
103 objects = malloc (sizeof (id) * 1);
104 objects[0] = @"One Object";
106 array = [[MyArray alloc] initWithLength: 1
109 for (object in array)
110 printf ("%p\n", object);
112 /* Test iterating over 20 objects. */
113 objects = malloc (sizeof (id) * 20);
114 for (i = 0; i < 20; i++)
115 objects[i] = @"object";
117 array = [[MyArray alloc] initWithLength: 20
120 for (object in array)
121 printf ("%p\n", object);
123 /* Test iterating over 200 objects. */
124 objects = malloc (sizeof (id) * 200);
125 for (i = 0; i < 200; i++)
126 objects[i] = @"object";
128 array = [[MyArray alloc] initWithLength: 200
132 for (object in array)
141 printf ("Counter was %d (should be 200)\n", counter);
143 /* Test iterating again over the same array. */
145 for (object in array)
154 printf ("Counter was %d (should be 200)\n", counter);
156 /* Test nested iterations. */
157 objects = malloc (sizeof (id) * 20);
158 for (i = 0; i < 20; i++)
159 objects[i] = @"object";
161 array = [[MyArray alloc] initWithLength: 20
164 for (object in array)
167 for (another_object in array)
168 if (another_object != nil)
172 printf ("Counter was %d (should be 400)\n", counter);
177 /* Test 'continue'. */
178 objects = malloc (sizeof (id) * 20);
179 for (i = 0; i < 20; i++)
180 objects[i] = @"object";
182 array = [[MyArray alloc] initWithLength: 20
185 for (object in array)
193 printf ("Counter was %d (should be 15)\n", counter);
199 objects = malloc (sizeof (id) * 20);
200 for (i = 0; i < 20; i++)
201 objects[i] = @"object";
203 array = [[MyArray alloc] initWithLength: 20
206 for (object in array)
214 printf ("Counter was %d (should be 15)\n", counter);
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";
224 array = [[MyArray alloc] initWithLength: 20
227 for (object in array)
229 int local_counter = 0;
232 /* Each internal loop should increase counter by 24. */
233 for (another_object in array)
237 if (local_counter == 10)
239 counter = counter + 20;
243 if (local_counter >= 5)
249 /* Exit after 4 iterations. */
254 printf ("Counter was %d (should be 96)\n", counter);
259 /* Test that if we 'break', the object is set to the last one, while
260 if we run out of objects, it is set to 'nil'. */
261 for (object in array)
267 for (object in array)
273 /* Test that C for loops still work. */
276 for (counter = 0; counter < 4; counter++)
279 if (test_variable != 4)