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" } */
10 #import "../objc-obj-c++-shared/TestsuiteObject.m"
11 #ifndef __NEXT_RUNTIME__
12 #include <objc/NXConstStr.h>
14 #include "../objc-obj-c++-shared/nsconstantstring-class.h"
17 extern int printf (const char *, ...);
21 struct __objcFastEnumerationState
25 unsigned long *mutationsPtr;
26 unsigned long extra[5];
30 /* A mini-array implementation that can be used to test fast
31 enumeration. You create the array with some objects; you can
32 mutate the array, and you can fast-enumerate it.
34 @interface MyArray : TestsuiteObject
38 unsigned long mutated;
40 - (id) initWithLength: (unsigned int)l objects: (id *)o;
42 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
43 objects:(id *)stackbuf
44 count:(unsigned long)len;
47 @implementation MyArray : TestsuiteObject
48 - (id) initWithLength: (unsigned int)l
60 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state
61 objects: (id*)stackbuf
62 count: (unsigned long)len
64 unsigned long i, batch_size;
66 /* We keep how many objects we served in the state->state counter. So the next batch
67 will contain up to length - state->state objects. */
68 batch_size = length - state->state;
70 /* Make obvious adjustments. */
77 /* Copy the objects. */
78 for (i = 0; i < batch_size; i++)
79 stackbuf[i] = objects[i];
81 state->state += batch_size;
82 state->itemsPtr = stackbuf;
83 state->mutationsPtr = &mutated;
92 int test_variable, counter, i;
95 array = [[MyArray alloc] initWithLength: 0
98 /* Test that an empty array does nothing. */
99 for (id object in array)
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 (id 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 (id 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 (id object in array)
141 printf ("Counter was %d (should be 200)\n", counter);
143 /* Test iterating again over the same array. */
145 for (id 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 (id object in array)
166 for (id another_object in array)
167 if (another_object != nil)
171 printf ("Counter was %d (should be 400)\n", counter);
176 /* Test 'continue'. */
177 objects = malloc (sizeof (id) * 20);
178 for (i = 0; i < 20; i++)
179 objects[i] = @"object";
181 array = [[MyArray alloc] initWithLength: 20
184 for (id object in array)
192 printf ("Counter was %d (should be 15)\n", counter);
198 objects = malloc (sizeof (id) * 20);
199 for (i = 0; i < 20; i++)
200 objects[i] = @"object";
202 array = [[MyArray alloc] initWithLength: 20
205 for (id object in array)
213 printf ("Counter was %d (should be 15)\n", counter);
218 /* Test 'break' and 'continue' in nested iterations. */
219 objects = malloc (sizeof (id) * 20);
220 for (i = 0; i < 20; i++)
221 objects[i] = @"object";
223 array = [[MyArray alloc] initWithLength: 20
226 for (id object in array)
228 int local_counter = 0;
230 /* Each internal loop should increase counter by 24. */
231 for (id another_object in array)
235 if (local_counter == 10)
237 counter = counter + 20;
241 if (local_counter >= 5)
247 /* Exit after 4 iterations. */
252 printf ("Counter was %d (should be 96)\n", counter);
257 /* Test that C for loops still work. */
260 for (counter = 0; counter < 4; counter++)
263 if (test_variable != 4)