]> git.saurik.com Git - apple/objc4.git/blob - test/initialize.m
objc4-493.9.tar.gz
[apple/objc4.git] / test / initialize.m
1 // TEST_CONFIG
2
3 // initialize.m
4 // Test basic +initialize behavior
5 // * +initialize before class method
6 // * superclass +initialize before subclass +initialize
7 // * subclass inheritance of superclass implementation
8 // * messaging during +initialize
9 #include "test.h"
10
11 int state = 0;
12
13 @interface Super0 { } @end
14 @implementation Super0
15 +(void)initialize {
16 fail("objc_getClass() must not trigger +initialize");
17 }
18 @end
19
20 @interface Super {} @end
21 @implementation Super
22 +(void)initialize {
23 testprintf("in [Super initialize]\n");
24 testassert(state == 0);
25 state = 1;
26 }
27 +(void)method {
28 fail("[Super method] shouldn't be called");
29 }
30 @end
31
32 @interface Sub : Super { } @end
33 @implementation Sub
34 +(void)initialize {
35 testprintf("in [Sub initialize]\n");
36 testassert(state == 1);
37 state = 2;
38 }
39 +(void)method {
40 testprintf("in [Sub method]\n");
41 testassert(state == 2);
42 state = 3;
43 }
44 @end
45
46
47 @interface Super2 { } @end
48 @interface Sub2 : Super2 { } @end
49
50 @implementation Super2
51 +(id)class { return self; }
52 +(void)initialize {
53 if (self == objc_getClass("Sub2")) {
54 testprintf("in [Super2 initialize] of Sub2\n");
55 testassert(state == 1);
56 state = 2;
57 } else if (self == objc_getClass("Super2")) {
58 testprintf("in [Super2 initialize] of Super2\n");
59 testassert(state == 0);
60 state = 1;
61 } else {
62 fail("in [Super2 initialize] of unknown class");
63 }
64 }
65 +(void)method {
66 testprintf("in [Super2 method]\n");
67 testassert(state == 2);
68 state = 3;
69 }
70 @end
71
72 @implementation Sub2
73 // nothing here
74 @end
75
76
77 @interface Super3 { } @end
78 @interface Sub3 : Super3 { } @end
79
80 @implementation Super3
81 +(id)class { return self; }
82 +(void)initialize {
83 if (self == [Sub3 class]) { // this message triggers [Sub3 initialize]
84 testprintf("in [Super3 initialize] of Sub3\n");
85 testassert(state == 0);
86 state = 1;
87 } else if (self == [Super3 class]) {
88 testprintf("in [Super3 initialize] of Super3\n");
89 testassert(state == 1);
90 state = 2;
91 } else {
92 fail("in [Super3 initialize] of unknown class");
93 }
94 }
95 +(void)method {
96 testprintf("in [Super3 method]\n");
97 testassert(state == 2);
98 state = 3;
99 }
100 @end
101
102 @implementation Sub3
103 // nothing here
104 @end
105
106 int main()
107 {
108 // objc_getClass() must not +initialize anything
109 state = 0;
110 objc_getClass("Super0");
111 testassert(state == 0);
112
113 // initialize superclass, then subclass
114 state = 0;
115 [Sub method];
116 testassert(state == 3);
117
118 // check subclass's inheritance of superclass initialize
119 state = 0;
120 [Sub2 method];
121 testassert(state == 3);
122
123 // check subclass method called from superclass initialize
124 state = 0;
125 [Sub3 method];
126 testassert(state == 3);
127
128 succeed(__FILE__);
129
130 return 0;
131 }