2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Memory.h>
18 #include <folly/io/async/DestructorCheck.h>
19 #include <folly/portability/GTest.h>
21 using namespace folly;
22 using namespace testing;
24 class Derived : public DestructorCheck { };
26 TEST(DestructorCheckTest, WithoutGuard) {
30 TEST(DestructorCheckTest, SingleGuard) {
33 ASSERT_FALSE(s.destroyed());
36 TEST(DestructorCheckTest, SingleGuardDestroyed) {
37 auto d = std::make_unique<Derived>();
38 Derived::Safety s(*d);
39 ASSERT_FALSE(s.destroyed());
41 ASSERT_TRUE(s.destroyed());
44 TEST(DestructorCheckTest, MultipleGuards) {
46 auto s1 = std::make_unique<Derived::Safety>(d);
47 auto s2 = std::make_unique<Derived::Safety>(d);
48 auto s3 = std::make_unique<Derived::Safety>(d);
50 // Remove the middle of the list.
51 ASSERT_FALSE(s2->destroyed());
54 // Add in a link after a removal has occurred.
55 auto s4 = std::make_unique<Derived::Safety>(d);
57 // Remove the beginning of the list.
58 ASSERT_FALSE(s1->destroyed());
60 // Remove the end of the list.
61 ASSERT_FALSE(s4->destroyed());
63 // Remove the last remaining of the list.
64 ASSERT_FALSE(s3->destroyed());
68 TEST(DestructorCheckTest, MultipleGuardsDestroyed) {
69 auto d = std::make_unique<Derived>();
70 auto s1 = std::make_unique<Derived::Safety>(*d);
71 auto s2 = std::make_unique<Derived::Safety>(*d);
72 auto s3 = std::make_unique<Derived::Safety>(*d);
73 auto s4 = std::make_unique<Derived::Safety>(*d);
75 // Remove something from the list.
76 ASSERT_FALSE(s2->destroyed());
79 ASSERT_FALSE(s1->destroyed());
80 ASSERT_FALSE(s3->destroyed());
81 ASSERT_FALSE(s4->destroyed());
85 ASSERT_TRUE(s1->destroyed());
86 ASSERT_TRUE(s3->destroyed());
87 ASSERT_TRUE(s4->destroyed());