Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/UniquePtr.tests.cpp

raw
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_test_macros.hpp>
10 #include <catch2/internal/catch_unique_ptr.hpp>
11
12 #include <tuple>
13
14 namespace {
15 struct unique_ptr_test_helper {
16 bool dummy = false;
17 };
18 } // end unnamed namespace
19
20 TEST_CASE("unique_ptr reimplementation: basic functionality", "[internals][unique-ptr]") {
21 using Catch::Detail::unique_ptr;
22 SECTION("Default constructed unique_ptr is empty") {
23 unique_ptr<int> ptr;
24 REQUIRE_FALSE(ptr);
25 REQUIRE(ptr.get() == nullptr);
26 }
27 SECTION("Take ownership of allocation") {
28 auto naked_ptr = new int{ 0 };
29 unique_ptr<int> ptr(naked_ptr);
30 REQUIRE(ptr);
31 REQUIRE(*ptr == 0);
32 REQUIRE(ptr.get() == naked_ptr);
33 SECTION("Plain reset deallocates") {
34 ptr.reset(); // this makes naked_ptr dangling!
35 REQUIRE_FALSE(ptr);
36 REQUIRE(ptr.get() == nullptr);
37 }
38 SECTION("Reset replaces ownership") {
39 ptr.reset(new int{ 2 });
40 REQUIRE(ptr);
41 REQUIRE(ptr.get() != nullptr);
42 REQUIRE(*ptr == 2);
43 }
44 }
45 SECTION("Release releases ownership") {
46 auto naked_ptr = new int{ 1 };
47 unique_ptr<int> ptr(naked_ptr);
48 ptr.release();
49 CHECK_FALSE(ptr);
50 CHECK(ptr.get() == nullptr);
51 delete naked_ptr;
52 }
53 SECTION("Move constructor") {
54 unique_ptr<int> ptr1(new int{ 1 });
55 auto ptr2(std::move(ptr1));
56 REQUIRE_FALSE(ptr1);
57 REQUIRE(ptr2);
58 REQUIRE(*ptr2 == 1);
59 }
60 SECTION("Move assignment") {
61 unique_ptr<int> ptr1(new int{ 1 }), ptr2(new int{ 2 });
62 ptr1 = std::move(ptr2);
63 REQUIRE_FALSE(ptr2);
64 REQUIRE(ptr1);
65 REQUIRE(*ptr1 == 2);
66 }
67 SECTION("free swap") {
68 unique_ptr<int> ptr1(new int{ 1 }), ptr2(new int{ 2 });
69 swap(ptr1, ptr2);
70 REQUIRE(*ptr1 == 2);
71 REQUIRE(*ptr2 == 1);
72 }
73 }
74
75
76 namespace {
77 struct base {
78 int i;
79 base(int i_) :i(i_) {}
80 };
81 struct derived : base { using base::base; };
82 struct unrelated {};
83
84 } // end unnamed namespace
85
86 static_assert( std::is_constructible<Catch::Detail::unique_ptr<base>,
87 Catch::Detail::unique_ptr<derived>>::value, "Upcasting is supported");
88 static_assert(!std::is_constructible<Catch::Detail::unique_ptr<derived>,
89 Catch::Detail::unique_ptr<base>>::value, "Downcasting is not supported");
90 static_assert(!std::is_constructible<Catch::Detail::unique_ptr<base>,
91 Catch::Detail::unique_ptr<unrelated>>::value, "Cannot just convert one ptr type to another");
92
93 TEST_CASE("Upcasting special member functions", "[internals][unique-ptr]") {
94 using Catch::Detail::unique_ptr;
95
96 unique_ptr<derived> dptr(new derived{3});
97 SECTION("Move constructor") {
98 unique_ptr<base> bptr(std::move(dptr));
99 REQUIRE(bptr->i == 3);
100 }
101 SECTION("move assignment") {
102 unique_ptr<base> bptr(new base{ 1 });
103 bptr = std::move(dptr);
104 REQUIRE(bptr->i == 3);
105 }
106 }
107
108 namespace {
109 struct move_detector {
110 bool has_moved = false;
111 move_detector() = default;
112 move_detector(move_detector const& rhs) = default;
113 move_detector& operator=(move_detector const& rhs) = default;
114
115 move_detector(move_detector&& rhs) noexcept {
116 rhs.has_moved = true;
117 }
118 move_detector& operator=(move_detector&& rhs) noexcept {
119 rhs.has_moved = true;
120 return *this;
121 }
122 };
123 } // end unnamed namespace
124
125 TEST_CASE("make_unique reimplementation", "[internals][unique-ptr]") {
126 using Catch::Detail::make_unique;
127 SECTION("From lvalue copies") {
128 move_detector lval;
129 auto ptr = make_unique<move_detector>(lval);
130 REQUIRE_FALSE(lval.has_moved);
131 }
132 SECTION("From rvalue moves") {
133 move_detector rval;
134 auto ptr = make_unique<move_detector>(std::move(rval));
135 REQUIRE(rval.has_moved);
136 }
137 SECTION("Variadic constructor") {
138 auto ptr = make_unique<std::tuple<int, double, int>>(1, 2., 3);
139 REQUIRE(*ptr == std::tuple<int, double, int>{1, 2., 3});
140 }
141 }