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.
20 #include <folly/Function.h>
22 #include <folly/Memory.h>
23 #include <folly/portability/GTest.h>
25 using folly::Function;
28 int func_int_int_add_25(int x) {
31 int func_int_int_add_111(int x) {
34 float floatMult(float a, float b) {
38 template <class T, size_t S>
40 std::array<T, S> data = {{0}};
42 // Two operator() with different argument types.
43 // The InvokeReference tests use both
44 T const& operator()(size_t index) const {
47 T operator()(size_t index, T const& value) {
48 T oldvalue = data[index];
54 template <typename Ret, typename... Args>
55 void deduceArgs(Function<Ret(Args...)>) {}
57 struct CallableButNotCopyable {
58 CallableButNotCopyable() {}
59 CallableButNotCopyable(CallableButNotCopyable const&) = delete;
60 CallableButNotCopyable(CallableButNotCopyable&&) = delete;
61 CallableButNotCopyable& operator=(CallableButNotCopyable const&) = delete;
62 CallableButNotCopyable& operator=(CallableButNotCopyable&&) = delete;
63 template <class... Args>
64 void operator()(Args&&...) const {}
69 // TEST =====================================================================
70 // Test constructibility and non-constructibility for some tricky conversions
72 !std::is_assignable<Function<void()>, CallableButNotCopyable>::value,
75 !std::is_constructible<Function<void()>, CallableButNotCopyable&>::value,
78 !std::is_constructible<Function<void() const>, CallableButNotCopyable>::
82 !std::is_constructible<Function<void() const>, CallableButNotCopyable&>::
87 !std::is_assignable<Function<void()>, CallableButNotCopyable>::value,
90 !std::is_assignable<Function<void()>, CallableButNotCopyable&>::value,
93 !std::is_assignable<Function<void() const>, CallableButNotCopyable>::value,
96 !std::is_assignable<Function<void() const>, CallableButNotCopyable&>::value,
100 std::is_constructible<Function<int(int)>, Function<int(int) const>>::value,
103 !std::is_constructible<Function<int(int) const>, Function<int(int)>>::value,
106 std::is_constructible<Function<int(short)>, Function<short(int) const>>::
110 !std::is_constructible<Function<int(short) const>, Function<short(int)>>::
114 !std::is_constructible<Function<int(int)>, Function<int(int) const>&>::
118 !std::is_constructible<Function<int(int) const>, Function<int(int)>&>::
122 !std::is_constructible<Function<int(short)>, Function<short(int) const>&>::
126 !std::is_constructible<Function<int(short) const>, Function<short(int)>&>::
131 std::is_assignable<Function<int(int)>, Function<int(int) const>>::value,
134 !std::is_assignable<Function<int(int) const>, Function<int(int)>>::value,
137 std::is_assignable<Function<int(short)>, Function<short(int) const>>::value,
140 !std::is_assignable<Function<int(short) const>, Function<short(int)>>::
144 !std::is_assignable<Function<int(int)>, Function<int(int) const>&>::value,
147 !std::is_assignable<Function<int(int) const>, Function<int(int)>&>::value,
150 !std::is_assignable<Function<int(short)>, Function<short(int) const>&>::
154 !std::is_assignable<Function<int(short) const>, Function<short(int)>&>::
159 std::is_nothrow_constructible<
161 Function<int(int) const>>::value,
164 !std::is_nothrow_constructible<
165 Function<int(short)>,
166 Function<short(int) const>>::value,
169 std::is_nothrow_assignable<Function<int(int)>, Function<int(int) const>>::
173 !std::is_nothrow_assignable<
174 Function<int(short)>,
175 Function<short(int) const>>::value,
178 // TEST =====================================================================
179 // InvokeFunctor & InvokeReference
181 TEST(Function, InvokeFunctor) {
182 Functor<int, 100> func;
184 sizeof(func) > sizeof(Function<int(size_t)>),
185 "sizeof(Function) is much larger than expected");
188 Function<int(size_t) const> getter = std::move(func);
190 // Function will allocate memory on the heap to store the functor object
191 EXPECT_TRUE(getter.hasAllocatedMemory());
193 EXPECT_EQ(123, getter(5));
196 TEST(Function, InvokeReference) {
197 Functor<int, 10> func;
200 // Have Functions for getter and setter, both referencing the same funtor
201 Function<int(size_t) const> getter = std::ref(func);
202 Function<int(size_t, int)> setter = std::ref(func);
204 EXPECT_EQ(123, getter(5));
205 EXPECT_EQ(123, setter(5, 456));
206 EXPECT_EQ(456, setter(5, 567));
207 EXPECT_EQ(567, getter(5));
210 // TEST =====================================================================
213 TEST(Function, Emptiness_T) {
214 Function<int(int)> f;
215 EXPECT_EQ(f, nullptr);
216 EXPECT_EQ(nullptr, f);
218 EXPECT_THROW(f(98), std::bad_function_call);
220 Function<int(int)> g([](int x) { return x + 1; });
221 EXPECT_NE(g, nullptr);
222 EXPECT_NE(nullptr, g);
223 // Explicitly convert to bool to work around
224 // https://github.com/google/googletest/issues/429
225 EXPECT_TRUE(bool(g));
226 EXPECT_EQ(100, g(99));
228 Function<int(int)> h(&func_int_int_add_25);
229 EXPECT_NE(h, nullptr);
230 EXPECT_NE(nullptr, h);
231 EXPECT_TRUE(bool(h));
232 EXPECT_EQ(125, h(100));
235 EXPECT_EQ(h, nullptr);
236 EXPECT_EQ(nullptr, h);
238 EXPECT_THROW(h(101), std::bad_function_call);
241 // TEST =====================================================================
244 template <bool UseSwapMethod>
246 Function<int(int)> mf1(func_int_int_add_25);
247 Function<int(int)> mf2(func_int_int_add_111);
249 EXPECT_EQ(125, mf1(100));
250 EXPECT_EQ(211, mf2(100));
258 EXPECT_EQ(125, mf2(100));
259 EXPECT_EQ(211, mf1(100));
261 Function<int(int)> mf3(nullptr);
262 EXPECT_EQ(mf3, nullptr);
270 EXPECT_EQ(211, mf3(100));
271 EXPECT_EQ(nullptr, mf1);
273 Function<int(int)> mf4([](int x) { return x + 222; });
274 EXPECT_EQ(322, mf4(100));
281 EXPECT_EQ(211, mf4(100));
282 EXPECT_EQ(322, mf3(100));
289 EXPECT_EQ(nullptr, mf3);
290 EXPECT_EQ(322, mf1(100));
292 TEST(Function, SwapMethod) {
295 TEST(Function, SwapFunction) {
299 // TEST =====================================================================
302 TEST(Function, Bind) {
303 Function<float(float, float)> fnc = floatMult;
304 auto task = std::bind(std::move(fnc), 2.f, 4.f);
305 EXPECT_THROW(fnc(0, 0), std::bad_function_call);
306 EXPECT_EQ(8, task());
307 auto task2(std::move(task));
308 EXPECT_THROW(task(), std::bad_function_call);
309 EXPECT_EQ(8, task2());
312 // TEST =====================================================================
315 TEST(Function, NonCopyableLambda) {
316 auto unique_ptr_int = folly::make_unique<int>(900);
317 EXPECT_EQ(900, *unique_ptr_int);
322 (void)fooData; // suppress gcc warning about fooData not being used
324 auto functor = std::bind(
325 [fooData](std::unique_ptr<int>& up) mutable { return ++*up; },
326 std::move(unique_ptr_int));
328 EXPECT_EQ(901, functor());
330 Function<int(void)> func = std::move(functor);
331 EXPECT_TRUE(func.hasAllocatedMemory());
333 EXPECT_EQ(902, func());
336 // TEST =====================================================================
339 TEST(Function, OverloadedFunctor) {
340 struct OverloadedFunctor {
342 int operator()(int x) {
346 // variant 2 (const-overload of v1)
347 int operator()(int x) const {
352 int operator()(int x, int) {
356 // variant 4 (const-overload of v3)
357 int operator()(int x, int) const {
361 // variant 5 (non-const, has no const-overload)
362 int operator()(int x, char const*) {
366 // variant 6 (const only)
367 int operator()(int x, std::vector<int> const&) const {
371 OverloadedFunctor of;
373 Function<int(int)> variant1 = of;
374 EXPECT_EQ(100 + 1 * 15, variant1(15));
376 Function<int(int) const> variant2 = of;
377 EXPECT_EQ(100 + 2 * 16, variant2(16));
379 Function<int(int, int)> variant3 = of;
380 EXPECT_EQ(100 + 3 * 17, variant3(17, 0));
382 Function<int(int, int) const> variant4 = of;
383 EXPECT_EQ(100 + 4 * 18, variant4(18, 0));
385 Function<int(int, char const*)> variant5 = of;
386 EXPECT_EQ(100 + 5 * 19, variant5(19, "foo"));
388 Function<int(int, std::vector<int> const&)> variant6 = of;
389 EXPECT_EQ(100 + 6 * 20, variant6(20, {}));
390 EXPECT_EQ(100 + 6 * 20, variant6(20, {1, 2, 3}));
392 Function<int(int, std::vector<int> const&) const> variant6const = of;
393 EXPECT_EQ(100 + 6 * 21, variant6const(21, {}));
395 // Cast const-functions to non-const and the other way around: if the functor
396 // has both const and non-const operator()s for a given parameter signature,
397 // constructing a Function must select one of them, depending on
398 // whether the function type template parameter is const-qualified or not.
399 // When the const-ness is later changed (by moving the
400 // Function<R(Args...)const> into a Function<R(Args...)> or by
401 // calling the folly::constCastFunction which moves it into a
402 // Function<R(Args...)const>), the Function must still execute
403 // the initially selected function.
405 auto variant1_const = folly::constCastFunction(std::move(variant1));
406 EXPECT_THROW(variant1(0), std::bad_function_call);
407 EXPECT_EQ(100 + 1 * 22, variant1_const(22));
409 Function<int(int)> variant2_nonconst = std::move(variant2);
410 EXPECT_THROW(variant2(0), std::bad_function_call);
411 EXPECT_EQ(100 + 2 * 23, variant2_nonconst(23));
413 auto variant3_const = folly::constCastFunction(std::move(variant3));
414 EXPECT_THROW(variant3(0, 0), std::bad_function_call);
415 EXPECT_EQ(100 + 3 * 24, variant3_const(24, 0));
417 Function<int(int, int)> variant4_nonconst = std::move(variant4);
418 EXPECT_THROW(variant4(0, 0), std::bad_function_call);
419 EXPECT_EQ(100 + 4 * 25, variant4_nonconst(25, 0));
421 auto variant5_const = folly::constCastFunction(std::move(variant5));
422 EXPECT_THROW(variant5(0, ""), std::bad_function_call);
423 EXPECT_EQ(100 + 5 * 26, variant5_const(26, "foo"));
425 auto variant6_const = folly::constCastFunction(std::move(variant6));
426 EXPECT_THROW(variant6(0, {}), std::bad_function_call);
427 EXPECT_EQ(100 + 6 * 27, variant6_const(27, {}));
429 Function<int(int, std::vector<int> const&)> variant6const_nonconst =
430 std::move(variant6const);
431 EXPECT_THROW(variant6const(0, {}), std::bad_function_call);
432 EXPECT_EQ(100 + 6 * 28, variant6const_nonconst(28, {}));
435 // TEST =====================================================================
438 TEST(Function, Lambda) {
439 // Non-mutable lambdas: can be stored in a non-const...
440 Function<int(int)> func = [](int x) { return 1000 + x; };
441 EXPECT_EQ(1001, func(1));
443 // ...as well as in a const Function
444 Function<int(int) const> func_const = [](int x) { return 2000 + x; };
445 EXPECT_EQ(2001, func_const(1));
447 // Mutable lambda: can only be stored in a const Function:
449 Function<int()> func_mutable = [number]() mutable { return ++number; };
450 EXPECT_EQ(3001, func_mutable());
451 EXPECT_EQ(3002, func_mutable());
453 // test after const-casting
455 Function<int(int) const> func_made_const =
456 folly::constCastFunction(std::move(func));
457 EXPECT_EQ(1002, func_made_const(2));
458 EXPECT_THROW(func(0), std::bad_function_call);
460 Function<int(int)> func_const_made_nonconst = std::move(func_const);
461 EXPECT_EQ(2002, func_const_made_nonconst(2));
462 EXPECT_THROW(func_const(0), std::bad_function_call);
464 Function<int() const> func_mutable_made_const =
465 folly::constCastFunction(std::move(func_mutable));
466 EXPECT_EQ(3003, func_mutable_made_const());
467 EXPECT_EQ(3004, func_mutable_made_const());
468 EXPECT_THROW(func_mutable(), std::bad_function_call);
471 // TEST =====================================================================
472 // DataMember & MemberFunction
484 TEST(Function, DataMember) {
486 MemberFunc const& cmf = mf;
489 Function<int(MemberFunc const*)> data_getter1 = &MemberFunc::x;
490 EXPECT_EQ(123, data_getter1(&cmf));
491 Function<int(MemberFunc*)> data_getter2 = &MemberFunc::x;
492 EXPECT_EQ(123, data_getter2(&mf));
493 Function<int(MemberFunc const&)> data_getter3 = &MemberFunc::x;
494 EXPECT_EQ(123, data_getter3(cmf));
495 Function<int(MemberFunc&)> data_getter4 = &MemberFunc::x;
496 EXPECT_EQ(123, data_getter4(mf));
499 TEST(Function, MemberFunction) {
501 MemberFunc const& cmf = mf;
504 Function<int(MemberFunc const*)> getter1 = &MemberFunc::getX;
505 EXPECT_EQ(123, getter1(&cmf));
506 Function<int(MemberFunc*)> getter2 = &MemberFunc::getX;
507 EXPECT_EQ(123, getter2(&mf));
508 Function<int(MemberFunc const&)> getter3 = &MemberFunc::getX;
509 EXPECT_EQ(123, getter3(cmf));
510 Function<int(MemberFunc&)> getter4 = &MemberFunc::getX;
511 EXPECT_EQ(123, getter4(mf));
513 Function<void(MemberFunc*, int)> setter1 = &MemberFunc::setX;
515 EXPECT_EQ(234, mf.x);
517 Function<void(MemberFunc&, int)> setter2 = &MemberFunc::setX;
519 EXPECT_EQ(345, mf.x);
522 // TEST =====================================================================
523 // CaptureCopyMoveCount & ParameterCopyMoveCount
525 class CopyMoveTracker {
527 struct ConstructorTag {};
529 CopyMoveTracker() = delete;
530 explicit CopyMoveTracker(ConstructorTag)
531 : data_(std::make_shared<std::pair<size_t, size_t>>(0, 0)) {}
533 CopyMoveTracker(CopyMoveTracker const& o) noexcept : data_(o.data_) {
536 CopyMoveTracker& operator=(CopyMoveTracker const& o) noexcept {
542 CopyMoveTracker(CopyMoveTracker&& o) noexcept : data_(o.data_) {
545 CopyMoveTracker& operator=(CopyMoveTracker&& o) noexcept {
551 size_t copyCount() const {
554 size_t moveCount() const {
555 return data_->second;
557 size_t refCount() const {
558 return data_.use_count();
560 void resetCounters() {
561 data_->first = data_->second = 0;
566 std::shared_ptr<std::pair<size_t, size_t>> data_;
569 TEST(Function, CaptureCopyMoveCount) {
570 // This test checks that no unnecessary copies/moves are made.
572 CopyMoveTracker cmt(CopyMoveTracker::ConstructorTag{});
573 EXPECT_EQ(0, cmt.copyCount());
574 EXPECT_EQ(0, cmt.moveCount());
575 EXPECT_EQ(1, cmt.refCount());
577 // Move into lambda, move lambda into Function
578 auto lambda1 = [cmt = std::move(cmt)]() {
579 return cmt.moveCount();
581 Function<size_t(void)> uf1 = std::move(lambda1);
583 // Max copies: 0. Max copy+moves: 2.
584 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
585 EXPECT_LE(cmt.copyCount(), 0);
589 // Move into lambda, copy lambda into Function
590 auto lambda2 = [cmt = std::move(cmt)]() {
591 return cmt.moveCount();
593 Function<size_t(void)> uf2 = lambda2;
595 // Max copies: 1. Max copy+moves: 2.
596 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
597 EXPECT_LE(cmt.copyCount(), 1);
599 // Invoking Function must not make copies/moves of the callable
603 EXPECT_EQ(0, cmt.copyCount());
604 EXPECT_EQ(0, cmt.moveCount());
607 TEST(Function, ParameterCopyMoveCount) {
608 // This test checks that no unnecessary copies/moves are made.
610 CopyMoveTracker cmt(CopyMoveTracker::ConstructorTag{});
611 EXPECT_EQ(0, cmt.copyCount());
612 EXPECT_EQ(0, cmt.moveCount());
613 EXPECT_EQ(1, cmt.refCount());
616 Function<size_t(CopyMoveTracker)> uf1 = [](CopyMoveTracker c) {
617 return c.moveCount();
622 // Max copies: 1. Max copy+moves: 2.
623 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
624 EXPECT_LE(cmt.copyCount(), 1);
628 // Max copies: 1. Max copy+moves: 2.
629 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
630 EXPECT_LE(cmt.copyCount(), 0);
633 Function<size_t(CopyMoveTracker&)> uf2 = [](CopyMoveTracker& c) {
634 return c.moveCount();
639 // Max copies: 0. Max copy+moves: 0.
640 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
641 EXPECT_LE(cmt.copyCount(), 0);
643 // pass by const reference
644 Function<size_t(CopyMoveTracker const&)> uf3 = [](CopyMoveTracker const& c) {
645 return c.moveCount();
650 // Max copies: 0. Max copy+moves: 0.
651 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
652 EXPECT_LE(cmt.copyCount(), 0);
654 // pass by rvalue reference
655 Function<size_t(CopyMoveTracker &&)> uf4 = [](CopyMoveTracker&& c) {
656 return c.moveCount();
661 // Max copies: 0. Max copy+moves: 0.
662 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
663 EXPECT_LE(cmt.copyCount(), 0);
666 // TEST =====================================================================
667 // VariadicTemplate & VariadicArguments
669 struct VariadicTemplateSum {
670 int operator()() const {
673 template <class... Args>
674 int operator()(int x, Args... args) const {
675 return x + (*this)(args...);
679 TEST(Function, VariadicTemplate) {
680 Function<int(int)> uf1 = VariadicTemplateSum();
681 Function<int(int, int)> uf2 = VariadicTemplateSum();
682 Function<int(int, int, int)> uf3 = VariadicTemplateSum();
684 EXPECT_EQ(66, uf1(66));
685 EXPECT_EQ(99, uf2(55, 44));
686 EXPECT_EQ(66, uf3(33, 22, 11));
689 struct VariadicArgumentsSum {
690 int operator()(int count, ...) const {
693 va_start(args, count);
694 for (int i = 0; i < count; ++i) {
695 result += va_arg(args, int);
702 TEST(Function, VariadicArguments) {
703 Function<int(int)> uf1 = VariadicArgumentsSum();
704 Function<int(int, int)> uf2 = VariadicArgumentsSum();
705 Function<int(int, int, int)> uf3 = VariadicArgumentsSum();
707 EXPECT_EQ(0, uf1(0));
708 EXPECT_EQ(66, uf2(1, 66));
709 EXPECT_EQ(99, uf3(2, 55, 44));
712 // TEST =====================================================================
713 // SafeCaptureByReference
715 // A function can use Function const& as a parameter to signal that it
716 // is safe to pass a lambda that captures local variables by reference.
717 // It is safe because we know the function called can only invoke the
718 // Function until it returns. It can't store a copy of the Function
719 // (because it's not copyable), and it can't move the Function somewhere
720 // else (because it gets only a const&).
722 template <typename T>
725 Function<void(typename T::value_type const&) const> const& func) {
726 for (auto const& elem : range) {
731 TEST(Function, SafeCaptureByReference) {
732 std::vector<int> const vec = {20, 30, 40, 2, 3, 4, 200, 300, 400};
736 // for_each's second parameter is of type Function<...> const&.
737 // Hence we know we can safely pass it a lambda that references local
738 // variables. There is no way the reference to x will be stored anywhere.
739 for_each<std::vector<int>>(vec, [&sum](int x) { sum += x; });
741 // gcc versions before 4.9 cannot deduce the type T in the above call
742 // to for_each. Modern compiler versions can compile the following line:
743 // for_each(vec, [&sum](int x) { sum += x; });
748 // TEST =====================================================================
751 TEST(Function, IgnoreReturnValue) {
754 // Assign a lambda that return int to a folly::Function that returns void.
755 Function<void()> f = [&]() -> int { return ++x; };
761 Function<int()> g = [&]() -> int { return ++x; };
762 Function<void()> cg = std::move(g);
769 // TEST =====================================================================
770 // ReturnConvertible, ConvertReturnType
772 TEST(Function, ReturnConvertible) {
776 struct CDerived : CBase {};
778 Function<double()> f1 = []() -> int { return 5; };
779 EXPECT_EQ(5.0, f1());
781 Function<int()> f2 = []() -> double { return 5.2; };
787 Function<CBase const&()> f3 = [&]() -> CDerived const& { return derived; };
788 EXPECT_EQ(55, f3().x);
790 Function<CBase const&()> f4 = [&]() -> CDerived& { return derived; };
791 EXPECT_EQ(55, f4().x);
793 Function<CBase&()> f5 = [&]() -> CDerived& { return derived; };
794 EXPECT_EQ(55, f5().x);
796 Function<CBase const*()> f6 = [&]() -> CDerived const* { return &derived; };
797 EXPECT_EQ(f6()->x, 55);
799 Function<CBase const*()> f7 = [&]() -> CDerived* { return &derived; };
800 EXPECT_EQ(55, f7()->x);
802 Function<CBase*()> f8 = [&]() -> CDerived* { return &derived; };
803 EXPECT_EQ(55, f8()->x);
805 Function<CBase()> f9 = [&]() -> CDerived {
810 EXPECT_EQ(66, f9().x);
813 TEST(Function, ConvertReturnType) {
817 struct CDerived : CBase {};
819 Function<int()> f1 = []() -> int { return 5; };
820 Function<double()> cf1 = std::move(f1);
821 EXPECT_EQ(5.0, cf1());
822 Function<int()> ccf1 = std::move(cf1);
823 EXPECT_EQ(5, ccf1());
825 Function<double()> f2 = []() -> double { return 5.2; };
826 Function<int()> cf2 = std::move(f2);
828 Function<double()> ccf2 = std::move(cf2);
829 EXPECT_EQ(5.0, ccf2());
834 Function<CDerived const&()> f3 = [&]() -> CDerived const& { return derived; };
835 Function<CBase const&()> cf3 = std::move(f3);
836 EXPECT_EQ(55, cf3().x);
838 Function<CDerived&()> f4 = [&]() -> CDerived& { return derived; };
839 Function<CBase const&()> cf4 = std::move(f4);
840 EXPECT_EQ(55, cf4().x);
842 Function<CDerived&()> f5 = [&]() -> CDerived& { return derived; };
843 Function<CBase&()> cf5 = std::move(f5);
844 EXPECT_EQ(55, cf5().x);
846 Function<CDerived const*()> f6 = [&]() -> CDerived const* {
849 Function<CBase const*()> cf6 = std::move(f6);
850 EXPECT_EQ(55, cf6()->x);
852 Function<CDerived const*()> f7 = [&]() -> CDerived* { return &derived; };
853 Function<CBase const*()> cf7 = std::move(f7);
854 EXPECT_EQ(55, cf7()->x);
856 Function<CDerived*()> f8 = [&]() -> CDerived* { return &derived; };
857 Function<CBase*()> cf8 = std::move(f8);
858 EXPECT_EQ(55, cf8()->x);
860 Function<CDerived()> f9 = [&]() -> CDerived {
865 Function<CBase()> cf9 = std::move(f9);
866 EXPECT_EQ(66, cf9().x);
869 // TEST =====================================================================
872 TEST(Function, asStdFunction_void) {
874 folly::Function<void()> f = [&] { ++i; };
875 auto sf = std::move(f).asStdFunction();
876 static_assert(std::is_same<decltype(sf), std::function<void()>>::value,
877 "std::function has wrong type");
882 TEST(Function, asStdFunction_void_const) {
884 folly::Function<void() const> f = [&] { ++i; };
885 auto sf = std::move(f).asStdFunction();
886 static_assert(std::is_same<decltype(sf), std::function<void()>>::value,
887 "std::function has wrong type");
892 TEST(Function, asStdFunction_return) {
894 folly::Function<int()> f = [&] {
898 auto sf = std::move(f).asStdFunction();
899 static_assert(std::is_same<decltype(sf), std::function<int()>>::value,
900 "std::function has wrong type");
905 TEST(Function, asStdFunction_return_const) {
907 folly::Function<int() const> f = [&] {
911 auto sf = std::move(f).asStdFunction();
912 static_assert(std::is_same<decltype(sf), std::function<int()>>::value,
913 "std::function has wrong type");
918 TEST(Function, asStdFunction_args) {
920 folly::Function<void(int, int)> f = [&](int x, int y) {
924 auto sf = std::move(f).asStdFunction();
925 static_assert(std::is_same<decltype(sf), std::function<void(int, int)>>::value,
926 "std::function has wrong type");
931 TEST(Function, asStdFunction_args_const) {
933 folly::Function<void(int, int) const> f = [&](int x, int y) {
937 auto sf = std::move(f).asStdFunction();
938 static_assert(std::is_same<decltype(sf), std::function<void(int, int)>>::value,
939 "std::function has wrong type");
944 // TEST =====================================================================
947 TEST(Function, asSharedProxy_void) {
949 folly::Function<void()> f = [&i] { ++i; };
950 auto sp = std::move(f).asSharedProxy();
958 TEST(Function, asSharedProxy_void_const) {
960 folly::Function<void() const> f = [&i] { ++i; };
961 auto sp = std::move(f).asSharedProxy();
969 TEST(Function, asSharedProxy_return) {
970 folly::Function<int()> f = [i = 0]() mutable {
974 auto sp = std::move(f).asSharedProxy();
977 EXPECT_EQ(2, spcopy());
980 TEST(Function, asSharedProxy_return_const) {
982 folly::Function<int() const> f = [&i] {
986 auto sp = std::move(f).asSharedProxy();
989 EXPECT_EQ(2, spcopy());
992 TEST(Function, asSharedProxy_args) {
994 folly::Function<int(int, int)> f = [&](int x, int y) mutable {
998 auto sp = std::move(f).asSharedProxy();
1000 EXPECT_EQ(120, sp(100, 10));
1002 EXPECT_EQ(120, spcopy(100, 10));
1006 TEST(Function, asSharedProxy_args_const) {
1008 folly::Function<int(int, int) const> f = [&i](int x, int y) {
1010 return x * 100 + y * 10 + i;
1012 auto sp = std::move(f).asSharedProxy();
1014 EXPECT_EQ(561, sp(5, 6));
1015 EXPECT_EQ(562, spcopy(5, 6));
1018 TEST(Function, NoAllocatedMemoryAfterMove) {
1019 Functor<int, 100> foo;
1021 Function<int(size_t)> func = foo;
1022 EXPECT_TRUE(func.hasAllocatedMemory());
1024 Function<int(size_t)> func2 = std::move(func);
1025 EXPECT_TRUE(func2.hasAllocatedMemory());
1026 EXPECT_FALSE(func.hasAllocatedMemory());
1029 TEST(Function, ConstCastEmbedded) {
1031 auto functor = [&x]() { ++x; };
1033 Function<void() const> func(functor);
1034 EXPECT_FALSE(func.hasAllocatedMemory());
1036 Function<void()> func2(std::move(func));
1037 EXPECT_FALSE(func2.hasAllocatedMemory());
1040 TEST(Function, EmptyAfterConstCast) {
1041 Function<int(size_t)> func;
1044 Function<int(size_t) const> func2 = constCastFunction(std::move(func));
1045 EXPECT_FALSE(func2);
1048 TEST(Function, SelfStdSwap) {
1049 Function<int()> f = [] { return 42; };
1051 EXPECT_TRUE(bool(f));
1054 EXPECT_TRUE(bool(f));
1057 EXPECT_TRUE(bool(f));
1061 TEST(Function, SelfMove) {
1062 Function<int()> f = [] { return 42; };
1063 Function<int()>& g = f;
1064 f = std::move(g); // shouldn't crash!
1065 (void)bool(f); // valid but unspecified state
1066 f = [] { return 43; };
1067 EXPECT_TRUE(bool(f));
1071 TEST(Function, DeducableArguments) {
1072 deduceArgs(Function<void()>{[] {}});
1073 deduceArgs(Function<void(int, float)>{[](int, float) {}});
1074 deduceArgs(Function<int(int, float)>{[](int i, float) { return i; }});
1077 TEST(Function, CtorWithCopy) {
1080 X(X const&) noexcept(true) {}
1081 X& operator=(X const&) = default;
1085 Y(Y const&) noexcept(false) {}
1086 Y(Y&&) noexcept(true) {}
1087 Y& operator=(Y&&) = default;
1088 Y& operator=(Y const&) = default;
1090 auto lx = [x = X()]{};
1091 auto ly = [y = Y()]{};
1092 EXPECT_TRUE(noexcept(Function<void()>(lx)));
1093 EXPECT_FALSE(noexcept(Function<void()>(ly)));