]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_static_ptr.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2017 Red Hat, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "common/static_ptr.h"
16 #include <gtest/gtest.h>
18 using ceph::static_ptr
;
19 using ceph::make_static
;
23 virtual int func() = 0;
24 virtual ~base() = default;
27 class sibling1
: public base
{
29 int func() override
{ return 0; }
32 class sibling2
: public base
{
34 int func() override
{ return 9; }
35 virtual int call(int) = 0;
38 class grandchild
: public sibling2
{
42 explicit grandchild(int val
) : val(val
) {}
43 virtual int call(int n
) override
{ return n
* val
; }
46 class great_grandchild
: public grandchild
{
48 explicit great_grandchild(int val
) : grandchild(val
) {}
49 int call(int n
) override
{ return n
+ val
; }
52 TEST(StaticPtr
, EmptyCreation
) {
53 static_ptr
<base
, sizeof(grandchild
)> p
;
55 EXPECT_EQ(p
, nullptr);
56 EXPECT_EQ(nullptr, p
);
57 EXPECT_TRUE(p
.get() == nullptr);
60 TEST(StaticPtr
, CreationCall
) {
62 static_ptr
<base
, sizeof(grandchild
)> p(std::in_place_type_t
<sibling1
>{});
64 EXPECT_FALSE(p
== nullptr);
65 EXPECT_FALSE(nullptr == p
);
66 EXPECT_FALSE(p
.get() == nullptr);
67 EXPECT_EQ(p
->func(), 0);
68 EXPECT_EQ((*p
).func(), 0);
69 EXPECT_EQ((p
.get())->func(), 0);
72 auto p
= make_static
<base
, sibling1
>();
74 EXPECT_FALSE(p
== nullptr);
75 EXPECT_FALSE(nullptr == p
);
76 EXPECT_FALSE(p
.get() == nullptr);
77 EXPECT_EQ(p
->func(), 0);
78 EXPECT_EQ((*p
).func(), 0);
79 EXPECT_EQ((p
.get())->func(), 0);
83 TEST(StaticPtr
, CreateReset
) {
85 static_ptr
<base
, sizeof(grandchild
)> p(std::in_place_type_t
<sibling1
>{});
86 EXPECT_EQ((p
.get())->func(), 0);
89 EXPECT_EQ(p
, nullptr);
90 EXPECT_EQ(nullptr, p
);
91 EXPECT_TRUE(p
.get() == nullptr);
94 static_ptr
<base
, sizeof(grandchild
)> p(std::in_place_type_t
<sibling1
>{});
95 EXPECT_EQ((p
.get())->func(), 0);
98 EXPECT_EQ(p
, nullptr);
99 EXPECT_EQ(nullptr, p
);
100 EXPECT_TRUE(p
.get() == nullptr);
104 TEST(StaticPtr
, CreateEmplace
) {
105 static_ptr
<base
, sizeof(grandchild
)> p(std::in_place_type_t
<sibling1
>{});
106 EXPECT_EQ((p
.get())->func(), 0);
107 p
.emplace
<grandchild
>(30);
108 EXPECT_EQ(p
->func(), 9);
111 TEST(StaticPtr
, Move
) {
112 // Won't compile. Good.
113 // static_ptr<base, sizeof(base)> p1(std::in_place_type_t<grandchild>{}, 3);
115 static_ptr
<base
, sizeof(base
)> p1(std::in_place_type_t
<sibling1
>{});
116 static_ptr
<base
, sizeof(grandchild
)> p2(std::in_place_type_t
<grandchild
>{},
120 EXPECT_EQ(p1
->func(), 0);
123 TEST(StaticPtr
, ImplicitUpcast
) {
124 static_ptr
<base
, sizeof(grandchild
)> p1
;
125 static_ptr
<sibling2
, sizeof(grandchild
)> p2(std::in_place_type_t
<grandchild
>{}, 3);
128 EXPECT_EQ(p1
->func(), 9);
132 // Doesn't compile. Good.
136 TEST(StaticPtr
, StaticCast
) {
137 static_ptr
<base
, sizeof(grandchild
)> p1(std::in_place_type_t
<grandchild
>{}, 3);
138 static_ptr
<sibling2
, sizeof(grandchild
)> p2
;
140 p2
= ceph::static_pointer_cast
<sibling2
, sizeof(grandchild
)>(std::move(p1
));
141 EXPECT_EQ(p2
->func(), 9);
142 EXPECT_EQ(p2
->call(10), 30);
145 TEST(StaticPtr
, DynamicCast
) {
146 static constexpr auto sz
= sizeof(great_grandchild
);
148 static_ptr
<base
, sz
> p1(std::in_place_type_t
<grandchild
>{}, 3);
149 auto p2
= ceph::dynamic_pointer_cast
<great_grandchild
, sz
>(std::move(p1
));
154 static_ptr
<base
, sz
> p1(std::in_place_type_t
<grandchild
>{}, 3);
155 auto p2
= ceph::dynamic_pointer_cast
<grandchild
, sz
>(std::move(p1
));
157 EXPECT_EQ(p2
->func(), 9);
158 EXPECT_EQ(p2
->call(10), 30);
172 TEST(StaticPtr
, ConstCast
) {
173 static constexpr auto sz
= sizeof(constable
);
175 auto p1
= make_static
<const constable
>();
176 EXPECT_EQ(p1
->foo(), 5);
177 auto p2
= ceph::const_pointer_cast
<constable
, sz
>(std::move(p1
));
178 static_assert(!std::is_const
<decltype(p2
)::element_type
>{},
179 "Things are more const than they ought to be.");
181 EXPECT_EQ(p2
->foo(), 2);
185 TEST(StaticPtr
, ReinterpretCast
) {
186 static constexpr auto sz
= sizeof(grandchild
);
188 auto p1
= make_static
<grandchild
>(3);
189 auto p2
= ceph::reinterpret_pointer_cast
<constable
, sz
>(std::move(p1
));
190 static_assert(std::is_same
<decltype(p2
)::element_type
, constable
>{},
191 "Reinterpret is screwy.");
192 auto p3
= ceph::reinterpret_pointer_cast
<grandchild
, sz
>(std::move(p2
));
193 static_assert(std::is_same
<decltype(p3
)::element_type
, grandchild
>{},
194 "Reinterpret is screwy.");
195 EXPECT_EQ(p3
->func(), 9);
196 EXPECT_EQ(p3
->call(10), 30);
201 exceptional() = default;
202 exceptional(const exceptional
& e
) {
203 throw std::exception();
205 exceptional(exceptional
&& e
) {
206 throw std::exception();
210 TEST(StaticPtr
, Exceptional
) {
211 static_ptr
<exceptional
> p1(std::in_place_type_t
<exceptional
>{});
212 EXPECT_ANY_THROW(static_ptr
<exceptional
> p2(std::move(p1
)));