A macro for creating member-invoke traits
[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
37 FOLLY_CREATE_MEMBER_INVOKE_TRAITS(test_invoke_traits, test);
38
39 struct Obj {
40   char test(int, int) noexcept {
41     return 'a';
42   }
43   int volatile&& test(int, char const*) {
44     return std::move(x_);
45   }
46   float test(float, float) {
47     return 3.14;
48   }
49   int volatile x_ = 17;
50 };
51
52 } // namespace
53
54 TEST_F(InvokeTest, invoke) {
55   Fn fn;
56
57   EXPECT_TRUE(noexcept(folly::invoke(fn, 1, 2)));
58   EXPECT_FALSE(noexcept(folly::invoke(fn, 1, "2")));
59
60   EXPECT_EQ('a', folly::invoke(fn, 1, 2));
61   EXPECT_EQ(17, folly::invoke(fn, 1, "2"));
62
63   using FnA = char (Fn::*)(int, int);
64   using FnB = int volatile && (Fn::*)(int, char const*);
65   EXPECT_EQ('a', folly::invoke(static_cast<FnA>(&Fn::operator()), fn, 1, 2));
66   EXPECT_EQ(17, folly::invoke(static_cast<FnB>(&Fn::operator()), fn, 1, "2"));
67 }
68
69 TEST_F(InvokeTest, invoke_result) {
70   EXPECT_TRUE(
71       (std::is_same<char, folly::invoke_result_t<Fn, int, char>>::value));
72   EXPECT_TRUE(
73       (std::is_same<int volatile&&, folly::invoke_result_t<Fn, int, char*>>::
74            value));
75 }
76
77 TEST_F(InvokeTest, is_invocable) {
78   EXPECT_TRUE((folly::is_invocable<Fn, int, char>::value));
79   EXPECT_TRUE((folly::is_invocable<Fn, int, char*>::value));
80   EXPECT_FALSE((folly::is_invocable<Fn, int>::value));
81 }
82
83 TEST_F(InvokeTest, is_invocable_r) {
84   EXPECT_TRUE((folly::is_invocable_r<int, Fn, int, char>::value));
85   EXPECT_TRUE((folly::is_invocable_r<int, Fn, int, char*>::value));
86   EXPECT_FALSE((folly::is_invocable_r<int, Fn, int>::value));
87 }
88
89 TEST_F(InvokeTest, is_nothrow_invocable) {
90   EXPECT_TRUE((folly::is_nothrow_invocable<Fn, int, char>::value));
91   EXPECT_FALSE((folly::is_nothrow_invocable<Fn, int, char*>::value));
92   EXPECT_FALSE((folly::is_nothrow_invocable<Fn, int>::value));
93 }
94
95 TEST_F(InvokeTest, is_nothrow_invocable_r) {
96   EXPECT_TRUE((folly::is_nothrow_invocable_r<int, Fn, int, char>::value));
97   EXPECT_FALSE((folly::is_nothrow_invocable_r<int, Fn, int, char*>::value));
98   EXPECT_FALSE((folly::is_nothrow_invocable_r<int, Fn, int>::value));
99 }
100
101 TEST_F(InvokeTest, member_invoke) {
102   using traits = test_invoke_traits;
103
104   Obj fn;
105
106   EXPECT_TRUE(noexcept(traits::invoke(fn, 1, 2)));
107   EXPECT_FALSE(noexcept(traits::invoke(fn, 1, "2")));
108
109   EXPECT_EQ('a', traits::invoke(fn, 1, 2));
110   EXPECT_EQ(17, traits::invoke(fn, 1, "2"));
111 }
112
113 TEST_F(InvokeTest, member_invoke_result) {
114   using traits = test_invoke_traits;
115
116   EXPECT_TRUE(
117       (std::is_same<char, traits::invoke_result_t<Obj, int, char>>::value));
118   EXPECT_TRUE(
119       (std::is_same<int volatile&&, traits::invoke_result_t<Obj, int, char*>>::
120            value));
121 }
122
123 TEST_F(InvokeTest, member_is_invocable) {
124   using traits = test_invoke_traits;
125
126   EXPECT_TRUE((traits::is_invocable<Obj, int, char>::value));
127   EXPECT_TRUE((traits::is_invocable<Obj, int, char*>::value));
128   EXPECT_FALSE((traits::is_invocable<Obj, int>::value));
129 }
130
131 TEST_F(InvokeTest, member_is_invocable_r) {
132   using traits = test_invoke_traits;
133
134   EXPECT_TRUE((traits::is_invocable_r<int, Obj, int, char>::value));
135   EXPECT_TRUE((traits::is_invocable_r<int, Obj, int, char*>::value));
136   EXPECT_FALSE((traits::is_invocable_r<int, Obj, int>::value));
137 }
138
139 TEST_F(InvokeTest, member_is_nothrow_invocable) {
140   using traits = test_invoke_traits;
141
142   EXPECT_TRUE((traits::is_nothrow_invocable<Obj, int, char>::value));
143   EXPECT_FALSE((traits::is_nothrow_invocable<Obj, int, char*>::value));
144   EXPECT_FALSE((traits::is_nothrow_invocable<Obj, int>::value));
145 }
146
147 TEST_F(InvokeTest, member_is_nothrow_invocable_r) {
148   using traits = test_invoke_traits;
149
150   EXPECT_TRUE((traits::is_nothrow_invocable_r<int, Obj, int, char>::value));
151   EXPECT_FALSE((traits::is_nothrow_invocable_r<int, Obj, int, char*>::value));
152   EXPECT_FALSE((traits::is_nothrow_invocable_r<int, Obj, int>::value));
153 }