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-options "-mno-constant-cfstrings" { target *-*-darwin* } } */
7 /* { dg-additional-sources "../objc-obj-c++-shared/Object1.m" } */
9 #import "../objc-obj-c++-shared/Object1.h"
10 #import "../objc-obj-c++-shared/next-mapping.h"
11 #ifndef __NEXT_RUNTIME__
12 #include <objc/NXConstStr.h>
15 extern int printf (const char *, ...);
19 struct __objcFastEnumerationState
23 unsigned long *mutationsPtr;
24 unsigned long extra[5];
28 /* A mini-array implementation that can be used to test fast
29 enumeration. You create the array with some objects; you can
30 mutate the array, and you can fast-enumerate it.
32 @interface MyArray : Object
36 unsigned long mutated;
38 - (id) initWithLength: (unsigned int)l objects: (id *)o;
40 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
41 objects:(id *)stackbuf
42 count:(unsigned long)len;
45 @implementation MyArray : Object
46 - (id) initWithLength: (unsigned int)l
57 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state
58 objects: (id*)stackbuf
59 count: (unsigned long)len
61 unsigned long i, batch_size;
63 /* We keep how many objects we served in the state->state counter. So the next batch
64 will contain up to length - state->state objects. */
65 batch_size = length - state->state;
67 /* Make obvious adjustments. */
74 /* Copy the objects. */
75 for (i = 0; i < batch_size; i++)
76 stackbuf[i] = objects[i];
78 state->state += batch_size;
79 state->itemsPtr = stackbuf;
80 state->mutationsPtr = &mutated;
89 int test_variable, counter, i;
92 array = [[MyArray alloc] initWithLength: 0
95 /* Test that an empty array does nothing. */
96 for (id object in array)
99 /* Test iterating over 1 object. */
100 objects = malloc (sizeof (id) * 1);
101 objects[0] = @"One Object";
103 array = [[MyArray alloc] initWithLength: 1
106 for (id object in array)
107 printf ("%p\n", object);
109 /* Test iterating over 20 objects. */
110 objects = malloc (sizeof (id) * 20);
111 for (i = 0; i < 20; i++)
112 objects[i] = @"object";
114 array = [[MyArray alloc] initWithLength: 20
117 for (id object in array)
118 printf ("%p\n", object);
120 /* Test iterating over 200 objects. */
121 objects = malloc (sizeof (id) * 200);
122 for (i = 0; i < 200; i++)
123 objects[i] = @"object";
125 array = [[MyArray alloc] initWithLength: 200
129 for (id object in array)
138 printf ("Counter was %d (should be 200)\n", counter);
140 /* Test iterating again over the same array. */
142 for (id object in array)
151 printf ("Counter was %d (should be 200)\n", counter);
153 /* Test nested iterations. */
154 objects = malloc (sizeof (id) * 20);
155 for (i = 0; i < 20; i++)
156 objects[i] = @"object";
158 array = [[MyArray alloc] initWithLength: 20
161 for (id object in array)
163 for (id another_object in array)
164 if (another_object != nil)
168 printf ("Counter was %d (should be 400)\n", counter);
173 /* Test 'continue'. */
174 objects = malloc (sizeof (id) * 20);
175 for (i = 0; i < 20; i++)
176 objects[i] = @"object";
178 array = [[MyArray alloc] initWithLength: 20
181 for (id object in array)
189 printf ("Counter was %d (should be 15)\n", counter);
195 objects = malloc (sizeof (id) * 20);
196 for (i = 0; i < 20; i++)
197 objects[i] = @"object";
199 array = [[MyArray alloc] initWithLength: 20
202 for (id object in array)
210 printf ("Counter was %d (should be 15)\n", counter);
215 /* Test 'break' and 'continue' in nested iterations. */
216 objects = malloc (sizeof (id) * 20);
217 for (i = 0; i < 20; i++)
218 objects[i] = @"object";
220 array = [[MyArray alloc] initWithLength: 20
223 for (id object in array)
225 int local_counter = 0;
227 /* Each internal loop should increase counter by 24. */
228 for (id another_object in array)
232 if (local_counter == 10)
234 counter = counter + 20;
238 if (local_counter >= 5)
244 /* Exit after 4 iterations. */
249 printf ("Counter was %d (should be 96)\n", counter);
254 /* Test that C for loops still work. */
257 for (counter = 0; counter < 4; counter++)
260 if (test_variable != 4)