2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Indestructible.h>
24 #include <folly/Memory.h>
25 #include <folly/portability/GTest.h>
28 using namespace folly;
33 function<void()> dtor_;
34 function<void()> move_;
35 Magic(function<void()> ctor, function<void()> dtor, function<void()> move)
36 : dtor_(std::move(dtor)), move_(std::move(move)) {
39 Magic(Magic&& other) /* may throw */ { *this = std::move(other); }
40 Magic& operator=(Magic&& other) {
41 dtor_ = std::move(other.dtor_);
42 move_ = std::move(other.move_);
49 class IndestructibleTest : public testing::Test {};
52 TEST_F(IndestructibleTest, access) {
53 static const Indestructible<map<string, int>> data{
54 map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}}};
57 EXPECT_EQ(19, m.at("key2"));
60 TEST_F(IndestructibleTest, no_destruction) {
64 static Indestructible<Magic> sing(
74 sing.~Indestructible();
78 TEST_F(IndestructibleTest, empty) {
79 static const Indestructible<map<string, int>> data;
81 EXPECT_EQ(0, m.size());
84 TEST_F(IndestructibleTest, move) {
89 static Indestructible<Magic> sing( // move assignment
102 static Indestructible<Magic> move_ctor(std::move(sing));
107 static Indestructible<Magic> move_assign = std::move(move_ctor);
112 TEST_F(IndestructibleTest, disabled_default_ctor) {
113 EXPECT_TRUE((std::is_constructible<Indestructible<int>>::value)) << "sanity";
118 EXPECT_FALSE((std::is_constructible<Indestructible<Foo>>::value));
119 EXPECT_FALSE((std::is_constructible<Indestructible<Foo>, Magic>::value));
120 EXPECT_TRUE((std::is_constructible<Indestructible<Foo>, int>::value));