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-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 *, ...);
22 unsigned long *mutationsPtr;
23 unsigned long extra[5];
24 } NSFastEnumerationState;
26 /* A mini-array implementation that can be used to test fast
27 enumeration. You create the array with some objects; you can
28 mutate the array, and you can fast-enumerate it.
30 @interface MyArray : Object
34 unsigned long mutated;
36 - (id) initWithLength: (unsigned int)l objects: (id *)o;
38 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState *)state
39 objects:(id *)stackbuf
40 count:(unsigned long)len;
43 @implementation MyArray : Object
44 - (id) initWithLength: (unsigned int)l
55 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState*)state
56 objects: (id*)stackbuf
57 count: (unsigned long)len
59 unsigned long i, batch_size;
61 /* We keep how many objects we served in the state->state counter. So the next batch
62 will contain up to length - state->state objects. */
63 batch_size = length - state->state;
65 /* Make obvious adjustments. */
72 /* Copy the objects. */
73 for (i = 0; i < batch_size; i++)
74 stackbuf[i] = objects[i];
76 state->state += batch_size;
77 state->itemsPtr = stackbuf;
78 state->mutationsPtr = &mutated;
87 int test_variable, counter, i;
90 array = [[MyArray alloc] initWithLength: 0
93 /* Test that an empty array does nothing. */
94 for (id object in array)
97 /* Test iterating over 1 object. */
98 objects = malloc (sizeof (id) * 1);
99 objects[0] = @"One Object";
101 array = [[MyArray alloc] initWithLength: 1
104 for (id object in array)
105 printf ("%p\n", object);
107 /* Test iterating over 20 objects. */
108 objects = malloc (sizeof (id) * 20);
109 for (i = 0; i < 20; i++)
110 objects[i] = @"object";
112 array = [[MyArray alloc] initWithLength: 20
115 for (id object in array)
116 printf ("%p\n", object);
118 /* Test iterating over 200 objects. */
119 objects = malloc (sizeof (id) * 200);
120 for (i = 0; i < 200; i++)
121 objects[i] = @"object";
123 array = [[MyArray alloc] initWithLength: 200
127 for (id object in array)
136 printf ("Counter was %d (should be 200)\n", counter);
138 /* Test iterating again over the same array. */
140 for (id object in array)
149 printf ("Counter was %d (should be 200)\n", counter);
151 /* Test nested iterations. */
152 objects = malloc (sizeof (id) * 20);
153 for (i = 0; i < 20; i++)
154 objects[i] = @"object";
156 array = [[MyArray alloc] initWithLength: 20
159 for (id object in array)
161 for (id another_object in array)
162 if (another_object != nil)
166 printf ("Counter was %d (should be 400)\n", counter);
171 /* Test 'continue'. */
172 objects = malloc (sizeof (id) * 20);
173 for (i = 0; i < 20; i++)
174 objects[i] = @"object";
176 array = [[MyArray alloc] initWithLength: 20
179 for (id object in array)
187 printf ("Counter was %d (should be 15)\n", counter);
193 objects = malloc (sizeof (id) * 20);
194 for (i = 0; i < 20; i++)
195 objects[i] = @"object";
197 array = [[MyArray alloc] initWithLength: 20
200 for (id object in array)
208 printf ("Counter was %d (should be 15)\n", counter);
213 /* Test 'break' and 'continue' in nested iterations. */
214 objects = malloc (sizeof (id) * 20);
215 for (i = 0; i < 20; i++)
216 objects[i] = @"object";
218 array = [[MyArray alloc] initWithLength: 20
221 for (id object in array)
223 int local_counter = 0;
225 /* Each internal loop should increase counter by 24. */
226 for (id another_object in array)
230 if (local_counter == 10)
232 counter = counter + 20;
236 if (local_counter >= 5)
242 /* Exit after 4 iterations. */
247 printf ("Counter was %d (should be 96)\n", counter);
252 /* Test that C for loops still work. */
255 for (counter = 0; counter < 4; counter++)
258 if (test_variable != 4)