1 /* Test basic Objective-C foreach syntax. This tests iterations, with
2 the declaration syntax 'for (id object in array) statements'
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" } */
11 #import "../objc-obj-c++-shared/TestsuiteObject.m"
12 #ifndef __NEXT_RUNTIME__
13 #include <objc/NXConstStr.h>
15 #include "../objc-obj-c++-shared/nsconstantstring-class.h"
18 extern int printf (const char *, ...);
22 struct __objcFastEnumerationState
26 unsigned long *mutationsPtr;
27 unsigned long extra[5];
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.
35 @interface MyArray : TestsuiteObject
39 unsigned long mutated;
41 - (id) initWithLength: (unsigned int)l objects: (id *)o;
43 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
44 objects:(id *)stackbuf
45 count:(unsigned long)len;
48 @implementation MyArray : TestsuiteObject
49 - (id) initWithLength: (unsigned int)l
61 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state
62 objects: (id*)stackbuf
63 count: (unsigned long)len
65 unsigned long i, batch_size;
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;
71 /* Make obvious adjustments. */
78 /* Copy the objects. */
79 for (i = 0; i < batch_size; i++)
80 stackbuf[i] = objects[i];
82 state->state += batch_size;
83 state->itemsPtr = stackbuf;
84 state->mutationsPtr = &mutated;
93 int test_variable, counter, i;
96 array = [[MyArray alloc] initWithLength: 0
99 /* Test that an empty array does nothing. */
100 for (id object in array)
103 /* Test iterating over 1 object. */
104 objects = malloc (sizeof (id) * 1);
105 objects[0] = @"One Object";
107 array = [[MyArray alloc] initWithLength: 1
110 for (id object in array)
111 printf ("%p\n", object);
113 /* Test iterating over 20 objects. */
114 objects = malloc (sizeof (id) * 20);
115 for (i = 0; i < 20; i++)
116 objects[i] = @"object";
118 array = [[MyArray alloc] initWithLength: 20
121 for (id object in array)
122 printf ("%p\n", object);
124 /* Test iterating over 200 objects. */
125 objects = malloc (sizeof (id) * 200);
126 for (i = 0; i < 200; i++)
127 objects[i] = @"object";
129 array = [[MyArray alloc] initWithLength: 200
133 for (id object in array)
142 printf ("Counter was %d (should be 200)\n", counter);
144 /* Test iterating again over the same array. */
146 for (id object in array)
155 printf ("Counter was %d (should be 200)\n", counter);
157 /* Test nested iterations. */
158 objects = malloc (sizeof (id) * 20);
159 for (i = 0; i < 20; i++)
160 objects[i] = @"object";
162 array = [[MyArray alloc] initWithLength: 20
165 for (id object in array)
167 for (id 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 (id 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 (id 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 (id object in array)
229 int local_counter = 0;
231 /* Each internal loop should increase counter by 24. */
232 for (id another_object in array)
236 if (local_counter == 10)
238 counter = counter + 20;
242 if (local_counter >= 5)
248 /* Exit after 4 iterations. */
253 printf ("Counter was %d (should be 96)\n", counter);
258 /* Test that C for loops still work. */
261 for (counter = 0; counter < 4; counter++)
264 if (test_variable != 4)