OSDN Git Service

2011-02-08 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[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-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-additional-sources "../objc-obj-c++-shared/Object1.m" } */
8 /* { dg-options "-Wall" } */
9
10 #import "../objc-obj-c++-shared/Object1.h"
11
12 extern void abort (void);
13 /*
14 struct __objcFastEnumerationState
15 {
16   unsigned long state;
17   id            *itemsPtr;
18   unsigned long *mutationsPtr;
19   unsigned long extra[5];
20 };
21 */
22 @interface Object (NSFastEnumeration)
23 - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state
24                                      objects:(id *)stackbuf 
25                                        count:(unsigned int)len;
26 @end
27
28 int main (void)
29 {
30   int test_variable = 0;
31   int counter = 0;
32   id array = nil;
33   id object = nil;
34
35   /* Test that 'for (object in array)' is recognized and that nothing
36      happens if array is nil.  */
37   for (object in array)
38     test_variable = 8;
39
40   if (test_variable == 8)
41     abort ();
42
43   if (object != nil)
44     abort ();
45
46   /* Test that if nothing is done, object is set to nil.  */
47   object = [Object new];
48
49   for (object in array)
50     ;
51
52   if (object != nil)
53     abort ();
54
55   /* Test that you can reference 'object' inside the body.  */
56   for (object in array)
57     object = nil;
58
59   if (object != nil)
60     abort ();
61
62   /* Test that 'for (id element in array) is recognized (and works).  */
63   for (id element in array)
64     test_variable = 8;
65
66   if (test_variable == 8)
67     abort ();
68
69   /* Test that you can reference 'object' inside the body.  */
70   for (id element in array)
71     element = nil;
72
73   /* Test that C for loops still work.  */
74   test_variable = 0;
75
76   for (counter = 0; counter < 4; counter++)
77     test_variable++;
78
79   if (test_variable != 4)
80     abort ();
81
82   return 0;
83 }