Fix data race in Futex<EmulatedFutexAtomic>
[folly.git] / folly / test / IndestructibleTest.cpp
index 6733138d87bc7dfc4551c189ad10800700043531..b5ca965690a4a22145b25e1af7a7b4cc2aa1c597 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -20,8 +20,9 @@
 #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;
@@ -49,7 +50,7 @@ class IndestructibleTest : public testing::Test {};
 }
 
 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;
@@ -60,7 +61,7 @@ TEST_F(IndestructibleTest, no_destruction) {
   int state = 0;
   int value = 0;
 
-  auto sing = make_unique<Indestructible<Magic>>(
+  static Indestructible<Magic> sing(
       [&] {
         ++state;
         value = 7;
@@ -70,16 +71,22 @@ TEST_F(IndestructibleTest, no_destruction) {
   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;
@@ -91,11 +98,24 @@ TEST_F(IndestructibleTest, move) {
   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));
+}