PR target/83368
[official-gcc.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-protocol.mm
blob9a2ceca1353fb2abeabe04916a0115a390c8cd35
1 /* Test the Modern GNU Objective-C Runtime API.
3   This is test 'protocol', covering all functions starting with 'protocol'.  */
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" } { "" } } */
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 <iostream>
14 #include <cstring>
16 @interface MyRootClass
17 { Class isa; }
18 + alloc;
19 - init;
20 + initialize;
21 @end
23 @implementation MyRootClass
24 + alloc { return class_createInstance (self, 0); }
25 - init  { return self; }
26 + initialize { return self; }
27 @end
29 @protocol MyProtocol
30 - (id) variable;
31 @end
33 @protocol MySecondProtocol
34 - (id) setVariable: (id)value;
35 @end
37 @protocol MyThirdProtocol <MySecondProtocol>
38 - (id) setAnotherVariable: (id)value;
39 @end
41 @interface MySubClass : MyRootClass <MyProtocol>
42 { id variable_ivar; }
43 - (void) setVariable: (id)value;
44 - (id) variable;
45 @end
47 @implementation MySubClass
48 - (void) setVariable: (id)value { variable_ivar = value; }
49 - (id) variable { return variable_ivar; }
50 @end
53 int main ()
55   /* Functions are tested in alphabetical order.  */
57   std::cout << "Testing protocol_conformsToProtocol ()...\n";
58   {
59     if (!protocol_conformsToProtocol (@protocol (MyProtocol),
60                                       @protocol (MyProtocol)))
61       abort ();
63     if (!protocol_conformsToProtocol (@protocol (MyThirdProtocol),
64                                       @protocol (MySecondProtocol)))
65       abort ();
67     if (protocol_conformsToProtocol (@protocol (MyProtocol),
68                                      @protocol (MySecondProtocol)))
69       abort ();
70   }
72   std::cout << "Testing protocol_copyMethodDescriptionList ()...\n";
73   {
74     unsigned int count;
75     struct objc_method_description *list;
77     list = protocol_copyMethodDescriptionList (@protocol (MyThirdProtocol),
78                                                YES, YES, &count);
79     
80     if (count != 1)
81       abort ();
83     if (std::strcmp (sel_getName (list[0].name), "setAnotherVariable:") != 0)
84       abort ();
85     
86     if (list[1].name != NULL  &&  list[1].types != NULL)
87       abort ();
88   }
90   /* TODO: Test new ABI (when available).  */
91   std::cout << "Testing protocol_copyPropertyList ()...\n";
92   {
93     unsigned int count;
94     objc_property_t *list;
96     list = protocol_copyPropertyList (@protocol (MyProtocol), &count);
98     if (count != 0  ||  list != NULL)
99       abort ();
100   }
102   std::cout << "Testing protocol_copyProtocolList ()...\n";
103   {
104     unsigned int count;
105     Protocol **list;
107     list = protocol_copyProtocolList (@protocol (MyThirdProtocol), &count);
108     
109     if (count != 1)
110       abort ();
112     if (std::strcmp (protocol_getName (list[0]), "MySecondProtocol") != 0)
113       abort ();
114     
115     if (list[1] != NULL)
116       abort ();
117   }
119   std::cout << "Testing protocol_getMethodDescription ()...\n";
120   {
121     struct objc_method_description description;
123     description = protocol_getMethodDescription (@protocol (MySecondProtocol),
124                                                  @selector (setVariable:),
125                                                  YES, YES);
126     if (description.name == NULL  &&  description.types == NULL)
127       abort ();
129     if (std::strcmp (sel_getName (description.name), "setVariable:") != 0)
130       abort ();
131   }
133   std::cout << "Testing protocol_getName ()...\n";
134   {
135     if (std::strcmp (protocol_getName (@protocol (MyProtocol)), "MyProtocol") != 0)
136       abort ();
137   }
139   /* TODO: Test new ABI (when available).  */
140   std::cout << "Testing protocol_getProperty ()...\n";
141   {
142     objc_property_t property;
144     property = protocol_getProperty (objc_getProtocol ("MyProtocol"), "someProperty",
145                                      YES, YES);
147     if (property != NULL)
148       abort ();
149   }
151   std::cout << "Testing protocol_isEqual ()...\n";
152   {
153     if (!protocol_isEqual (@protocol (MyProtocol),
154                            @protocol (MyProtocol)))
155       abort ();
157     if (!protocol_isEqual (@protocol (MyProtocol),
158                            objc_getProtocol ("MyProtocol")))
159       abort ();
160   }
162   return (0);