7 #include <objc/objc-runtime.h>
9 @interface Super : TestRoot {
15 @interface Sub : Super {
21 @implementation Super @end
22 @implementation Sub @end
28 Runtime layout of Sub:
36 sub->subIvar = [Sub class];
37 testassert(((Class *)objc_unretainedPointer(sub))[2] == [Sub class]);
39 ivar = class_getInstanceVariable([Sub class], "subIvar");
41 testassert(2*sizeof(intptr_t) == (size_t)ivar_getOffset(ivar));
42 testassert(0 == strcmp(ivar_getName(ivar), "subIvar"));
43 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "@"));
45 ivar = class_getInstanceVariable([Super class], "superIvar");
47 testassert(sizeof(intptr_t) == (size_t)ivar_getOffset(ivar));
48 testassert(0 == strcmp(ivar_getName(ivar), "superIvar"));
49 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "c"));
50 testassert(ivar == class_getInstanceVariable([Sub class], "superIvar"));
52 ivar = class_getInstanceVariable([Super class], "subIvar");
55 ivar = class_getInstanceVariable(object_getClass([Sub class]), "subIvar");
58 ivar = class_getInstanceVariable([Sub class], "subIvar");
59 object_setIvar(sub, ivar, sub);
60 testassert(sub->subIvar == sub);
61 testassert(sub == object_getIvar(sub, ivar));
63 testassert(NULL == class_getInstanceVariable(NULL, "foo"));
64 testassert(NULL == class_getInstanceVariable([Sub class], NULL));
65 testassert(NULL == class_getInstanceVariable(NULL, NULL));
67 testassert(NULL == object_getIvar(sub, NULL));
68 testassert(NULL == object_getIvar(NULL, ivar));
69 testassert(NULL == object_getIvar(NULL, NULL));
71 object_setIvar(sub, NULL, NULL);
72 object_setIvar(NULL, ivar, NULL);
73 object_setIvar(NULL, NULL, NULL);
75 #if !__has_feature(objc_arc)
79 sub->subIvar = (id)10;
81 object_getInstanceVariable(sub, "subIvar", (void **)&value);
82 testassert(value == 10);
84 object_setInstanceVariable(sub, "subIvar", (id)11);
85 testassert(sub->subIvar == (id)11);
87 ivar = class_getInstanceVariable([Sub class], "subIvar");
88 testassert(ivar == object_getInstanceVariable(sub, "subIvar", NULL));
90 testassert(NULL == object_getInstanceVariable(sub, NULL, NULL));
91 testassert(NULL == object_getInstanceVariable(NULL, "foo", NULL));
92 testassert(NULL == object_getInstanceVariable(NULL, NULL, NULL));
94 testassert(NULL == object_getInstanceVariable(sub, NULL, (void **)&value));
95 testassert(value == 0);
97 testassert(NULL == object_getInstanceVariable(NULL, "foo", (void **)&value));
98 testassert(value == 0);
100 testassert(NULL == object_getInstanceVariable(NULL, NULL, (void **)&value));
101 testassert(value == 0);
103 testassert(NULL == object_setInstanceVariable(sub, NULL, NULL));
104 testassert(NULL == object_setInstanceVariable(NULL, "foo", NULL));
105 testassert(NULL == object_setInstanceVariable(NULL, NULL, NULL));