/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <map>
#include <memory>
#include <string>
-#include <gtest/gtest.h>
+
#include <folly/Memory.h>
+#include <folly/portability/GTest.h>
using namespace std;
using namespace folly;
}
TEST_F(IndestructibleTest, access) {
- const Indestructible<map<string, int>> data{
+ static const Indestructible<map<string, int>> data{
map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}}};
auto& m = *data;
int state = 0;
int value = 0;
- auto sing = make_unique<Indestructible<Magic>>(
+ static Indestructible<Magic> sing(
[&] {
++state;
value = 7;
EXPECT_EQ(1, state);
EXPECT_EQ(7, value);
- sing = nullptr;
+ sing.~Indestructible();
EXPECT_EQ(1, state);
}
+TEST_F(IndestructibleTest, empty) {
+ static const Indestructible<map<string, int>> data;
+ auto& m = *data;
+ EXPECT_EQ(0, m.size());
+}
+
TEST_F(IndestructibleTest, move) {
int state = 0;
int value = 0;
int moves = 0;
- Indestructible<Magic> sing( // move assignment
+ static Indestructible<Magic> sing( // move assignment
[&] {
++state;
value = 7;
EXPECT_EQ(7, value);
EXPECT_EQ(0, moves);
- Indestructible<Magic> move_ctor(std::move(sing)); // move constructor
+ // move constructor
+ static Indestructible<Magic> move_ctor(std::move(sing));
EXPECT_EQ(1, state);
EXPECT_EQ(1, moves);
- Indestructible<Magic> move_assign = std::move(move_ctor); // move assignment
+ // move assignment
+ static Indestructible<Magic> move_assign = std::move(move_ctor);
EXPECT_EQ(1, state);
EXPECT_EQ(2, moves);
}
+
+TEST_F(IndestructibleTest, disabled_default_ctor) {
+ EXPECT_TRUE((std::is_constructible<Indestructible<int>>::value)) << "sanity";
+
+ struct Foo {
+ Foo(int) {}
+ };
+ EXPECT_FALSE((std::is_constructible<Indestructible<Foo>>::value));
+ EXPECT_FALSE((std::is_constructible<Indestructible<Foo>, Magic>::value));
+ EXPECT_TRUE((std::is_constructible<Indestructible<Foo>, int>::value));
+}