File: test_impl_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 (290 lines) | stat: -rw-r--r-- 12,259 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
282
283
284
285
286
287
288
289
290
// 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_impl_ptr.hpp"

#include <libdnf5/common/impl_ptr.hpp>

#include <type_traits>
#include <utility>


CPPUNIT_TEST_SUITE_REGISTRATION(ImplPtrTest);

namespace {

// Test class with instance counter. It is non move constructible, non move assignable, and non swappable.
class CTest {
public:
    CTest() noexcept { ++instance_counter; }
    CTest(int a) : a(a) { ++instance_counter; }
    CTest(const CTest & src) noexcept : a(src.a) { ++instance_counter; }
    CTest(CTest && src) = delete;

    CTest & operator=(const CTest & src) noexcept {
        a = src.a;
        return *this;
    }

    CTest & operator=(CTest && src) = delete;

    ~CTest() { --instance_counter; }

    // Returns the number of existing class instances.
    static int get_instance_counter() noexcept { return instance_counter; }

    int get_a() const noexcept { return a; }
    void set_a(int value) noexcept { a = value; }

private:
    int a{0};
    static int instance_counter;
};

int CTest::instance_counter = 0;

}  // namespace


static_assert(std::is_nothrow_default_constructible_v<libdnf5::ImplPtr<CTest>>);
static_assert(std::is_nothrow_constructible_v<libdnf5::ImplPtr<CTest>, CTest *>);
static_assert(std::is_copy_constructible_v<libdnf5::ImplPtr<CTest>>);
static_assert(std::is_copy_assignable_v<libdnf5::ImplPtr<CTest>>);

// `ImplPtr` is_nothrow_move_constructible, is_nothrow_move_assignable, and nothrow_swappable, even if CTest is not.
static_assert(!std::is_move_constructible_v<CTest>);
static_assert(!std::is_move_assignable_v<CTest>);
static_assert(!std::is_swappable_v<CTest>);
static_assert(std::is_nothrow_move_constructible_v<libdnf5::ImplPtr<CTest>>);
static_assert(std::is_nothrow_move_assignable_v<libdnf5::ImplPtr<CTest>>);
static_assert(std::is_nothrow_swappable_v<libdnf5::ImplPtr<CTest>>);


void ImplPtrTest::test_default_constructor() {
    // Tests an empty constructor. `p_impl` is` nullptr`. No new CTest instance is created.
    libdnf5::ImplPtr<CTest> empty_object;
    CPPUNIT_ASSERT(nullptr == empty_object.get());
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_constructor_from_pointer() {
    {
        // Tests constructor that takes ownership of existing `CTtest` instance.
        libdnf5::ImplPtr<CTest> object(new CTest);
        CPPUNIT_ASSERT(nullptr != object.get());
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
    }

    // `ImplPtr` was the owner of the` CTest` instance and destroyed it.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_access_to_managed_object() {
    {
        // Constructs mutable (non-constant) object.
        libdnf5::ImplPtr<CTest> object(new CTest(10));

        // Tests the `T * operator->()`.
        static_assert(!std::is_const_v<std::remove_pointer_t<decltype(object.operator->())>>);
        CPPUNIT_ASSERT_EQUAL(10, object->get_a());
        object->set_a(0);
        CPPUNIT_ASSERT_EQUAL(0, object->get_a());

        // Tests the `T & operator*()`.
        static_assert(!std::is_const_v<std::remove_reference_t<decltype(object.operator*())>>);
        CPPUNIT_ASSERT_EQUAL(0, (*object).get_a());
        (*object).set_a(10);
        CPPUNIT_ASSERT_EQUAL(10, (*object).get_a());

        // Tests the `T * get()`.
        static_assert(!std::is_const_v<std::remove_pointer_t<decltype(object.get())>>);
        CPPUNIT_ASSERT_EQUAL(10, object.get()->get_a());
        object.get()->set_a(0);
        CPPUNIT_ASSERT_EQUAL(0, object.get()->get_a());
    }

    // Tests that all instances of the `CTest` class are destroyed. No leaks.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_const_access_to_managed_object() {
    {
        // Constructs constant object.
        const libdnf5::ImplPtr<CTest> const_object(new CTest(10));

        // Tests the `const T * operator->() const`
        static_assert(std::is_const_v<std::remove_pointer_t<decltype(const_object.operator->())>>);
        CPPUNIT_ASSERT_EQUAL(10, const_object->get_a());

        // Tests the `const T & operator*() const`.
        static_assert(std::is_const_v<std::remove_reference_t<decltype(const_object.operator*())>>);
        CPPUNIT_ASSERT_EQUAL(10, (*const_object).get_a());

        // Tests the `const T * get() const`.
        static_assert(std::is_const_v<std::remove_pointer_t<decltype(const_object.get())>>);
        CPPUNIT_ASSERT_EQUAL(10, const_object.get()->get_a());
    }

    // Tests that all instances of the `CTest` class are destroyed. No leaks.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_copy_constructor() {
    {
        libdnf5::ImplPtr<CTest> src_object(new CTest(10));
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());

        // Tests the copy constructor. The source object remains unchanged.
        // A new `CTest` instance is created.
        libdnf5::ImplPtr<CTest> new_object(src_object);
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, new_object->get_a());
        CPPUNIT_ASSERT(nullptr != src_object.get());
        CPPUNIT_ASSERT_EQUAL(10, src_object->get_a());

        // Tests the copy constructor. The empty source object.
        // No new `CTest` instance is created.
        libdnf5::ImplPtr<CTest> empty_object;
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
        libdnf5::ImplPtr<CTest> new_empty_object_copy(empty_object);
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
        CPPUNIT_ASSERT(nullptr == empty_object.get());
        CPPUNIT_ASSERT(nullptr == new_empty_object_copy.get());
    }

    // Tests that all instances of the `CTest` class are destroyed. No leaks.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_move_constructor() {
    {
        libdnf5::ImplPtr<CTest> src_object(new CTest(10));
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());

        // Tests the move constructor. Ownership is transferred from the source object to `*this`.
        // `p_impl` in the source object becomes `nullptr`.
        // No new `CTest` instances are created.
        libdnf5::ImplPtr<CTest> new_object(std::move(src_object));
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, new_object->get_a());
        CPPUNIT_ASSERT(nullptr == src_object.get());

        // Tests the move constructor. The empty source object.
        // No new `CTest` instances are created.
        libdnf5::ImplPtr<CTest> empty_object;
        libdnf5::ImplPtr<CTest> new_object2(std::move(empty_object));
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
        CPPUNIT_ASSERT(nullptr == new_object2.get());
        CPPUNIT_ASSERT(nullptr == src_object.get());
    }

    // Tests that all instances of the `CTest` class are destroyed. No leaks.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_copy_assignment() {
    {
        libdnf5::ImplPtr<CTest> src_object(new CTest(10));
        libdnf5::ImplPtr<CTest> dst_object(new CTest(5));
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());

        // Tests the copy assignment operator. The source object remains unchanged.
        // The value of the `CTest` instance in the source is copied to the CTest instance in the destination.
        dst_object = src_object;
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, dst_object->get_a());
        CPPUNIT_ASSERT(nullptr != src_object.get());
        CPPUNIT_ASSERT_EQUAL(10, src_object->get_a());

        // Tests the copy assignment operator - self-assignment.
        // The object must remain unchanged.
        dst_object = dst_object;
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, dst_object->get_a());

        // Tests the copy assignment to empty (moved from) object. The source object remains unchanged.
        // A new `CTest` instance is created.
        libdnf5::ImplPtr<CTest> empty_dst_object;
        CPPUNIT_ASSERT(nullptr == empty_dst_object.get());
        empty_dst_object = dst_object;
        CPPUNIT_ASSERT_EQUAL(3, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, empty_dst_object->get_a());
        CPPUNIT_ASSERT_EQUAL(10, dst_object->get_a());

        // Tests the copy assignment from empty to non empty object.
        // The old `CTest` instance in the destination is destroyed.
        libdnf5::ImplPtr<CTest> empty_src_object;
        CPPUNIT_ASSERT_EQUAL(3, CTest::get_instance_counter());
        CPPUNIT_ASSERT(nullptr == empty_src_object.get());
        CPPUNIT_ASSERT(nullptr != dst_object.get());
        dst_object = empty_src_object;
        CPPUNIT_ASSERT(nullptr == empty_src_object.get());
        CPPUNIT_ASSERT(nullptr == dst_object.get());
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
    }

    // Tests that all instances of the `CTest` class are destroyed. No leaks.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}

void ImplPtrTest::test_move_assignment() {
    {
        libdnf5::ImplPtr<CTest> src_object(new CTest(10));
        libdnf5::ImplPtr<CTest> dst_object(new CTest(5));
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());

        // Tests the move assignment operator.
        // `CTest` instance ownership is transferred from the source object to the destination.
        // The old `CTest` instance in the destination is destroyed.
        // `p_impl` in the original object becomes nullptr.
        dst_object = std::move(src_object);
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, dst_object->get_a());
        CPPUNIT_ASSERT(nullptr == src_object.get());

        // Tests the move assignment operator - self-assignment.
        // The object must remain unchanged.
        dst_object = std::move(dst_object);
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, dst_object->get_a());

        // Tests the move assignment to empty (moved from) object.
        // `CTest` instance ownership is transferred from the source object to the destination.
        // `p_impl` in the original object becomes nullptr.
        libdnf5::ImplPtr<CTest> empty_object;
        CPPUNIT_ASSERT(nullptr == empty_object.get());
        empty_object = std::move(dst_object);
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
        CPPUNIT_ASSERT_EQUAL(10, empty_object->get_a());
        CPPUNIT_ASSERT(nullptr == dst_object.get());

        // Tests the move assignment from empty to non empty object.
        // The old `CTest` instance in the destination is destroyed.
        libdnf5::ImplPtr<CTest> empty_src_object;
        libdnf5::ImplPtr<CTest> non_empty_dst_object(new CTest(5));
        CPPUNIT_ASSERT_EQUAL(2, CTest::get_instance_counter());
        non_empty_dst_object = std::move(empty_src_object);
        CPPUNIT_ASSERT(nullptr == empty_src_object.get());
        CPPUNIT_ASSERT(nullptr == non_empty_dst_object.get());
        CPPUNIT_ASSERT_EQUAL(1, CTest::get_instance_counter());
    }

    // Tests that all instances of the `CTest` class are destroyed. No leaks.
    CPPUNIT_ASSERT_EQUAL(0, CTest::get_instance_counter());
}