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 = std::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 {
329 std::move(unique_ptr_int));
331 EXPECT_EQ(901, functor());
333 Function<int(void)> func = std::move(functor);
334 EXPECT_TRUE(func.hasAllocatedMemory());
336 EXPECT_EQ(902, func());
339 // TEST =====================================================================
342 TEST(Function, OverloadedFunctor) {
343 struct OverloadedFunctor {
345 int operator()(int x) {
349 // variant 2 (const-overload of v1)
350 int operator()(int x) const {
355 int operator()(int x, int) {
359 // variant 4 (const-overload of v3)
360 int operator()(int x, int) const {
364 // variant 5 (non-const, has no const-overload)
365 int operator()(int x, char const*) {
369 // variant 6 (const only)
370 int operator()(int x, std::vector<int> const&) const {
374 OverloadedFunctor of;
376 Function<int(int)> variant1 = of;
377 EXPECT_EQ(100 + 1 * 15, variant1(15));
379 Function<int(int) const> variant2 = of;
380 EXPECT_EQ(100 + 2 * 16, variant2(16));
382 Function<int(int, int)> variant3 = of;
383 EXPECT_EQ(100 + 3 * 17, variant3(17, 0));
385 Function<int(int, int) const> variant4 = of;
386 EXPECT_EQ(100 + 4 * 18, variant4(18, 0));
388 Function<int(int, char const*)> variant5 = of;
389 EXPECT_EQ(100 + 5 * 19, variant5(19, "foo"));
391 Function<int(int, std::vector<int> const&)> variant6 = of;
392 EXPECT_EQ(100 + 6 * 20, variant6(20, {}));
393 EXPECT_EQ(100 + 6 * 20, variant6(20, {1, 2, 3}));
395 Function<int(int, std::vector<int> const&) const> variant6const = of;
396 EXPECT_EQ(100 + 6 * 21, variant6const(21, {}));
398 // Cast const-functions to non-const and the other way around: if the functor
399 // has both const and non-const operator()s for a given parameter signature,
400 // constructing a Function must select one of them, depending on
401 // whether the function type template parameter is const-qualified or not.
402 // When the const-ness is later changed (by moving the
403 // Function<R(Args...)const> into a Function<R(Args...)> or by
404 // calling the folly::constCastFunction which moves it into a
405 // Function<R(Args...)const>), the Function must still execute
406 // the initially selected function.
408 auto variant1_const = folly::constCastFunction(std::move(variant1));
409 EXPECT_THROW(variant1(0), std::bad_function_call);
410 EXPECT_EQ(100 + 1 * 22, variant1_const(22));
412 Function<int(int)> variant2_nonconst = std::move(variant2);
413 EXPECT_THROW(variant2(0), std::bad_function_call);
414 EXPECT_EQ(100 + 2 * 23, variant2_nonconst(23));
416 auto variant3_const = folly::constCastFunction(std::move(variant3));
417 EXPECT_THROW(variant3(0, 0), std::bad_function_call);
418 EXPECT_EQ(100 + 3 * 24, variant3_const(24, 0));
420 Function<int(int, int)> variant4_nonconst = std::move(variant4);
421 EXPECT_THROW(variant4(0, 0), std::bad_function_call);
422 EXPECT_EQ(100 + 4 * 25, variant4_nonconst(25, 0));
424 auto variant5_const = folly::constCastFunction(std::move(variant5));
425 EXPECT_THROW(variant5(0, ""), std::bad_function_call);
426 EXPECT_EQ(100 + 5 * 26, variant5_const(26, "foo"));
428 auto variant6_const = folly::constCastFunction(std::move(variant6));
429 EXPECT_THROW(variant6(0, {}), std::bad_function_call);
430 EXPECT_EQ(100 + 6 * 27, variant6_const(27, {}));
432 Function<int(int, std::vector<int> const&)> variant6const_nonconst =
433 std::move(variant6const);
434 EXPECT_THROW(variant6const(0, {}), std::bad_function_call);
435 EXPECT_EQ(100 + 6 * 28, variant6const_nonconst(28, {}));
438 // TEST =====================================================================
441 TEST(Function, Lambda) {
442 // Non-mutable lambdas: can be stored in a non-const...
443 Function<int(int)> func = [](int x) { return 1000 + x; };
444 EXPECT_EQ(1001, func(1));
446 // ...as well as in a const Function
447 Function<int(int) const> func_const = [](int x) { return 2000 + x; };
448 EXPECT_EQ(2001, func_const(1));
450 // Mutable lambda: can only be stored in a const Function:
452 Function<int()> func_mutable = [number]() mutable { return ++number; };
453 EXPECT_EQ(3001, func_mutable());
454 EXPECT_EQ(3002, func_mutable());
456 // test after const-casting
458 Function<int(int) const> func_made_const =
459 folly::constCastFunction(std::move(func));
460 EXPECT_EQ(1002, func_made_const(2));
461 EXPECT_THROW(func(0), std::bad_function_call);
463 Function<int(int)> func_const_made_nonconst = std::move(func_const);
464 EXPECT_EQ(2002, func_const_made_nonconst(2));
465 EXPECT_THROW(func_const(0), std::bad_function_call);
467 Function<int() const> func_mutable_made_const =
468 folly::constCastFunction(std::move(func_mutable));
469 EXPECT_EQ(3003, func_mutable_made_const());
470 EXPECT_EQ(3004, func_mutable_made_const());
471 EXPECT_THROW(func_mutable(), std::bad_function_call);
474 // TEST =====================================================================
475 // DataMember & MemberFunction
487 TEST(Function, DataMember) {
489 MemberFunc const& cmf = mf;
492 Function<int(MemberFunc const*)> data_getter1 = &MemberFunc::x;
493 EXPECT_EQ(123, data_getter1(&cmf));
494 Function<int(MemberFunc*)> data_getter2 = &MemberFunc::x;
495 EXPECT_EQ(123, data_getter2(&mf));
496 Function<int(MemberFunc const&)> data_getter3 = &MemberFunc::x;
497 EXPECT_EQ(123, data_getter3(cmf));
498 Function<int(MemberFunc&)> data_getter4 = &MemberFunc::x;
499 EXPECT_EQ(123, data_getter4(mf));
502 TEST(Function, MemberFunction) {
504 MemberFunc const& cmf = mf;
507 Function<int(MemberFunc const*)> getter1 = &MemberFunc::getX;
508 EXPECT_EQ(123, getter1(&cmf));
509 Function<int(MemberFunc*)> getter2 = &MemberFunc::getX;
510 EXPECT_EQ(123, getter2(&mf));
511 Function<int(MemberFunc const&)> getter3 = &MemberFunc::getX;
512 EXPECT_EQ(123, getter3(cmf));
513 Function<int(MemberFunc&)> getter4 = &MemberFunc::getX;
514 EXPECT_EQ(123, getter4(mf));
516 Function<void(MemberFunc*, int)> setter1 = &MemberFunc::setX;
518 EXPECT_EQ(234, mf.x);
520 Function<void(MemberFunc&, int)> setter2 = &MemberFunc::setX;
522 EXPECT_EQ(345, mf.x);
525 // TEST =====================================================================
526 // CaptureCopyMoveCount & ParameterCopyMoveCount
528 class CopyMoveTracker {
530 struct ConstructorTag {};
532 CopyMoveTracker() = delete;
533 explicit CopyMoveTracker(ConstructorTag)
534 : data_(std::make_shared<std::pair<size_t, size_t>>(0, 0)) {}
536 CopyMoveTracker(CopyMoveTracker const& o) noexcept : data_(o.data_) {
539 CopyMoveTracker& operator=(CopyMoveTracker const& o) noexcept {
545 CopyMoveTracker(CopyMoveTracker&& o) noexcept : data_(o.data_) {
548 CopyMoveTracker& operator=(CopyMoveTracker&& o) noexcept {
554 size_t copyCount() const {
557 size_t moveCount() const {
558 return data_->second;
560 size_t refCount() const {
561 return data_.use_count();
563 void resetCounters() {
564 data_->first = data_->second = 0;
569 std::shared_ptr<std::pair<size_t, size_t>> data_;
572 TEST(Function, CaptureCopyMoveCount) {
573 // This test checks that no unnecessary copies/moves are made.
575 CopyMoveTracker cmt(CopyMoveTracker::ConstructorTag{});
576 EXPECT_EQ(0, cmt.copyCount());
577 EXPECT_EQ(0, cmt.moveCount());
578 EXPECT_EQ(1, cmt.refCount());
580 // Move into lambda, move lambda into Function
581 auto lambda1 = [cmt = std::move(cmt)]() {
582 return cmt.moveCount();
584 Function<size_t(void)> uf1 = std::move(lambda1);
586 // Max copies: 0. Max copy+moves: 2.
587 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
588 EXPECT_LE(cmt.copyCount(), 0);
592 // Move into lambda, copy lambda into Function
593 auto lambda2 = [cmt = std::move(cmt)]() {
594 return cmt.moveCount();
596 Function<size_t(void)> uf2 = lambda2;
598 // Max copies: 1. Max copy+moves: 2.
599 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
600 EXPECT_LE(cmt.copyCount(), 1);
602 // Invoking Function must not make copies/moves of the callable
606 EXPECT_EQ(0, cmt.copyCount());
607 EXPECT_EQ(0, cmt.moveCount());
610 TEST(Function, ParameterCopyMoveCount) {
611 // This test checks that no unnecessary copies/moves are made.
613 CopyMoveTracker cmt(CopyMoveTracker::ConstructorTag{});
614 EXPECT_EQ(0, cmt.copyCount());
615 EXPECT_EQ(0, cmt.moveCount());
616 EXPECT_EQ(1, cmt.refCount());
619 Function<size_t(CopyMoveTracker)> uf1 = [](CopyMoveTracker c) {
620 return c.moveCount();
625 // Max copies: 1. Max copy+moves: 2.
626 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
627 EXPECT_LE(cmt.copyCount(), 1);
631 // Max copies: 1. Max copy+moves: 2.
632 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
633 EXPECT_LE(cmt.copyCount(), 0);
636 Function<size_t(CopyMoveTracker&)> uf2 = [](CopyMoveTracker& c) {
637 return c.moveCount();
642 // Max copies: 0. Max copy+moves: 0.
643 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
644 EXPECT_LE(cmt.copyCount(), 0);
646 // pass by const reference
647 Function<size_t(CopyMoveTracker const&)> uf3 = [](CopyMoveTracker const& c) {
648 return c.moveCount();
653 // Max copies: 0. Max copy+moves: 0.
654 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
655 EXPECT_LE(cmt.copyCount(), 0);
657 // pass by rvalue reference
658 Function<size_t(CopyMoveTracker &&)> uf4 = [](CopyMoveTracker&& c) {
659 return c.moveCount();
664 // Max copies: 0. Max copy+moves: 0.
665 EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
666 EXPECT_LE(cmt.copyCount(), 0);
669 // TEST =====================================================================
670 // VariadicTemplate & VariadicArguments
672 struct VariadicTemplateSum {
673 int operator()() const {
676 template <class... Args>
677 int operator()(int x, Args... args) const {
678 return x + (*this)(args...);
682 TEST(Function, VariadicTemplate) {
683 Function<int(int)> uf1 = VariadicTemplateSum();
684 Function<int(int, int)> uf2 = VariadicTemplateSum();
685 Function<int(int, int, int)> uf3 = VariadicTemplateSum();
687 EXPECT_EQ(66, uf1(66));
688 EXPECT_EQ(99, uf2(55, 44));
689 EXPECT_EQ(66, uf3(33, 22, 11));
692 struct VariadicArgumentsSum {
693 int operator()(int count, ...) const {
696 va_start(args, count);
697 for (int i = 0; i < count; ++i) {
698 result += va_arg(args, int);
705 TEST(Function, VariadicArguments) {
706 Function<int(int)> uf1 = VariadicArgumentsSum();
707 Function<int(int, int)> uf2 = VariadicArgumentsSum();
708 Function<int(int, int, int)> uf3 = VariadicArgumentsSum();
710 EXPECT_EQ(0, uf1(0));
711 EXPECT_EQ(66, uf2(1, 66));
712 EXPECT_EQ(99, uf3(2, 55, 44));
715 // TEST =====================================================================
716 // SafeCaptureByReference
718 // A function can use Function const& as a parameter to signal that it
719 // is safe to pass a lambda that captures local variables by reference.
720 // It is safe because we know the function called can only invoke the
721 // Function until it returns. It can't store a copy of the Function
722 // (because it's not copyable), and it can't move the Function somewhere
723 // else (because it gets only a const&).
725 template <typename T>
728 Function<void(typename T::value_type const&) const> const& func) {
729 for (auto const& elem : range) {
734 TEST(Function, SafeCaptureByReference) {
735 std::vector<int> const vec = {20, 30, 40, 2, 3, 4, 200, 300, 400};
739 // for_each's second parameter is of type Function<...> const&.
740 // Hence we know we can safely pass it a lambda that references local
741 // variables. There is no way the reference to x will be stored anywhere.
742 for_each<std::vector<int>>(vec, [&sum](int x) { sum += x; });
744 // gcc versions before 4.9 cannot deduce the type T in the above call
745 // to for_each. Modern compiler versions can compile the following line:
746 // for_each(vec, [&sum](int x) { sum += x; });
751 // TEST =====================================================================
754 TEST(Function, IgnoreReturnValue) {
757 // Assign a lambda that return int to a folly::Function that returns void.
758 Function<void()> f = [&]() -> int { return ++x; };
764 Function<int()> g = [&]() -> int { return ++x; };
765 Function<void()> cg = std::move(g);
772 // TEST =====================================================================
773 // ReturnConvertible, ConvertReturnType
775 TEST(Function, ReturnConvertible) {
779 struct CDerived : CBase {};
781 Function<double()> f1 = []() -> int { return 5; };
782 EXPECT_EQ(5.0, f1());
784 Function<int()> f2 = []() -> double { return 5.2; };
790 Function<CBase const&()> f3 = [&]() -> CDerived const& { return derived; };
791 EXPECT_EQ(55, f3().x);
793 Function<CBase const&()> f4 = [&]() -> CDerived& { return derived; };
794 EXPECT_EQ(55, f4().x);
796 Function<CBase&()> f5 = [&]() -> CDerived& { return derived; };
797 EXPECT_EQ(55, f5().x);
799 Function<CBase const*()> f6 = [&]() -> CDerived const* { return &derived; };
800 EXPECT_EQ(f6()->x, 55);
802 Function<CBase const*()> f7 = [&]() -> CDerived* { return &derived; };
803 EXPECT_EQ(55, f7()->x);
805 Function<CBase*()> f8 = [&]() -> CDerived* { return &derived; };
806 EXPECT_EQ(55, f8()->x);
808 Function<CBase()> f9 = [&]() -> CDerived {
813 EXPECT_EQ(66, f9().x);
816 TEST(Function, ConvertReturnType) {
820 struct CDerived : CBase {};
822 Function<int()> f1 = []() -> int { return 5; };
823 Function<double()> cf1 = std::move(f1);
824 EXPECT_EQ(5.0, cf1());
825 Function<int()> ccf1 = std::move(cf1);
826 EXPECT_EQ(5, ccf1());
828 Function<double()> f2 = []() -> double { return 5.2; };
829 Function<int()> cf2 = std::move(f2);
831 Function<double()> ccf2 = std::move(cf2);
832 EXPECT_EQ(5.0, ccf2());
837 Function<CDerived const&()> f3 = [&]() -> CDerived const& { return derived; };
838 Function<CBase const&()> cf3 = std::move(f3);
839 EXPECT_EQ(55, cf3().x);
841 Function<CDerived&()> f4 = [&]() -> CDerived& { return derived; };
842 Function<CBase const&()> cf4 = std::move(f4);
843 EXPECT_EQ(55, cf4().x);
845 Function<CDerived&()> f5 = [&]() -> CDerived& { return derived; };
846 Function<CBase&()> cf5 = std::move(f5);
847 EXPECT_EQ(55, cf5().x);
849 Function<CDerived const*()> f6 = [&]() -> CDerived const* {
852 Function<CBase const*()> cf6 = std::move(f6);
853 EXPECT_EQ(55, cf6()->x);
855 Function<CDerived const*()> f7 = [&]() -> CDerived* { return &derived; };
856 Function<CBase const*()> cf7 = std::move(f7);
857 EXPECT_EQ(55, cf7()->x);
859 Function<CDerived*()> f8 = [&]() -> CDerived* { return &derived; };
860 Function<CBase*()> cf8 = std::move(f8);
861 EXPECT_EQ(55, cf8()->x);
863 Function<CDerived()> f9 = [&]() -> CDerived {
868 Function<CBase()> cf9 = std::move(f9);
869 EXPECT_EQ(66, cf9().x);
872 // TEST =====================================================================
875 TEST(Function, asStdFunction_void) {
877 folly::Function<void()> f = [&] { ++i; };
878 auto sf = std::move(f).asStdFunction();
879 static_assert(std::is_same<decltype(sf), std::function<void()>>::value,
880 "std::function has wrong type");
885 TEST(Function, asStdFunction_void_const) {
887 folly::Function<void() const> f = [&] { ++i; };
888 auto sf = std::move(f).asStdFunction();
889 static_assert(std::is_same<decltype(sf), std::function<void()>>::value,
890 "std::function has wrong type");
895 TEST(Function, asStdFunction_return) {
897 folly::Function<int()> f = [&] {
901 auto sf = std::move(f).asStdFunction();
902 static_assert(std::is_same<decltype(sf), std::function<int()>>::value,
903 "std::function has wrong type");
908 TEST(Function, asStdFunction_return_const) {
910 folly::Function<int() const> f = [&] {
914 auto sf = std::move(f).asStdFunction();
915 static_assert(std::is_same<decltype(sf), std::function<int()>>::value,
916 "std::function has wrong type");
921 TEST(Function, asStdFunction_args) {
923 folly::Function<void(int, int)> f = [&](int x, int y) {
927 auto sf = std::move(f).asStdFunction();
928 static_assert(std::is_same<decltype(sf), std::function<void(int, int)>>::value,
929 "std::function has wrong type");
934 TEST(Function, asStdFunction_args_const) {
936 folly::Function<void(int, int) const> f = [&](int x, int y) {
940 auto sf = std::move(f).asStdFunction();
941 static_assert(std::is_same<decltype(sf), std::function<void(int, int)>>::value,
942 "std::function has wrong type");
947 // TEST =====================================================================
950 TEST(Function, asSharedProxy_void) {
952 folly::Function<void()> f = [&i] { ++i; };
953 auto sp = std::move(f).asSharedProxy();
961 TEST(Function, asSharedProxy_void_const) {
963 folly::Function<void() const> f = [&i] { ++i; };
964 auto sp = std::move(f).asSharedProxy();
972 TEST(Function, asSharedProxy_return) {
973 folly::Function<int()> f = [i = 0]() mutable {
977 auto sp = std::move(f).asSharedProxy();
980 EXPECT_EQ(2, spcopy());
983 TEST(Function, asSharedProxy_return_const) {
985 folly::Function<int() const> f = [&i] {
989 auto sp = std::move(f).asSharedProxy();
992 EXPECT_EQ(2, spcopy());
995 TEST(Function, asSharedProxy_args) {
997 folly::Function<int(int, int)> f = [&](int x, int y) mutable {
1001 auto sp = std::move(f).asSharedProxy();
1003 EXPECT_EQ(120, sp(100, 10));
1005 EXPECT_EQ(120, spcopy(100, 10));
1009 TEST(Function, asSharedProxy_args_const) {
1011 folly::Function<int(int, int) const> f = [&i](int x, int y) {
1013 return x * 100 + y * 10 + i;
1015 auto sp = std::move(f).asSharedProxy();
1017 EXPECT_EQ(561, sp(5, 6));
1018 EXPECT_EQ(562, spcopy(5, 6));
1021 TEST(Function, NoAllocatedMemoryAfterMove) {
1022 Functor<int, 100> foo;
1024 Function<int(size_t)> func = foo;
1025 EXPECT_TRUE(func.hasAllocatedMemory());
1027 Function<int(size_t)> func2 = std::move(func);
1028 EXPECT_TRUE(func2.hasAllocatedMemory());
1029 EXPECT_FALSE(func.hasAllocatedMemory());
1032 TEST(Function, ConstCastEmbedded) {
1034 auto functor = [&x]() { ++x; };
1036 Function<void() const> func(functor);
1037 EXPECT_FALSE(func.hasAllocatedMemory());
1039 Function<void()> func2(std::move(func));
1040 EXPECT_FALSE(func2.hasAllocatedMemory());
1043 TEST(Function, EmptyAfterConstCast) {
1044 Function<int(size_t)> func;
1047 Function<int(size_t) const> func2 = constCastFunction(std::move(func));
1048 EXPECT_FALSE(func2);
1051 TEST(Function, SelfStdSwap) {
1052 Function<int()> f = [] { return 42; };
1054 EXPECT_TRUE(bool(f));
1057 EXPECT_TRUE(bool(f));
1060 EXPECT_TRUE(bool(f));
1064 TEST(Function, SelfMove) {
1065 Function<int()> f = [] { return 42; };
1066 Function<int()>& g = f;
1067 f = std::move(g); // shouldn't crash!
1068 (void)bool(f); // valid but unspecified state
1069 f = [] { return 43; };
1070 EXPECT_TRUE(bool(f));
1074 TEST(Function, DeducableArguments) {
1075 deduceArgs(Function<void()>{[] {}});
1076 deduceArgs(Function<void(int, float)>{[](int, float) {}});
1077 deduceArgs(Function<int(int, float)>{[](int i, float) { return i; }});
1080 TEST(Function, CtorWithCopy) {
1083 X(X const&) noexcept(true) {}
1084 X& operator=(X const&) = default;
1088 Y(Y const&) noexcept(false) {}
1089 Y(Y&&) noexcept(true) {}
1090 Y& operator=(Y&&) = default;
1091 Y& operator=(Y const&) = default;
1093 auto lx = [x = X()]{};
1094 auto ly = [y = Y()]{};
1095 EXPECT_TRUE(noexcept(Function<void()>(lx)));
1096 EXPECT_FALSE(noexcept(Function<void()>(ly)));