aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/misc/weak_ptr_ut.cpp
blob: 9677ae21b5bd3a2965ba64c3477cc4291458e980 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <library/cpp/testing/unittest/registar.h>

#include "weak_ptr.h"

Y_UNIT_TEST_SUITE(TWeakPtrTest) {
    struct TWeakPtrTester: public TWeakRefCounted<TWeakPtrTester> {
        int* const CounterPtr;

        TWeakPtrTester(int* counterPtr) 
            : CounterPtr(counterPtr) 
        { 
        } 
        ~TWeakPtrTester() { 
            ++*CounterPtr; 
        } 
    };

    Y_UNIT_TEST(Simple) {
        int destroyCount = 0;

        TIntrusivePtr<TWeakPtrTester> p(new TWeakPtrTester(&destroyCount));

        UNIT_ASSERT(!!p);
        UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());

        TWeakPtr<TWeakPtrTester> p2(p);

        UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());

        {
            TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
            UNIT_ASSERT(!!p3);
            UNIT_ASSERT_VALUES_EQUAL(2u, p->RefCount());
        }

        p.Drop();
        UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);

        {
            TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
            UNIT_ASSERT(!p3);
        }

        UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
    }
}