Lift the invoke helper in Function.h
[folly.git] / folly / functional / test / InvokeTest.cpp
1 /*
2  * Copyright 2017-present 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 <folly/functional/Invoke.h>
18
19 #include <folly/portability/GTest.h>
20
21 class InvokeTest : public testing::Test {};
22
23 namespace {
24 struct Fn {
25   char operator()(int, int) noexcept {
26     return 'a';
27   }
28   int volatile&& operator()(int, char const*) {
29     return std::move(x_);
30   }
31   float operator()(float, float) {
32     return 3.14;
33   }
34   int volatile x_ = 17;
35 };
36 } // namespace
37
38 TEST_F(InvokeTest, invoke) {
39   Fn fn;
40
41   EXPECT_TRUE(noexcept(folly::invoke(fn, 1, 2)));
42   EXPECT_FALSE(noexcept(folly::invoke(fn, 1, "2")));
43
44   EXPECT_EQ('a', folly::invoke(fn, 1, 2));
45   EXPECT_EQ(17, folly::invoke(fn, 1, "2"));
46
47   using FnA = char (Fn::*)(int, int);
48   using FnB = int volatile && (Fn::*)(int, char const*);
49   EXPECT_EQ('a', folly::invoke(static_cast<FnA>(&Fn::operator()), fn, 1, 2));
50   EXPECT_EQ(17, folly::invoke(static_cast<FnB>(&Fn::operator()), fn, 1, "2"));
51 }
52
53 TEST_F(InvokeTest, invoke_result) {
54   EXPECT_TRUE(
55       (std::is_same<char, folly::invoke_result_t<Fn, int, char>>::value));
56   EXPECT_TRUE(
57       (std::is_same<int volatile&&, folly::invoke_result_t<Fn, int, char*>>::
58            value));
59 }
60
61 TEST_F(InvokeTest, is_invocable) {
62   EXPECT_TRUE((folly::is_invocable<Fn, int, char>::value));
63   EXPECT_TRUE((folly::is_invocable<Fn, int, char*>::value));
64   EXPECT_FALSE((folly::is_invocable<Fn, int>::value));
65 }
66
67 TEST_F(InvokeTest, is_invocable_r) {
68   EXPECT_TRUE((folly::is_invocable_r<int, Fn, int, char>::value));
69   EXPECT_TRUE((folly::is_invocable_r<int, Fn, int, char*>::value));
70   EXPECT_FALSE((folly::is_invocable_r<int, Fn, int>::value));
71 }
72
73 TEST_F(InvokeTest, is_nothrow_invocable) {
74   EXPECT_TRUE((folly::is_nothrow_invocable<Fn, int, char>::value));
75   EXPECT_FALSE((folly::is_nothrow_invocable<Fn, int, char*>::value));
76   EXPECT_FALSE((folly::is_nothrow_invocable<Fn, int>::value));
77 }
78
79 TEST_F(InvokeTest, is_nothrow_invocable_r) {
80   EXPECT_TRUE((folly::is_nothrow_invocable_r<int, Fn, int, char>::value));
81   EXPECT_FALSE((folly::is_nothrow_invocable_r<int, Fn, int, char*>::value));
82   EXPECT_FALSE((folly::is_nothrow_invocable_r<int, Fn, int>::value));
83 }