4 #include <objc/objc-runtime.h>
15 +class { return self; }
16 +new { return class_createInstance(self, 0); }
20 @interface Sub : Super {
26 @implementation Sub @end
32 Runtime layout of Sub:
42 testassert(((uintptr_t *)sub)[2] == 10);
44 ivar = class_getInstanceVariable([Sub class], "subIvar");
46 testassert(2*sizeof(intptr_t) == ivar_getOffset(ivar));
47 testassert(0 == strcmp(ivar_getName(ivar), "subIvar"));
49 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "Q"));
51 testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "I"));
54 ivar = class_getInstanceVariable([Super class], "superIvar");
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"));
61 ivar = class_getInstanceVariable([Super class], "subIvar");
64 ivar = class_getInstanceVariable([Sub class]->isa, "subIvar");
70 object_getInstanceVariable(sub, "subIvar", (void **)&value);
71 testassert(value == 10);
73 object_setInstanceVariable(sub, "subIvar", (void *)11);
74 testassert(sub->subIvar == 11);
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));
81 ivar = class_getInstanceVariable([Sub class], "subIvar");
82 testassert(ivar == object_getInstanceVariable(sub, "subIvar", NULL));
85 testassert(NULL == class_getInstanceVariable(NULL, "foo"));
86 testassert(NULL == class_getInstanceVariable([Sub class], NULL));
87 testassert(NULL == class_getInstanceVariable(NULL, NULL));
89 testassert(NULL == object_getIvar(sub, NULL));
90 testassert(NULL == object_getIvar(NULL, ivar));
91 testassert(NULL == object_getIvar(NULL, NULL));
93 testassert(NULL == object_getInstanceVariable(sub, NULL, NULL));
94 testassert(NULL == object_getInstanceVariable(NULL, "foo", NULL));
95 testassert(NULL == object_getInstanceVariable(NULL, NULL, NULL));
97 testassert(NULL == object_getInstanceVariable(sub, NULL, (void **)&value));
98 testassert(value == 0);
100 testassert(NULL == object_getInstanceVariable(NULL, "foo", (void **)&value));
101 testassert(value == 0);
103 testassert(NULL == object_getInstanceVariable(NULL, NULL, (void **)&value));
104 testassert(value == 0);
106 object_setIvar(sub, NULL, NULL);
107 object_setIvar(NULL, ivar, NULL);
108 object_setIvar(NULL, NULL, NULL);
110 testassert(NULL == object_setInstanceVariable(sub, NULL, NULL));
111 testassert(NULL == object_setInstanceVariable(NULL, "foo", NULL));
112 testassert(NULL == object_setInstanceVariable(NULL, NULL, NULL));