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/Object1.m ../objc-obj-c++-shared/nsconstantstring-class-impl.m" } */
10 #import "../objc-obj-c++-shared/Object1.h"
11 #import "../objc-obj-c++-shared/next-mapping.h"
12 #ifndef __NEXT_RUNTIME__
13 #include <objc/NXConstStr.h>
15 #include "../objc-obj-c++-shared/nsconstantstring-class.h"
18 extern int printf (const char *, ...);
25 unsigned long *mutationsPtr;
26 unsigned long extra[5];
27 } NSFastEnumerationState;
29 /* A mini-array implementation that can be used to test fast
30 enumeration. You create the array with some objects; you can
31 mutate the array, and you can fast-enumerate it.
33 @interface MyArray : Object
37 unsigned long mutated;
39 - (id) initWithLength: (unsigned int)l objects: (id *)o;
41 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState *)state
42 objects:(id *)stackbuf
43 count:(unsigned long)len;
46 @implementation MyArray : Object
47 - (id) initWithLength: (unsigned int)l
59 - (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState*)state
60 objects: (id*)stackbuf
61 count: (unsigned long)len
63 unsigned long i, batch_size;
65 /* We keep how many objects we served in the state->state counter. So the next batch
66 will contain up to length - state->state objects. */
67 batch_size = length - state->state;
69 /* Make obvious adjustments. */
76 /* Copy the objects. */
77 for (i = 0; i < batch_size; i++)
78 stackbuf[i] = objects[i];
80 state->state += batch_size;
81 state->itemsPtr = stackbuf;
82 state->mutationsPtr = &mutated;
91 int test_variable, counter, i;
94 array = [[MyArray alloc] initWithLength: 0
97 /* Test that an empty array does nothing. */
98 for (id object in array)
101 /* Test iterating over 1 object. */
102 objects = malloc (sizeof (id) * 1);
103 objects[0] = @"One Object";
105 array = [[MyArray alloc] initWithLength: 1
108 for (id object in array)
109 printf ("%p\n", object);
111 /* Test iterating over 20 objects. */
112 objects = malloc (sizeof (id) * 20);
113 for (i = 0; i < 20; i++)
114 objects[i] = @"object";
116 array = [[MyArray alloc] initWithLength: 20
119 for (id object in array)
120 printf ("%p\n", object);
122 /* Test iterating over 200 objects. */
123 objects = malloc (sizeof (id) * 200);
124 for (i = 0; i < 200; i++)
125 objects[i] = @"object";
127 array = [[MyArray alloc] initWithLength: 200
131 for (id object in array)
140 printf ("Counter was %d (should be 200)\n", counter);
142 /* Test iterating again over the same array. */
144 for (id object in array)
153 printf ("Counter was %d (should be 200)\n", counter);
155 /* Test nested iterations. */
156 objects = malloc (sizeof (id) * 20);
157 for (i = 0; i < 20; i++)
158 objects[i] = @"object";
160 array = [[MyArray alloc] initWithLength: 20
163 for (id object in array)
165 for (id another_object in array)
166 if (another_object != nil)
170 printf ("Counter was %d (should be 400)\n", counter);
175 /* Test 'continue'. */
176 objects = malloc (sizeof (id) * 20);
177 for (i = 0; i < 20; i++)
178 objects[i] = @"object";
180 array = [[MyArray alloc] initWithLength: 20
183 for (id object in array)
191 printf ("Counter was %d (should be 15)\n", counter);
197 objects = malloc (sizeof (id) * 20);
198 for (i = 0; i < 20; i++)
199 objects[i] = @"object";
201 array = [[MyArray alloc] initWithLength: 20
204 for (id object in array)
212 printf ("Counter was %d (should be 15)\n", counter);
217 /* Test 'break' and 'continue' in nested iterations. */
218 objects = malloc (sizeof (id) * 20);
219 for (i = 0; i < 20; i++)
220 objects[i] = @"object";
222 array = [[MyArray alloc] initWithLength: 20
225 for (id object in array)
227 int local_counter = 0;
229 /* Each internal loop should increase counter by 24. */
230 for (id another_object in array)
234 if (local_counter == 10)
236 counter = counter + 20;
240 if (local_counter >= 5)
246 /* Exit after 4 iterations. */
251 printf ("Counter was %d (should be 96)\n", counter);
256 /* Test that C for loops still work. */
259 for (counter = 0; counter < 4; counter++)
262 if (test_variable != 4)