1 // Copyright 2019 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/functional/function_ref.h"
16
17 #include <functional>
18 #include <memory>
19
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/container/internal/test_instance_tracker.h"
23 #include "absl/functional/any_invocable.h"
24 #include "absl/memory/memory.h"
25
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 namespace {
29
RunFun(FunctionRef<void ()> f)30 void RunFun(FunctionRef<void()> f) { f(); }
31
TEST(FunctionRefTest,Lambda)32 TEST(FunctionRefTest, Lambda) {
33 bool ran = false;
34 RunFun([&] { ran = true; });
35 EXPECT_TRUE(ran);
36 }
37
Function()38 int Function() { return 1337; }
39
TEST(FunctionRefTest,Function1)40 TEST(FunctionRefTest, Function1) {
41 FunctionRef<int()> ref(&Function);
42 EXPECT_EQ(1337, ref());
43 }
44
TEST(FunctionRefTest,Function2)45 TEST(FunctionRefTest, Function2) {
46 FunctionRef<int()> ref(Function);
47 EXPECT_EQ(1337, ref());
48 }
49
NoExceptFunction()50 int NoExceptFunction() noexcept { return 1337; }
51
52 // TODO(jdennett): Add a test for noexcept member functions.
TEST(FunctionRefTest,NoExceptFunction)53 TEST(FunctionRefTest, NoExceptFunction) {
54 FunctionRef<int()> ref(NoExceptFunction);
55 EXPECT_EQ(1337, ref());
56 }
57
TEST(FunctionRefTest,ForwardsArgs)58 TEST(FunctionRefTest, ForwardsArgs) {
59 auto l = [](std::unique_ptr<int> i) { return *i; };
60 FunctionRef<int(std::unique_ptr<int>)> ref(l);
61 EXPECT_EQ(42, ref(absl::make_unique<int>(42)));
62 }
63
TEST(FunctionRef,ReturnMoveOnly)64 TEST(FunctionRef, ReturnMoveOnly) {
65 auto l = [] { return absl::make_unique<int>(29); };
66 FunctionRef<std::unique_ptr<int>()> ref(l);
67 EXPECT_EQ(29, *ref());
68 }
69
TEST(FunctionRef,ManyArgs)70 TEST(FunctionRef, ManyArgs) {
71 auto l = [](int a, int b, int c) { return a + b + c; };
72 FunctionRef<int(int, int, int)> ref(l);
73 EXPECT_EQ(6, ref(1, 2, 3));
74 }
75
TEST(FunctionRef,VoidResultFromNonVoidFunctor)76 TEST(FunctionRef, VoidResultFromNonVoidFunctor) {
77 bool ran = false;
78 auto l = [&]() -> int {
79 ran = true;
80 return 2;
81 };
82 FunctionRef<void()> ref(l);
83 ref();
84 EXPECT_TRUE(ran);
85 }
86
TEST(FunctionRef,CastFromDerived)87 TEST(FunctionRef, CastFromDerived) {
88 struct Base {};
89 struct Derived : public Base {};
90
91 Derived d;
92 auto l1 = [&](Base* b) { EXPECT_EQ(&d, b); };
93 FunctionRef<void(Derived*)> ref1(l1);
94 ref1(&d);
95
96 auto l2 = [&]() -> Derived* { return &d; };
97 FunctionRef<Base*()> ref2(l2);
98 EXPECT_EQ(&d, ref2());
99 }
100
TEST(FunctionRef,VoidResultFromNonVoidFuncton)101 TEST(FunctionRef, VoidResultFromNonVoidFuncton) {
102 FunctionRef<void()> ref(Function);
103 ref();
104 }
105
TEST(FunctionRef,MemberPtr)106 TEST(FunctionRef, MemberPtr) {
107 struct S {
108 int i;
109 };
110
111 S s{1100111};
112 auto mem_ptr = &S::i;
113 FunctionRef<int(const S& s)> ref(mem_ptr);
114 EXPECT_EQ(1100111, ref(s));
115 }
116
TEST(FunctionRef,MemberFun)117 TEST(FunctionRef, MemberFun) {
118 struct S {
119 int i;
120 int get_i() const { return i; }
121 };
122
123 S s{22};
124 auto mem_fun_ptr = &S::get_i;
125 FunctionRef<int(const S& s)> ref(mem_fun_ptr);
126 EXPECT_EQ(22, ref(s));
127 }
128
TEST(FunctionRef,MemberFunRefqualified)129 TEST(FunctionRef, MemberFunRefqualified) {
130 struct S {
131 int i;
132 int get_i() && { return i; }
133 };
134 auto mem_fun_ptr = &S::get_i;
135 S s{22};
136 FunctionRef<int(S && s)> ref(mem_fun_ptr);
137 EXPECT_EQ(22, ref(std::move(s)));
138 }
139
140 #if !defined(_WIN32) && defined(GTEST_HAS_DEATH_TEST)
141
TEST(FunctionRef,MemberFunRefqualifiedNull)142 TEST(FunctionRef, MemberFunRefqualifiedNull) {
143 struct S {
144 int i;
145 int get_i() && { return i; }
146 };
147 auto mem_fun_ptr = &S::get_i;
148 mem_fun_ptr = nullptr;
149 EXPECT_DEBUG_DEATH({ FunctionRef<int(S && s)> ref(mem_fun_ptr); }, "");
150 }
151
TEST(FunctionRef,NullMemberPtrAssertFails)152 TEST(FunctionRef, NullMemberPtrAssertFails) {
153 struct S {
154 int i;
155 };
156 using MemberPtr = int S::*;
157 MemberPtr mem_ptr = nullptr;
158 EXPECT_DEBUG_DEATH({ FunctionRef<int(const S& s)> ref(mem_ptr); }, "");
159 }
160
TEST(FunctionRef,NullStdFunctionAssertPasses)161 TEST(FunctionRef, NullStdFunctionAssertPasses) {
162 std::function<void()> function = []() {};
163 FunctionRef<void()> ref(function);
164 }
165
TEST(FunctionRef,NullStdFunctionAssertFails)166 TEST(FunctionRef, NullStdFunctionAssertFails) {
167 std::function<void()> function = nullptr;
168 EXPECT_DEBUG_DEATH({ FunctionRef<void()> ref(function); }, "");
169 }
170
TEST(FunctionRef,NullAnyInvocableAssertPasses)171 TEST(FunctionRef, NullAnyInvocableAssertPasses) {
172 AnyInvocable<void() const> invocable = []() {};
173 FunctionRef<void()> ref(invocable);
174 }
TEST(FunctionRef,NullAnyInvocableAssertFails)175 TEST(FunctionRef, NullAnyInvocableAssertFails) {
176 AnyInvocable<void() const> invocable = nullptr;
177 EXPECT_DEBUG_DEATH({ FunctionRef<void()> ref(invocable); }, "");
178 }
179
180 #endif // GTEST_HAS_DEATH_TEST
181
TEST(FunctionRef,CopiesAndMovesPerPassByValue)182 TEST(FunctionRef, CopiesAndMovesPerPassByValue) {
183 absl::test_internal::InstanceTracker tracker;
184 absl::test_internal::CopyableMovableInstance instance(0);
185 auto l = [](absl::test_internal::CopyableMovableInstance) {};
186 FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
187 ref(instance);
188 EXPECT_EQ(tracker.copies(), 1);
189 EXPECT_EQ(tracker.moves(), 1);
190 }
191
TEST(FunctionRef,CopiesAndMovesPerPassByRef)192 TEST(FunctionRef, CopiesAndMovesPerPassByRef) {
193 absl::test_internal::InstanceTracker tracker;
194 absl::test_internal::CopyableMovableInstance instance(0);
195 auto l = [](const absl::test_internal::CopyableMovableInstance&) {};
196 FunctionRef<void(const absl::test_internal::CopyableMovableInstance&)> ref(l);
197 ref(instance);
198 EXPECT_EQ(tracker.copies(), 0);
199 EXPECT_EQ(tracker.moves(), 0);
200 }
201
TEST(FunctionRef,CopiesAndMovesPerPassByValueCallByMove)202 TEST(FunctionRef, CopiesAndMovesPerPassByValueCallByMove) {
203 absl::test_internal::InstanceTracker tracker;
204 absl::test_internal::CopyableMovableInstance instance(0);
205 auto l = [](absl::test_internal::CopyableMovableInstance) {};
206 FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
207 ref(std::move(instance));
208 EXPECT_EQ(tracker.copies(), 0);
209 EXPECT_EQ(tracker.moves(), 2);
210 }
211
TEST(FunctionRef,CopiesAndMovesPerPassByValueToRef)212 TEST(FunctionRef, CopiesAndMovesPerPassByValueToRef) {
213 absl::test_internal::InstanceTracker tracker;
214 absl::test_internal::CopyableMovableInstance instance(0);
215 auto l = [](const absl::test_internal::CopyableMovableInstance&) {};
216 FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
217 ref(std::move(instance));
218 EXPECT_EQ(tracker.copies(), 0);
219 EXPECT_EQ(tracker.moves(), 1);
220 }
221
TEST(FunctionRef,PassByValueTypes)222 TEST(FunctionRef, PassByValueTypes) {
223 using absl::functional_internal::Invoker;
224 using absl::functional_internal::VoidPtr;
225 using absl::test_internal::CopyableMovableInstance;
226 struct Trivial {
227 void* p[2];
228 };
229 struct LargeTrivial {
230 void* p[3];
231 };
232
233 static_assert(std::is_same<Invoker<void, int>, void (*)(VoidPtr, int)>::value,
234 "Scalar types should be passed by value");
235 static_assert(
236 std::is_same<Invoker<void, Trivial>, void (*)(VoidPtr, Trivial)>::value,
237 "Small trivial types should be passed by value");
238 static_assert(std::is_same<Invoker<void, LargeTrivial>,
239 void (*)(VoidPtr, LargeTrivial &&)>::value,
240 "Large trivial types should be passed by rvalue reference");
241 static_assert(
242 std::is_same<Invoker<void, CopyableMovableInstance>,
243 void (*)(VoidPtr, CopyableMovableInstance &&)>::value,
244 "Types with copy/move ctor should be passed by rvalue reference");
245
246 // References are passed as references.
247 static_assert(
248 std::is_same<Invoker<void, int&>, void (*)(VoidPtr, int&)>::value,
249 "Reference types should be preserved");
250 static_assert(
251 std::is_same<Invoker<void, CopyableMovableInstance&>,
252 void (*)(VoidPtr, CopyableMovableInstance&)>::value,
253 "Reference types should be preserved");
254 static_assert(
255 std::is_same<Invoker<void, CopyableMovableInstance&&>,
256 void (*)(VoidPtr, CopyableMovableInstance &&)>::value,
257 "Reference types should be preserved");
258
259 // Make sure the address of an object received by reference is the same as the
260 // address of the object passed by the caller.
261 {
262 LargeTrivial obj;
263 auto test = [&obj](LargeTrivial& input) { ASSERT_EQ(&input, &obj); };
264 absl::FunctionRef<void(LargeTrivial&)> ref(test);
265 ref(obj);
266 }
267
268 {
269 Trivial obj;
270 auto test = [&obj](Trivial& input) { ASSERT_EQ(&input, &obj); };
271 absl::FunctionRef<void(Trivial&)> ref(test);
272 ref(obj);
273 }
274 }
275
TEST(FunctionRef,ReferenceToIncompleteType)276 TEST(FunctionRef, ReferenceToIncompleteType) {
277 struct IncompleteType;
278 auto test = [](IncompleteType&) {};
279 absl::FunctionRef<void(IncompleteType&)> ref(test);
280
281 struct IncompleteType {};
282 IncompleteType obj;
283 ref(obj);
284 }
285
286 } // namespace
287 ABSL_NAMESPACE_END
288 } // namespace absl
289