]> git.saurik.com Git - apple/objc4.git/blob - test/copyFixupHandler.mm
objc4-779.1.tar.gz
[apple/objc4.git] / test / copyFixupHandler.mm
1 // TEST_CONFIG MEM=mrc
2
3 #define TEST_CALLS_OPERATOR_NEW
4 #include "test.h"
5 #include "testroot.i"
6 #include "swift-class-def.m"
7
8 #include <objc/objc-internal.h>
9
10 #include <vector>
11
12 static Class expectedOldClass;
13
14 static std::vector<Class> observedNewClasses1;
15 static void handler1(Class _Nonnull oldClass, Class _Nonnull newClass) {
16 testprintf("%s(%p, %p)", __func__, oldClass, newClass);
17 testassert(oldClass == expectedOldClass);
18 observedNewClasses1.push_back(newClass);
19 }
20
21 static std::vector<Class> observedNewClasses2;
22 static void handler2(Class _Nonnull oldClass, Class _Nonnull newClass) {
23 testprintf("%s(%p, %p)", __func__, oldClass, newClass);
24 testassert(oldClass == expectedOldClass);
25 observedNewClasses2.push_back(newClass);
26 }
27
28 static std::vector<Class> observedNewClasses3;
29 static void handler3(Class _Nonnull oldClass, Class _Nonnull newClass) {
30 testprintf("%s(%p, %p)", __func__, oldClass, newClass);
31 testassert(oldClass == expectedOldClass);
32 observedNewClasses3.push_back(newClass);
33 }
34
35 EXTERN_C Class _objc_realizeClassFromSwift(Class, void *);
36
37 EXTERN_C Class init(Class cls, void *arg) {
38 (void)arg;
39 _objc_realizeClassFromSwift(cls, cls);
40 return cls;
41 }
42
43 @interface SwiftRoot: TestRoot @end
44 SWIFT_CLASS(SwiftRoot, TestRoot, init);
45
46 int main()
47 {
48 expectedOldClass = [SwiftRoot class];
49 Class A = objc_allocateClassPair([RawSwiftRoot class], "A", 0);
50 objc_registerClassPair(A);
51 testassert(observedNewClasses1.size() == 0);
52 testassert(observedNewClasses2.size() == 0);
53 testassert(observedNewClasses3.size() == 0);
54
55 _objc_setClassCopyFixupHandler(handler1);
56
57 expectedOldClass = A;
58 Class B = objc_allocateClassPair(A, "B", 0);
59 objc_registerClassPair(B);
60 testassert(observedNewClasses1.size() == 2);
61 testassert(observedNewClasses2.size() == 0);
62 testassert(observedNewClasses3.size() == 0);
63 testassert(observedNewClasses1[0] == B);
64
65 _objc_setClassCopyFixupHandler(handler2);
66
67 expectedOldClass = B;
68 Class C = objc_allocateClassPair(B, "C", 0);
69 objc_registerClassPair(C);
70 testassert(observedNewClasses1.size() == 4);
71 testassert(observedNewClasses2.size() == 2);
72 testassert(observedNewClasses3.size() == 0);
73 testassert(observedNewClasses1[2] == C);
74 testassert(observedNewClasses2[0] == C);
75
76 _objc_setClassCopyFixupHandler(handler3);
77
78 expectedOldClass = C;
79 Class D = objc_allocateClassPair(C, "D", 0);
80 objc_registerClassPair(D);
81 testassert(observedNewClasses1.size() == 6);
82 testassert(observedNewClasses2.size() == 4);
83 testassert(observedNewClasses3.size() == 2);
84 testassert(observedNewClasses1[4] == D);
85 testassert(observedNewClasses2[2] == D);
86 testassert(observedNewClasses3[0] == D);
87
88 succeed(__FILE__);
89 }