OSDN Git Service

In gcc/testsuite/:
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / gnu-api-2-protocol.m
1 /* Test the Modern GNU Objective-C Runtime API.
2
3   This is test 'protocol', covering all functions starting with 'protocol'.  */
4
5 /* { dg-do run } */
6 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
7
8 /* To get the modern GNU Objective-C Runtime API, you include
9    objc/runtime.h.  */
10 #include <objc/runtime.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 @interface MyRootClass
16 { Class isa; }
17 + alloc;
18 - init;
19 + initialize;
20 @end
21
22 @implementation MyRootClass
23 + alloc { return class_createInstance (self, 0); }
24 - init  { return self; }
25 + initialize { return self; }
26 @end
27
28 @protocol MyProtocol
29 - (id) variable;
30 @end
31
32 @protocol MySecondProtocol
33 - (id) setVariable: (id)value;
34 @end
35
36 @protocol MyThirdProtocol <MySecondProtocol>
37 - (id) setAnotherVariable: (id)value;
38 @end
39
40 @interface MySubClass : MyRootClass <MyProtocol>
41 { id variable_ivar; }
42 - (void) setVariable: (id)value;
43 - (id) variable;
44 @end
45
46 @implementation MySubClass
47 - (void) setVariable: (id)value { variable_ivar = value; }
48 - (id) variable { return variable_ivar; }
49 @end
50
51
52 int main(int argc, void **args)
53 {
54   /* Functions are tested in alphabetical order.  */
55
56   printf ("Testing protocol_conformsToProtocol ()...\n");
57   {
58     if (!protocol_conformsToProtocol (@protocol (MyProtocol),
59                                       @protocol (MyProtocol)))
60       abort ();
61
62     if (!protocol_conformsToProtocol (@protocol (MyThirdProtocol),
63                                       @protocol (MySecondProtocol)))
64       abort ();
65
66     if (protocol_conformsToProtocol (@protocol (MyProtocol),
67                                      @protocol (MySecondProtocol)))
68       abort ();
69   }
70
71   printf ("Testing protocol_copyMethodDescriptionList ()...\n");
72   {
73     unsigned int count;
74     struct objc_method_description *list;
75
76     list = protocol_copyMethodDescriptionList (@protocol (MyThirdProtocol),
77                                                YES, YES, &count);
78     
79     if (count != 1)
80       abort ();
81
82     if (strcmp (sel_getName (list[0].name), "setAnotherVariable:") != 0)
83       abort ();
84     
85     if (list[1].name != NULL  &&  list[1].types != NULL)
86       abort ();
87   }
88
89   /* TODO: Test new ABI (when available).  */
90   printf ("Testing protocol_copyPropertyList ()...\n");
91   {
92     unsigned int count;
93     objc_property_t *list;
94
95     list = protocol_copyPropertyList (@protocol (MyProtocol), &count);
96
97     if (count != 0  ||  list != NULL)
98       abort ();
99   }
100
101   printf ("Testing protocol_copyProtocolList ()...\n");
102   {
103     unsigned int count;
104     Protocol **list;
105
106     list = protocol_copyProtocolList (@protocol (MyThirdProtocol), &count);
107     
108     if (count != 1)
109       abort ();
110
111     if (strcmp (protocol_getName (list[0]), "MySecondProtocol") != 0)
112       abort ();
113     
114     if (list[1] != NULL)
115       abort ();
116   }
117
118   printf ("Testing protocol_getMethodDescription ()...\n");
119   {
120     struct objc_method_description description;
121
122     description = protocol_getMethodDescription (@protocol (MySecondProtocol),
123                                                  @selector (setVariable:),
124                                                  YES, YES);
125     if (description.name == NULL  &&  description.types == NULL)
126       abort ();
127
128     if (strcmp (sel_getName (description.name), "setVariable:") != 0)
129       abort ();
130   }
131
132   printf ("Testing protocol_getName ()...\n");
133   {
134     if (strcmp (protocol_getName (@protocol (MyProtocol)), "MyProtocol") != 0)
135       abort ();
136   }
137
138   /* TODO: Test new ABI (when available).  */
139   printf ("Testing protocol_getProperty ()...\n");
140   {
141     objc_property_t property;
142
143     property = protocol_getProperty (objc_getProtocol ("MyProtocol"), "someProperty",
144                                      YES, YES);
145
146     if (property != NULL)
147       abort ();
148   }
149
150   printf ("Testing protocol_isEqual ()...\n");
151   {
152     if (!protocol_isEqual (@protocol (MyProtocol),
153                            @protocol (MyProtocol)))
154       abort ();
155
156     if (!protocol_isEqual (@protocol (MyProtocol),
157                            objc_getProtocol ("MyProtocol")))
158       abort ();
159   }
160
161   return 0;
162 }