]> git.saurik.com Git - apple/objc4.git/blob - test/setAssociatedObjectHook.m
objc4-779.1.tar.gz
[apple/objc4.git] / test / setAssociatedObjectHook.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include "testroot.i"
5
6 id sawObject;
7 const void *sawKey;
8 id sawValue;
9 objc_AssociationPolicy sawPolicy;
10
11 objc_hook_setAssociatedObject originalSetAssociatedObject;
12
13 void hook(id _Nonnull object, const void * _Nonnull key, id _Nullable value, objc_AssociationPolicy policy) {
14 sawObject = object;
15 sawKey = key;
16 sawValue = value;
17 sawPolicy = policy;
18 originalSetAssociatedObject(object, key, value, policy);
19 }
20
21 int main() {
22 id obj = [TestRoot new];
23 id value = [TestRoot new];
24 const void *key = "key";
25 objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN);
26 testassert(sawObject == nil);
27 testassert(sawKey == nil);
28 testassert(sawValue == nil);
29 testassert(sawPolicy == 0);
30
31 id out = objc_getAssociatedObject(obj, key);
32 testassert(out == value);
33
34 objc_setHook_setAssociatedObject(hook, &originalSetAssociatedObject);
35
36 key = "key2";
37 objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN);
38 testassert(sawObject == obj);
39 testassert(sawKey == key);
40 testassert(sawValue == value);
41 testassert(sawPolicy == OBJC_ASSOCIATION_RETAIN);
42
43 out = objc_getAssociatedObject(obj, key);
44 testassert(out == value);
45
46 succeed(__FILE__);
47 }