IndestructibleTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #include <folly/Indestructible.h>
  17. #include <functional>
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <tuple>
  22. #include <folly/Memory.h>
  23. #include <folly/portability/GTest.h>
  24. using namespace std;
  25. using namespace folly;
  26. namespace {
  27. struct Magic {
  28. function<void()> dtor_;
  29. function<void()> move_;
  30. Magic(function<void()> ctor, function<void()> dtor, function<void()> move)
  31. : dtor_(std::move(dtor)), move_(std::move(move)) {
  32. ctor();
  33. }
  34. Magic(Magic&& other) /* may throw */ {
  35. *this = std::move(other);
  36. }
  37. Magic& operator=(Magic&& other) {
  38. dtor_ = std::move(other.dtor_);
  39. move_ = std::move(other.move_);
  40. move_();
  41. return *this;
  42. }
  43. ~Magic() {
  44. dtor_();
  45. }
  46. };
  47. class IndestructibleTest : public testing::Test {};
  48. } // namespace
  49. TEST_F(IndestructibleTest, access) {
  50. static const Indestructible<map<string, int>> data{
  51. map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}}};
  52. auto& m = *data;
  53. EXPECT_EQ(19, m.at("key2"));
  54. }
  55. TEST_F(IndestructibleTest, no_destruction) {
  56. int state = 0;
  57. int value = 0;
  58. static Indestructible<Magic> sing(
  59. [&] {
  60. ++state;
  61. value = 7;
  62. },
  63. [&] { state = -1; },
  64. [] {});
  65. EXPECT_EQ(1, state);
  66. EXPECT_EQ(7, value);
  67. sing.~Indestructible();
  68. EXPECT_EQ(1, state);
  69. }
  70. TEST_F(IndestructibleTest, empty) {
  71. static const Indestructible<map<string, int>> data;
  72. auto& m = *data;
  73. EXPECT_EQ(0, m.size());
  74. }
  75. TEST_F(IndestructibleTest, move) {
  76. int state = 0;
  77. int value = 0;
  78. int moves = 0;
  79. static Indestructible<Magic> sing( // move assignment
  80. [&] {
  81. ++state;
  82. value = 7;
  83. },
  84. [&] { state = -1; },
  85. [&] { ++moves; });
  86. EXPECT_EQ(1, state);
  87. EXPECT_EQ(7, value);
  88. EXPECT_EQ(0, moves);
  89. // move constructor
  90. static Indestructible<Magic> move_ctor(std::move(sing));
  91. EXPECT_EQ(1, state);
  92. EXPECT_EQ(1, moves);
  93. // move assignment
  94. static Indestructible<Magic> move_assign = std::move(move_ctor);
  95. EXPECT_EQ(1, state);
  96. EXPECT_EQ(2, moves);
  97. }
  98. TEST_F(IndestructibleTest, disabled_default_ctor) {
  99. EXPECT_TRUE((std::is_constructible<Indestructible<int>>::value)) << "sanity";
  100. struct Foo {
  101. Foo(int) {}
  102. };
  103. EXPECT_FALSE((std::is_constructible<Indestructible<Foo>>::value));
  104. EXPECT_FALSE((std::is_constructible<Indestructible<Foo>, Magic>::value));
  105. EXPECT_TRUE((std::is_constructible<Indestructible<Foo>, int>::value));
  106. }
  107. TEST_F(IndestructibleTest, list_initialization) {
  108. auto map = folly::Indestructible<std::map<int, int>>{{{1, 2}}};
  109. EXPECT_EQ(map->at(1), 2);
  110. }
  111. namespace {
  112. class InitializerListConstructible {
  113. public:
  114. InitializerListConstructible(InitializerListConstructible&&) = default;
  115. explicit InitializerListConstructible(std::initializer_list<int>) {}
  116. InitializerListConstructible(std::initializer_list<double>, double) {}
  117. };
  118. } // namespace
  119. TEST_F(IndestructibleTest, initializer_list_in_place_initialization) {
  120. using I = InitializerListConstructible;
  121. std::ignore = Indestructible<I>{{1, 2, 3, 4}};
  122. std::ignore = Indestructible<I>{{1.2}, 4.2};
  123. }
  124. namespace {
  125. class ExplicitlyMoveConstructible {
  126. public:
  127. ExplicitlyMoveConstructible() = default;
  128. explicit ExplicitlyMoveConstructible(ExplicitlyMoveConstructible&&) = default;
  129. };
  130. } // namespace
  131. TEST_F(IndestructibleTest, list_initialization_explicit_implicit) {
  132. using E = ExplicitlyMoveConstructible;
  133. using I = std::map<int, int>;
  134. EXPECT_TRUE((!std::is_convertible<E, Indestructible<E>>::value));
  135. EXPECT_TRUE((std::is_convertible<I, Indestructible<I>>::value));
  136. }