-
Notifications
You must be signed in to change notification settings - Fork 510
/
accessors2.m
143 lines (123 loc) · 4.39 KB
/
accessors2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// TEST_CONFIG
#include "test.h"
#include <objc/runtime.h>
#include <objc/objc-abi.h>
#include "testroot.i"
@interface Base : TestRoot {
@public
id ivar;
}
@end
@implementation Base @end
int main()
{
SEL _cmd = @selector(foo);
Base *o = [Base new];
ptrdiff_t offset = ivar_getOffset(class_getInstanceVariable([Base class], "ivar"));
testassert(offset == sizeof(id));
TestRoot *value = [TestRoot new];
// fixme test atomicity
// Original setter API
testprintf("original nonatomic retain\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, NO/*copy*/);
testassert(TestRootRetain == 1);
testassert(TestRootCopyWithZone == 0);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar == value);
testprintf("original atomic retain\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, NO/*copy*/);
testassert(TestRootRetain == 1);
testassert(TestRootCopyWithZone == 0);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar == value);
testprintf("original nonatomic copy\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, YES/*copy*/);
testassert(TestRootRetain == 0);
testassert(TestRootCopyWithZone == 1);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar && o->ivar != value);
testprintf("original atomic copy\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, YES/*copy*/);
testassert(TestRootRetain == 0);
testassert(TestRootCopyWithZone == 1);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar && o->ivar != value);
testprintf("original nonatomic mutablecopy\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, 2/*copy*/);
testassert(TestRootRetain == 0);
testassert(TestRootCopyWithZone == 0);
testassert(TestRootMutableCopyWithZone == 1);
testassert(o->ivar && o->ivar != value);
testprintf("original atomic mutablecopy\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, 2/*copy*/);
testassert(TestRootRetain == 0);
testassert(TestRootCopyWithZone == 0);
testassert(TestRootMutableCopyWithZone == 1);
testassert(o->ivar && o->ivar != value);
// Optimized setter API
testprintf("optimized nonatomic retain\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty_nonatomic(o, _cmd, value, offset);
testassert(TestRootRetain == 1);
testassert(TestRootCopyWithZone == 0);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar == value);
testprintf("optimized atomic retain\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty_atomic(o, _cmd, value, offset);
testassert(TestRootRetain == 1);
testassert(TestRootCopyWithZone == 0);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar == value);
testprintf("optimized nonatomic copy\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty_nonatomic_copy(o, _cmd, value, offset);
testassert(TestRootRetain == 0);
testassert(TestRootCopyWithZone == 1);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar && o->ivar != value);
testprintf("optimized atomic copy\n");
o->ivar = nil;
TestRootRetain = 0;
TestRootCopyWithZone = 0;
TestRootMutableCopyWithZone = 0;
objc_setProperty_atomic_copy(o, _cmd, value, offset);
testassert(TestRootRetain == 0);
testassert(TestRootCopyWithZone == 1);
testassert(TestRootMutableCopyWithZone == 0);
testassert(o->ivar && o->ivar != value);
succeed(__FILE__);
}