1 /* Test basic Objective-C foreach syntax. This tests the mutation.
3 /* { dg-do compile } */
5 /* FIXME: This test should be run, and it succeeds if the program
6 aborts at the right time (when the mutation happens). It currently
7 works, but how do we tell the testsuite to test for it ?
10 #include "../objc-obj-c++-shared/TestsuiteObject.m"
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 : TestsuiteObject
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 : TestsuiteObject
46 - (id) initWithLength: (unsigned int)l
58 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state
59 objects: (id*)stackbuf
60 count: (unsigned long)len
62 unsigned long i, batch_size;
64 /* Change the mutationsPtr if 'mutate' is called. */
65 state->mutationsPtr = &mutated;
67 /* We keep how many objects we served in the state->state counter. So the next batch
68 will contain up to length - state->state objects. */
69 batch_size = length - state->state;
71 /* Make obvious adjustments. */
78 /* Copy the objects. */
79 for (i = 0; i < batch_size; i++)
80 stackbuf[i] = objects[i];
82 state->state += batch_size;
83 state->itemsPtr = stackbuf;
92 TestsuiteObject *object;
96 /* Test iterating over 20 objects, mutating after 15. */
97 objects = malloc (sizeof (id) * 20);
98 for (i = 0; i < 20; i++)
99 objects[i] = @"object";
101 array = [[MyArray alloc] initWithLength: 20
105 for (object in array)
108 printf ("%d\n", counter);
111 printf ("Mutating (should abort at next iteration)\n");