3 #define TEST_CALLS_OPERATOR_NEW
6 #include "swift-class-def.m"
8 #include <objc/objc-internal.h>
12 static Class expectedOldClass;
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);
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);
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);
35 EXTERN_C Class _objc_realizeClassFromSwift(Class, void *);
37 EXTERN_C Class init(Class cls, void *arg) {
39 _objc_realizeClassFromSwift(cls, cls);
43 @interface SwiftRoot: TestRoot @end
44 SWIFT_CLASS(SwiftRoot, TestRoot, init);
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);
55 _objc_setClassCopyFixupHandler(handler1);
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);
65 _objc_setClassCopyFixupHandler(handler2);
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);
76 _objc_setClassCopyFixupHandler(handler3);
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);