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