exchangeImp.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. TEST_BUILD_OUTPUT
  3. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  4. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  5. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  6. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  7. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  8. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  9. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  10. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  11. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  12. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  13. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  14. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  15. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  16. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  17. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  18. .*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')?
  19. END
  20. */
  21. #include "test.h"
  22. #include "testroot.i"
  23. #include <objc/runtime.h>
  24. static int state;
  25. #define ONE 1
  26. #define TWO 2
  27. #define LENGTH 3
  28. #define COUNT 4
  29. @interface Super : TestRoot @end
  30. @implementation Super
  31. +(void) one { state = ONE; }
  32. +(void) two { state = TWO; }
  33. +(void) length { state = LENGTH; }
  34. +(void) count { state = COUNT; }
  35. @end
  36. #define checkExchange(s1, v1, s2, v2) \
  37. do { \
  38. Method m1, m2; \
  39. \
  40. testprintf("Check unexchanged version\n"); \
  41. state = 0; \
  42. [Super s1]; \
  43. testassert(state == v1); \
  44. state = 0; \
  45. [Super s2]; \
  46. testassert(state == v2); \
  47. \
  48. testprintf("Exchange\n"); \
  49. m1 = class_getClassMethod([Super class], @selector(s1)); \
  50. m2 = class_getClassMethod([Super class], @selector(s2)); \
  51. testassert(m1); \
  52. testassert(m2); \
  53. method_exchangeImplementations(m1, m2); \
  54. \
  55. testprintf("Check exchanged version\n"); \
  56. state = 0; \
  57. [Super s1]; \
  58. testassert(state == v2); \
  59. state = 0; \
  60. [Super s2]; \
  61. testassert(state == v1); \
  62. \
  63. testprintf("NULL should do nothing\n"); \
  64. method_exchangeImplementations(m1, NULL); \
  65. method_exchangeImplementations(NULL, m2); \
  66. method_exchangeImplementations(NULL, NULL); \
  67. \
  68. testprintf("Make sure NULL did nothing\n"); \
  69. state = 0; \
  70. [Super s1]; \
  71. testassert(state == v2); \
  72. state = 0; \
  73. [Super s2]; \
  74. testassert(state == v1); \
  75. \
  76. testprintf("Put them back\n"); \
  77. method_exchangeImplementations(m1, m2); \
  78. \
  79. testprintf("Check restored version\n"); \
  80. state = 0; \
  81. [Super s1]; \
  82. testassert(state == v1); \
  83. state = 0; \
  84. [Super s2]; \
  85. testassert(state == v2); \
  86. } while (0)
  87. int main()
  88. {
  89. // Check ordinary selectors
  90. checkExchange(one, ONE, two, TWO);
  91. // Check vtable selectors
  92. checkExchange(length, LENGTH, count, COUNT);
  93. // Check ordinary<->vtable and vtable<->ordinary
  94. checkExchange(count, COUNT, one, ONE);
  95. checkExchange(two, TWO, length, LENGTH);
  96. succeed(__FILE__);
  97. }