1 /* Test basic Objective-C foreach syntax. This tests iterations, with
2 the declaration syntax 'for (id 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;
88 int test_variable, counter, i;
91 array = [[MyArray alloc] initWithLength: 0
94 /* Test that an empty array does nothing. */
95 for (id object in array)
98 /* Test iterating over 1 object. */
99 objects = malloc (sizeof (id) * 1);
100 objects[0] = @"One Object";
102 array = [[MyArray alloc] initWithLength: 1
105 for (id object in array)
106 printf ("%p\n", object);
108 /* Test iterating over 20 objects. */
109 objects = malloc (sizeof (id) * 20);
110 for (i = 0; i < 20; i++)
111 objects[i] = @"object";
113 array = [[MyArray alloc] initWithLength: 20
116 for (id object in array)
117 printf ("%p\n", object);
119 /* Test iterating over 200 objects. */
120 objects = malloc (sizeof (id) * 200);
121 for (i = 0; i < 200; i++)
122 objects[i] = @"object";
124 array = [[MyArray alloc] initWithLength: 200
128 for (id object in array)
137 printf ("Counter was %d (should be 200)\n", counter);
139 /* Test iterating again over the same array. */
141 for (id object in array)
150 printf ("Counter was %d (should be 200)\n", counter);
152 /* Test nested iterations. */
153 objects = malloc (sizeof (id) * 20);
154 for (i = 0; i < 20; i++)
155 objects[i] = @"object";
157 array = [[MyArray alloc] initWithLength: 20
160 for (id object in array)
162 for (id another_object in array)
163 if (another_object != nil)
167 printf ("Counter was %d (should be 400)\n", counter);
172 /* Test 'continue'. */
173 objects = malloc (sizeof (id) * 20);
174 for (i = 0; i < 20; i++)
175 objects[i] = @"object";
177 array = [[MyArray alloc] initWithLength: 20
180 for (id object in array)
188 printf ("Counter was %d (should be 15)\n", counter);
194 objects = malloc (sizeof (id) * 20);
195 for (i = 0; i < 20; i++)
196 objects[i] = @"object";
198 array = [[MyArray alloc] initWithLength: 20
201 for (id object in array)
209 printf ("Counter was %d (should be 15)\n", counter);
214 /* Test 'break' and 'continue' in nested iterations. */
215 objects = malloc (sizeof (id) * 20);
216 for (i = 0; i < 20; i++)
217 objects[i] = @"object";
219 array = [[MyArray alloc] initWithLength: 20
222 for (id object in array)
224 int local_counter = 0;
226 /* Each internal loop should increase counter by 24. */
227 for (id another_object in array)
231 if (local_counter == 10)
233 counter = counter + 20;
237 if (local_counter >= 5)
243 /* Exit after 4 iterations. */
248 printf ("Counter was %d (should be 96)\n", counter);
253 /* Test that C for loops still work. */
256 for (counter = 0; counter < 4; counter++)
259 if (test_variable != 4)