]> git.saurik.com Git - apple/objc4.git/blob - test/weakcopy.m
objc4-647.tar.gz
[apple/objc4.git] / test / weakcopy.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include "testroot.i"
5 #include <stdint.h>
6 #include <string.h>
7 #include <objc/objc-runtime.h>
8
9 @interface Weak : TestRoot {
10 @public
11 __weak id value;
12 }
13 @end
14 @implementation Weak
15 @end
16
17 Weak *oldObject;
18 Weak *newObject;
19
20 void *fn(void *arg __unused)
21 {
22 objc_registerThreadWithCollector();
23
24 return NULL;
25 }
26
27 int main()
28 {
29 testonthread(^{
30 TestRoot *value;
31
32 PUSH_POOL {
33 value = [TestRoot new];
34 testassert(value);
35 oldObject = [Weak new];
36 testassert(oldObject);
37
38 oldObject->value = value;
39 testassert(oldObject->value == value);
40
41 newObject = [oldObject copy];
42 testassert(newObject);
43 testassert(newObject->value == oldObject->value);
44
45 newObject->value = nil;
46 testassert(newObject->value == nil);
47 testassert(oldObject->value == value);
48 } POP_POOL;
49
50 testcollect();
51 TestRootDealloc = 0;
52 TestRootFinalize = 0;
53 RELEASE_VAR(value);
54 });
55
56 testcollect();
57 testassert(TestRootDealloc || TestRootFinalize);
58
59 #if defined(__OBJC_GC__) || __has_feature(objc_arc)
60 testassert(oldObject->value == nil);
61 #else
62 testassert(oldObject->value != nil);
63 #endif
64 testassert(newObject->value == nil);
65
66 RELEASE_VAR(newObject);
67 RELEASE_VAR(oldObject);
68
69 succeed(__FILE__);
70 return 0;
71 }