Split get_default() into two for deferred default construction and added forwarding...
[folly.git] / folly / test / MapUtilTest.cpp
index 693f7235260c83bdac27e893d424198db93cea6a..7ef13f718fcf757e50fc2e7a202bcbb118cabc01 100644 (file)
@@ -239,7 +239,6 @@ struct GetRefDefaultPathCompiles<
         std::declval<int>(),
         std::declval<int>(),
         std::declval<T>()))>> : std::true_type {};
-
 } // namespace
 
 TEST(MapUtil, get_ref_default_path_temporary) {
@@ -253,36 +252,47 @@ namespace {
 
 class TestConstruction {
  public:
-  static std::size_t numberDefaultConstructs;
   TestConstruction() {
-    ++numberDefaultConstructs;
+    EXPECT_TRUE(false);
+  }
+  TestConstruction(TestConstruction&&) {
+    EXPECT_TRUE(false);
+  }
+  TestConstruction(const TestConstruction&) {
+    EXPECT_TRUE(false);
   }
-  TestConstruction(TestConstruction&&) = default;
-  TestConstruction(const TestConstruction&) = default;
+
+  explicit TestConstruction(std::string&& string)
+      : string_{std::move(string)} {}
+  explicit TestConstruction(int&& integer) : integer_{integer} {}
 
   TestConstruction& operator=(const TestConstruction&) = delete;
   TestConstruction& operator=(TestConstruction&&) = delete;
-};
 
-std::size_t TestConstruction::numberDefaultConstructs = 0;
+  int integer_{};
+  std::string string_{};
+};
 
 } // namespace
 
 TEST(MapUtil, test_get_default_deferred_construction) {
   auto map = std::unordered_map<int, TestConstruction>{};
-  map.insert({1, TestConstruction{}});
+  map.emplace(
+      std::piecewise_construct,
+      std::forward_as_tuple(1),
+      std::forward_as_tuple(1));
 
-  EXPECT_EQ(TestConstruction::numberDefaultConstructs, 1);
+  EXPECT_EQ(map.at(1).integer_, 1);
 
   {
-    auto val = get_default(map, 1);
-    EXPECT_EQ(TestConstruction::numberDefaultConstructs, 1);
-    static_cast<void>(val);
+    auto val = get_default(map, 0, 1);
+    EXPECT_EQ(val.integer_, 1);
+    EXPECT_EQ(val.string_, "");
   }
 
   {
-    auto val = get_default(map, 1, TestConstruction{});
-    EXPECT_EQ(TestConstruction::numberDefaultConstructs, 2);
-    static_cast<void>(val);
+    auto val = get_default(map, 0, "something");
+    EXPECT_EQ(val.integer_, 0);
+    EXPECT_EQ(val.string_, "something");
   }
 }