1 /* Test basic Objective-C foreach syntax. This tests that if you
2 define your own NSFastEnumeration struct, the compiler picks it up.
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 *, ...);
21 unsigned long *mutationsPtr;
22 unsigned long extra[5];
23 } NSFastEnumerationState;
25 /* A mini-array implementation that can be used to test fast
26 enumeration. You create the array with some objects; you can
27 mutate the array, and you can fast-enumerate it.
29 @interface MyArray : Object
33 unsigned long mutated;
35 - (id) initWithLength: (unsigned int)l objects: (id *)o;
37 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState *)state
38 objects:(id *)stackbuf
39 count:(unsigned long)len;
42 @implementation MyArray : Object
43 - (id) initWithLength: (unsigned int)l
54 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState*)state
55 objects: (id*)stackbuf
56 count: (unsigned long)len
58 unsigned long i, batch_size;
60 /* We keep how many objects we served in the state->state counter. So the next batch
61 will contain up to length - state->state objects. */
62 batch_size = length - state->state;
64 /* Make obvious adjustments. */
71 /* Copy the objects. */
72 for (i = 0; i < batch_size; i++)
73 stackbuf[i] = objects[i];
75 state->state += batch_size;
76 state->itemsPtr = stackbuf;
77 state->mutationsPtr = &mutated;
86 int test_variable, counter, i;
89 array = [[MyArray alloc] initWithLength: 0
92 /* Test that an empty array does nothing. */
93 for (id object in array)
96 /* Test iterating over 1 object. */
97 objects = malloc (sizeof (id) * 1);
98 objects[0] = @"One Object";
100 array = [[MyArray alloc] initWithLength: 1
103 for (id object in array)
104 printf ("%p\n", object);
106 /* Test iterating over 20 objects. */
107 objects = malloc (sizeof (id) * 20);
108 for (i = 0; i < 20; i++)
109 objects[i] = @"object";
111 array = [[MyArray alloc] initWithLength: 20
114 for (id object in array)
115 printf ("%p\n", object);
117 /* Test iterating over 200 objects. */
118 objects = malloc (sizeof (id) * 200);
119 for (i = 0; i < 200; i++)
120 objects[i] = @"object";
122 array = [[MyArray alloc] initWithLength: 200
126 for (id object in array)
135 printf ("Counter was %d (should be 200)\n", counter);
137 /* Test iterating again over the same array. */
139 for (id object in array)
148 printf ("Counter was %d (should be 200)\n", counter);
150 /* Test nested iterations. */
151 objects = malloc (sizeof (id) * 20);
152 for (i = 0; i < 20; i++)
153 objects[i] = @"object";
155 array = [[MyArray alloc] initWithLength: 20
158 for (id object in array)
160 for (id another_object in array)
161 if (another_object != nil)
165 printf ("Counter was %d (should be 400)\n", counter);
170 /* Test 'continue'. */
171 objects = malloc (sizeof (id) * 20);
172 for (i = 0; i < 20; i++)
173 objects[i] = @"object";
175 array = [[MyArray alloc] initWithLength: 20
178 for (id object in array)
186 printf ("Counter was %d (should be 15)\n", counter);
192 objects = malloc (sizeof (id) * 20);
193 for (i = 0; i < 20; i++)
194 objects[i] = @"object";
196 array = [[MyArray alloc] initWithLength: 20
199 for (id object in array)
207 printf ("Counter was %d (should be 15)\n", counter);
212 /* Test 'break' and 'continue' in nested iterations. */
213 objects = malloc (sizeof (id) * 20);
214 for (i = 0; i < 20; i++)
215 objects[i] = @"object";
217 array = [[MyArray alloc] initWithLength: 20
220 for (id object in array)
222 int local_counter = 0;
224 /* Each internal loop should increase counter by 24. */
225 for (id another_object in array)
229 if (local_counter == 10)
231 counter = counter + 20;
235 if (local_counter >= 5)
241 /* Exit after 4 iterations. */
246 printf ("Counter was %d (should be 96)\n", counter);
251 /* Test that C for loops still work. */
254 for (counter = 0; counter < 4; counter++)
257 if (test_variable != 4)