instanceSize.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // TEST_CONFIG
  2. #include "test.h"
  3. #include "testroot.i"
  4. #include <objc/runtime.h>
  5. @interface Sub1 : TestRoot {
  6. // id isa; // 0..4
  7. BOOL b; // 4..5
  8. }
  9. @end
  10. @implementation Sub1 @end
  11. @interface Sub2 : Sub1 {
  12. // id isa // 0..4 0..8
  13. // BOOL b // 4..5 8..9
  14. BOOL b2; // 5..6 9..10
  15. id o; // 8..12 16..24
  16. }
  17. @end
  18. @implementation Sub2 @end
  19. @interface Sub3 : Sub1 {
  20. // id isa; // 0..4 0..8
  21. // BOOL b; // 4..5 8..9
  22. id o; // 8..12 16..24
  23. BOOL b2; // 12..13 24..25
  24. }
  25. @end
  26. @implementation Sub3 @end
  27. int main()
  28. {
  29. testassert(sizeof(id) == class_getInstanceSize([TestRoot class]));
  30. testassert(2*sizeof(id) == class_getInstanceSize([Sub1 class]));
  31. testassert(3*sizeof(id) == class_getInstanceSize([Sub2 class]));
  32. testassert(4*sizeof(id) == class_getInstanceSize([Sub3 class]));
  33. #if !__has_feature(objc_arc)
  34. id o;
  35. o = [TestRoot new];
  36. testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o)));
  37. RELEASE_VAR(o);
  38. o = [Sub1 new];
  39. testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o)));
  40. RELEASE_VAR(o);
  41. o = [Sub2 new];
  42. testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o)));
  43. RELEASE_VAR(o);
  44. o = [Sub3 new];
  45. testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o)));
  46. RELEASE_VAR(o);
  47. #endif
  48. succeed(__FILE__);
  49. }