PR inline-asm/84742
[official-gcc.git] / gcc / testsuite / objc.dg / foreach-3.m
blobba94797427e69c08c742c982e4f1635c72872740
1 /* Test basic Objective-C foreach syntax.  This tests the mutation.
2 */
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 ?
8 */
10 #include "../objc-obj-c++-shared/TestsuiteObject.m"
11 #ifndef __NEXT_RUNTIME__
12 #include <objc/NXConstStr.h>
13 #endif
15 extern int printf (const char *, ...);
16 #include <stdlib.h>
19 struct __objcFastEnumerationState
21   unsigned long state;
22   id            *itemsPtr;
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.
31  */
32 @interface MyArray : TestsuiteObject
34   unsigned int length;
35   id *objects;
36   unsigned long mutated;
38 - (id) initWithLength: (unsigned int)l  objects: (id *)o;
39 - (void) mutate;
40 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
41                                      objects:(id *)stackbuf 
42                                        count:(unsigned long)len;
43 @end
45 @implementation MyArray : TestsuiteObject
46 - (id) initWithLength: (unsigned int)l
47               objects: (id *)o
49   length = l;
50   objects = o;
51   mutated = 0;
52   return self;
54 - (void) mutate
56   mutated = 1;
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.  */
72   if (batch_size < 0)
73     batch_size = 0;
75   if (batch_size > len)
76     batch_size = len;
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;
85   return batch_size;
87 @end
89 int main (void)
91   MyArray *array;
92   TestsuiteObject *object;
93   int counter, i;
94   id *objects;
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";
100   
101   array = [[MyArray alloc] initWithLength: 20
102                            objects: objects];
103   
104   counter = 0;
105   for (object in array)
106     {
107       counter++;
108       printf ("%d\n", counter);
109       if (counter == 14)
110         {
111           printf ("Mutating (should abort at next iteration)\n");
112           [array mutate];
113         }
114     }
116   return 0;