]> git.saurik.com Git - apple/objc4.git/blob - test/association.m
objc4-532.tar.gz
[apple/objc4.git] / test / association.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include <Foundation/NSObject.h>
5 #include <objc/runtime.h>
6
7 static int values;
8 static int supers;
9 static int subs;
10
11 static const char *key = "key";
12
13
14 @interface Value : NSObject @end
15 @interface Super : NSObject @end
16 @interface Sub : NSObject @end
17
18 @interface Super2 : NSObject @end
19 @interface Sub2 : NSObject @end
20
21 @implementation Super
22 -(id) init
23 {
24 // rdar://8270243 don't lose associations after isa swizzling
25
26 id value = [Value new];
27 objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN);
28 RELEASE_VAR(value);
29
30 object_setClass(self, [Sub class]);
31
32 return self;
33 }
34
35 -(void) dealloc
36 {
37 supers++;
38 SUPER_DEALLOC();
39 }
40 -(void) finalize
41 {
42 supers++;
43 [super finalize];
44 }
45
46 @end
47
48 @implementation Sub
49 -(void) dealloc
50 {
51 subs++;
52 SUPER_DEALLOC();
53 }
54 -(void) finalize
55 {
56 subs++;
57 [super finalize];
58 }
59 @end
60
61 @implementation Super2
62 -(id) init
63 {
64 // rdar://9617109 don't lose associations after isa swizzling
65
66 id value = [Value new];
67 object_setClass(self, [Sub2 class]);
68 objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN);
69 RELEASE_VAR(value);
70 object_setClass(self, [Super2 class]);
71
72 return self;
73 }
74
75 -(void) dealloc
76 {
77 supers++;
78 SUPER_DEALLOC();
79 }
80 -(void) finalize
81 {
82 supers++;
83 [super finalize];
84 }
85
86 @end
87
88 @implementation Sub2
89 -(void) dealloc
90 {
91 subs++;
92 SUPER_DEALLOC();
93 }
94 -(void) finalize
95 {
96 subs++;
97 [super finalize];
98 }
99 @end
100
101 @implementation Value
102 -(void) dealloc {
103 values++;
104 SUPER_DEALLOC();
105 }
106 -(void) finalize {
107 values++;
108 [super finalize];
109 }
110 @end
111
112
113 int main()
114 {
115 testonthread(^{
116 int i;
117 for (i = 0; i < 100; i++) {
118 RELEASE_VALUE([[Super alloc] init]);
119 }
120 });
121 testcollect();
122
123 testassert(supers == 0);
124 testassert(subs > 0);
125 testassert(subs == values);
126
127
128 supers = 0;
129 subs = 0;
130 values = 0;
131
132 testonthread(^{
133 int i;
134 for (i = 0; i < 100; i++) {
135 RELEASE_VALUE([[Super2 alloc] init]);
136 }
137 });
138 testcollect();
139
140 testassert(supers > 0);
141 testassert(subs == 0);
142 testassert(supers == values);
143
144 succeed(__FILE__);
145 }