]> git.saurik.com Git - apple/objc4.git/blob - test/instanceSize.m
objc4-493.9.tar.gz
[apple/objc4.git] / test / instanceSize.m
1 // TEST_CONFIG
2
3 #include "test.h"
4
5 #include <objc/runtime.h>
6
7
8 @interface Super { id isa; } @end
9 @implementation Super
10 +(void)initialize { }
11 +class { return self; }
12 +new { return class_createInstance(self, 0); }
13 -(void)dealloc { object_dispose(self); }
14 @end
15
16 @interface Sub1 : Super {
17 // id isa; // 0..4
18 BOOL b; // 4..5
19 }
20 @end
21
22 @implementation Sub1 @end
23
24 @interface Sub2 : Sub1 {
25 // id isa // 0..4 0..8
26 // BOOL b // 4..5 8..9
27 BOOL b2; // 5..6 9..10
28 id o; // 8..12 16..24
29 }
30 @end
31 @implementation Sub2 @end
32
33 @interface Sub3 : Sub1 {
34 // id isa; // 0..4 0..8
35 // BOOL b; // 4..5 8..9
36 id o; // 8..12 16..24
37 BOOL b2; // 12..13 24..25
38 }
39 @end
40 @implementation Sub3 @end
41
42 int main()
43 {
44 testassert(sizeof(id) == class_getInstanceSize([Super class]));
45 testassert(2*sizeof(id) == class_getInstanceSize([Sub1 class]));
46 testassert(3*sizeof(id) == class_getInstanceSize([Sub2 class]));
47 testassert(4*sizeof(id) == class_getInstanceSize([Sub3 class]));
48
49 id o;
50
51 o = [Super new];
52 testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(o->isa));
53 [o dealloc];
54 o = [Sub1 new];
55 testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(o->isa));
56 [o dealloc];
57 o = [Sub2 new];
58 testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(o->isa));
59 [o dealloc];
60 o = [Sub3 new];
61 testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(o->isa));
62 [o dealloc];
63
64 succeed(__FILE__);
65 }