X-Git-Url: https://git.saurik.com/apple/objc4.git/blobdiff_plain/7c0e6487d7b67b6bf6c632300ee4b74e8950b051..7af964d1562d70f51a8e9aca24215ac3d83d0624:/test/getMethod.m diff --git a/test/getMethod.m b/test/getMethod.m new file mode 100644 index 0000000..c6babd8 --- /dev/null +++ b/test/getMethod.m @@ -0,0 +1,117 @@ +#include "test.h" +#include +#include + +static int state = 0; + +@interface Super { id isa; } @end +@implementation Super ++class { return self; } ++(void)initialize { } ++(void)classMethod { state = 1; } +-(void)instanceMethod { state = 4; } ++(void)classMethodSuperOnly { state = 3; } +-(void)instanceMethodSuperOnly { state = 6; } +@end + +@interface Sub : Super @end +@implementation Sub ++(void)classMethod { state = 2; } +-(void)instanceMethod { state = 5; } +@end + + +int main() +{ + Class Super_cls, Sub_cls; + Class buf[10]; + Method m; + SEL sel; + IMP imp; + + // don't use [Super class] to check laziness handing + Super_cls = objc_getClass("Super"); + Sub_cls = objc_getClass("Sub"); + + sel = sel_registerName("classMethod"); + m = class_getClassMethod(Super_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Super_cls->isa, sel)); + state = 0; + (*imp)(Super_cls, sel); + testassert(state == 1); + + sel = sel_registerName("classMethod"); + m = class_getClassMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Sub_cls->isa, sel)); + state = 0; + (*imp)(Sub_cls, sel); + testassert(state == 2); + + sel = sel_registerName("classMethodSuperOnly"); + m = class_getClassMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Sub_cls->isa, sel)); + state = 0; + (*imp)(Sub_cls, sel); + testassert(state == 3); + + sel = sel_registerName("instanceMethod"); + m = class_getInstanceMethod(Super_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Super_cls, sel)); + state = 0; + buf[0] = Super_cls; + (*imp)((Super *)buf, sel); + testassert(state == 4); + + sel = sel_registerName("instanceMethod"); + m = class_getInstanceMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Sub_cls, sel)); + state = 0; + buf[0] = Sub_cls; + (*imp)((Sub *)buf, sel); + testassert(state == 5); + + sel = sel_registerName("instanceMethodSuperOnly"); + m = class_getInstanceMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Sub_cls, sel)); + state = 0; + buf[0] = Sub_cls; + (*imp)((Sub *)buf, sel); + testassert(state == 6); + + // check class_getClassMethod(cls) == class_getInstanceMethod(cls->isa) + sel = sel_registerName("classMethod"); + testassert(class_getClassMethod(Sub_cls, sel) == class_getInstanceMethod(Sub_cls->isa, sel)); + + sel = sel_registerName("nonexistent"); + testassert(! class_getInstanceMethod(Sub_cls, sel)); + testassert(! class_getClassMethod(Sub_cls, sel)); + testassert(class_getMethodImplementation(Sub_cls, sel) == (IMP)&_objc_msgForward); + testassert(class_getMethodImplementation_stret(Sub_cls, sel) == (IMP)&_objc_msgForward_stret); + + testassert(! class_getInstanceMethod(NULL, NULL)); + testassert(! class_getInstanceMethod(NULL, sel)); + testassert(! class_getInstanceMethod(Sub_cls, NULL)); + testassert(! class_getClassMethod(NULL, NULL)); + testassert(! class_getClassMethod(NULL, sel)); + testassert(! class_getClassMethod(Sub_cls, NULL)); + + succeed(__FILE__); +}