File: test_weak_ptr.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (281 lines) | stat: -rw-r--r-- 14,027 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#include "test_weak_ptr.hpp"

#include <libdnf5/common/weak_ptr.hpp>

#include <memory>
#include <string>
#include <vector>

CPPUNIT_TEST_SUITE_REGISTRATION(WeakPtrTest);


void WeakPtrTest::setUp() {}


void WeakPtrTest::tearDown() {}


// In this test the WeakPtr instances point to data owned by Sack instance.
void WeakPtrTest::test_weak_ptr() {
    class Sack {
    public:
        using DataItemWeakPtr = libdnf5::WeakPtr<std::string, false>;

        DataItemWeakPtr add_item_with_return(std::unique_ptr<std::string> && item) {
            auto ret = DataItemWeakPtr(item.get(), &data_guard);
            data.push_back(std::move(item));
            return ret;
        }

        libdnf5::WeakPtrGuard<std::string, false> data_guard;

    private:
        std::vector<std::unique_ptr<std::string>>
            data;  // Owns the data set. Objects get deleted when the Sack is deleted.
    };

    // add weak pointers, test WeakPtrGuard::empty(), and WeakPtrGuard::size()
    auto sack1 = std::make_unique<Sack>();
    CPPUNIT_ASSERT(sack1->data_guard.empty());
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(0));
    auto item1_weak_ptr = sack1->add_item_with_return(std::make_unique<std::string>("sack1_item1"));
    auto item2_weak_ptr = sack1->add_item_with_return(std::make_unique<std::string>("sack1_item2"));
    CPPUNIT_ASSERT(!sack1->data_guard.empty());
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(2));

    auto sack2 = std::make_unique<Sack>();
    auto item3_weak_ptr = sack2->add_item_with_return(std::make_unique<std::string>("sack2_item1"));
    auto item4_weak_ptr = sack2->add_item_with_return(std::make_unique<std::string>("sack2_item2"));

    // test access to data managed by WeakPtr (by get() and operator ->)
    CPPUNIT_ASSERT(*item1_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(item2_weak_ptr->compare("sack1_item2") == 0);
    CPPUNIT_ASSERT(*item3_weak_ptr.get() == "sack2_item1");
    CPPUNIT_ASSERT(item4_weak_ptr->compare("sack2_item2") == 0);

    // test hase_same_guard() method
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr.has_same_guard(item1_weak_ptr), true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr.has_same_guard(item2_weak_ptr), true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr.has_same_guard(item3_weak_ptr), false);

    // delete sack2
    sack2.reset();

    // data from sack1 must be still accessible, but access to data from sack2 must throw exception
    CPPUNIT_ASSERT(*item1_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(item2_weak_ptr->compare("sack1_item2") == 0);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item3_weak_ptr.get() == "sack2_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(item4_weak_ptr->compare("sack2_item2") == 0), libdnf5::AssertionError);

    // test is_valid() method
    CPPUNIT_ASSERT(item1_weak_ptr.is_valid());
    CPPUNIT_ASSERT(item2_weak_ptr.is_valid());
    CPPUNIT_ASSERT(!item3_weak_ptr.is_valid());
    CPPUNIT_ASSERT(!item4_weak_ptr.is_valid());

    // test copy constructor
    auto item5_weak_ptr(item1_weak_ptr);
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(3));
    CPPUNIT_ASSERT(*item1_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(*item5_weak_ptr.get() == "sack1_item1");

    // there is no move constructor, copy constructor must be used
    auto item6_weak_ptr(std::move(item5_weak_ptr));
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(4));
    CPPUNIT_ASSERT(*item5_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(*item6_weak_ptr.get() == "sack1_item1");

    // test copy assignment operator =
    item3_weak_ptr = item1_weak_ptr;
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(5));
    CPPUNIT_ASSERT(*item1_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(*item3_weak_ptr.get() == "sack1_item1");

    // there is no move assignment operator =, copy assignment must be used
    item4_weak_ptr = std::move(item2_weak_ptr);
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(6));
    CPPUNIT_ASSERT(*item2_weak_ptr.get() == "sack1_item2");
    CPPUNIT_ASSERT(*item4_weak_ptr.get() == "sack1_item2");

    // test operator ==
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr == item1_weak_ptr, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr == item3_weak_ptr, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr == item4_weak_ptr, false);

    // test operator !=
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item1_weak_ptr, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item3_weak_ptr, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item4_weak_ptr, true);

    // test WeakPtrGuard::clear()
    // It must deregister and invalidate all registered weak pointers.
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(6));
    CPPUNIT_ASSERT(*item1_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(*item2_weak_ptr.get() == "sack1_item2");
    CPPUNIT_ASSERT(*item3_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(*item4_weak_ptr.get() == "sack1_item2");
    CPPUNIT_ASSERT(*item5_weak_ptr.get() == "sack1_item1");
    CPPUNIT_ASSERT(*item6_weak_ptr.get() == "sack1_item1");
    sack1->data_guard.clear();
    CPPUNIT_ASSERT(sack1->data_guard.empty());
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(0));
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item1_weak_ptr.get() == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item2_weak_ptr.get() == "sack1_item2"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item3_weak_ptr.get() == "sack1_item2"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item4_weak_ptr.get() == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item5_weak_ptr.get() == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item6_weak_ptr.get() == "sack1_item1"), libdnf5::AssertionError);
}


// In this test the WeakPtr instances own DependentItem instances that point to data owned by Sack instance.
void WeakPtrTest::test_weak_ptr_is_owner() {
    struct DependentItem {
        std::string * remote_data;
    };

    class Sack {
    public:
        using DataItemWeakPtr = libdnf5::WeakPtr<DependentItem, true>;

        DataItemWeakPtr add_item_with_return(std::unique_ptr<std::string> && item) {
            auto ret = DataItemWeakPtr(new DependentItem{item.get()}, &data_guard);
            data.push_back(std::move(item));
            return ret;
        }

        libdnf5::WeakPtrGuard<DependentItem, true> data_guard;

    private:
        std::vector<std::unique_ptr<std::string>>
            data;  // Owns the data set. Objects get deleted when the Sack is deleted.
    };

    // add weak pointers, test WeakPtrGuard::empty(), and WeakPtrGuard::size()
    auto sack1 = std::make_unique<Sack>();
    CPPUNIT_ASSERT(sack1->data_guard.empty());
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(0));
    auto item1_weak_ptr = sack1->add_item_with_return(std::make_unique<std::string>("sack1_item1"));
    auto item2_weak_ptr = sack1->add_item_with_return(std::make_unique<std::string>("sack1_item2"));
    CPPUNIT_ASSERT(!sack1->data_guard.empty());
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(2));

    auto sack2 = std::make_unique<Sack>();
    auto item3_weak_ptr = sack2->add_item_with_return(std::make_unique<std::string>("sack2_item1"));
    auto item4_weak_ptr = sack2->add_item_with_return(std::make_unique<std::string>("sack2_item2"));

    // test access to data managed by WeakPtr (by get() and operator ->)
    CPPUNIT_ASSERT(*item1_weak_ptr.get()->remote_data == "sack1_item1");
    CPPUNIT_ASSERT(*item2_weak_ptr->remote_data == "sack1_item2");
    CPPUNIT_ASSERT(*item3_weak_ptr.get()->remote_data == "sack2_item1");
    CPPUNIT_ASSERT(*item4_weak_ptr->remote_data == "sack2_item2");

    // test hase_same_guard() method
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr.has_same_guard(item1_weak_ptr), true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr.has_same_guard(item2_weak_ptr), true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr.has_same_guard(item3_weak_ptr), false);

    // delete sack2
    sack2.reset();

    // data from sack1 must be still accessible, but access to data from sack2 must throw exception
    CPPUNIT_ASSERT(*item1_weak_ptr.get()->remote_data == "sack1_item1");
    CPPUNIT_ASSERT(*item2_weak_ptr->remote_data == "sack1_item2");
    CPPUNIT_ASSERT_THROW(
        static_cast<void>(*item3_weak_ptr.get()->remote_data == "sack2_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item4_weak_ptr->remote_data == "sack2_item2"), libdnf5::AssertionError);

    // test is_valid() method
    CPPUNIT_ASSERT(item1_weak_ptr.is_valid());
    CPPUNIT_ASSERT(item2_weak_ptr.is_valid());
    CPPUNIT_ASSERT(!item3_weak_ptr.is_valid());
    CPPUNIT_ASSERT(!item4_weak_ptr.is_valid());

    // test copy constructor
    auto item5_weak_ptr(item1_weak_ptr);
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(3));
    CPPUNIT_ASSERT(*item1_weak_ptr->remote_data == "sack1_item1");
    CPPUNIT_ASSERT(*item5_weak_ptr->remote_data == "sack1_item1");

    // there move constructor
    auto item6_weak_ptr(std::move(item5_weak_ptr));
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(3));
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item5_weak_ptr->remote_data == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT(*item6_weak_ptr->remote_data == "sack1_item1");

    // test copy assignment operator =
    item3_weak_ptr = item1_weak_ptr;
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(4));
    CPPUNIT_ASSERT(*item1_weak_ptr->remote_data == "sack1_item1");
    CPPUNIT_ASSERT(*item3_weak_ptr->remote_data == "sack1_item1");

    // test copy self-assignment operator =
    item3_weak_ptr = item3_weak_ptr;
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(4));
    CPPUNIT_ASSERT(*item3_weak_ptr->remote_data == "sack1_item1");

    // test move assignment operator =
    item4_weak_ptr = std::move(item2_weak_ptr);
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(4));
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item2_weak_ptr->remote_data == "sack1_item2"), libdnf5::AssertionError);
    CPPUNIT_ASSERT(*item4_weak_ptr->remote_data == "sack1_item2");

    // test move self-assignment operator =
    item4_weak_ptr = std::move(item4_weak_ptr);
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(4));
    CPPUNIT_ASSERT(*item4_weak_ptr->remote_data == "sack1_item2");

    // test operator ==
    // Each WeakPtr instance has its own data. However, the data may depend on the same remote data.
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr == item1_weak_ptr, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr->remote_data == item1_weak_ptr->remote_data, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr == item3_weak_ptr, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr->remote_data == item3_weak_ptr->remote_data, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr == item4_weak_ptr, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr->remote_data == item4_weak_ptr->remote_data, false);

    // test operator !=
    // Each WeakPtr instance has its own data. However, the data may depend on the same remote data.
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item1_weak_ptr, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr->remote_data != item1_weak_ptr->remote_data, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item3_weak_ptr, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr->remote_data != item3_weak_ptr->remote_data, false);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item4_weak_ptr, true);
    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr->remote_data != item4_weak_ptr->remote_data, true);

    CPPUNIT_ASSERT_EQUAL(item1_weak_ptr != item4_weak_ptr, true);

    // test WeakPtrGuard::clear()
    // It must deregister and invalidate all registered weak pointers.
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(4));
    CPPUNIT_ASSERT(*item1_weak_ptr->remote_data == "sack1_item1");
    CPPUNIT_ASSERT(*item3_weak_ptr->remote_data == "sack1_item1");
    CPPUNIT_ASSERT(*item4_weak_ptr->remote_data == "sack1_item2");
    CPPUNIT_ASSERT(*item6_weak_ptr->remote_data == "sack1_item1");
    sack1->data_guard.clear();
    CPPUNIT_ASSERT(sack1->data_guard.empty());
    CPPUNIT_ASSERT_EQUAL(sack1->data_guard.size(), static_cast<std::size_t>(0));
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item1_weak_ptr->remote_data == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item3_weak_ptr->remote_data == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item4_weak_ptr->remote_data == "sack1_item1"), libdnf5::AssertionError);
    CPPUNIT_ASSERT_THROW(static_cast<void>(*item6_weak_ptr->remote_data == "sack1_item2"), libdnf5::AssertionError);
}