15 #include "absl/functional/function_ref.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/container/internal/test_instance_tracker.h"
23 #include "absl/memory/memory.h"
31 TEST(FunctionRefTest, Lambda) {
33 RunFun([&] { ran =
true; });
39 TEST(FunctionRefTest, Function1) {
44 TEST(FunctionRefTest, Function2) {
57 TEST(FunctionRefTest, ForwardsArgs) {
58 auto l = [](std::unique_ptr<int>
i) {
return *
i; };
64 auto l = [] {
return absl::make_unique<int>(29); };
70 auto l = [](
int a,
int b,
int c) {
return a +
b + c; };
77 auto l = [&]() ->
int {
88 struct Derived :
public Base {};
95 auto l2 = [&]() -> Derived* {
return &d; };
111 auto mem_ptr = &
S::i;
119 int get_i()
const {
return i; }
123 auto mem_fun_ptr = &S::get_i;
131 int get_i() && {
return i; }
133 auto mem_fun_ptr = &S::get_i;
139 #if !defined(_WIN32) && defined(GTEST_HAS_DEATH_TEST)
144 int get_i() && {
return i; }
146 auto mem_fun_ptr = &S::get_i;
147 mem_fun_ptr =
nullptr;
148 EXPECT_DEBUG_DEATH({ FunctionRef<
int(
S && s)>
ref(mem_fun_ptr); },
"");
151 TEST(FunctionRef, NullMemberPtrAssertFails) {
155 using MemberPtr =
int S::*;
156 MemberPtr mem_ptr =
nullptr;
157 EXPECT_DEBUG_DEATH({ FunctionRef<
int(
const S& s)>
ref(mem_ptr); },
"");
160 #endif // GTEST_HAS_DEATH_TEST
209 struct LargeTrivial {
213 static_assert(std::is_same<Invoker<void, int>,
void (*)(VoidPtr,
int)>::
value,
214 "Scalar types should be passed by value");
216 std::is_same<Invoker<void, Trivial>,
void (*)(VoidPtr, Trivial)>::
value,
217 "Small trivial types should be passed by value");
218 static_assert(std::is_same<Invoker<void, LargeTrivial>,
219 void (*)(VoidPtr, LargeTrivial &&)>::
value,
220 "Large trivial types should be passed by rvalue reference");
222 std::is_same<Invoker<void, CopyableMovableInstance>,
223 void (*)(VoidPtr, CopyableMovableInstance &&)>::
value,
224 "Types with copy/move ctor should be passed by rvalue reference");
228 std::is_same<Invoker<void, int&>,
void (*)(VoidPtr,
int&)>::
value,
229 "Reference types should be preserved");
231 std::is_same<Invoker<void, CopyableMovableInstance&>,
232 void (*)(VoidPtr, CopyableMovableInstance&)>::
value,
233 "Reference types should be preserved");
235 std::is_same<Invoker<void, CopyableMovableInstance&&>,
236 void (*)(VoidPtr, CopyableMovableInstance &&)>::
value,
237 "Reference types should be preserved");