DestructorCheckTest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/io/async/DestructorCheck.h>
  17. #include <folly/Memory.h>
  18. #include <folly/portability/GTest.h>
  19. using namespace folly;
  20. using namespace testing;
  21. class Derived : public DestructorCheck {};
  22. TEST(DestructorCheckTest, WithoutGuard) {
  23. Derived d;
  24. }
  25. TEST(DestructorCheckTest, SingleGuard) {
  26. Derived d;
  27. Derived::Safety s(d);
  28. ASSERT_FALSE(s.destroyed());
  29. }
  30. TEST(DestructorCheckTest, SingleGuardDestroyed) {
  31. auto d = std::make_unique<Derived>();
  32. Derived::Safety s(*d);
  33. ASSERT_FALSE(s.destroyed());
  34. d.reset();
  35. ASSERT_TRUE(s.destroyed());
  36. }
  37. TEST(DestructorCheckTest, MultipleGuards) {
  38. Derived d;
  39. auto s1 = std::make_unique<Derived::Safety>(d);
  40. auto s2 = std::make_unique<Derived::Safety>(d);
  41. auto s3 = std::make_unique<Derived::Safety>(d);
  42. // Remove the middle of the list.
  43. ASSERT_FALSE(s2->destroyed());
  44. s2.reset();
  45. // Add in a link after a removal has occurred.
  46. auto s4 = std::make_unique<Derived::Safety>(d);
  47. // Remove the beginning of the list.
  48. ASSERT_FALSE(s1->destroyed());
  49. s1.reset();
  50. // Remove the end of the list.
  51. ASSERT_FALSE(s4->destroyed());
  52. s4.reset();
  53. // Remove the last remaining of the list.
  54. ASSERT_FALSE(s3->destroyed());
  55. s3.reset();
  56. }
  57. TEST(DestructorCheckTest, MultipleGuardsDestroyed) {
  58. auto d = std::make_unique<Derived>();
  59. auto s1 = std::make_unique<Derived::Safety>(*d);
  60. auto s2 = std::make_unique<Derived::Safety>(*d);
  61. auto s3 = std::make_unique<Derived::Safety>(*d);
  62. auto s4 = std::make_unique<Derived::Safety>(*d);
  63. // Remove something from the list.
  64. ASSERT_FALSE(s2->destroyed());
  65. s2.reset();
  66. ASSERT_FALSE(s1->destroyed());
  67. ASSERT_FALSE(s3->destroyed());
  68. ASSERT_FALSE(s4->destroyed());
  69. d.reset();
  70. ASSERT_TRUE(s1->destroyed());
  71. ASSERT_TRUE(s3->destroyed());
  72. ASSERT_TRUE(s4->destroyed());
  73. }