OSDN Git Service

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