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 BOOST_AUTO_TEST_CASE(test_weak_ptr_is_empty_when_default_initialized
) {
34 BOOST_REQUIRE(!bool(wp
));
37 BOOST_AUTO_TEST_CASE(test_weak_ptr_is_reset
) {
38 auto owning_ptr
= std::make_unique
<myclass
>();
39 weak_ptr
<myclass
> wp
= owning_ptr
->weak_from_this();
40 BOOST_REQUIRE(bool(wp
));
41 BOOST_REQUIRE(&*wp
== &*owning_ptr
);
43 BOOST_REQUIRE(!bool(wp
));
46 BOOST_AUTO_TEST_CASE(test_weak_ptr_can_be_moved
) {
47 auto owning_ptr
= std::make_unique
<myclass
>();
48 weak_ptr
<myclass
> wp1
= owning_ptr
->weak_from_this();
49 weak_ptr
<myclass
> wp2
= owning_ptr
->weak_from_this();
50 weak_ptr
<myclass
> wp3
= owning_ptr
->weak_from_this();
52 auto wp3_moved
= std::move(wp3
);
53 auto wp1_moved
= std::move(wp1
);
54 auto wp2_moved
= std::move(wp2
);
55 BOOST_REQUIRE(!bool(wp1
));
56 BOOST_REQUIRE(!bool(wp2
));
57 BOOST_REQUIRE(!bool(wp3
));
58 BOOST_REQUIRE(bool(wp1_moved
));
59 BOOST_REQUIRE(bool(wp2_moved
));
60 BOOST_REQUIRE(bool(wp3_moved
));
64 BOOST_REQUIRE(!bool(wp1_moved
));
65 BOOST_REQUIRE(!bool(wp2_moved
));
66 BOOST_REQUIRE(!bool(wp3_moved
));
69 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs
) {
70 auto owning_ptr
= std::make_unique
<myclass
>();
72 weak_ptr
<myclass
> wp1
= owning_ptr
->weak_from_this();
73 BOOST_REQUIRE(bool(wp1
));
74 BOOST_REQUIRE(&*wp1
== &*owning_ptr
);
76 weak_ptr
<myclass
> wp2
= owning_ptr
->weak_from_this();
77 BOOST_REQUIRE(bool(wp2
));
78 BOOST_REQUIRE(&*wp2
== &*owning_ptr
);
82 BOOST_REQUIRE(!bool(wp1
));
83 BOOST_REQUIRE(!bool(wp2
));
86 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs_going_away_first
) {
87 auto owning_ptr
= std::make_unique
<myclass
>();
89 weak_ptr
<myclass
> wp1
= owning_ptr
->weak_from_this();
90 weak_ptr
<myclass
> wp2
= owning_ptr
->weak_from_this();
91 weak_ptr
<myclass
> wp3
= owning_ptr
->weak_from_this();
93 BOOST_REQUIRE(bool(wp1
));
94 BOOST_REQUIRE(bool(wp2
));
95 BOOST_REQUIRE(bool(wp3
));
99 owning_ptr
= std::make_unique
<myclass
>();
101 BOOST_REQUIRE(!bool(wp1
));
102 BOOST_REQUIRE(!bool(wp2
));
103 BOOST_REQUIRE(!bool(wp3
));
105 wp1
= owning_ptr
->weak_from_this();
106 wp2
= owning_ptr
->weak_from_this();
107 wp3
= owning_ptr
->weak_from_this();
109 BOOST_REQUIRE(bool(wp1
));
110 BOOST_REQUIRE(bool(wp2
));
111 BOOST_REQUIRE(bool(wp3
));
114 owning_ptr
= std::make_unique
<myclass
>();
116 BOOST_REQUIRE(!bool(wp1
));
117 BOOST_REQUIRE(!bool(wp2
));
118 BOOST_REQUIRE(!bool(wp3
));
120 wp1
= owning_ptr
->weak_from_this();
121 wp2
= owning_ptr
->weak_from_this();
122 wp3
= owning_ptr
->weak_from_this();
126 owning_ptr
= std::make_unique
<myclass
>();
128 BOOST_REQUIRE(!bool(wp1
));
129 BOOST_REQUIRE(!bool(wp2
));
130 BOOST_REQUIRE(!bool(wp3
));