function_ref_test.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include "absl/functional/function_ref.h"
  15. #include <memory>
  16. #include "gmock/gmock.h"
  17. #include "gtest/gtest.h"
  18. #include "absl/container/internal/test_instance_tracker.h"
  19. #include "absl/memory/memory.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace {
  23. void RunFun(FunctionRef<void()> f) { f(); }
  24. TEST(FunctionRefTest, Lambda) {
  25. bool ran = false;
  26. RunFun([&] { ran = true; });
  27. EXPECT_TRUE(ran);
  28. }
  29. int Function() { return 1337; }
  30. TEST(FunctionRefTest, Function1) {
  31. FunctionRef<int()> ref(&Function);
  32. EXPECT_EQ(1337, ref());
  33. }
  34. TEST(FunctionRefTest, Function2) {
  35. FunctionRef<int()> ref(Function);
  36. EXPECT_EQ(1337, ref());
  37. }
  38. int NoExceptFunction() noexcept { return 1337; }
  39. // TODO(jdennett): Add a test for noexcept member functions.
  40. TEST(FunctionRefTest, NoExceptFunction) {
  41. FunctionRef<int()> ref(NoExceptFunction);
  42. EXPECT_EQ(1337, ref());
  43. }
  44. TEST(FunctionRefTest, ForwardsArgs) {
  45. auto l = [](std::unique_ptr<int> i) { return *i; };
  46. FunctionRef<int(std::unique_ptr<int>)> ref(l);
  47. EXPECT_EQ(42, ref(absl::make_unique<int>(42)));
  48. }
  49. TEST(FunctionRef, ReturnMoveOnly) {
  50. auto l = [] { return absl::make_unique<int>(29); };
  51. FunctionRef<std::unique_ptr<int>()> ref(l);
  52. EXPECT_EQ(29, *ref());
  53. }
  54. TEST(FunctionRef, ManyArgs) {
  55. auto l = [](int a, int b, int c) { return a + b + c; };
  56. FunctionRef<int(int, int, int)> ref(l);
  57. EXPECT_EQ(6, ref(1, 2, 3));
  58. }
  59. TEST(FunctionRef, VoidResultFromNonVoidFunctor) {
  60. bool ran = false;
  61. auto l = [&]() -> int {
  62. ran = true;
  63. return 2;
  64. };
  65. FunctionRef<void()> ref(l);
  66. ref();
  67. EXPECT_TRUE(ran);
  68. }
  69. TEST(FunctionRef, CastFromDerived) {
  70. struct Base {};
  71. struct Derived : public Base {};
  72. Derived d;
  73. auto l1 = [&](Base* b) { EXPECT_EQ(&d, b); };
  74. FunctionRef<void(Derived*)> ref1(l1);
  75. ref1(&d);
  76. auto l2 = [&]() -> Derived* { return &d; };
  77. FunctionRef<Base*()> ref2(l2);
  78. EXPECT_EQ(&d, ref2());
  79. }
  80. TEST(FunctionRef, VoidResultFromNonVoidFuncton) {
  81. FunctionRef<void()> ref(Function);
  82. ref();
  83. }
  84. TEST(FunctionRef, MemberPtr) {
  85. struct S {
  86. int i;
  87. };
  88. S s{1100111};
  89. auto mem_ptr = &S::i;
  90. FunctionRef<int(const S& s)> ref(mem_ptr);
  91. EXPECT_EQ(1100111, ref(s));
  92. }
  93. TEST(FunctionRef, MemberFun) {
  94. struct S {
  95. int i;
  96. int get_i() const { return i; }
  97. };
  98. S s{22};
  99. auto mem_fun_ptr = &S::get_i;
  100. FunctionRef<int(const S& s)> ref(mem_fun_ptr);
  101. EXPECT_EQ(22, ref(s));
  102. }
  103. TEST(FunctionRef, MemberFunRefqualified) {
  104. struct S {
  105. int i;
  106. int get_i() && { return i; }
  107. };
  108. auto mem_fun_ptr = &S::get_i;
  109. S s{22};
  110. FunctionRef<int(S && s)> ref(mem_fun_ptr);
  111. EXPECT_EQ(22, ref(std::move(s)));
  112. }
  113. #if !defined(_WIN32) && defined(GTEST_HAS_DEATH_TEST)
  114. TEST(FunctionRef, MemberFunRefqualifiedNull) {
  115. struct S {
  116. int i;
  117. int get_i() && { return i; }
  118. };
  119. auto mem_fun_ptr = &S::get_i;
  120. mem_fun_ptr = nullptr;
  121. EXPECT_DEBUG_DEATH({ FunctionRef<int(S && s)> ref(mem_fun_ptr); }, "");
  122. }
  123. TEST(FunctionRef, NullMemberPtrAssertFails) {
  124. struct S {
  125. int i;
  126. };
  127. using MemberPtr = int S::*;
  128. MemberPtr mem_ptr = nullptr;
  129. EXPECT_DEBUG_DEATH({ FunctionRef<int(const S& s)> ref(mem_ptr); }, "");
  130. }
  131. #endif // GTEST_HAS_DEATH_TEST
  132. TEST(FunctionRef, CopiesAndMovesPerPassByValue) {
  133. absl::test_internal::InstanceTracker tracker;
  134. absl::test_internal::CopyableMovableInstance instance(0);
  135. auto l = [](absl::test_internal::CopyableMovableInstance) {};
  136. FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
  137. ref(instance);
  138. EXPECT_EQ(tracker.copies(), 1);
  139. EXPECT_EQ(tracker.moves(), 1);
  140. }
  141. TEST(FunctionRef, CopiesAndMovesPerPassByRef) {
  142. absl::test_internal::InstanceTracker tracker;
  143. absl::test_internal::CopyableMovableInstance instance(0);
  144. auto l = [](const absl::test_internal::CopyableMovableInstance&) {};
  145. FunctionRef<void(const absl::test_internal::CopyableMovableInstance&)> ref(l);
  146. ref(instance);
  147. EXPECT_EQ(tracker.copies(), 0);
  148. EXPECT_EQ(tracker.moves(), 0);
  149. }
  150. TEST(FunctionRef, CopiesAndMovesPerPassByValueCallByMove) {
  151. absl::test_internal::InstanceTracker tracker;
  152. absl::test_internal::CopyableMovableInstance instance(0);
  153. auto l = [](absl::test_internal::CopyableMovableInstance) {};
  154. FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
  155. ref(std::move(instance));
  156. EXPECT_EQ(tracker.copies(), 0);
  157. EXPECT_EQ(tracker.moves(), 2);
  158. }
  159. TEST(FunctionRef, CopiesAndMovesPerPassByValueToRef) {
  160. absl::test_internal::InstanceTracker tracker;
  161. absl::test_internal::CopyableMovableInstance instance(0);
  162. auto l = [](const absl::test_internal::CopyableMovableInstance&) {};
  163. FunctionRef<void(absl::test_internal::CopyableMovableInstance)> ref(l);
  164. ref(std::move(instance));
  165. EXPECT_EQ(tracker.copies(), 0);
  166. EXPECT_EQ(tracker.moves(), 1);
  167. }
  168. TEST(FunctionRef, PassByValueTypes) {
  169. using absl::functional_internal::Invoker;
  170. using absl::functional_internal::VoidPtr;
  171. using absl::test_internal::CopyableMovableInstance;
  172. struct Trivial {
  173. void* p[2];
  174. };
  175. struct LargeTrivial {
  176. void* p[3];
  177. };
  178. static_assert(std::is_same<Invoker<void, int>, void (*)(VoidPtr, int)>::value,
  179. "Scalar types should be passed by value");
  180. static_assert(
  181. std::is_same<Invoker<void, Trivial>, void (*)(VoidPtr, Trivial)>::value,
  182. "Small trivial types should be passed by value");
  183. static_assert(std::is_same<Invoker<void, LargeTrivial>,
  184. void (*)(VoidPtr, LargeTrivial &&)>::value,
  185. "Large trivial types should be passed by rvalue reference");
  186. static_assert(
  187. std::is_same<Invoker<void, CopyableMovableInstance>,
  188. void (*)(VoidPtr, CopyableMovableInstance &&)>::value,
  189. "Types with copy/move ctor should be passed by rvalue reference");
  190. // References are passed as references.
  191. static_assert(
  192. std::is_same<Invoker<void, int&>, void (*)(VoidPtr, int&)>::value,
  193. "Reference types should be preserved");
  194. static_assert(
  195. std::is_same<Invoker<void, CopyableMovableInstance&>,
  196. void (*)(VoidPtr, CopyableMovableInstance&)>::value,
  197. "Reference types should be preserved");
  198. static_assert(
  199. std::is_same<Invoker<void, CopyableMovableInstance&&>,
  200. void (*)(VoidPtr, CopyableMovableInstance &&)>::value,
  201. "Reference types should be preserved");
  202. // Make sure the address of an object received by reference is the same as the
  203. // addess of the object passed by the caller.
  204. {
  205. LargeTrivial obj;
  206. auto test = [&obj](LargeTrivial& input) { ASSERT_EQ(&input, &obj); };
  207. absl::FunctionRef<void(LargeTrivial&)> ref(test);
  208. ref(obj);
  209. }
  210. {
  211. Trivial obj;
  212. auto test = [&obj](Trivial& input) { ASSERT_EQ(&input, &obj); };
  213. absl::FunctionRef<void(Trivial&)> ref(test);
  214. ref(obj);
  215. }
  216. }
  217. } // namespace
  218. ABSL_NAMESPACE_END
  219. } // namespace absl