]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/weak_ptr_test.cc
update sources to ceph Nautilus 14.2.1
[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 BOOST_AUTO_TEST_CASE(test_weak_ptr_is_empty_when_default_initialized) {
33 weak_ptr<myclass> wp;
34 BOOST_REQUIRE(!bool(wp));
35 }
36
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);
42 owning_ptr = {};
43 BOOST_REQUIRE(!bool(wp));
44 }
45
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();
51
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));
61
62 owning_ptr = {};
63
64 BOOST_REQUIRE(!bool(wp1_moved));
65 BOOST_REQUIRE(!bool(wp2_moved));
66 BOOST_REQUIRE(!bool(wp3_moved));
67 }
68
69 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs) {
70 auto owning_ptr = std::make_unique<myclass>();
71
72 weak_ptr<myclass> wp1 = owning_ptr->weak_from_this();
73 BOOST_REQUIRE(bool(wp1));
74 BOOST_REQUIRE(&*wp1 == &*owning_ptr);
75
76 weak_ptr<myclass> wp2 = owning_ptr->weak_from_this();
77 BOOST_REQUIRE(bool(wp2));
78 BOOST_REQUIRE(&*wp2 == &*owning_ptr);
79
80 owning_ptr = {};
81
82 BOOST_REQUIRE(!bool(wp1));
83 BOOST_REQUIRE(!bool(wp2));
84 }
85
86 BOOST_AUTO_TEST_CASE(test_multipe_weak_ptrs_going_away_first) {
87 auto owning_ptr = std::make_unique<myclass>();
88
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();
92
93 BOOST_REQUIRE(bool(wp1));
94 BOOST_REQUIRE(bool(wp2));
95 BOOST_REQUIRE(bool(wp3));
96
97 wp2 = {};
98
99 owning_ptr = std::make_unique<myclass>();
100
101 BOOST_REQUIRE(!bool(wp1));
102 BOOST_REQUIRE(!bool(wp2));
103 BOOST_REQUIRE(!bool(wp3));
104
105 wp1 = owning_ptr->weak_from_this();
106 wp2 = owning_ptr->weak_from_this();
107 wp3 = owning_ptr->weak_from_this();
108
109 BOOST_REQUIRE(bool(wp1));
110 BOOST_REQUIRE(bool(wp2));
111 BOOST_REQUIRE(bool(wp3));
112
113 wp3 = {};
114 owning_ptr = std::make_unique<myclass>();
115
116 BOOST_REQUIRE(!bool(wp1));
117 BOOST_REQUIRE(!bool(wp2));
118 BOOST_REQUIRE(!bool(wp3));
119
120 wp1 = owning_ptr->weak_from_this();
121 wp2 = owning_ptr->weak_from_this();
122 wp3 = owning_ptr->weak_from_this();
123
124 wp1 = {};
125 wp3 = {};
126 owning_ptr = std::make_unique<myclass>();
127
128 BOOST_REQUIRE(!bool(wp1));
129 BOOST_REQUIRE(!bool(wp2));
130 BOOST_REQUIRE(!bool(wp3));
131 }