1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'protocol', covering all functions starting with 'protocol'. */
6 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
8 /* To get the modern GNU Objective-C Runtime API, you include
10 #include <objc/runtime.h>
15 @interface MyRootClass
22 @implementation MyRootClass
23 + alloc { return class_createInstance (self, 0); }
24 - init { return self; }
25 + initialize { return self; }
32 @protocol MySecondProtocol
33 - (id) setVariable: (id)value;
36 @protocol MyThirdProtocol <MySecondProtocol>
37 - (id) setAnotherVariable: (id)value;
40 @interface MySubClass : MyRootClass <MyProtocol>
42 - (void) setVariable: (id)value;
46 @implementation MySubClass
47 - (void) setVariable: (id)value { variable_ivar = value; }
48 - (id) variable { return variable_ivar; }
52 int main(int argc, void **args)
54 /* Functions are tested in alphabetical order. */
56 printf ("Testing protocol_conformsToProtocol ()...\n");
58 if (!protocol_conformsToProtocol (@protocol (MyProtocol),
59 @protocol (MyProtocol)))
62 if (!protocol_conformsToProtocol (@protocol (MyThirdProtocol),
63 @protocol (MySecondProtocol)))
66 if (protocol_conformsToProtocol (@protocol (MyProtocol),
67 @protocol (MySecondProtocol)))
71 printf ("Testing protocol_copyMethodDescriptionList ()...\n");
74 struct objc_method_description *list;
76 list = protocol_copyMethodDescriptionList (@protocol (MyThirdProtocol),
82 if (strcmp (sel_getName (list[0].name), "setAnotherVariable:") != 0)
85 if (list[1].name != NULL && list[1].types != NULL)
89 /* TODO: Test new ABI (when available). */
90 printf ("Testing protocol_copyPropertyList ()...\n");
93 objc_property_t *list;
95 list = protocol_copyPropertyList (@protocol (MyProtocol), &count);
97 if (count != 0 || list != NULL)
101 printf ("Testing protocol_copyProtocolList ()...\n");
106 list = protocol_copyProtocolList (@protocol (MyThirdProtocol), &count);
111 if (strcmp (protocol_getName (list[0]), "MySecondProtocol") != 0)
118 printf ("Testing protocol_getMethodDescription ()...\n");
120 struct objc_method_description description;
122 description = protocol_getMethodDescription (@protocol (MySecondProtocol),
123 @selector (setVariable:),
125 if (description.name == NULL && description.types == NULL)
128 if (strcmp (sel_getName (description.name), "setVariable:") != 0)
132 printf ("Testing protocol_getName ()...\n");
134 if (strcmp (protocol_getName (@protocol (MyProtocol)), "MyProtocol") != 0)
138 /* TODO: Test new ABI (when available). */
139 printf ("Testing protocol_getProperty ()...\n");
141 objc_property_t property;
143 property = protocol_getProperty (objc_getProtocol ("MyProtocol"), "someProperty",
146 if (property != NULL)
150 printf ("Testing protocol_isEqual ()...\n");
152 if (!protocol_isEqual (@protocol (MyProtocol),
153 @protocol (MyProtocol)))
156 if (!protocol_isEqual (@protocol (MyProtocol),
157 objc_getProtocol ("MyProtocol")))