]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/tests/unit/checked_ptr_test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / tests / unit / checked_ptr_test.cc
CommitLineData
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
29using namespace seastar;
30
31struct my_st : public weakly_referencable<my_st> {
32 my_st(int a_) : a(a_) {}
33 int a;
34};
35
36void const_ref_check_naked(const seastar::checked_ptr<my_st*>& cp) {
37 BOOST_REQUIRE(bool(cp));
38 BOOST_REQUIRE((*cp).a == 3);
39 BOOST_REQUIRE(cp->a == 3);
40 BOOST_REQUIRE(cp.get()->a == 3);
41}
42
43void const_ref_check_smart(const seastar::checked_ptr<::weak_ptr<my_st>>& cp) {
44 BOOST_REQUIRE(bool(cp));
45 BOOST_REQUIRE((*cp).a == 3);
46 BOOST_REQUIRE(cp->a == 3);
47 BOOST_REQUIRE(cp.get()->a == 3);
48}
49
50BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_default_initialized) {
51 seastar::checked_ptr<int*> cp;
52 BOOST_REQUIRE(!bool(cp));
53}
54
55BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_nullptr_initialized_nakes_ptr) {
56 seastar::checked_ptr<int*> cp = nullptr;
57 BOOST_REQUIRE(!bool(cp));
58}
59
60BOOST_AUTO_TEST_CASE(test_checked_ptr_is_empty_when_nullptr_initialized_smart_ptr) {
61 seastar::checked_ptr<::weak_ptr<my_st>> cp = nullptr;
62 BOOST_REQUIRE(!bool(cp));
63}
64
65BOOST_AUTO_TEST_CASE(test_checked_ptr_is_initialized_after_assignment_naked_ptr) {
66 seastar::checked_ptr<my_st*> cp = nullptr;
67 BOOST_REQUIRE(!bool(cp));
68 my_st i(3);
69 my_st k(3);
70 cp = &i;
71 seastar::checked_ptr<my_st*> cp1(&i);
72 seastar::checked_ptr<my_st*> cp2(&k);
73 BOOST_REQUIRE(bool(cp));
74 BOOST_REQUIRE(cp == cp1);
75 BOOST_REQUIRE(cp != cp2);
76 BOOST_REQUIRE((*cp).a == 3);
77 BOOST_REQUIRE(cp->a == 3);
78 BOOST_REQUIRE(cp.get()->a == 3);
79
80 const_ref_check_naked(cp);
81
82 cp = nullptr;
83 BOOST_REQUIRE(!bool(cp));
84}
85
86BOOST_AUTO_TEST_CASE(test_checked_ptr_is_initialized_after_assignment_smart_ptr) {
87 seastar::checked_ptr<::weak_ptr<my_st>> cp = nullptr;
88 BOOST_REQUIRE(!bool(cp));
89 std::unique_ptr<my_st> i = std::make_unique<my_st>(3);
90 cp = i->weak_from_this();
91 seastar::checked_ptr<::weak_ptr<my_st>> cp1(i->weak_from_this());
92 seastar::checked_ptr<::weak_ptr<my_st>> cp2;
93 BOOST_REQUIRE(bool(cp));
94 BOOST_REQUIRE(cp == cp1);
95 BOOST_REQUIRE(cp != cp2);
96 BOOST_REQUIRE((*cp).a == 3);
97 BOOST_REQUIRE(cp->a == 3);
98 BOOST_REQUIRE(cp.get()->a == 3);
99
100 const_ref_check_smart(cp);
101
102 i = nullptr;
103 BOOST_REQUIRE(!bool(cp));
104 BOOST_REQUIRE(!bool(cp1));
105 BOOST_REQUIRE(!bool(cp2));
106}
107