Use the GTest portability headers
[folly.git] / folly / test / FunctionTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <array>
18 #include <cstdarg>
19
20 #include <folly/Function.h>
21
22 #include <folly/Memory.h>
23 #include <folly/portability/GTest.h>
24
25 using folly::Function;
26
27 namespace {
28 int func_int_int_add_25(int x) {
29   return x + 25;
30 }
31 int func_int_int_add_111(int x) {
32   return x + 111;
33 }
34 float floatMult(float a, float b) {
35   return a * b;
36 }
37
38 template <class T, size_t S>
39 struct Functor {
40   std::array<T, S> data = {{0}};
41
42   // Two operator() with different argument types.
43   // The InvokeReference tests use both
44   T const& operator()(size_t index) const {
45     return data[index];
46   }
47   T operator()(size_t index, T const& value) {
48     T oldvalue = data[index];
49     data[index] = value;
50     return oldvalue;
51   }
52 };
53
54 template <typename Ret, typename... Args>
55 void deduceArgs(Function<Ret(Args...)>) {}
56
57 } // namespace
58
59 // TEST =====================================================================
60 // InvokeFunctor & InvokeReference
61
62 TEST(Function, InvokeFunctor) {
63   Functor<int, 100> func;
64   static_assert(
65       sizeof(func) > sizeof(Function<int(size_t)>),
66       "sizeof(Function) is much larger than expected");
67   func(5, 123);
68
69   Function<int(size_t) const> getter = std::move(func);
70
71   // Function will allocate memory on the heap to store the functor object
72   EXPECT_TRUE(getter.hasAllocatedMemory());
73
74   EXPECT_EQ(123, getter(5));
75 }
76
77 TEST(Function, InvokeReference) {
78   Functor<int, 10> func;
79   func(5, 123);
80
81   // Have Functions for getter and setter, both referencing the same funtor
82   Function<int(size_t) const> getter = std::ref(func);
83   Function<int(size_t, int)> setter = std::ref(func);
84
85   EXPECT_EQ(123, getter(5));
86   EXPECT_EQ(123, setter(5, 456));
87   EXPECT_EQ(456, setter(5, 567));
88   EXPECT_EQ(567, getter(5));
89 }
90
91 // TEST =====================================================================
92 // Emptiness
93
94 TEST(Function, Emptiness_T) {
95   Function<int(int)> f;
96   EXPECT_EQ(f, nullptr);
97   EXPECT_EQ(nullptr, f);
98   EXPECT_FALSE(f);
99   EXPECT_THROW(f(98), std::bad_function_call);
100
101   Function<int(int)> g([](int x) { return x + 1; });
102   EXPECT_NE(g, nullptr);
103   EXPECT_NE(nullptr, g);
104   // Explicitly convert to bool to work around
105   // https://github.com/google/googletest/issues/429
106   EXPECT_TRUE(bool(g));
107   EXPECT_EQ(100, g(99));
108
109   Function<int(int)> h(&func_int_int_add_25);
110   EXPECT_NE(h, nullptr);
111   EXPECT_NE(nullptr, h);
112   EXPECT_TRUE(bool(h));
113   EXPECT_EQ(125, h(100));
114
115   h = {};
116   EXPECT_EQ(h, nullptr);
117   EXPECT_EQ(nullptr, h);
118   EXPECT_FALSE(h);
119   EXPECT_THROW(h(101), std::bad_function_call);
120 }
121
122 // TEST =====================================================================
123 // Swap
124
125 template <bool UseSwapMethod>
126 void swap_test() {
127   Function<int(int)> mf1(func_int_int_add_25);
128   Function<int(int)> mf2(func_int_int_add_111);
129
130   EXPECT_EQ(125, mf1(100));
131   EXPECT_EQ(211, mf2(100));
132
133   if (UseSwapMethod) {
134     mf1.swap(mf2);
135   } else {
136     swap(mf1, mf2);
137   }
138
139   EXPECT_EQ(125, mf2(100));
140   EXPECT_EQ(211, mf1(100));
141
142   Function<int(int)> mf3(nullptr);
143   EXPECT_EQ(mf3, nullptr);
144
145   if (UseSwapMethod) {
146     mf1.swap(mf3);
147   } else {
148     swap(mf1, mf3);
149   }
150
151   EXPECT_EQ(211, mf3(100));
152   EXPECT_EQ(nullptr, mf1);
153
154   Function<int(int)> mf4([](int x) { return x + 222; });
155   EXPECT_EQ(322, mf4(100));
156
157   if (UseSwapMethod) {
158     mf4.swap(mf3);
159   } else {
160     swap(mf4, mf3);
161   }
162   EXPECT_EQ(211, mf4(100));
163   EXPECT_EQ(322, mf3(100));
164
165   if (UseSwapMethod) {
166     mf3.swap(mf1);
167   } else {
168     swap(mf3, mf1);
169   }
170   EXPECT_EQ(nullptr, mf3);
171   EXPECT_EQ(322, mf1(100));
172 }
173 TEST(Function, SwapMethod) {
174   swap_test<true>();
175 }
176 TEST(Function, SwapFunction) {
177   swap_test<false>();
178 }
179
180 // TEST =====================================================================
181 // Bind
182
183 TEST(Function, Bind) {
184   Function<float(float, float)> fnc = floatMult;
185   auto task = std::bind(std::move(fnc), 2.f, 4.f);
186   EXPECT_THROW(fnc(0, 0), std::bad_function_call);
187   EXPECT_EQ(8, task());
188   auto task2(std::move(task));
189   EXPECT_THROW(task(), std::bad_function_call);
190   EXPECT_EQ(8, task2());
191 }
192
193 // TEST =====================================================================
194 // NonCopyableLambda
195
196 TEST(Function, NonCopyableLambda) {
197   auto unique_ptr_int = folly::make_unique<int>(900);
198   EXPECT_EQ(900, *unique_ptr_int);
199
200   char fooData[64] = {0};
201   EXPECT_EQ(0, fooData[0]); // suppress gcc warning about fooData not being used
202
203   auto functor = std::bind(
204       [fooData](std::unique_ptr<int>& up) mutable { return ++*up; },
205       std::move(unique_ptr_int));
206
207   EXPECT_EQ(901, functor());
208
209   Function<int(void)> func = std::move(functor);
210   EXPECT_TRUE(func.hasAllocatedMemory());
211
212   EXPECT_EQ(902, func());
213 }
214
215 // TEST =====================================================================
216 // OverloadedFunctor
217
218 TEST(Function, OverloadedFunctor) {
219   struct OverloadedFunctor {
220     // variant 1
221     int operator()(int x) {
222       return 100 + 1 * x;
223     }
224
225     // variant 2 (const-overload of v1)
226     int operator()(int x) const {
227       return 100 + 2 * x;
228     }
229
230     // variant 3
231     int operator()(int x, int) {
232       return 100 + 3 * x;
233     }
234
235     // variant 4 (const-overload of v3)
236     int operator()(int x, int) const {
237       return 100 + 4 * x;
238     }
239
240     // variant 5 (non-const, has no const-overload)
241     int operator()(int x, char const*) {
242       return 100 + 5 * x;
243     }
244
245     // variant 6 (const only)
246     int operator()(int x, std::vector<int> const&) const {
247       return 100 + 6 * x;
248     }
249   };
250   OverloadedFunctor of;
251
252   Function<int(int)> variant1 = of;
253   EXPECT_EQ(100 + 1 * 15, variant1(15));
254
255   Function<int(int) const> variant2 = of;
256   EXPECT_EQ(100 + 2 * 16, variant2(16));
257
258   Function<int(int, int)> variant3 = of;
259   EXPECT_EQ(100 + 3 * 17, variant3(17, 0));
260
261   Function<int(int, int) const> variant4 = of;
262   EXPECT_EQ(100 + 4 * 18, variant4(18, 0));
263
264   Function<int(int, char const*)> variant5 = of;
265   EXPECT_EQ(100 + 5 * 19, variant5(19, "foo"));
266
267   Function<int(int, std::vector<int> const&)> variant6 = of;
268   EXPECT_EQ(100 + 6 * 20, variant6(20, {}));
269   EXPECT_EQ(100 + 6 * 20, variant6(20, {1, 2, 3}));
270
271   Function<int(int, std::vector<int> const&) const> variant6const = of;
272   EXPECT_EQ(100 + 6 * 21, variant6const(21, {}));
273
274   // Cast const-functions to non-const and the other way around: if the functor
275   // has both const and non-const operator()s for a given parameter signature,
276   // constructing a Function must select one of them, depending on
277   // whether the function type template parameter is const-qualified or not.
278   // When the const-ness is later changed (by moving the
279   // Function<R(Args...)const> into a Function<R(Args...)> or by
280   // calling the folly::constCastFunction which moves it into a
281   // Function<R(Args...)const>), the Function must still execute
282   // the initially selected function.
283
284   auto variant1_const = folly::constCastFunction(std::move(variant1));
285   EXPECT_THROW(variant1(0), std::bad_function_call);
286   EXPECT_EQ(100 + 1 * 22, variant1_const(22));
287
288   Function<int(int)> variant2_nonconst = std::move(variant2);
289   EXPECT_THROW(variant2(0), std::bad_function_call);
290   EXPECT_EQ(100 + 2 * 23, variant2_nonconst(23));
291
292   auto variant3_const = folly::constCastFunction(std::move(variant3));
293   EXPECT_THROW(variant3(0, 0), std::bad_function_call);
294   EXPECT_EQ(100 + 3 * 24, variant3_const(24, 0));
295
296   Function<int(int, int)> variant4_nonconst = std::move(variant4);
297   EXPECT_THROW(variant4(0, 0), std::bad_function_call);
298   EXPECT_EQ(100 + 4 * 25, variant4_nonconst(25, 0));
299
300   auto variant5_const = folly::constCastFunction(std::move(variant5));
301   EXPECT_THROW(variant5(0, ""), std::bad_function_call);
302   EXPECT_EQ(100 + 5 * 26, variant5_const(26, "foo"));
303
304   auto variant6_const = folly::constCastFunction(std::move(variant6));
305   EXPECT_THROW(variant6(0, {}), std::bad_function_call);
306   EXPECT_EQ(100 + 6 * 27, variant6_const(27, {}));
307
308   Function<int(int, std::vector<int> const&)> variant6const_nonconst =
309       std::move(variant6const);
310   EXPECT_THROW(variant6const(0, {}), std::bad_function_call);
311   EXPECT_EQ(100 + 6 * 28, variant6const_nonconst(28, {}));
312 }
313
314 // TEST =====================================================================
315 // Lambda
316
317 TEST(Function, Lambda) {
318   // Non-mutable lambdas: can be stored in a non-const...
319   Function<int(int)> func = [](int x) { return 1000 + x; };
320   EXPECT_EQ(1001, func(1));
321
322   // ...as well as in a const Function
323   Function<int(int) const> func_const = [](int x) { return 2000 + x; };
324   EXPECT_EQ(2001, func_const(1));
325
326   // Mutable lambda: can only be stored in a const Function:
327   int number = 3000;
328   Function<int()> func_mutable = [number]() mutable { return ++number; };
329   EXPECT_EQ(3001, func_mutable());
330   EXPECT_EQ(3002, func_mutable());
331
332   // test after const-casting
333
334   Function<int(int) const> func_made_const =
335       folly::constCastFunction(std::move(func));
336   EXPECT_EQ(1002, func_made_const(2));
337   EXPECT_THROW(func(0), std::bad_function_call);
338
339   Function<int(int)> func_const_made_nonconst = std::move(func_const);
340   EXPECT_EQ(2002, func_const_made_nonconst(2));
341   EXPECT_THROW(func_const(0), std::bad_function_call);
342
343   Function<int() const> func_mutable_made_const =
344       folly::constCastFunction(std::move(func_mutable));
345   EXPECT_EQ(3003, func_mutable_made_const());
346   EXPECT_EQ(3004, func_mutable_made_const());
347   EXPECT_THROW(func_mutable(), std::bad_function_call);
348 }
349
350 // TEST =====================================================================
351 // DataMember & MemberFunction
352
353 struct MemberFunc {
354   int x;
355   int getX() const {
356     return x;
357   }
358   void setX(int xx) {
359     x = xx;
360   }
361 };
362
363 TEST(Function, DataMember) {
364   MemberFunc mf;
365   MemberFunc const& cmf = mf;
366   mf.x = 123;
367
368   Function<int(MemberFunc const*)> data_getter1 = &MemberFunc::x;
369   EXPECT_EQ(123, data_getter1(&cmf));
370   Function<int(MemberFunc*)> data_getter2 = &MemberFunc::x;
371   EXPECT_EQ(123, data_getter2(&mf));
372   Function<int(MemberFunc const&)> data_getter3 = &MemberFunc::x;
373   EXPECT_EQ(123, data_getter3(cmf));
374   Function<int(MemberFunc&)> data_getter4 = &MemberFunc::x;
375   EXPECT_EQ(123, data_getter4(mf));
376 }
377
378 TEST(Function, MemberFunction) {
379   MemberFunc mf;
380   MemberFunc const& cmf = mf;
381   mf.x = 123;
382
383   Function<int(MemberFunc const*)> getter1 = &MemberFunc::getX;
384   EXPECT_EQ(123, getter1(&cmf));
385   Function<int(MemberFunc*)> getter2 = &MemberFunc::getX;
386   EXPECT_EQ(123, getter2(&mf));
387   Function<int(MemberFunc const&)> getter3 = &MemberFunc::getX;
388   EXPECT_EQ(123, getter3(cmf));
389   Function<int(MemberFunc&)> getter4 = &MemberFunc::getX;
390   EXPECT_EQ(123, getter4(mf));
391
392   Function<void(MemberFunc*, int)> setter1 = &MemberFunc::setX;
393   setter1(&mf, 234);
394   EXPECT_EQ(234, mf.x);
395
396   Function<void(MemberFunc&, int)> setter2 = &MemberFunc::setX;
397   setter2(mf, 345);
398   EXPECT_EQ(345, mf.x);
399 }
400
401 // TEST =====================================================================
402 // CaptureCopyMoveCount & ParameterCopyMoveCount
403
404 class CopyMoveTracker {
405  public:
406   struct ConstructorTag {};
407
408   CopyMoveTracker() = delete;
409   explicit CopyMoveTracker(ConstructorTag)
410       : data_(std::make_shared<std::pair<size_t, size_t>>(0, 0)) {}
411
412   CopyMoveTracker(CopyMoveTracker const& o) noexcept : data_(o.data_) {
413     ++data_->first;
414   }
415   CopyMoveTracker& operator=(CopyMoveTracker const& o) noexcept {
416     data_ = o.data_;
417     ++data_->first;
418     return *this;
419   }
420
421   CopyMoveTracker(CopyMoveTracker&& o) noexcept : data_(o.data_) {
422     ++data_->second;
423   }
424   CopyMoveTracker& operator=(CopyMoveTracker&& o) noexcept {
425     data_ = o.data_;
426     ++data_->second;
427     return *this;
428   }
429
430   size_t copyCount() const {
431     return data_->first;
432   }
433   size_t moveCount() const {
434     return data_->second;
435   }
436   size_t refCount() const {
437     return data_.use_count();
438   }
439   void resetCounters() {
440     data_->first = data_->second = 0;
441   }
442
443  private:
444   // copy, move
445   std::shared_ptr<std::pair<size_t, size_t>> data_;
446 };
447
448 TEST(Function, CaptureCopyMoveCount) {
449   // This test checks that no unnecessary copies/moves are made.
450
451   CopyMoveTracker cmt(CopyMoveTracker::ConstructorTag{});
452   EXPECT_EQ(0, cmt.copyCount());
453   EXPECT_EQ(0, cmt.moveCount());
454   EXPECT_EQ(1, cmt.refCount());
455
456   // Move into lambda, move lambda into Function
457   auto lambda1 = [cmt = std::move(cmt)]() {
458     return cmt.moveCount();
459   };
460   Function<size_t(void)> uf1 = std::move(lambda1);
461
462   // Max copies: 0. Max copy+moves: 2.
463   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
464   EXPECT_LE(cmt.copyCount(), 0);
465
466   cmt.resetCounters();
467
468   // Move into lambda, copy lambda into Function
469   auto lambda2 = [cmt = std::move(cmt)]() {
470     return cmt.moveCount();
471   };
472   Function<size_t(void)> uf2 = lambda2;
473
474   // Max copies: 1. Max copy+moves: 2.
475   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
476   EXPECT_LE(cmt.copyCount(), 1);
477
478   // Invoking Function must not make copies/moves of the callable
479   cmt.resetCounters();
480   uf1();
481   uf2();
482   EXPECT_EQ(0, cmt.copyCount());
483   EXPECT_EQ(0, cmt.moveCount());
484 }
485
486 TEST(Function, ParameterCopyMoveCount) {
487   // This test checks that no unnecessary copies/moves are made.
488
489   CopyMoveTracker cmt(CopyMoveTracker::ConstructorTag{});
490   EXPECT_EQ(0, cmt.copyCount());
491   EXPECT_EQ(0, cmt.moveCount());
492   EXPECT_EQ(1, cmt.refCount());
493
494   // pass by value
495   Function<size_t(CopyMoveTracker)> uf1 = [](CopyMoveTracker c) {
496     return c.moveCount();
497   };
498
499   cmt.resetCounters();
500   uf1(cmt);
501   // Max copies: 1. Max copy+moves: 2.
502   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
503   EXPECT_LE(cmt.copyCount(), 1);
504
505   cmt.resetCounters();
506   uf1(std::move(cmt));
507   // Max copies: 1. Max copy+moves: 2.
508   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 2);
509   EXPECT_LE(cmt.copyCount(), 0);
510
511   // pass by reference
512   Function<size_t(CopyMoveTracker&)> uf2 = [](CopyMoveTracker& c) {
513     return c.moveCount();
514   };
515
516   cmt.resetCounters();
517   uf2(cmt);
518   // Max copies: 0. Max copy+moves: 0.
519   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
520   EXPECT_LE(cmt.copyCount(), 0);
521
522   // pass by const reference
523   Function<size_t(CopyMoveTracker const&)> uf3 = [](CopyMoveTracker const& c) {
524     return c.moveCount();
525   };
526
527   cmt.resetCounters();
528   uf3(cmt);
529   // Max copies: 0. Max copy+moves: 0.
530   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
531   EXPECT_LE(cmt.copyCount(), 0);
532
533   // pass by rvalue reference
534   Function<size_t(CopyMoveTracker &&)> uf4 = [](CopyMoveTracker&& c) {
535     return c.moveCount();
536   };
537
538   cmt.resetCounters();
539   uf4(std::move(cmt));
540   // Max copies: 0. Max copy+moves: 0.
541   EXPECT_LE(cmt.moveCount() + cmt.copyCount(), 0);
542   EXPECT_LE(cmt.copyCount(), 0);
543 }
544
545 // TEST =====================================================================
546 // VariadicTemplate & VariadicArguments
547
548 struct VariadicTemplateSum {
549   int operator()() const {
550     return 0;
551   }
552   template <class... Args>
553   int operator()(int x, Args... args) const {
554     return x + (*this)(args...);
555   }
556 };
557
558 TEST(Function, VariadicTemplate) {
559   Function<int(int)> uf1 = VariadicTemplateSum();
560   Function<int(int, int)> uf2 = VariadicTemplateSum();
561   Function<int(int, int, int)> uf3 = VariadicTemplateSum();
562
563   EXPECT_EQ(66, uf1(66));
564   EXPECT_EQ(99, uf2(55, 44));
565   EXPECT_EQ(66, uf3(33, 22, 11));
566 }
567
568 struct VariadicArgumentsSum {
569   int operator()(int count, ...) const {
570     int result = 0;
571     va_list args;
572     va_start(args, count);
573     for (int i = 0; i < count; ++i) {
574       result += va_arg(args, int);
575     }
576     va_end(args);
577     return result;
578   }
579 };
580
581 TEST(Function, VariadicArguments) {
582   Function<int(int)> uf1 = VariadicArgumentsSum();
583   Function<int(int, int)> uf2 = VariadicArgumentsSum();
584   Function<int(int, int, int)> uf3 = VariadicArgumentsSum();
585
586   EXPECT_EQ(0, uf1(0));
587   EXPECT_EQ(66, uf2(1, 66));
588   EXPECT_EQ(99, uf3(2, 55, 44));
589 }
590
591 // TEST =====================================================================
592 // SafeCaptureByReference
593
594 // A function can use Function const& as a parameter to signal that it
595 // is safe to pass a lambda that captures local variables by reference.
596 // It is safe because we know the function called can only invoke the
597 // Function until it returns. It can't store a copy of the Function
598 // (because it's not copyable), and it can't move the Function somewhere
599 // else (because it gets only a const&).
600
601 template <typename T>
602 void for_each(
603     T const& range,
604     Function<void(typename T::value_type const&) const> const& func) {
605   for (auto const& elem : range) {
606     func(elem);
607   }
608 }
609
610 TEST(Function, SafeCaptureByReference) {
611   std::vector<int> const vec = {20, 30, 40, 2, 3, 4, 200, 300, 400};
612
613   int sum = 0;
614
615   // for_each's second parameter is of type Function<...> const&.
616   // Hence we know we can safely pass it a lambda that references local
617   // variables. There is no way the reference to x will be stored anywhere.
618   for_each<std::vector<int>>(vec, [&sum](int x) { sum += x; });
619
620   // gcc versions before 4.9 cannot deduce the type T in the above call
621   // to for_each. Modern compiler versions can compile the following line:
622   //   for_each(vec, [&sum](int x) { sum += x; });
623
624   EXPECT_EQ(999, sum);
625 }
626
627 // TEST =====================================================================
628 // IgnoreReturnValue
629
630 TEST(Function, IgnoreReturnValue) {
631   int x = 95;
632
633   // Assign a lambda that return int to a folly::Function that returns void.
634   Function<void()> f = [&]() -> int { return ++x; };
635
636   EXPECT_EQ(95, x);
637   f();
638   EXPECT_EQ(96, x);
639
640   Function<int()> g = [&]() -> int { return ++x; };
641   Function<void()> cg = std::move(g);
642
643   EXPECT_EQ(96, x);
644   cg();
645   EXPECT_EQ(97, x);
646 }
647
648 // TEST =====================================================================
649 // ReturnConvertible, ConvertReturnType
650
651 TEST(Function, ReturnConvertible) {
652   struct CBase {
653     int x;
654   };
655   struct CDerived : CBase {};
656
657   Function<double()> f1 = []() -> int { return 5; };
658   EXPECT_EQ(5.0, f1());
659
660   Function<int()> f2 = []() -> double { return 5.2; };
661   EXPECT_EQ(5, f2());
662
663   CDerived derived;
664   derived.x = 55;
665
666   Function<CBase const&()> f3 = [&]() -> CDerived const& { return derived; };
667   EXPECT_EQ(55, f3().x);
668
669   Function<CBase const&()> f4 = [&]() -> CDerived& { return derived; };
670   EXPECT_EQ(55, f4().x);
671
672   Function<CBase&()> f5 = [&]() -> CDerived& { return derived; };
673   EXPECT_EQ(55, f5().x);
674
675   Function<CBase const*()> f6 = [&]() -> CDerived const* { return &derived; };
676   EXPECT_EQ(f6()->x, 55);
677
678   Function<CBase const*()> f7 = [&]() -> CDerived* { return &derived; };
679   EXPECT_EQ(55, f7()->x);
680
681   Function<CBase*()> f8 = [&]() -> CDerived* { return &derived; };
682   EXPECT_EQ(55, f8()->x);
683
684   Function<CBase()> f9 = [&]() -> CDerived {
685     auto d = derived;
686     d.x = 66;
687     return d;
688   };
689   EXPECT_EQ(66, f9().x);
690 }
691
692 TEST(Function, ConvertReturnType) {
693   struct CBase {
694     int x;
695   };
696   struct CDerived : CBase {};
697
698   Function<int()> f1 = []() -> int { return 5; };
699   Function<double()> cf1 = std::move(f1);
700   EXPECT_EQ(5.0, cf1());
701   Function<int()> ccf1 = std::move(cf1);
702   EXPECT_EQ(5, ccf1());
703
704   Function<double()> f2 = []() -> double { return 5.2; };
705   Function<int()> cf2 = std::move(f2);
706   EXPECT_EQ(5, cf2());
707   Function<double()> ccf2 = std::move(cf2);
708   EXPECT_EQ(5.0, ccf2());
709
710   CDerived derived;
711   derived.x = 55;
712
713   Function<CDerived const&()> f3 = [&]() -> CDerived const& { return derived; };
714   Function<CBase const&()> cf3 = std::move(f3);
715   EXPECT_EQ(55, cf3().x);
716
717   Function<CDerived&()> f4 = [&]() -> CDerived& { return derived; };
718   Function<CBase const&()> cf4 = std::move(f4);
719   EXPECT_EQ(55, cf4().x);
720
721   Function<CDerived&()> f5 = [&]() -> CDerived& { return derived; };
722   Function<CBase&()> cf5 = std::move(f5);
723   EXPECT_EQ(55, cf5().x);
724
725   Function<CDerived const*()> f6 = [&]() -> CDerived const* {
726     return &derived;
727   };
728   Function<CBase const*()> cf6 = std::move(f6);
729   EXPECT_EQ(55, cf6()->x);
730
731   Function<CDerived const*()> f7 = [&]() -> CDerived* { return &derived; };
732   Function<CBase const*()> cf7 = std::move(f7);
733   EXPECT_EQ(55, cf7()->x);
734
735   Function<CDerived*()> f8 = [&]() -> CDerived* { return &derived; };
736   Function<CBase*()> cf8 = std::move(f8);
737   EXPECT_EQ(55, cf8()->x);
738
739   Function<CDerived()> f9 = [&]() -> CDerived {
740     auto d = derived;
741     d.x = 66;
742     return d;
743   };
744   Function<CBase()> cf9 = std::move(f9);
745   EXPECT_EQ(66, cf9().x);
746 }
747
748 // TEST =====================================================================
749 // asStdFunction_*
750
751 TEST(Function, asStdFunction_void) {
752   int i = 0;
753   folly::Function<void()> f = [&] { ++i; };
754   auto sf = std::move(f).asStdFunction();
755   static_assert(std::is_same<decltype(sf), std::function<void()>>::value,
756       "std::function has wrong type");
757   sf();
758   EXPECT_EQ(1, i);
759 }
760
761 TEST(Function, asStdFunction_void_const) {
762   int i = 0;
763   folly::Function<void() const> f = [&] { ++i; };
764   auto sf = std::move(f).asStdFunction();
765   static_assert(std::is_same<decltype(sf), std::function<void()>>::value,
766       "std::function has wrong type");
767   sf();
768   EXPECT_EQ(1, i);
769 }
770
771 TEST(Function, asStdFunction_return) {
772   int i = 0;
773   folly::Function<int()> f = [&] {
774     ++i;
775     return 42;
776   };
777   auto sf = std::move(f).asStdFunction();
778   static_assert(std::is_same<decltype(sf), std::function<int()>>::value,
779       "std::function has wrong type");
780   EXPECT_EQ(42, sf());
781   EXPECT_EQ(1, i);
782 }
783
784 TEST(Function, asStdFunction_return_const) {
785   int i = 0;
786   folly::Function<int() const> f = [&] {
787     ++i;
788     return 42;
789   };
790   auto sf = std::move(f).asStdFunction();
791   static_assert(std::is_same<decltype(sf), std::function<int()>>::value,
792       "std::function has wrong type");
793   EXPECT_EQ(42, sf());
794   EXPECT_EQ(1, i);
795 }
796
797 TEST(Function, asStdFunction_args) {
798   int i = 0;
799   folly::Function<void(int, int)> f = [&](int x, int y) {
800     ++i;
801     return x + y;
802   };
803   auto sf = std::move(f).asStdFunction();
804   static_assert(std::is_same<decltype(sf), std::function<void(int, int)>>::value,
805       "std::function has wrong type");
806   sf(42, 42);
807   EXPECT_EQ(1, i);
808 }
809
810 TEST(Function, asStdFunction_args_const) {
811   int i = 0;
812   folly::Function<void(int, int) const> f = [&](int x, int y) {
813     ++i;
814     return x + y;
815   };
816   auto sf = std::move(f).asStdFunction();
817   static_assert(std::is_same<decltype(sf), std::function<void(int, int)>>::value,
818       "std::function has wrong type");
819   sf(42, 42);
820   EXPECT_EQ(1, i);
821 }
822
823 TEST(Function, NoAllocatedMemoryAfterMove) {
824   Functor<int, 100> foo;
825
826   Function<int(size_t)> func = foo;
827   EXPECT_TRUE(func.hasAllocatedMemory());
828
829   Function<int(size_t)> func2 = std::move(func);
830   EXPECT_TRUE(func2.hasAllocatedMemory());
831   EXPECT_FALSE(func.hasAllocatedMemory());
832 }
833
834 TEST(Function, ConstCastEmbedded) {
835   int x = 0;
836   auto functor = [&x]() { ++x; };
837
838   Function<void() const> func(functor);
839   EXPECT_FALSE(func.hasAllocatedMemory());
840
841   Function<void()> func2(std::move(func));
842   EXPECT_FALSE(func2.hasAllocatedMemory());
843 }
844
845 TEST(Function, EmptyAfterConstCast) {
846   Function<int(size_t)> func;
847   EXPECT_FALSE(func);
848
849   Function<int(size_t) const> func2 = constCastFunction(std::move(func));
850   EXPECT_FALSE(func2);
851 }
852
853 TEST(Function, SelfMoveAssign) {
854   Function<int()> f = [] { return 0; };
855   Function<int()>& g = f;
856   f = std::move(g);
857   EXPECT_TRUE(bool(f));
858 }
859
860 TEST(Function, DeducableArguments) {
861   deduceArgs(Function<void()>{[] {}});
862   deduceArgs(Function<void(int, float)>{[](int, float) {}});
863   deduceArgs(Function<int(int, float)>{[](int i, float) { return i; }});
864 }