]> git.saurik.com Git - apple/objc4.git/blob - test/getMethod.m
c6babd80b55e7d3d99c631f59396c4e4a92b3b85
[apple/objc4.git] / test / getMethod.m
1 #include "test.h"
2 #include <objc/runtime.h>
3 #include <objc/message.h>
4
5 static int state = 0;
6
7 @interface Super { id isa; } @end
8 @implementation Super
9 +class { return self; }
10 +(void)initialize { }
11 +(void)classMethod { state = 1; }
12 -(void)instanceMethod { state = 4; }
13 +(void)classMethodSuperOnly { state = 3; }
14 -(void)instanceMethodSuperOnly { state = 6; }
15 @end
16
17 @interface Sub : Super @end
18 @implementation Sub
19 +(void)classMethod { state = 2; }
20 -(void)instanceMethod { state = 5; }
21 @end
22
23
24 int main()
25 {
26 Class Super_cls, Sub_cls;
27 Class buf[10];
28 Method m;
29 SEL sel;
30 IMP imp;
31
32 // don't use [Super class] to check laziness handing
33 Super_cls = objc_getClass("Super");
34 Sub_cls = objc_getClass("Sub");
35
36 sel = sel_registerName("classMethod");
37 m = class_getClassMethod(Super_cls, sel);
38 testassert(m);
39 testassert(sel == method_getName(m));
40 imp = method_getImplementation(m);
41 testassert(imp == class_getMethodImplementation(Super_cls->isa, sel));
42 state = 0;
43 (*imp)(Super_cls, sel);
44 testassert(state == 1);
45
46 sel = sel_registerName("classMethod");
47 m = class_getClassMethod(Sub_cls, sel);
48 testassert(m);
49 testassert(sel == method_getName(m));
50 imp = method_getImplementation(m);
51 testassert(imp == class_getMethodImplementation(Sub_cls->isa, sel));
52 state = 0;
53 (*imp)(Sub_cls, sel);
54 testassert(state == 2);
55
56 sel = sel_registerName("classMethodSuperOnly");
57 m = class_getClassMethod(Sub_cls, sel);
58 testassert(m);
59 testassert(sel == method_getName(m));
60 imp = method_getImplementation(m);
61 testassert(imp == class_getMethodImplementation(Sub_cls->isa, sel));
62 state = 0;
63 (*imp)(Sub_cls, sel);
64 testassert(state == 3);
65
66 sel = sel_registerName("instanceMethod");
67 m = class_getInstanceMethod(Super_cls, sel);
68 testassert(m);
69 testassert(sel == method_getName(m));
70 imp = method_getImplementation(m);
71 testassert(imp == class_getMethodImplementation(Super_cls, sel));
72 state = 0;
73 buf[0] = Super_cls;
74 (*imp)((Super *)buf, sel);
75 testassert(state == 4);
76
77 sel = sel_registerName("instanceMethod");
78 m = class_getInstanceMethod(Sub_cls, sel);
79 testassert(m);
80 testassert(sel == method_getName(m));
81 imp = method_getImplementation(m);
82 testassert(imp == class_getMethodImplementation(Sub_cls, sel));
83 state = 0;
84 buf[0] = Sub_cls;
85 (*imp)((Sub *)buf, sel);
86 testassert(state == 5);
87
88 sel = sel_registerName("instanceMethodSuperOnly");
89 m = class_getInstanceMethod(Sub_cls, sel);
90 testassert(m);
91 testassert(sel == method_getName(m));
92 imp = method_getImplementation(m);
93 testassert(imp == class_getMethodImplementation(Sub_cls, sel));
94 state = 0;
95 buf[0] = Sub_cls;
96 (*imp)((Sub *)buf, sel);
97 testassert(state == 6);
98
99 // check class_getClassMethod(cls) == class_getInstanceMethod(cls->isa)
100 sel = sel_registerName("classMethod");
101 testassert(class_getClassMethod(Sub_cls, sel) == class_getInstanceMethod(Sub_cls->isa, sel));
102
103 sel = sel_registerName("nonexistent");
104 testassert(! class_getInstanceMethod(Sub_cls, sel));
105 testassert(! class_getClassMethod(Sub_cls, sel));
106 testassert(class_getMethodImplementation(Sub_cls, sel) == (IMP)&_objc_msgForward);
107 testassert(class_getMethodImplementation_stret(Sub_cls, sel) == (IMP)&_objc_msgForward_stret);
108
109 testassert(! class_getInstanceMethod(NULL, NULL));
110 testassert(! class_getInstanceMethod(NULL, sel));
111 testassert(! class_getInstanceMethod(Sub_cls, NULL));
112 testassert(! class_getClassMethod(NULL, NULL));
113 testassert(! class_getClassMethod(NULL, sel));
114 testassert(! class_getClassMethod(Sub_cls, NULL));
115
116 succeed(__FILE__);
117 }