1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'protocol', covering all functions starting with 'protocol'. */
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 /* { dg-additional-options "-Wno-objc-root-class" } */
10 /* To get the modern GNU Objective-C Runtime API, you include
12 #include <objc/runtime.h>
17 @interface MyRootClass
24 @implementation MyRootClass
25 + alloc { return class_createInstance (self, 0); }
26 - init { return self; }
27 + initialize { return self; }
34 @protocol MySecondProtocol
35 - (id) setVariable: (id)value;
38 @protocol MyThirdProtocol <MySecondProtocol>
39 - (id) setAnotherVariable: (id)value;
42 @interface MySubClass : MyRootClass <MyProtocol>
44 - (void) setVariable: (id)value;
48 @implementation MySubClass
49 - (void) setVariable: (id)value { variable_ivar = value; }
50 - (id) variable { return variable_ivar; }
54 int main(int argc, void **args)
56 /* Functions are tested in alphabetical order. */
58 printf ("Testing protocol_conformsToProtocol ()...\n");
60 if (!protocol_conformsToProtocol (@protocol (MyProtocol),
61 @protocol (MyProtocol)))
64 if (!protocol_conformsToProtocol (@protocol (MyThirdProtocol),
65 @protocol (MySecondProtocol)))
68 if (protocol_conformsToProtocol (@protocol (MyProtocol),
69 @protocol (MySecondProtocol)))
73 printf ("Testing protocol_copyMethodDescriptionList ()...\n");
76 struct objc_method_description *list;
78 list = protocol_copyMethodDescriptionList (@protocol (MyThirdProtocol),
84 if (strcmp (sel_getName (list[0].name), "setAnotherVariable:") != 0)
87 if (list[1].name != NULL && list[1].types != NULL)
91 /* TODO: Test new ABI (when available). */
92 printf ("Testing protocol_copyPropertyList ()...\n");
95 objc_property_t *list;
97 list = protocol_copyPropertyList (@protocol (MyProtocol), &count);
99 if (count != 0 || list != NULL)
103 printf ("Testing protocol_copyProtocolList ()...\n");
108 list = protocol_copyProtocolList (@protocol (MyThirdProtocol), &count);
113 if (strcmp (protocol_getName (list[0]), "MySecondProtocol") != 0)
120 printf ("Testing protocol_getMethodDescription ()...\n");
122 struct objc_method_description description;
124 description = protocol_getMethodDescription (@protocol (MySecondProtocol),
125 @selector (setVariable:),
127 if (description.name == NULL && description.types == NULL)
130 if (strcmp (sel_getName (description.name), "setVariable:") != 0)
134 printf ("Testing protocol_getName ()...\n");
136 if (strcmp (protocol_getName (@protocol (MyProtocol)), "MyProtocol") != 0)
140 /* TODO: Test new ABI (when available). */
141 printf ("Testing protocol_getProperty ()...\n");
143 objc_property_t property;
145 property = protocol_getProperty (objc_getProtocol ("MyProtocol"), "someProperty",
148 if (property != NULL)
152 printf ("Testing protocol_isEqual ()...\n");
154 if (!protocol_isEqual (@protocol (MyProtocol),
155 @protocol (MyProtocol)))
158 if (!protocol_isEqual (@protocol (MyProtocol),
159 objc_getProtocol ("MyProtocol")))