weak_ptr_ut.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "weak_ptr.h"
  3. Y_UNIT_TEST_SUITE(TWeakPtrTest) {
  4. struct TWeakPtrTester: public TWeakRefCounted<TWeakPtrTester> {
  5. int* const CounterPtr;
  6. TWeakPtrTester(int* counterPtr)
  7. : CounterPtr(counterPtr)
  8. {
  9. }
  10. ~TWeakPtrTester() {
  11. ++*CounterPtr;
  12. }
  13. };
  14. Y_UNIT_TEST(Simple) {
  15. int destroyCount = 0;
  16. TIntrusivePtr<TWeakPtrTester> p(new TWeakPtrTester(&destroyCount));
  17. UNIT_ASSERT(!!p);
  18. UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());
  19. TWeakPtr<TWeakPtrTester> p2(p);
  20. UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());
  21. {
  22. TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
  23. UNIT_ASSERT(!!p3);
  24. UNIT_ASSERT_VALUES_EQUAL(2u, p->RefCount());
  25. }
  26. p.Drop();
  27. UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
  28. {
  29. TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
  30. UNIT_ASSERT(!p3);
  31. }
  32. UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
  33. }
  34. }