2 * Copyright 2017-present 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.
17 #include <folly/functional/Invoke.h>
19 #include <folly/portability/GTest.h>
21 class InvokeTest : public testing::Test {};
25 char operator()(int, int) noexcept {
28 int volatile&& operator()(int, char const*) {
31 float operator()(float, float) {
37 FOLLY_CREATE_MEMBER_INVOKE_TRAITS(test_invoke_traits, test);
40 char test(int, int) noexcept {
43 int volatile&& test(int, char const*) {
46 float test(float, float) {
54 TEST_F(InvokeTest, invoke) {
57 EXPECT_TRUE(noexcept(folly::invoke(fn, 1, 2)));
58 EXPECT_FALSE(noexcept(folly::invoke(fn, 1, "2")));
60 EXPECT_EQ('a', folly::invoke(fn, 1, 2));
61 EXPECT_EQ(17, folly::invoke(fn, 1, "2"));
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"));
69 TEST_F(InvokeTest, invoke_result) {
71 (std::is_same<char, folly::invoke_result_t<Fn, int, char>>::value));
73 (std::is_same<int volatile&&, folly::invoke_result_t<Fn, int, char*>>::
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));
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));
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));
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));
101 TEST_F(InvokeTest, member_invoke) {
102 using traits = test_invoke_traits;
106 EXPECT_TRUE(noexcept(traits::invoke(fn, 1, 2)));
107 EXPECT_FALSE(noexcept(traits::invoke(fn, 1, "2")));
109 EXPECT_EQ('a', traits::invoke(fn, 1, 2));
110 EXPECT_EQ(17, traits::invoke(fn, 1, "2"));
113 TEST_F(InvokeTest, member_invoke_result) {
114 using traits = test_invoke_traits;
117 (std::is_same<char, traits::invoke_result_t<Obj, int, char>>::value));
119 (std::is_same<int volatile&&, traits::invoke_result_t<Obj, int, char*>>::
123 TEST_F(InvokeTest, member_is_invocable) {
124 using traits = test_invoke_traits;
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));
131 TEST_F(InvokeTest, member_is_invocable_r) {
132 using traits = test_invoke_traits;
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));
139 TEST_F(InvokeTest, member_is_nothrow_invocable) {
140 using traits = test_invoke_traits;
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));
147 TEST_F(InvokeTest, member_is_nothrow_invocable_r) {
148 using traits = test_invoke_traits;
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));