15 #include "absl/cleanup/cleanup.h"
18 #include <type_traits>
21 #include "gtest/gtest.h"
22 #include "absl/base/config.h"
23 #include "absl/utility/utility.h"
29 template <
typename Type1,
typename Type2>
30 constexpr
bool IsSame() {
34 struct IdentityFactory {
35 template <
typename Callback>
36 static Callback AsCallback(Callback
callback) {
50 FunctorClass(FunctorClass&& other)
51 : callback_(
absl::
exchange(other.callback_, Callback())) {}
53 FunctorClass(
const FunctorClass&) =
delete;
55 FunctorClass& operator=(
const FunctorClass&) =
delete;
57 FunctorClass& operator=(FunctorClass&&) =
delete;
61 void operator()() && {
71 struct FunctorClassFactory {
72 template <
typename Callback>
73 static FunctorClass AsCallback(Callback
callback) {
78 struct StdFunctionFactory {
79 template <
typename Callback>
85 using CleanupTestParams =
91 bool fn_ptr_called =
false;
92 void FnPtrFunction() { fn_ptr_called =
true; }
94 TYPED_TEST(CleanupTest, FactoryProducesCorrectType) {
96 auto callback = TypeParam::AsCallback([] {});
119 #if defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
120 TYPED_TEST(CleanupTest, CTADProducesCorrectType) {
122 auto callback = TypeParam::AsCallback([] {});
145 TYPED_TEST(CleanupTest, FactoryAndCTADProduceSameType) {
147 auto callback = IdentityFactory::AsCallback([] {});
152 IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(),
"");
156 auto factory_cleanup =
158 absl::Cleanup deduction_cleanup = FunctorClassFactory::AsCallback([] {});
161 IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(),
"");
165 auto factory_cleanup =
167 absl::Cleanup deduction_cleanup = StdFunctionFactory::AsCallback([] {});
170 IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(),
"");
178 IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(),
"");
186 IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(),
"");
189 #endif // defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
203 TYPED_TEST(CleanupTest, BasicUsageWithFunctionPointer) {
204 fn_ptr_called =
false;
250 auto moved_from_cleanup =
255 auto moved_to_cleanup =
std::move(moved_from_cleanup);
267 int DestructionCount = 0;
269 struct DestructionCounter {
272 ~DestructionCounter() { ++DestructionCount; }
279 DestructionCount = 0;
289 DestructionCount = 0;
302 DestructionCount = 0;