initialize.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // TEST_CONFIG
  2. // initialize.m
  3. // Test basic +initialize behavior
  4. // * +initialize before class method
  5. // * superclass +initialize before subclass +initialize
  6. // * subclass inheritance of superclass implementation
  7. // * messaging during +initialize
  8. // * +initialize provoked by class_getMethodImplementation
  9. // * +initialize not provoked by objc_getClass
  10. #include "test.h"
  11. #include "testroot.i"
  12. int state = 0;
  13. @interface Super0 : TestRoot @end
  14. @implementation Super0
  15. +(void)initialize {
  16. fail("objc_getClass() must not trigger +initialize");
  17. }
  18. @end
  19. @interface Super : TestRoot @end
  20. @implementation Super
  21. +(void)initialize {
  22. testprintf("in [Super initialize]\n");
  23. testassert(state == 0);
  24. state = 1;
  25. }
  26. +(void)method {
  27. fail("[Super method] shouldn't be called");
  28. }
  29. @end
  30. @interface Sub : Super @end
  31. @implementation Sub
  32. +(void)initialize {
  33. testprintf("in [Sub initialize]\n");
  34. testassert(state == 1);
  35. state = 2;
  36. }
  37. +(void)method {
  38. testprintf("in [Sub method]\n");
  39. testassert(state == 2);
  40. state = 3;
  41. }
  42. @end
  43. @interface Super2 : TestRoot @end
  44. @interface Sub2 : Super2 @end
  45. @implementation Super2
  46. +(void)initialize {
  47. if (self == objc_getClass("Sub2")) {
  48. testprintf("in [Super2 initialize] of Sub2\n");
  49. testassert(state == 1);
  50. state = 2;
  51. } else if (self == objc_getClass("Super2")) {
  52. testprintf("in [Super2 initialize] of Super2\n");
  53. testassert(state == 0);
  54. state = 1;
  55. } else {
  56. fail("in [Super2 initialize] of unknown class");
  57. }
  58. }
  59. +(void)method {
  60. testprintf("in [Super2 method]\n");
  61. testassert(state == 2);
  62. state = 3;
  63. }
  64. @end
  65. @implementation Sub2
  66. // nothing here
  67. @end
  68. @interface Super3 : TestRoot @end
  69. @interface Sub3 : Super3 @end
  70. @implementation Super3
  71. +(void)initialize {
  72. if (self == [Sub3 class]) { // this message triggers [Sub3 initialize]
  73. testprintf("in [Super3 initialize] of Sub3\n");
  74. testassert(state == 0);
  75. state = 1;
  76. } else if (self == [Super3 class]) {
  77. testprintf("in [Super3 initialize] of Super3\n");
  78. testassert(state == 1);
  79. state = 2;
  80. } else {
  81. fail("in [Super3 initialize] of unknown class");
  82. }
  83. }
  84. +(void)method {
  85. testprintf("in [Super3 method]\n");
  86. testassert(state == 2);
  87. state = 3;
  88. }
  89. @end
  90. @implementation Sub3
  91. // nothing here
  92. @end
  93. @interface Super4 : TestRoot @end
  94. @implementation Super4
  95. -(void)instanceMethod {
  96. testassert(state == 1);
  97. state = 2;
  98. }
  99. +(void)initialize {
  100. testprintf("in [Super4 initialize]\n");
  101. testassert(state == 0);
  102. state = 1;
  103. id x = [[self alloc] init];
  104. [x instanceMethod];
  105. RELEASE_VALUE(x);
  106. }
  107. @end
  108. @interface Super5 : TestRoot @end
  109. @implementation Super5
  110. -(void)instanceMethod {
  111. }
  112. +(void)classMethod {
  113. testassert(state == 1);
  114. state = 2;
  115. }
  116. +(void)initialize {
  117. testprintf("in [Super5 initialize]\n");
  118. testassert(state == 0);
  119. state = 1;
  120. class_getMethodImplementation(self, @selector(instanceMethod));
  121. // this is the "memoized" case for getNonMetaClass
  122. class_getMethodImplementation(object_getClass(self), @selector(classMethod));
  123. [self classMethod];
  124. }
  125. @end
  126. @interface Super6 : TestRoot @end
  127. @interface Sub6 : Super6 @end
  128. @implementation Super6
  129. +(void)initialize {
  130. static bool once;
  131. bool wasOnce;
  132. testprintf("in [Super6 initialize] (#%d)\n", 1+(int)once);
  133. if (!once) {
  134. once = true;
  135. wasOnce = true;
  136. testassert(state == 0);
  137. state = 1;
  138. } else {
  139. wasOnce = false;
  140. testassert(state == 2);
  141. state = 3;
  142. }
  143. [Sub6 class];
  144. if (wasOnce) {
  145. testassert(state == 5);
  146. state = 6;
  147. } else {
  148. testassert(state == 3);
  149. state = 4;
  150. }
  151. }
  152. @end
  153. @implementation Sub6
  154. +(void)initialize {
  155. testprintf("in [Sub6 initialize]\n");
  156. testassert(state == 1);
  157. state = 2;
  158. [super initialize];
  159. testassert(state == 4);
  160. state = 5;
  161. }
  162. @end
  163. @interface Super7 : TestRoot @end
  164. @interface Sub7 : Super7 @end
  165. @implementation Super7
  166. +(void)initialize {
  167. static bool once;
  168. bool wasOnce;
  169. testprintf("in [Super7 initialize] (#%d)\n", 1+(int)once);
  170. if (!once) {
  171. once = true;
  172. wasOnce = true;
  173. testassert(state == 0);
  174. state = 1;
  175. } else {
  176. wasOnce = false;
  177. testassert(state == 2);
  178. state = 3;
  179. }
  180. [Sub7 class];
  181. if (wasOnce) {
  182. testassert(state == 5);
  183. state = 6;
  184. } else {
  185. testassert(state == 3);
  186. state = 4;
  187. }
  188. }
  189. @end
  190. @implementation Sub7
  191. +(void)initialize {
  192. testprintf("in [Sub7 initialize]\n");
  193. testassert(state == 1);
  194. state = 2;
  195. [super initialize];
  196. testassert(state == 4);
  197. state = 5;
  198. }
  199. @end
  200. @interface SuperThrower : TestRoot @end
  201. @implementation SuperThrower
  202. +(void)initialize {
  203. testprintf("in [SuperThrower initialize]\n");
  204. testassert(state == 0);
  205. state = 10;
  206. @throw AUTORELEASE([TestRoot new]);
  207. fail("@throw didn't throw");
  208. }
  209. @end
  210. @interface SubThrower : SuperThrower @end
  211. @implementation SubThrower
  212. +(void)initialize {
  213. testprintf("in [SubThrower initialize]\n");
  214. testassert(state == 0);
  215. state = 20;
  216. }
  217. @end
  218. int main()
  219. {
  220. Class cls;
  221. // objc_getClass() must not +initialize anything
  222. state = 0;
  223. objc_getClass("Super0");
  224. testassert(state == 0);
  225. // initialize superclass, then subclass
  226. state = 0;
  227. [Sub method];
  228. testassert(state == 3);
  229. // check subclass's inheritance of superclass initialize
  230. state = 0;
  231. [Sub2 method];
  232. testassert(state == 3);
  233. // check subclass method called from superclass initialize
  234. state = 0;
  235. [Sub3 method];
  236. testassert(state == 3);
  237. // check class_getMethodImplementation (instance method)
  238. state = 0;
  239. cls = objc_getClass("Super4");
  240. testassert(state == 0);
  241. class_getMethodImplementation(cls, @selector(classMethod));
  242. testassert(state == 2);
  243. // check class_getMethodImplementation (class method)
  244. // this is the "slow" case for getNonMetaClass
  245. state = 0;
  246. cls = objc_getClass("Super5");
  247. testassert(state == 0);
  248. class_getMethodImplementation(object_getClass(cls), @selector(instanceMethod));
  249. testassert(state == 2);
  250. // check +initialize cycles
  251. // this is the "cls is a subclass" case for getNonMetaClass
  252. state = 0;
  253. [Super6 class];
  254. testassert(state == 6);
  255. // check +initialize cycles
  256. // this is the "cls is a subclass" case for getNonMetaClass
  257. state = 0;
  258. [Sub7 class];
  259. testassert(state == 6);
  260. // exception from +initialize must be handled cleanly
  261. PUSH_POOL {
  262. alarm(3);
  263. testonthread( ^{
  264. @try {
  265. state = 0;
  266. [SuperThrower class];
  267. fail("where's the beef^Wexception?");
  268. } @catch (...) {
  269. testassert(state == 10);
  270. state = 11;
  271. }
  272. testassert(state == 11);
  273. });
  274. @try {
  275. state = 0;
  276. [SuperThrower class];
  277. testassert(state == 0);
  278. [SubThrower class];
  279. testassert(state == 20);
  280. } @catch (...) {
  281. fail("+initialize called again after exception");
  282. }
  283. } POP_POOL;
  284. succeed(__FILE__);
  285. return 0;
  286. }