2 * Copyright 2016 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>
23 #include <gtest/gtest.h>
24 #include <folly/Memory.h>
27 using namespace folly;
32 function<void()> dtor_;
33 function<void()> move_;
34 Magic(function<void()> ctor, function<void()> dtor, function<void()> move)
35 : dtor_(std::move(dtor)), move_(std::move(move)) {
38 Magic(Magic&& other) /* may throw */ { *this = std::move(other); }
39 Magic& operator=(Magic&& other) {
40 dtor_ = std::move(other.dtor_);
41 move_ = std::move(other.move_);
48 class IndestructibleTest : public testing::Test {};
51 TEST_F(IndestructibleTest, access) {
52 static const Indestructible<map<string, int>> data{
53 map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}}};
56 EXPECT_EQ(19, m.at("key2"));
59 TEST_F(IndestructibleTest, no_destruction) {
63 static Indestructible<Magic> sing(
73 sing.~Indestructible();
77 TEST_F(IndestructibleTest, move) {
82 static Indestructible<Magic> sing( // move assignment
95 static Indestructible<Magic> move_ctor(std::move(sing));
100 static Indestructible<Magic> move_assign = std::move(move_ctor);