]> git.saurik.com Git - apple/objc4.git/blob - test/future.m
objc4-493.9.tar.gz
[apple/objc4.git] / test / future.m
1 /*
2 TEST_BUILD
3 $C{COMPILE} $DIR/future0.m -o future0.dylib -dynamiclib
4 $C{COMPILE} $DIR/future2.m -x none future0.dylib -o future2.dylib -dynamiclib
5 $C{COMPILE} $DIR/future.m -x none future0.dylib -o future.out
6 END
7 */
8
9 #include "test.h"
10 #include <objc/objc-runtime.h>
11 #include <malloc/malloc.h>
12 #include <string.h>
13 #include <dlfcn.h>
14 #include "future.h"
15
16 @implementation Sub2
17 +(int)method {
18 return 2;
19 }
20 +(Class)classref {
21 return [Sub2 class];
22 }
23 @end
24
25 @implementation SubSub2
26 +(int)method {
27 return 1 + [super method];
28 }
29 @end
30
31 int main()
32 {
33 Class oldSuper;
34 Class oldSub1;
35 Class newSub1;
36 #if !__OBJC2__
37 Class oldSub2;
38 Class newSub2;
39 uintptr_t buf[20];
40 #endif
41
42 // objc_getFutureClass with existing class
43 oldSuper = objc_getFutureClass("Super");
44 testassert(oldSuper == [Super class]);
45
46 // objc_getFutureClass with missing class
47 oldSub1 = objc_getFutureClass("Sub1");
48 testassert(oldSub1);
49 testassert(malloc_size(oldSub1) > 0);
50 testassert(objc_getClass("Sub1") == Nil);
51
52 // objc_getFutureClass a second time
53 testassert(oldSub1 == objc_getFutureClass("Sub1"));
54
55 #if !__OBJC2__
56 // objc_setFutureClass with existing class
57 oldSub2 = objc_getClass("Sub2");
58 testassert(oldSub2 == [Sub2 class]);
59 testassert(oldSub2 == class_getSuperclass(objc_getClass("SubSub2")));
60 objc_setFutureClass((Class)buf, "Sub2");
61 testassert(0 == strcmp(class_getName((Class)buf), "Sub2"));
62 newSub2 = objc_getClass("Sub2");
63 testassert(newSub2 == (Class)buf);
64 testassert(newSub2 != oldSub2);
65 // check classrefs
66 testassert(newSub2 == [Sub2 class]);
67 testassert(newSub2 == [newSub2 class]);
68 testassert(newSub2 == [newSub2 classref]);
69 testassert(newSub2 != [oldSub2 class]);
70 // check superclass chains
71 testassert(newSub2 == class_getSuperclass(objc_getClass("SubSub2")));
72 #else
73 // 64-bit ABI ignores objc_setFutureClass.
74 #endif
75
76 // Load class Sub1
77 dlopen("future2.dylib", 0);
78
79 // Verify use of future class
80 newSub1 = objc_getClass("Sub1");
81 testassert(oldSub1 == newSub1);
82 testassert(newSub1 == [newSub1 classref]);
83 testassert(newSub1 == class_getSuperclass(objc_getClass("SubSub1")));
84
85 testassert(1 == [oldSub1 method]);
86 testassert(1 == [newSub1 method]);
87 #if !__OBJC2__
88 testassert(2 == [newSub2 method]);
89 testassert(2 == [oldSub2 method]);
90 testassert(3 == [SubSub2 method]);
91 #endif
92
93 succeed(__FILE__);
94 }