OSDN Git Service

2010-11-13 Paolo Bonzini <bonzini@gnu.org>
[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 "" { *-*-* } { "-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 @end
20
21 @implementation MyRootClass
22 + alloc { return class_createInstance (self, 0); }
23 - init  { return self; }
24 @end
25
26 @protocol MyProtocol
27 - (id) variable;
28 @end
29
30 @protocol MySecondProtocol
31 - (id) setVariable: (id)value;
32 @end
33
34 @protocol MyThirdProtocol <MySecondProtocol>
35 - (id) setAnotherVariable: (id)value;
36 @end
37
38 @interface MySubClass : MyRootClass <MyProtocol>
39 { id variable_ivar; }
40 - (void) setVariable: (id)value;
41 - (id) variable;
42 @end
43
44 @implementation MySubClass
45 - (void) setVariable: (id)value { variable_ivar = value; }
46 - (id) variable { return variable_ivar; }
47 @end
48
49
50 int main(int argc, void **args)
51 {
52   /* Functions are tested in alphabetical order.  */
53
54   printf ("Testing protocol_conformsToProtocol ()...\n");
55   {
56     if (!protocol_conformsToProtocol (@protocol (MyProtocol),
57                                       @protocol (MyProtocol)))
58       abort ();
59
60     if (!protocol_conformsToProtocol (@protocol (MyThirdProtocol),
61                                       @protocol (MySecondProtocol)))
62       abort ();
63
64     if (protocol_conformsToProtocol (@protocol (MyProtocol),
65                                      @protocol (MySecondProtocol)))
66       abort ();
67   }
68
69   printf ("Testing protocol_copyMethodDescriptionList ()...\n");
70   {
71     unsigned int count;
72     struct objc_method_description *list;
73
74     list = protocol_copyMethodDescriptionList (@protocol (MyThirdProtocol),
75                                                YES, YES, &count);
76     
77     if (count != 1)
78       abort ();
79
80     if (strcmp (sel_getName (list[0].name), "setAnotherVariable:") != 0)
81       abort ();
82     
83     if (list[1].name != NULL  &&  list[1].types != NULL)
84       abort ();
85   }
86
87   /* TODO: Test new ABI (when available).  */
88   printf ("Testing protocol_copyPropertyList ()...\n");
89   {
90     unsigned int count;
91     Property *list;
92
93     list = protocol_copyPropertyList (@protocol (MyProtocol), &count);
94
95     if (count != 0  ||  list != NULL)
96       abort ();
97   }
98
99   printf ("Testing protocol_copyProtocolList ()...\n");
100   {
101     unsigned int count;
102     Protocol **list;
103
104     list = protocol_copyProtocolList (@protocol (MyThirdProtocol), &count);
105     
106     if (count != 1)
107       abort ();
108
109     if (strcmp (protocol_getName (list[0]), "MySecondProtocol") != 0)
110       abort ();
111     
112     if (list[1] != NULL)
113       abort ();
114   }
115
116   printf ("Testing protocol_getMethodDescription ()...\n");
117   {
118     struct objc_method_description description;
119
120     description = protocol_getMethodDescription (@protocol (MySecondProtocol),
121                                                  @selector (setVariable:),
122                                                  YES, YES);
123     if (description.name == NULL  &&  description.types == NULL)
124       abort ();
125
126     if (strcmp (sel_getName (description.name), "setVariable:") != 0)
127       abort ();
128   }
129
130   printf ("Testing protocol_getName ()...\n");
131   {
132     if (strcmp (protocol_getName (@protocol (MyProtocol)), "MyProtocol") != 0)
133       abort ();
134   }
135
136   /* TODO: Test new ABI (when available).  */
137   printf ("Testing protocol_getProperty ()...\n");
138   {
139     Property property;
140
141     property = protocol_getProperty (objc_getProtocol ("MyProtocol"), "someProperty",
142                                      YES, YES);
143
144     if (property != NULL)
145       abort ();
146   }
147
148   printf ("Testing protocol_isEqual ()...\n");
149   {
150     if (!protocol_isEqual (@protocol (MyProtocol),
151                            @protocol (MyProtocol)))
152       abort ();
153
154     if (!protocol_isEqual (@protocol (MyProtocol),
155                            objc_getProtocol ("MyProtocol")))
156       abort ();
157   }
158
159   return 0;
160 }