]> git.saurik.com Git - apple/objc4.git/blob - test/association.m
objc4-756.2.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
41 @end
42
43 @implementation Sub
44 -(void) dealloc
45 {
46 subs++;
47 SUPER_DEALLOC();
48 }
49 @end
50
51 @implementation Super2
52 -(id) init
53 {
54 // rdar://9617109 don't lose associations after isa swizzling
55
56 id value = [Value new];
57 object_setClass(self, [Sub2 class]);
58 objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN);
59 RELEASE_VAR(value);
60 object_setClass(self, [Super2 class]);
61
62 return self;
63 }
64
65 -(void) dealloc
66 {
67 supers++;
68 SUPER_DEALLOC();
69 }
70
71 @end
72
73 @implementation Sub2
74 -(void) dealloc
75 {
76 subs++;
77 SUPER_DEALLOC();
78 }
79 @end
80
81 @implementation Value
82 -(void) dealloc {
83 values++;
84 SUPER_DEALLOC();
85 }
86 @end
87
88
89 int main()
90 {
91 testonthread(^{
92 int i;
93 for (i = 0; i < 100; i++) {
94 RELEASE_VALUE([[Super alloc] init]);
95 }
96 });
97 testcollect();
98
99 testassert(supers == 0);
100 testassert(subs > 0);
101 testassert(subs == values);
102
103
104 supers = 0;
105 subs = 0;
106 values = 0;
107
108 testonthread(^{
109 int i;
110 for (i = 0; i < 100; i++) {
111 RELEASE_VALUE([[Super2 alloc] init]);
112 }
113 });
114 testcollect();
115
116 testassert(supers > 0);
117 testassert(subs == 0);
118 testassert(supers == values);
119
120 // rdar://44094390 tolerate nil object and nil value
121 #pragma clang diagnostic push
122 #pragma clang diagnostic ignored "-Wnonnull"
123 objc_setAssociatedObject(nil, &key, nil, 0);
124 #pragma clang diagnostic pop
125
126 succeed(__FILE__);
127 }