rr-autorelease-fastarc.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // TEST_CFLAGS -Os -framework Foundation
  2. // TEST_DISABLED pending clang support for rdar://20530049
  3. #include "test.h"
  4. #include "testroot.i"
  5. #include <objc/objc-internal.h>
  6. #include <objc/objc-abi.h>
  7. #include <Foundation/Foundation.h>
  8. @interface TestObject : TestRoot @end
  9. @implementation TestObject @end
  10. @interface Tester : NSObject @end
  11. @implementation Tester {
  12. @public
  13. id ivar;
  14. }
  15. -(id) return0 {
  16. return ivar;
  17. }
  18. -(id) return1 {
  19. id x = ivar;
  20. [x self];
  21. return x;
  22. }
  23. @end
  24. OBJC_EXPORT
  25. id
  26. objc_retainAutoreleasedReturnValue(id obj);
  27. // Accept a value returned through a +0 autoreleasing convention for use at +0.
  28. OBJC_EXPORT
  29. id
  30. objc_unsafeClaimAutoreleasedReturnValue(id obj);
  31. int
  32. main()
  33. {
  34. TestObject *obj;
  35. Tester *tt = [Tester new];
  36. #ifdef __x86_64__
  37. // need to get DYLD to resolve the stubs on x86
  38. PUSH_POOL {
  39. TestObject *warm_up = [[TestObject alloc] init];
  40. testassert(warm_up);
  41. warm_up = objc_retainAutoreleasedReturnValue(warm_up);
  42. warm_up = objc_unsafeClaimAutoreleasedReturnValue(warm_up);
  43. warm_up = nil;
  44. } POP_POOL;
  45. #endif
  46. testprintf(" Successful +1 -> +1 handshake\n");
  47. PUSH_POOL {
  48. obj = [[TestObject alloc] init];
  49. testassert(obj);
  50. tt->ivar = obj;
  51. obj = nil;
  52. TestRootRetain = 0;
  53. TestRootRelease = 0;
  54. TestRootAutorelease = 0;
  55. TestRootDealloc = 0;
  56. TestObject *tmp = [tt return1];
  57. testassert(TestRootDealloc == 0);
  58. testassert(TestRootRetain == 1);
  59. testassert(TestRootRelease == 0);
  60. testassert(TestRootAutorelease == 0);
  61. tt->ivar = nil;
  62. testassert(TestRootDealloc == 0);
  63. testassert(TestRootRetain == 1);
  64. testassert(TestRootRelease == 1);
  65. testassert(TestRootAutorelease == 0);
  66. tmp = nil;
  67. testassert(TestRootDealloc == 1);
  68. testassert(TestRootRetain == 1);
  69. testassert(TestRootRelease == 2);
  70. testassert(TestRootAutorelease == 0);
  71. } POP_POOL;
  72. testprintf(" Successful +0 -> +0 handshake\n");
  73. PUSH_POOL {
  74. obj = [[TestObject alloc] init];
  75. testassert(obj);
  76. tt->ivar = obj;
  77. obj = nil;
  78. TestRootRetain = 0;
  79. TestRootRelease = 0;
  80. TestRootAutorelease = 0;
  81. TestRootDealloc = 0;
  82. __unsafe_unretained TestObject *tmp = [tt return0];
  83. testassert(TestRootDealloc == 0);
  84. testassert(TestRootRetain == 0);
  85. testassert(TestRootRelease == 0);
  86. testassert(TestRootAutorelease == 0);
  87. tmp = nil;
  88. testassert(TestRootDealloc == 0);
  89. testassert(TestRootRetain == 0);
  90. testassert(TestRootRelease == 0);
  91. testassert(TestRootAutorelease == 0);
  92. tt->ivar = nil;
  93. testassert(TestRootDealloc == 1);
  94. testassert(TestRootRetain == 0);
  95. testassert(TestRootRelease == 1);
  96. testassert(TestRootAutorelease == 0);
  97. } POP_POOL;
  98. testprintf(" Successful +1 -> +0 handshake\n");
  99. PUSH_POOL {
  100. obj = [[TestObject alloc] init];
  101. testassert(obj);
  102. tt->ivar = obj;
  103. obj = nil;
  104. TestRootRetain = 0;
  105. TestRootRelease = 0;
  106. TestRootAutorelease = 0;
  107. TestRootDealloc = 0;
  108. __unsafe_unretained TestObject *tmp = [tt return1];
  109. testassert(TestRootDealloc == 0);
  110. testassert(TestRootRetain == 1);
  111. testassert(TestRootRelease == 1);
  112. testassert(TestRootAutorelease == 0);
  113. tmp = nil;
  114. testassert(TestRootDealloc == 0);
  115. testassert(TestRootRetain == 1);
  116. testassert(TestRootRelease == 1);
  117. testassert(TestRootAutorelease == 0);
  118. tt->ivar = nil;
  119. testassert(TestRootDealloc == 1);
  120. testassert(TestRootRetain == 1);
  121. testassert(TestRootRelease == 2);
  122. testassert(TestRootAutorelease == 0);
  123. } POP_POOL;
  124. testprintf(" Successful +0 -> +1 handshake\n");
  125. PUSH_POOL {
  126. obj = [[TestObject alloc] init];
  127. testassert(obj);
  128. tt->ivar = obj;
  129. obj = nil;
  130. TestRootRetain = 0;
  131. TestRootRelease = 0;
  132. TestRootAutorelease = 0;
  133. TestRootDealloc = 0;
  134. TestObject *tmp = [tt return0];
  135. testassert(TestRootDealloc == 0);
  136. testassert(TestRootRetain == 1);
  137. testassert(TestRootRelease == 0);
  138. testassert(TestRootAutorelease == 0);
  139. tmp = nil;
  140. testassert(TestRootDealloc == 0);
  141. testassert(TestRootRetain == 1);
  142. testassert(TestRootRelease == 1);
  143. testassert(TestRootAutorelease == 0);
  144. tt->ivar = nil;
  145. testassert(TestRootDealloc == 1);
  146. testassert(TestRootRetain == 1);
  147. testassert(TestRootRelease == 2);
  148. testassert(TestRootAutorelease == 0);
  149. } POP_POOL;
  150. succeed(__FILE__);
  151. return 0;
  152. }
  153. #endif