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/checked_ptr.hh>
27 #include <seastar/core/weak_ptr.hh>
29 using namespace seastar
;
31 static_assert(std::is_nothrow_default_constructible_v
<checked_ptr
<int*>>);
32 static_assert(std::is_nothrow_move_constructible_v
<checked_ptr
<int*>>);
33 static_assert(std::is_nothrow_copy_constructible_v
<checked_ptr
<int*>>);
35 static_assert(std::is_nothrow_default_constructible_v
<checked_ptr
<weak_ptr
<int>>>);
36 static_assert(std::is_nothrow_move_constructible_v
<checked_ptr
<weak_ptr
<int>>>);
39 class may_throw_on_null_ptr
: public seastar::weak_ptr
<T
> {
41 may_throw_on_null_ptr(std::nullptr_t
) {}
44 static_assert(!std::is_nothrow_default_constructible_v
<may_throw_on_null_ptr
<int>>);
45 static_assert(!std::is_nothrow_default_constructible_v
<checked_ptr
<may_throw_on_null_ptr
<int>>>);
46 static_assert(std::is_nothrow_move_constructible_v
<checked_ptr
<may_throw_on_null_ptr
<int>>>);
47 static_assert(std::is_nothrow_copy_constructible_v
<checked_ptr
<may_throw_on_null_ptr
<int>>>);
49 struct my_st
: public weakly_referencable
<my_st
> {
50 my_st(int a_
) : a(a_
) {}
54 void const_ref_check_naked(const seastar::checked_ptr
<my_st
*>& cp
) {
55 BOOST_REQUIRE(bool(cp
));
56 BOOST_REQUIRE((*cp
).a
== 3);
57 BOOST_REQUIRE(cp
->a
== 3);
58 BOOST_REQUIRE(cp
.get()->a
== 3);
61 void const_ref_check_smart(const seastar::checked_ptr
<::weak_ptr
<my_st
>>& cp
) {
62 BOOST_REQUIRE(bool(cp
));
63 BOOST_REQUIRE((*cp
).a
== 3);
64 BOOST_REQUIRE(cp
->a
== 3);
65 BOOST_REQUIRE(cp
.get()->a
== 3);
68 BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_default_initialized
) {
69 seastar::checked_ptr
<int*> cp
;
70 BOOST_REQUIRE(!bool(cp
));
73 BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_nullptr_initialized_nakes_ptr
) {
74 seastar::checked_ptr
<int*> cp
= nullptr;
75 BOOST_REQUIRE(!bool(cp
));
78 BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_nullptr_initialized_smart_ptr
) {
79 seastar::checked_ptr
<::weak_ptr
<my_st
>> cp
= nullptr;
80 BOOST_REQUIRE(!bool(cp
));
83 BOOST_AUTO_TEST_CASE(test_checked_ptr_is_initialized_after_assignment_naked_ptr
) {
84 seastar::checked_ptr
<my_st
*> cp
= nullptr;
85 BOOST_REQUIRE(!bool(cp
));
89 seastar::checked_ptr
<my_st
*> cp1(&i
);
90 seastar::checked_ptr
<my_st
*> cp2(&k
);
91 BOOST_REQUIRE(bool(cp
));
92 BOOST_REQUIRE(cp
== cp1
);
93 BOOST_REQUIRE(cp
!= cp2
);
94 BOOST_REQUIRE((*cp
).a
== 3);
95 BOOST_REQUIRE(cp
->a
== 3);
96 BOOST_REQUIRE(cp
.get()->a
== 3);
98 const_ref_check_naked(cp
);
101 BOOST_REQUIRE(!bool(cp
));
104 BOOST_AUTO_TEST_CASE(test_checked_ptr_is_initialized_after_assignment_smart_ptr
) {
105 seastar::checked_ptr
<::weak_ptr
<my_st
>> cp
= nullptr;
106 BOOST_REQUIRE(!bool(cp
));
107 std::unique_ptr
<my_st
> i
= std::make_unique
<my_st
>(3);
108 cp
= i
->weak_from_this();
109 seastar::checked_ptr
<::weak_ptr
<my_st
>> cp1(i
->weak_from_this());
110 seastar::checked_ptr
<::weak_ptr
<my_st
>> cp2
;
111 BOOST_REQUIRE(bool(cp
));
112 BOOST_REQUIRE(cp
== cp1
);
113 BOOST_REQUIRE(cp
!= cp2
);
114 BOOST_REQUIRE((*cp
).a
== 3);
115 BOOST_REQUIRE(cp
->a
== 3);
116 BOOST_REQUIRE(cp
.get()->a
== 3);
118 const_ref_check_smart(cp
);
121 BOOST_REQUIRE(!bool(cp
));
122 BOOST_REQUIRE(!bool(cp1
));
123 BOOST_REQUIRE(!bool(cp2
));