OSDN Git Service

In gcc/objc/:
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / foreach-1.m
1 /* Test basic Objective-C foreach syntax.  This tests iterations that
2    do nothing.
3 */
4 /* { dg-do run } */
5 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
6 /* { dg-additional-sources "../objc-obj-c++-shared/Object1.m" } */
7
8 #import "../objc-obj-c++-shared/Object1.h"
9
10 extern void abort (void);
11 /*
12 struct __objcFastEnumerationState
13 {
14   unsigned long state;
15   id            *itemsPtr;
16   unsigned long *mutationsPtr;
17   unsigned long extra[5];
18 };
19 */
20 @interface Object (NSFastEnumeration)
21 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
22                                      objects:(id *)stackbuf 
23                                        count:(unsigned int)len;
24 @end
25
26 int main (void)
27 {
28   int test_variable = 0;
29   int counter = 0;
30   id array = nil;
31   id object = nil;
32
33   /* Test that 'for (object in array)' is recognized and that nothing
34      happens if array is nil.  */
35   for (object in array)
36     test_variable = 8;
37
38   if (test_variable == 8)
39     abort ();
40
41   if (object != nil)
42     abort ();
43
44   /* Test that if nothing is done, object is set to nil.  */
45   object = [Object new];
46
47   for (object in array)
48     ;
49
50   if (object != nil)
51     abort ();
52
53   /* Test that you can reference 'object' inside the body.  */
54   for (object in array)
55     object = nil;
56
57   if (object != nil)
58     abort ();
59
60   /* Test that 'for (id element in array) is recognized (and works).  */
61   for (id element in array)
62     test_variable = 8;
63
64   if (test_variable == 8)
65     abort ();
66
67   /* Test that you can reference 'object' inside the body.  */
68   for (id element in array)
69     element = nil;
70
71   /* Test that C for loops still work.  */
72   test_variable = 0;
73
74   for (counter = 0; counter < 4; counter++)
75     test_variable++;
76
77   if (test_variable != 4)
78     abort ();
79
80   return 0;
81 }