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-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 *, ...);
24 unsigned long *mutationsPtr;
25 unsigned long extra[5];
26 } NSFastEnumerationState;
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 : TestsuiteObject
36 unsigned long mutated;
38 - (id) initWithLength: (unsigned int)l objects: (id *)o;
40 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState *)state
41 objects:(id *)stackbuf
42 count:(unsigned long)len;
45 @implementation MyArray : TestsuiteObject
46 - (id) initWithLength: (unsigned int)l
58 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState*)state
59 objects: (id*)stackbuf
60 count: (unsigned long)len
62 unsigned long i, batch_size;
64 /* We keep how many objects we served in the state->state counter. So the next batch
65 will contain up to length - state->state objects. */
66 batch_size = length - state->state;
68 /* Make obvious adjustments. */
75 /* Copy the objects. */
76 for (i = 0; i < batch_size; i++)
77 stackbuf[i] = objects[i];
79 state->state += batch_size;
80 state->itemsPtr = stackbuf;
81 state->mutationsPtr = &mutated;
90 int test_variable, counter, i;
93 array = [[MyArray alloc] initWithLength: 0
96 /* Test that an empty array does nothing. */
97 for (id object in array)
100 /* Test iterating over 1 object. */
101 objects = malloc (sizeof (id) * 1);
102 objects[0] = @"One Object";
104 array = [[MyArray alloc] initWithLength: 1
107 for (id object in array)
108 printf ("%p\n", object);
110 /* Test iterating over 20 objects. */
111 objects = malloc (sizeof (id) * 20);
112 for (i = 0; i < 20; i++)
113 objects[i] = @"object";
115 array = [[MyArray alloc] initWithLength: 20
118 for (id object in array)
119 printf ("%p\n", object);
121 /* Test iterating over 200 objects. */
122 objects = malloc (sizeof (id) * 200);
123 for (i = 0; i < 200; i++)
124 objects[i] = @"object";
126 array = [[MyArray alloc] initWithLength: 200
130 for (id object in array)
139 printf ("Counter was %d (should be 200)\n", counter);
141 /* Test iterating again over the same array. */
143 for (id object in array)
152 printf ("Counter was %d (should be 200)\n", counter);
154 /* Test nested iterations. */
155 objects = malloc (sizeof (id) * 20);
156 for (i = 0; i < 20; i++)
157 objects[i] = @"object";
159 array = [[MyArray alloc] initWithLength: 20
162 for (id object in array)
164 for (id another_object in array)
165 if (another_object != nil)
169 printf ("Counter was %d (should be 400)\n", counter);
174 /* Test 'continue'. */
175 objects = malloc (sizeof (id) * 20);
176 for (i = 0; i < 20; i++)
177 objects[i] = @"object";
179 array = [[MyArray alloc] initWithLength: 20
182 for (id object in array)
190 printf ("Counter was %d (should be 15)\n", counter);
196 objects = malloc (sizeof (id) * 20);
197 for (i = 0; i < 20; i++)
198 objects[i] = @"object";
200 array = [[MyArray alloc] initWithLength: 20
203 for (id object in array)
211 printf ("Counter was %d (should be 15)\n", counter);
216 /* Test 'break' and 'continue' in nested iterations. */
217 objects = malloc (sizeof (id) * 20);
218 for (i = 0; i < 20; i++)
219 objects[i] = @"object";
221 array = [[MyArray alloc] initWithLength: 20
224 for (id object in array)
226 int local_counter = 0;
228 /* Each internal loop should increase counter by 24. */
229 for (id another_object in array)
233 if (local_counter == 10)
235 counter = counter + 20;
239 if (local_counter >= 5)
245 /* Exit after 4 iterations. */
250 printf ("Counter was %d (should be 96)\n", counter);
255 /* Test that C for loops still work. */
258 for (counter = 0; counter < 4; counter++)
261 if (test_variable != 4)