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" } { "" } } */
9 /* To get the modern GNU Objective-C Runtime API, you include
11 #include <objc/runtime.h>
16 @interface MyRootClass
23 @implementation MyRootClass
24 + alloc { return class_createInstance (self, 0); }
25 - init { return self; }
26 + initialize { return self; }
33 @protocol MySecondProtocol
34 - (id) setVariable: (id)value;
37 @protocol MyThirdProtocol <MySecondProtocol>
38 - (id) setAnotherVariable: (id)value;
41 @interface MySubClass : MyRootClass <MyProtocol>
43 - (void) setVariable: (id)value;
47 @implementation MySubClass
48 - (void) setVariable: (id)value { variable_ivar = value; }
49 - (id) variable { return variable_ivar; }
53 int main(int argc, void **args)
55 /* Functions are tested in alphabetical order. */
57 printf ("Testing protocol_conformsToProtocol ()...\n");
59 if (!protocol_conformsToProtocol (@protocol (MyProtocol),
60 @protocol (MyProtocol)))
63 if (!protocol_conformsToProtocol (@protocol (MyThirdProtocol),
64 @protocol (MySecondProtocol)))
67 if (protocol_conformsToProtocol (@protocol (MyProtocol),
68 @protocol (MySecondProtocol)))
72 printf ("Testing protocol_copyMethodDescriptionList ()...\n");
75 struct objc_method_description *list;
77 list = protocol_copyMethodDescriptionList (@protocol (MyThirdProtocol),
83 if (strcmp (sel_getName (list[0].name), "setAnotherVariable:") != 0)
86 if (list[1].name != NULL && list[1].types != NULL)
90 /* TODO: Test new ABI (when available). */
91 printf ("Testing protocol_copyPropertyList ()...\n");
94 objc_property_t *list;
96 list = protocol_copyPropertyList (@protocol (MyProtocol), &count);
98 if (count != 0 || list != NULL)
102 printf ("Testing protocol_copyProtocolList ()...\n");
107 list = protocol_copyProtocolList (@protocol (MyThirdProtocol), &count);
112 if (strcmp (protocol_getName (list[0]), "MySecondProtocol") != 0)
119 printf ("Testing protocol_getMethodDescription ()...\n");
121 struct objc_method_description description;
123 description = protocol_getMethodDescription (@protocol (MySecondProtocol),
124 @selector (setVariable:),
126 if (description.name == NULL && description.types == NULL)
129 if (strcmp (sel_getName (description.name), "setVariable:") != 0)
133 printf ("Testing protocol_getName ()...\n");
135 if (strcmp (protocol_getName (@protocol (MyProtocol)), "MyProtocol") != 0)
139 /* TODO: Test new ABI (when available). */
140 printf ("Testing protocol_getProperty ()...\n");
142 objc_property_t property;
144 property = protocol_getProperty (objc_getProtocol ("MyProtocol"), "someProperty",
147 if (property != NULL)
151 printf ("Testing protocol_isEqual ()...\n");
153 if (!protocol_isEqual (@protocol (MyProtocol),
154 @protocol (MyProtocol)))
157 if (!protocol_isEqual (@protocol (MyProtocol),
158 objc_getProtocol ("MyProtocol")))