createInstance.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // TEST_CONFIG
  2. #import <objc/runtime.h>
  3. #import <objc/objc-auto.h>
  4. #include "test.h"
  5. #include "testroot.i"
  6. @interface Super : TestRoot @end
  7. @implementation Super @end
  8. @interface Sub : Super { int array[128]; } @end
  9. @implementation Sub @end
  10. #if __has_feature(objc_arc)
  11. #define object_dispose(x) do {} while (0)
  12. #endif
  13. int main()
  14. {
  15. Super *s;
  16. s = class_createInstance([Super class], 0);
  17. testassert(s);
  18. testassert(object_getClass(s) == [Super class]);
  19. testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Super class]));
  20. object_dispose(s);
  21. s = class_createInstance([Sub class], 0);
  22. testassert(s);
  23. testassert(object_getClass(s) == [Sub class]);
  24. testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Sub class]));
  25. object_dispose(s);
  26. s = class_createInstance([Super class], 100);
  27. testassert(s);
  28. testassert(object_getClass(s) == [Super class]);
  29. testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Super class]) + 100);
  30. object_dispose(s);
  31. s = class_createInstance([Sub class], 100);
  32. testassert(s);
  33. testassert(object_getClass(s) == [Sub class]);
  34. testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Sub class]) + 100);
  35. object_dispose(s);
  36. s = class_createInstance(Nil, 0);
  37. testassert(!s);
  38. testassert(TestRootAlloc == 0);
  39. #if __has_feature(objc_arc)
  40. // ARC version didn't use object_dispose()
  41. // and should have called -dealloc on 4 objects
  42. testassert(TestRootDealloc == 4);
  43. #else
  44. // MRC version used object_dispose()
  45. // which doesn't call -dealloc
  46. testassert(TestRootDealloc == 0);
  47. #endif
  48. succeed(__FILE__);
  49. }