]> git.saurik.com Git - apple/objc4.git/blob - test/setAssociatedObjectHook.m
objc4-818.2.tar.gz
[apple/objc4.git] / test / setAssociatedObjectHook.m
1 /*
2 TEST_CONFIG MEM=mrc
3 TEST_ENV OBJC_DISABLE_NONPOINTER_ISA=YES
4 */
5
6 #include "test.h"
7 #include "testroot.i"
8
9 bool hasAssociations = false;
10
11 @interface TestRoot (AssocHooks)
12 @end
13
14 @implementation TestRoot (AssocHooks)
15
16 - (void)_noteAssociatedObjects {
17 hasAssociations = true;
18 }
19
20 // -_noteAssociatedObjects is currently limited to raw-isa custom-rr to avoid overhead
21 - (void) release {
22 }
23
24 @end
25
26 int main() {
27 id obj = [TestRoot new];
28 id value = [TestRoot new];
29 const void *key = "key";
30 objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN);
31 testassert(hasAssociations == true);
32
33 id out = objc_getAssociatedObject(obj, key);
34 testassert(out == value);
35
36 hasAssociations = false;
37 key = "key2";
38 objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN);
39 testassert(hasAssociations == false); //only called once
40
41
42 out = objc_getAssociatedObject(obj, key);
43 testassert(out == value);
44
45 succeed(__FILE__);
46 }