accessors2.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // TEST_CONFIG
  2. #include "test.h"
  3. #include <objc/runtime.h>
  4. #include <objc/objc-abi.h>
  5. #include "testroot.i"
  6. @interface Base : TestRoot {
  7. @public
  8. id ivar;
  9. }
  10. @end
  11. @implementation Base @end
  12. int main()
  13. {
  14. SEL _cmd = @selector(foo);
  15. Base *o = [Base new];
  16. ptrdiff_t offset = ivar_getOffset(class_getInstanceVariable([Base class], "ivar"));
  17. testassert(offset == sizeof(id));
  18. TestRoot *value = [TestRoot new];
  19. // fixme test atomicity
  20. // Original setter API
  21. testprintf("original nonatomic retain\n");
  22. o->ivar = nil;
  23. TestRootRetain = 0;
  24. TestRootCopyWithZone = 0;
  25. TestRootMutableCopyWithZone = 0;
  26. objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, NO/*copy*/);
  27. testassert(TestRootRetain == 1);
  28. testassert(TestRootCopyWithZone == 0);
  29. testassert(TestRootMutableCopyWithZone == 0);
  30. testassert(o->ivar == value);
  31. testprintf("original atomic retain\n");
  32. o->ivar = nil;
  33. TestRootRetain = 0;
  34. TestRootCopyWithZone = 0;
  35. TestRootMutableCopyWithZone = 0;
  36. objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, NO/*copy*/);
  37. testassert(TestRootRetain == 1);
  38. testassert(TestRootCopyWithZone == 0);
  39. testassert(TestRootMutableCopyWithZone == 0);
  40. testassert(o->ivar == value);
  41. testprintf("original nonatomic copy\n");
  42. o->ivar = nil;
  43. TestRootRetain = 0;
  44. TestRootCopyWithZone = 0;
  45. TestRootMutableCopyWithZone = 0;
  46. objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, YES/*copy*/);
  47. testassert(TestRootRetain == 0);
  48. testassert(TestRootCopyWithZone == 1);
  49. testassert(TestRootMutableCopyWithZone == 0);
  50. testassert(o->ivar && o->ivar != value);
  51. testprintf("original atomic copy\n");
  52. o->ivar = nil;
  53. TestRootRetain = 0;
  54. TestRootCopyWithZone = 0;
  55. TestRootMutableCopyWithZone = 0;
  56. objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, YES/*copy*/);
  57. testassert(TestRootRetain == 0);
  58. testassert(TestRootCopyWithZone == 1);
  59. testassert(TestRootMutableCopyWithZone == 0);
  60. testassert(o->ivar && o->ivar != value);
  61. testprintf("original nonatomic mutablecopy\n");
  62. o->ivar = nil;
  63. TestRootRetain = 0;
  64. TestRootCopyWithZone = 0;
  65. TestRootMutableCopyWithZone = 0;
  66. objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, 2/*copy*/);
  67. testassert(TestRootRetain == 0);
  68. testassert(TestRootCopyWithZone == 0);
  69. testassert(TestRootMutableCopyWithZone == 1);
  70. testassert(o->ivar && o->ivar != value);
  71. testprintf("original atomic mutablecopy\n");
  72. o->ivar = nil;
  73. TestRootRetain = 0;
  74. TestRootCopyWithZone = 0;
  75. TestRootMutableCopyWithZone = 0;
  76. objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, 2/*copy*/);
  77. testassert(TestRootRetain == 0);
  78. testassert(TestRootCopyWithZone == 0);
  79. testassert(TestRootMutableCopyWithZone == 1);
  80. testassert(o->ivar && o->ivar != value);
  81. // Optimized setter API
  82. testprintf("optimized nonatomic retain\n");
  83. o->ivar = nil;
  84. TestRootRetain = 0;
  85. TestRootCopyWithZone = 0;
  86. TestRootMutableCopyWithZone = 0;
  87. objc_setProperty_nonatomic(o, _cmd, value, offset);
  88. testassert(TestRootRetain == 1);
  89. testassert(TestRootCopyWithZone == 0);
  90. testassert(TestRootMutableCopyWithZone == 0);
  91. testassert(o->ivar == value);
  92. testprintf("optimized atomic retain\n");
  93. o->ivar = nil;
  94. TestRootRetain = 0;
  95. TestRootCopyWithZone = 0;
  96. TestRootMutableCopyWithZone = 0;
  97. objc_setProperty_atomic(o, _cmd, value, offset);
  98. testassert(TestRootRetain == 1);
  99. testassert(TestRootCopyWithZone == 0);
  100. testassert(TestRootMutableCopyWithZone == 0);
  101. testassert(o->ivar == value);
  102. testprintf("optimized nonatomic copy\n");
  103. o->ivar = nil;
  104. TestRootRetain = 0;
  105. TestRootCopyWithZone = 0;
  106. TestRootMutableCopyWithZone = 0;
  107. objc_setProperty_nonatomic_copy(o, _cmd, value, offset);
  108. testassert(TestRootRetain == 0);
  109. testassert(TestRootCopyWithZone == 1);
  110. testassert(TestRootMutableCopyWithZone == 0);
  111. testassert(o->ivar && o->ivar != value);
  112. testprintf("optimized atomic copy\n");
  113. o->ivar = nil;
  114. TestRootRetain = 0;
  115. TestRootCopyWithZone = 0;
  116. TestRootMutableCopyWithZone = 0;
  117. objc_setProperty_atomic_copy(o, _cmd, value, offset);
  118. testassert(TestRootRetain == 0);
  119. testassert(TestRootCopyWithZone == 1);
  120. testassert(TestRootMutableCopyWithZone == 0);
  121. testassert(o->ivar && o->ivar != value);
  122. succeed(__FILE__);
  123. }