5 #include <objc/runtime.h>
6 #include <objc/message.h>
10 @interface Super : TestRoot @end
12 +(void)classMethod { state = 1; }
13 -(void)instanceMethod { state = 4; }
14 +(void)classMethodSuperOnly { state = 3; }
15 -(void)instanceMethodSuperOnly { state = 6; }
18 @interface Sub : Super @end
20 +(void)classMethod { state = 2; }
21 -(void)instanceMethod { state = 5; }
24 typedef void (*imp_t)(id, SEL);
28 Class Super_cls, Sub_cls;
34 // don't use [Super class] to check laziness handing
35 Super_cls = objc_getClass("Super");
36 Sub_cls = objc_getClass("Sub");
38 sel = sel_registerName("classMethod");
39 m = class_getClassMethod(Super_cls, sel);
41 testassert(sel == method_getName(m));
42 imp = method_getImplementation(m);
43 testassert(imp == class_getMethodImplementation(object_getClass(Super_cls), sel));
44 testassert(imp == object_getMethodImplementation(Super_cls, sel));
46 (*(imp_t)imp)(Super_cls, sel);
47 testassert(state == 1);
49 sel = sel_registerName("classMethod");
50 m = class_getClassMethod(Sub_cls, sel);
52 testassert(sel == method_getName(m));
53 imp = method_getImplementation(m);
54 testassert(imp == class_getMethodImplementation(object_getClass(Sub_cls), sel));
55 testassert(imp == object_getMethodImplementation(Sub_cls, sel));
57 (*(imp_t)imp)(Sub_cls, sel);
58 testassert(state == 2);
60 sel = sel_registerName("classMethodSuperOnly");
61 m = class_getClassMethod(Sub_cls, sel);
63 testassert(sel == method_getName(m));
64 imp = method_getImplementation(m);
65 testassert(imp == class_getMethodImplementation(object_getClass(Sub_cls), sel));
66 testassert(imp == object_getMethodImplementation(Sub_cls, sel));
68 (*(imp_t)imp)(Sub_cls, sel);
69 testassert(state == 3);
71 sel = sel_registerName("instanceMethod");
72 m = class_getInstanceMethod(Super_cls, sel);
74 testassert(sel == method_getName(m));
75 imp = method_getImplementation(m);
76 testassert(imp == class_getMethodImplementation(Super_cls, sel));
78 testassert(imp == object_getMethodImplementation(objc_unretainedObject(buf), sel));
80 (*(imp_t)imp)(objc_unretainedObject(buf), sel);
81 testassert(state == 4);
83 sel = sel_registerName("instanceMethod");
84 m = class_getInstanceMethod(Sub_cls, sel);
86 testassert(sel == method_getName(m));
87 imp = method_getImplementation(m);
88 testassert(imp == class_getMethodImplementation(Sub_cls, sel));
90 testassert(imp == object_getMethodImplementation(objc_unretainedObject(buf), sel));
92 (*(imp_t)imp)(objc_unretainedObject(buf), sel);
93 testassert(state == 5);
95 sel = sel_registerName("instanceMethodSuperOnly");
96 m = class_getInstanceMethod(Sub_cls, sel);
98 testassert(sel == method_getName(m));
99 imp = method_getImplementation(m);
100 testassert(imp == class_getMethodImplementation(Sub_cls, sel));
102 testassert(imp == object_getMethodImplementation(objc_unretainedObject(buf), sel));
104 (*(imp_t)imp)(objc_unretainedObject(buf), sel);
105 testassert(state == 6);
107 // check class_getClassMethod(cls) == class_getInstanceMethod(cls->isa)
108 sel = sel_registerName("classMethod");
109 testassert(class_getClassMethod(Sub_cls, sel) == class_getInstanceMethod(object_getClass(Sub_cls), sel));
111 sel = sel_registerName("nonexistent");
112 testassert(! class_getInstanceMethod(Sub_cls, sel));
113 testassert(! class_getClassMethod(Sub_cls, sel));
114 testassert(class_getMethodImplementation(Sub_cls, sel) == (IMP)&_objc_msgForward);
116 testassert(object_getMethodImplementation(objc_unretainedObject(buf), sel) == (IMP)&_objc_msgForward);
118 testassert(class_getMethodImplementation_stret(Sub_cls, sel) == (IMP)&_objc_msgForward_stret);
119 testassert(object_getMethodImplementation_stret(objc_unretainedObject(buf), sel) == (IMP)&_objc_msgForward_stret);
122 testassert(! class_getInstanceMethod(NULL, NULL));
123 testassert(! class_getInstanceMethod(NULL, sel));
124 testassert(! class_getInstanceMethod(Sub_cls, NULL));
125 testassert(! class_getClassMethod(NULL, NULL));
126 testassert(! class_getClassMethod(NULL, sel));
127 testassert(! class_getClassMethod(Sub_cls, NULL));