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) {
38 TEST_F(InvokeTest, invoke) {
41 EXPECT_TRUE(noexcept(folly::invoke(fn, 1, 2)));
42 EXPECT_FALSE(noexcept(folly::invoke(fn, 1, "2")));
44 EXPECT_EQ('a', folly::invoke(fn, 1, 2));
45 EXPECT_EQ(17, folly::invoke(fn, 1, "2"));
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"));
53 TEST_F(InvokeTest, invoke_result) {
55 (std::is_same<char, folly::invoke_result_t<Fn, int, char>>::value));
57 (std::is_same<int volatile&&, folly::invoke_result_t<Fn, int, char*>>::
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));
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));
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));
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));