File: test_weak_ref_counted_st.cpp

package info (click to toggle)
intrusive-shared-ptr 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: cpp: 2,852; ansic: 173; makefile: 3
file content (232 lines) | stat: -rw-r--r-- 7,693 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#if ISPTR_USE_MODULES
    import isptr;
#else
    #include <intrusive_shared_ptr/ref_counted.h>
    #include <intrusive_shared_ptr/refcnt_ptr.h>
#endif

#include <doctest/doctest.h>
#include <type_traits>
#include <cstdint>

using namespace isptr;

namespace
{
    int derived_count = 0;

    struct derived_counted : weak_ref_counted_st<derived_counted>
    {
        friend ref_counted;
    public:
        derived_counted() noexcept 
        {
            ++derived_count;
        }
    private:
        ~derived_counted() noexcept
        {
            auto weak = get_weak_ptr();
            CHECK(weak);
            --derived_count;
        }    
    };

    
    int wrapped_count = 0;
    struct wrapped
    {
        wrapped()
        {
            ++wrapped_count;
        }
        ~wrapped()
        {
            --wrapped_count;
        }
        
        int value = 5;
    };

    using wrapped_counted = weak_ref_counted_adapter_st<wrapped>;

    int with_custom_weak_reference_count = 0;

    struct custom_weak_reference;

    class with_custom_weak_reference : public ref_counted<with_custom_weak_reference, 
                                                            ref_counted_flags::provide_weak_references |
                                                            ref_counted_flags::single_threaded>
    {
    friend ref_counted;
        
    public:
        with_custom_weak_reference()
        {
            ++with_custom_weak_reference_count;
        }
    private:
        
        ~with_custom_weak_reference() noexcept
        {
            auto weak = get_weak_ptr();
            CHECK(weak);
            --with_custom_weak_reference_count;
        }
        
        custom_weak_reference * make_weak_reference(intptr_t count) const;
    };

    struct custom_weak_reference : weak_reference<with_custom_weak_reference>
    {
        custom_weak_reference(intptr_t count, with_custom_weak_reference * obj): weak_reference(count, obj)
        {}
        
        ~custom_weak_reference()
        {
            CHECK(on_owner_destruction_called);
            CHECK(with_custom_weak_reference_count == 0);
        }
        
        void on_owner_destruction() const
        {
            CHECK(with_custom_weak_reference_count == 0); //owner is still alive but its refcount is 0, you cannot ressurrect!
            on_owner_destruction_called = true;
        }
        
        mutable bool on_owner_destruction_called = false;
    };

    inline custom_weak_reference * with_custom_weak_reference::make_weak_reference(intptr_t count) const
        { return new custom_weak_reference(count, const_cast<with_custom_weak_reference *>(this)); }
}

TEST_SUITE("traits") {

TEST_CASE( "Weak ref counted st type traits are correct" ) {

    SUBCASE( "Base" ) {

        CHECK( weak_ref_counted_st<derived_counted>::single_threaded);
        CHECK( !std::is_default_constructible_v<weak_ref_counted_st<derived_counted>> );
        CHECK( !std::is_copy_constructible_v<weak_ref_counted_st<derived_counted>> );
        CHECK( !std::is_move_constructible_v<weak_ref_counted_st<derived_counted>> );
        CHECK( !std::is_copy_assignable_v<weak_ref_counted_st<derived_counted>> );
        CHECK( !std::is_move_assignable_v<weak_ref_counted_st<derived_counted>> );
        CHECK( !std::is_swappable_v<weak_ref_counted_st<derived_counted>> );
        CHECK( !std::is_destructible_v<weak_ref_counted_st<derived_counted>> );
    }

    SUBCASE( "Derived" ) {
        CHECK( derived_counted::single_threaded);
        CHECK( !std::is_default_constructible_v<derived_counted> );
        CHECK( !std::is_copy_constructible_v<derived_counted> );
        CHECK( !std::is_move_constructible_v<derived_counted> );
        CHECK( !std::is_copy_assignable_v<derived_counted> );
        CHECK( !std::is_move_assignable_v<derived_counted> );
        CHECK( !std::is_swappable_v<derived_counted> );
        CHECK( !std::is_destructible_v<derived_counted> );
    }

    SUBCASE( "Derived WeakRef" ) {

        CHECK( derived_counted::weak_value_type::single_threaded);
        CHECK( !std::is_default_constructible_v<derived_counted::weak_value_type> );
        CHECK( !std::is_copy_constructible_v<derived_counted::weak_value_type> );
        CHECK( !std::is_move_constructible_v<derived_counted::weak_value_type> );
        CHECK( !std::is_copy_assignable_v<derived_counted::weak_value_type> );
        CHECK( !std::is_move_assignable_v<derived_counted::weak_value_type> );
        CHECK( !std::is_swappable_v<derived_counted::weak_value_type> );
        CHECK( !std::is_destructible_v<derived_counted::weak_value_type> );
    }

    SUBCASE( "Wrapped" ) {
        CHECK( wrapped_counted::single_threaded);
        CHECK( !std::is_default_constructible_v<wrapped_counted> );
        CHECK( !std::is_copy_constructible_v<wrapped_counted> );
        CHECK( !std::is_move_constructible_v<wrapped_counted> );
        CHECK( !std::is_copy_assignable_v<wrapped_counted> );
        CHECK( !std::is_move_assignable_v<wrapped_counted> );
        CHECK( !std::is_destructible_v<wrapped_counted> );
    }

    SUBCASE( "Wrapped WeakRef" ) {
        CHECK( wrapped_counted::weak_value_type::single_threaded);
        CHECK( !std::is_default_constructible_v<wrapped_counted::weak_value_type> );
        CHECK( !std::is_copy_constructible_v<wrapped_counted::weak_value_type> );
        CHECK( !std::is_move_constructible_v<wrapped_counted::weak_value_type> );
        CHECK( !std::is_copy_assignable_v<wrapped_counted::weak_value_type> );
        CHECK( !std::is_move_assignable_v<wrapped_counted::weak_value_type> );
        CHECK( !std::is_destructible_v<wrapped_counted::weak_value_type> );
    }
}

}

TEST_SUITE("ref_counted_st") {

TEST_CASE( "Weak ref counted st works" ) {

    SUBCASE( "Derived" ) {
        auto original = refcnt_attach(new derived_counted());
        auto weak1 = original->get_weak_ptr();
        CHECK(derived_count == 1);
        auto strong1 = weak1->lock();
        CHECK(original == strong1);
        auto weak2 = strong1->get_weak_ptr();
        CHECK(weak1 == weak2);
        auto weak3 = weak_cast(strong1);
        CHECK(weak1 == weak3);
        original.reset();
        strong1.reset();
        CHECK(derived_count == 0);

        strong1 = weak1->lock();
        CHECK(!strong1);
    }
    
    SUBCASE( "Const Derived" ) {
        refcnt_ptr<const derived_counted> original = refcnt_attach(new derived_counted());
        auto weak1 = original->get_weak_ptr();
        CHECK(derived_count == 1);
        auto strong1 = weak1->lock();
        CHECK(original == strong1);
        auto weak2 = strong1->get_weak_ptr();
        CHECK(weak1 == weak2);
        original.reset();
        strong1.reset();
        CHECK(derived_count == 0);

        strong1 = weak1->lock();
        CHECK(!strong1);
        strong1 = strong_cast(weak2);
        CHECK(!strong1);
    }

    SUBCASE( "Wrapped" ) {
        
        auto p = refcnt_attach(new wrapped_counted());
        CHECK(wrapped_count == 1);
        CHECK(p->value == 5);
        p.reset();
        CHECK( wrapped_count == 0 );
    }
    
    SUBCASE( "Custom Weak Reference" ) {
        
        auto strong = refcnt_attach(new with_custom_weak_reference);
        CHECK(with_custom_weak_reference_count == 1);
        auto weak = strong->get_weak_ptr();
        auto strong1 = weak->lock();
        CHECK( strong1 == strong );
        CHECK(with_custom_weak_reference_count == 1);
        strong.reset();
        CHECK(with_custom_weak_reference_count == 1);
        strong1.reset();
        CHECK(with_custom_weak_reference_count == 0);
        strong1 = weak->lock();
        CHECK(!strong1);
    }
}

}