]> git.saurik.com Git - apple/objc4.git/blob - test/bigrc.m
objc4-818.2.tar.gz
[apple/objc4.git] / test / bigrc.m
1 // TEST_CONFIG MEM=mrc
2
3 #include "test.h"
4 #include "testroot.i"
5
6 static size_t LOTS;
7
8 @interface Deallocator : TestRoot @end
9 @implementation Deallocator
10
11 -(void)dealloc
12 {
13 id o = self;
14
15
16 testprintf("Retain/release during dealloc\n");
17
18 testassertequal([o retainCount], 0);
19 [o retain];
20 testassertequal([o retainCount], 0);
21 [o release];
22 testassertequal([o retainCount], 0);
23
24 [super dealloc];
25 }
26
27 @end
28
29 size_t clz(uintptr_t isa) {
30 if (sizeof(uintptr_t) == 4)
31 return __builtin_clzl(isa);
32 testassert(sizeof(uintptr_t) == 8);
33 return __builtin_clzll(isa);
34 }
35
36 int main()
37 {
38 Deallocator *o = [Deallocator new];
39 size_t rc = 1;
40
41 [o retain];
42
43 uintptr_t isa = *(uintptr_t *)o;
44 if (isa & 1) {
45 // Assume refcount in high bits.
46 LOTS = 1 << (4 + clz(isa));
47 testprintf("LOTS %zu via cntlzw\n", LOTS);
48 } else {
49 LOTS = 0x1000000;
50 testprintf("LOTS %zu via guess\n", LOTS);
51 }
52
53 [o release];
54
55
56 testprintf("Retain a lot\n");
57
58 testassert(rc == 1);
59 testassert([o retainCount] == rc);
60 do {
61 [o retain];
62 if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS);
63 } while (++rc < LOTS);
64
65 testassert([o retainCount] == rc);
66
67 do {
68 [o release];
69 if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS);
70 } while (--rc > 1);
71
72 testassert(rc == 1);
73 testassert([o retainCount] == rc);
74
75
76 testprintf("tryRetain a lot\n");
77
78 id w;
79 objc_storeWeak(&w, o);
80 testassert(w == o);
81
82 testassert(rc == 1);
83 testassert([o retainCount] == rc);
84 do {
85 objc_loadWeakRetained(&w);
86 if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS);
87 } while (++rc < LOTS);
88
89 testassert([o retainCount] == rc);
90
91 do {
92 [o release];
93 if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS);
94 } while (--rc > 1);
95
96 testassert(rc == 1);
97 testassert([o retainCount] == rc);
98
99 testprintf("dealloc\n");
100
101 testassert(TestRootDealloc == 0);
102 testassert(w != nil);
103 [o release];
104 testassert(TestRootDealloc == 1);
105 testassert(w == nil);
106
107 succeed(__FILE__);
108 }