]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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/checked_ptr.hh> | |
27 | #include <seastar/core/weak_ptr.hh> | |
28 | ||
29 | using namespace seastar; | |
30 | ||
f67539c2 TL |
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*>>); | |
34 | ||
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>>>); | |
37 | ||
38 | template <typename T> | |
39 | class may_throw_on_null_ptr : public seastar::weak_ptr<T> { | |
40 | public: | |
41 | may_throw_on_null_ptr(std::nullptr_t) {} | |
42 | }; | |
43 | ||
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>>>); | |
20effc67 | 47 | static_assert(std::is_nothrow_copy_constructible_v<checked_ptr<may_throw_on_null_ptr<int>>>); |
f67539c2 | 48 | |
11fdf7f2 TL |
49 | struct my_st : public weakly_referencable<my_st> { |
50 | my_st(int a_) : a(a_) {} | |
51 | int a; | |
52 | }; | |
53 | ||
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); | |
59 | } | |
60 | ||
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); | |
66 | } | |
67 | ||
68 | BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_default_initialized) { | |
69 | seastar::checked_ptr<int*> cp; | |
70 | BOOST_REQUIRE(!bool(cp)); | |
71 | } | |
72 | ||
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)); | |
76 | } | |
77 | ||
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)); | |
81 | } | |
82 | ||
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)); | |
86 | my_st i(3); | |
87 | my_st k(3); | |
88 | cp = &i; | |
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); | |
97 | ||
98 | const_ref_check_naked(cp); | |
99 | ||
100 | cp = nullptr; | |
101 | BOOST_REQUIRE(!bool(cp)); | |
102 | } | |
103 | ||
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); | |
117 | ||
118 | const_ref_check_smart(cp); | |
119 | ||
120 | i = nullptr; | |
121 | BOOST_REQUIRE(!bool(cp)); | |
122 | BOOST_REQUIRE(!bool(cp1)); | |
123 | BOOST_REQUIRE(!bool(cp2)); | |
124 | } | |
125 |