OSDN Git Service

In gcc/objc/:
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / obj-c++.dg / property / dotsyntax-6.mm
1 /* Contributed by Nicola Pero <nicola.pero@meta-innovation.com>, November 2010.  */
2 /* { dg-do run } */
3 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
4
5 /* Test nested 'dot syntax' (xxx.yyy.zzz or [xxx yyy].zzz).  */
6
7 #include <stdlib.h>
8 #include <objc/objc.h>
9 #include <objc/runtime.h>
10
11 @class MyRootClass;
12
13 static int c;
14 static MyRootClass *shared_root = nil;
15
16 @interface MyRootClass
17 {
18   Class isa;
19   int a;
20   int b;
21   MyRootClass *next;
22 }
23 @property int b;
24 @property (assign) MyRootClass *next;
25 + (id) initialize;
26 + (MyRootClass *)sharedInstance;
27 + (id) alloc;
28 - (id) init;
29 - (MyRootClass *)same;
30 - (int) count;
31 - (void) setCount: (int)count;
32 @end
33
34 @implementation MyRootClass
35 @synthesize b;
36 @synthesize next;
37 + (id) initialize { return self; }
38 + (id) alloc { return class_createInstance (self, 0); }
39 - (id) init { return self; }
40 + (MyRootClass *)sharedInstance
41 {
42   if (!shared_root)
43     shared_root = [[self alloc] init];
44
45   return shared_root;
46 }
47 - (MyRootClass *)same
48 {
49   return self;
50 }
51 - (int) count
52 {
53   return a;
54 }
55 - (void) setCount: (int)count
56 {
57   a = count;
58 }
59 @end
60
61 int main (void)
62 {
63   MyRootClass *object = [[MyRootClass alloc] init];
64
65   /* Test ClassName.accessor.accessor.  */
66   MyRootClass.sharedInstance.count = 500;
67   if (MyRootClass.sharedInstance.count != 500)
68     abort ();
69
70   /* Test object.accessor.accessor.  */
71   object.same.count = 1000;
72   if (object.same.count != 1000)
73     abort ();
74
75   /* Test object.accessor.property.  */
76   object.same.next = object;
77   if (object.same.next != object)
78     abort ();
79
80   /* Test lots of nesting.  */
81   if (object.next.next.same.same.next.next.same != object)
82     abort ();
83
84   /* Test more nesting.  */
85   MyRootClass.sharedInstance.next = object;
86   MyRootClass.sharedInstance.next.next.next.next.next.count = 2000;
87   if (MyRootClass.sharedInstance.next.next.next.next.next.count != 2000)
88     abort ();
89
90   /* Test more nesting.  */
91   MyRootClass.sharedInstance.same.same.same.same.same.count = 3000;
92   if (MyRootClass.sharedInstance.same.same.same.same.same.count != 3000)
93     abort ();
94
95   /* Test [object method].property.  */
96   [MyRootClass sharedInstance].count = 5000;
97   if ([MyRootClass sharedInstance].count != 5000)
98     abort ();
99
100   /* Just a final check.  */
101   if (shared_root.count != 5000)
102     abort ();
103
104   return 0;
105 }
106
107