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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
20 * Copyright 2016 ScyllaDB
23 #define BOOST_TEST_MODULE core
25 #include <boost/test/included/unit_test.hpp>
26 #include <seastar/core/weak_ptr.hh>
28 using namespace seastar
;
30 class myclass
: public weakly_referencable
<myclass
> {};
32 static_assert(std::is_nothrow_default_constructible_v
<myclass
>);
34 static_assert(std::is_nothrow_default_constructible_v
<weak_ptr
<myclass
>>);
35 static_assert(std::is_nothrow_move_constructible_v
<weak_ptr
<myclass
>>);
37 BOOST_AUTO_TEST_CASE(test_weak_ptr_is_empty_when_default_initialized
) {
39 BOOST_REQUIRE(!bool(wp
));
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
);
48 BOOST_REQUIRE(!bool(wp
));
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();
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());
73 BOOST_REQUIRE(!bool(wp1_moved
));
74 BOOST_REQUIRE(!bool(wp2_moved
));
75 BOOST_REQUIRE(!bool(wp3_moved
));
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();
84 weak_ptr
<myclass
> wp1_copied(wp1
);
85 auto wp2_copied
= wp2
;
86 weak_ptr
<myclass
> wp3_copied
;
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
));
95 BOOST_REQUIRE(wp1
.get() == wp1_copied
.get());
96 BOOST_REQUIRE(wp2
.get() == wp2_copied
.get());
97 BOOST_REQUIRE(wp3
.get() == wp3_copied
.get());
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
));
109 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs
) {
110 auto owning_ptr
= std::make_unique
<myclass
>();
112 weak_ptr
<myclass
> wp1
= owning_ptr
->weak_from_this();
113 BOOST_REQUIRE(bool(wp1
));
114 BOOST_REQUIRE(&*wp1
== &*owning_ptr
);
116 weak_ptr
<myclass
> wp2
= owning_ptr
->weak_from_this();
117 BOOST_REQUIRE(bool(wp2
));
118 BOOST_REQUIRE(&*wp2
== &*owning_ptr
);
122 BOOST_REQUIRE(!bool(wp1
));
123 BOOST_REQUIRE(!bool(wp2
));
126 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs_going_away_first
) {
127 auto owning_ptr
= std::make_unique
<myclass
>();
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();
133 BOOST_REQUIRE(bool(wp1
));
134 BOOST_REQUIRE(bool(wp2
));
135 BOOST_REQUIRE(bool(wp3
));
139 owning_ptr
= std::make_unique
<myclass
>();
141 BOOST_REQUIRE(!bool(wp1
));
142 BOOST_REQUIRE(!bool(wp2
));
143 BOOST_REQUIRE(!bool(wp3
));
145 wp1
= owning_ptr
->weak_from_this();
146 wp2
= owning_ptr
->weak_from_this();
147 wp3
= owning_ptr
->weak_from_this();
149 BOOST_REQUIRE(bool(wp1
));
150 BOOST_REQUIRE(bool(wp2
));
151 BOOST_REQUIRE(bool(wp3
));
154 owning_ptr
= std::make_unique
<myclass
>();
156 BOOST_REQUIRE(!bool(wp1
));
157 BOOST_REQUIRE(!bool(wp2
));
158 BOOST_REQUIRE(!bool(wp3
));
160 wp1
= owning_ptr
->weak_from_this();
161 wp2
= owning_ptr
->weak_from_this();
162 wp3
= owning_ptr
->weak_from_this();
166 owning_ptr
= std::make_unique
<myclass
>();
168 BOOST_REQUIRE(!bool(wp1
));
169 BOOST_REQUIRE(!bool(wp2
));
170 BOOST_REQUIRE(!bool(wp3
));