weakcopy.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // TEST_CFLAGS -fobjc-weak
  2. #include "test.h"
  3. #include "testroot.i"
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <objc/objc-runtime.h>
  7. @interface Weak : TestRoot {
  8. @public
  9. __weak id value;
  10. }
  11. @end
  12. @implementation Weak
  13. @end
  14. Weak *oldObject;
  15. Weak *newObject;
  16. int main()
  17. {
  18. testonthread(^{
  19. TestRoot *value;
  20. PUSH_POOL {
  21. value = [TestRoot new];
  22. testassert(value);
  23. oldObject = [Weak new];
  24. testassert(oldObject);
  25. oldObject->value = value;
  26. testassert(oldObject->value == value);
  27. newObject = [oldObject copy];
  28. testassert(newObject);
  29. testassert(newObject->value == oldObject->value);
  30. newObject->value = nil;
  31. testassert(newObject->value == nil);
  32. testassert(oldObject->value == value);
  33. } POP_POOL;
  34. testcollect();
  35. TestRootDealloc = 0;
  36. RELEASE_VAR(value);
  37. });
  38. testcollect();
  39. testassert(TestRootDealloc);
  40. #if __has_feature(objc_arc_weak)
  41. testassert(oldObject->value == nil);
  42. #endif
  43. testassert(newObject->value == nil);
  44. RELEASE_VAR(newObject);
  45. RELEASE_VAR(oldObject);
  46. succeed(__FILE__);
  47. return 0;
  48. }