]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/weak_ptr_test.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / tests / unit / weak_ptr_test.cc
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19 /*
20 * Copyright 2016 ScyllaDB
21 */
22
23 #define BOOST_TEST_MODULE core
24
25 #include <boost/test/included/unit_test.hpp>
26 #include <seastar/core/weak_ptr.hh>
27
28 using namespace seastar;
29
30 class myclass : public weakly_referencable<myclass> {};
31
32 static_assert(std::is_nothrow_default_constructible_v<myclass>);
33
34 static_assert(std::is_nothrow_default_constructible_v<weak_ptr<myclass>>);
35 static_assert(std::is_nothrow_move_constructible_v<weak_ptr<myclass>>);
36
37 BOOST_AUTO_TEST_CASE(test_weak_ptr_is_empty_when_default_initialized) {
38 weak_ptr<myclass> wp;
39 BOOST_REQUIRE(!bool(wp));
40 }
41
42 BOOST_AUTO_TEST_CASE(test_weak_ptr_is_reset) {
43 auto owning_ptr = std::make_unique<myclass>();
44 weak_ptr<myclass> wp = owning_ptr->weak_from_this();
45 BOOST_REQUIRE(bool(wp));
46 BOOST_REQUIRE(&*wp == &*owning_ptr);
47 owning_ptr = {};
48 BOOST_REQUIRE(!bool(wp));
49 }
50
51 BOOST_AUTO_TEST_CASE(test_weak_ptr_can_be_moved) {
52 auto owning_ptr = std::make_unique<myclass>();
53 weak_ptr<myclass> wp1 = owning_ptr->weak_from_this();
54 weak_ptr<myclass> wp2 = owning_ptr->weak_from_this();
55 weak_ptr<myclass> wp3 = owning_ptr->weak_from_this();
56
57 weak_ptr<myclass> wp3_moved;
58 wp3_moved = std::move(wp3);
59 weak_ptr<myclass> wp1_moved(std::move(wp1));
60 auto wp2_moved = std::move(wp2);
61 BOOST_REQUIRE(!bool(wp1));
62 BOOST_REQUIRE(!bool(wp2));
63 BOOST_REQUIRE(!bool(wp3));
64 BOOST_REQUIRE(bool(wp1_moved));
65 BOOST_REQUIRE(bool(wp2_moved));
66 BOOST_REQUIRE(bool(wp3_moved));
67 BOOST_REQUIRE(wp1_moved.get() == owning_ptr.get());
68 BOOST_REQUIRE(wp2_moved.get() == owning_ptr.get());
69 BOOST_REQUIRE(wp3_moved.get() == owning_ptr.get());
70
71 owning_ptr = {};
72
73 BOOST_REQUIRE(!bool(wp1_moved));
74 BOOST_REQUIRE(!bool(wp2_moved));
75 BOOST_REQUIRE(!bool(wp3_moved));
76 }
77
78 BOOST_AUTO_TEST_CASE(test_weak_ptr_can_be_copied) {
79 auto owning_ptr = std::make_unique<myclass>();
80 weak_ptr<myclass> wp1 = owning_ptr->weak_from_this();
81 weak_ptr<myclass> wp2 = owning_ptr->weak_from_this();
82 weak_ptr<myclass> wp3 = owning_ptr->weak_from_this();
83
84 weak_ptr<myclass> wp1_copied(wp1);
85 auto wp2_copied = wp2;
86 weak_ptr<myclass> wp3_copied;
87 wp3_copied = wp3;
88 BOOST_REQUIRE(bool(wp1));
89 BOOST_REQUIRE(bool(wp2));
90 BOOST_REQUIRE(bool(wp3));
91 BOOST_REQUIRE(bool(wp1_copied));
92 BOOST_REQUIRE(bool(wp2_copied));
93 BOOST_REQUIRE(bool(wp3_copied));
94
95 BOOST_REQUIRE(wp1.get() == wp1_copied.get());
96 BOOST_REQUIRE(wp2.get() == wp2_copied.get());
97 BOOST_REQUIRE(wp3.get() == wp3_copied.get());
98
99 owning_ptr = {};
100
101 BOOST_REQUIRE(!bool(wp1));
102 BOOST_REQUIRE(!bool(wp2));
103 BOOST_REQUIRE(!bool(wp3));
104 BOOST_REQUIRE(!bool(wp1_copied));
105 BOOST_REQUIRE(!bool(wp2_copied));
106 BOOST_REQUIRE(!bool(wp3_copied));
107 }
108
109 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs) {
110 auto owning_ptr = std::make_unique<myclass>();
111
112 weak_ptr<myclass> wp1 = owning_ptr->weak_from_this();
113 BOOST_REQUIRE(bool(wp1));
114 BOOST_REQUIRE(&*wp1 == &*owning_ptr);
115
116 weak_ptr<myclass> wp2 = owning_ptr->weak_from_this();
117 BOOST_REQUIRE(bool(wp2));
118 BOOST_REQUIRE(&*wp2 == &*owning_ptr);
119
120 owning_ptr = {};
121
122 BOOST_REQUIRE(!bool(wp1));
123 BOOST_REQUIRE(!bool(wp2));
124 }
125
126 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs_going_away_first) {
127 auto owning_ptr = std::make_unique<myclass>();
128
129 weak_ptr<myclass> wp1 = owning_ptr->weak_from_this();
130 weak_ptr<myclass> wp2 = owning_ptr->weak_from_this();
131 weak_ptr<myclass> wp3 = owning_ptr->weak_from_this();
132
133 BOOST_REQUIRE(bool(wp1));
134 BOOST_REQUIRE(bool(wp2));
135 BOOST_REQUIRE(bool(wp3));
136
137 wp2 = {};
138
139 owning_ptr = std::make_unique<myclass>();
140
141 BOOST_REQUIRE(!bool(wp1));
142 BOOST_REQUIRE(!bool(wp2));
143 BOOST_REQUIRE(!bool(wp3));
144
145 wp1 = owning_ptr->weak_from_this();
146 wp2 = owning_ptr->weak_from_this();
147 wp3 = owning_ptr->weak_from_this();
148
149 BOOST_REQUIRE(bool(wp1));
150 BOOST_REQUIRE(bool(wp2));
151 BOOST_REQUIRE(bool(wp3));
152
153 wp3 = {};
154 owning_ptr = std::make_unique<myclass>();
155
156 BOOST_REQUIRE(!bool(wp1));
157 BOOST_REQUIRE(!bool(wp2));
158 BOOST_REQUIRE(!bool(wp3));
159
160 wp1 = owning_ptr->weak_from_this();
161 wp2 = owning_ptr->weak_from_this();
162 wp3 = owning_ptr->weak_from_this();
163
164 wp1 = {};
165 wp3 = {};
166 owning_ptr = std::make_unique<myclass>();
167
168 BOOST_REQUIRE(!bool(wp1));
169 BOOST_REQUIRE(!bool(wp2));
170 BOOST_REQUIRE(!bool(wp3));
171 }