]> git.saurik.com Git - apple/objc4.git/blob - test/ivar.m
objc4-437.tar.gz
[apple/objc4.git] / test / ivar.m
1 #include "test.h"
2 #include <stdint.h>
3 #include <string.h>
4 #include <objc/objc-runtime.h>
5
6 @interface Super {
7 @public
8 id isa;
9 char superIvar;
10 }
11 @end
12
13 @implementation Super
14 +(void)initialize { }
15 +class { return self; }
16 +new { return class_createInstance(self, 0); }
17 @end
18
19
20 @interface Sub : Super {
21 @public
22 uintptr_t subIvar;
23 }
24 @end
25
26 @implementation Sub @end
27
28
29 int main()
30 {
31 /*
32 Runtime layout of Sub:
33 [0] isa
34 [1] superIvar
35 [2] subIvar
36 */
37
38 Ivar ivar;
39 uintptr_t value;
40 Sub *sub = [Sub new];
41 sub->subIvar = 10;
42 testassert(((uintptr_t *)sub)[2] == 10);
43
44 ivar = class_getInstanceVariable([Sub class], "subIvar");
45 testassert(ivar);
46 testassert(2*sizeof(intptr_t) == ivar_getOffset(ivar));
47 testassert(0 == strcmp(ivar_getName(ivar), "subIvar"));
48 #if __LP64__
49 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "Q"));
50 #else
51 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "I"));
52 #endif
53
54 ivar = class_getInstanceVariable([Super class], "superIvar");
55 testassert(ivar);
56 testassert(sizeof(intptr_t) == ivar_getOffset(ivar));
57 testassert(0 == strcmp(ivar_getName(ivar), "superIvar"));
58 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "c"));
59 testassert(ivar == class_getInstanceVariable([Sub class], "superIvar"));
60
61 ivar = class_getInstanceVariable([Super class], "subIvar");
62 testassert(!ivar);
63
64 ivar = class_getInstanceVariable([Sub class]->isa, "subIvar");
65 testassert(!ivar);
66
67
68 sub->subIvar = 10;
69 value = 0;
70 object_getInstanceVariable(sub, "subIvar", (void **)&value);
71 testassert(value == 10);
72
73 object_setInstanceVariable(sub, "subIvar", (void *)11);
74 testassert(sub->subIvar == 11);
75
76 ivar = class_getInstanceVariable([Sub class], "subIvar");
77 object_setIvar(sub, ivar, (id)12);
78 testassert(sub->subIvar == 12);
79 testassert((id)12 == object_getIvar(sub, ivar));
80
81 ivar = class_getInstanceVariable([Sub class], "subIvar");
82 testassert(ivar == object_getInstanceVariable(sub, "subIvar", NULL));
83
84
85 testassert(NULL == class_getInstanceVariable(NULL, "foo"));
86 testassert(NULL == class_getInstanceVariable([Sub class], NULL));
87 testassert(NULL == class_getInstanceVariable(NULL, NULL));
88
89 testassert(NULL == object_getIvar(sub, NULL));
90 testassert(NULL == object_getIvar(NULL, ivar));
91 testassert(NULL == object_getIvar(NULL, NULL));
92
93 testassert(NULL == object_getInstanceVariable(sub, NULL, NULL));
94 testassert(NULL == object_getInstanceVariable(NULL, "foo", NULL));
95 testassert(NULL == object_getInstanceVariable(NULL, NULL, NULL));
96 value = 10;
97 testassert(NULL == object_getInstanceVariable(sub, NULL, (void **)&value));
98 testassert(value == 0);
99 value = 10;
100 testassert(NULL == object_getInstanceVariable(NULL, "foo", (void **)&value));
101 testassert(value == 0);
102 value = 10;
103 testassert(NULL == object_getInstanceVariable(NULL, NULL, (void **)&value));
104 testassert(value == 0);
105
106 object_setIvar(sub, NULL, NULL);
107 object_setIvar(NULL, ivar, NULL);
108 object_setIvar(NULL, NULL, NULL);
109
110 testassert(NULL == object_setInstanceVariable(sub, NULL, NULL));
111 testassert(NULL == object_setInstanceVariable(NULL, "foo", NULL));
112 testassert(NULL == object_setInstanceVariable(NULL, NULL, NULL));
113
114 succeed(__FILE__);
115 return 0;
116 }