15 #include "absl/base/internal/invoke.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/memory/memory.h"
25 #include "absl/strings/str_cat.h"
29 namespace base_internal {
34 void VoidFunction(
int& a,
int&
b) {
40 int ZeroArgFunction() {
return -1937; }
42 int Sink(std::unique_ptr<int> p) {
46 std::unique_ptr<int> Factory(
int n) {
47 return make_unique<int>(n);
53 int operator()(
int a,
int b)
const {
return a -
b; }
56 struct MutableFunctor {
57 int operator()(
int a,
int b) {
return a -
b; }
60 struct EphemeralFunctor {
61 int operator()(
int a,
int b) && {
return a -
b; }
64 struct OverloadedFunctor {
65 template <
typename...
Args>
69 template <
typename...
Args>
73 template <
typename...
Args>
80 int Method(
int a,
int b) {
return a -
b; }
81 int ConstMethod(
int a,
int b)
const {
return a -
b; }
82 int RefMethod(
int a,
int b) & {
return a -
b; }
83 int RefRefMethod(
int a,
int b) && {
return a -
b; }
84 int NoExceptMethod(
int a,
int b) noexcept {
return a -
b; }
85 int VolatileMethod(
int a,
int b)
volatile {
return a -
b; }
91 int ConstMethod()
const {
return member; }
105 template <
typename F>
116 TEST(InvokeTest, NonCopyableArgument) {
120 TEST(InvokeTest, NonCopyableResult) {
126 TEST(InvokeTest, ConstFunctor) {
130 TEST(InvokeTest, MutableFunctor) {
136 TEST(InvokeTest, EphemeralFunctor) {
142 TEST(InvokeTest, OverloadedFunctor) {
144 const OverloadedFunctor& cf =
f;
154 OverloadedFunctor
f2;
158 TEST(InvokeTest, ReferenceWrapper) {
166 TEST(InvokeTest, MemberFunction) {
167 std::unique_ptr<Class>
p(
new Class);
168 std::unique_ptr<const Class> cp(
new Class);
169 std::unique_ptr<volatile Class> vp(
new Class);
203 make_unique<const Class>(), 3, 2));
206 TEST(InvokeTest, DataMember) {
207 std::unique_ptr<Class>
p(
new Class{42});
208 std::unique_ptr<const Class> cp(
new Class{42});
221 TEST(InvokeTest, FlipFlop) {
229 TEST(InvokeTest, SfinaeFriendly) {
230 CallMaybeWithArg(
NoOp);
234 TEST(IsInvocableRTest, CallableExactMatch) {
237 "Should be true for exact match of types on a free function");
240 TEST(IsInvocableRTest, CallableArgumentConversionMatch) {
243 "Should be true for convertible argument type");
246 TEST(IsInvocableRTest, CallableReturnConversionMatch) {
249 "Should be true for convertible return type");
252 TEST(IsInvocableRTest, CallableReturnVoid) {
255 "Should be true for void expected and actual return types");
258 "Should be true for void expected and non-void actual return types");
261 TEST(IsInvocableRTest, CallableRefQualifierMismatch) {
263 int&,
const int&>::
value,
264 "Should be false for reference constness mismatch");
267 "Should be false for reference value category mismatch");
270 TEST(IsInvocableRTest, CallableArgumentTypeMismatch) {
273 "Should be false for argument type mismatch");
276 TEST(IsInvocableRTest, CallableReturnTypeMismatch) {
279 "Should be false for return type mismatch");
282 TEST(IsInvocableRTest, CallableTooFewArgs) {
285 "Should be false for too few arguments");
288 TEST(IsInvocableRTest, CallableTooManyArgs) {
291 "Should be false for too many arguments");
294 TEST(IsInvocableRTest, MemberFunctionAndReference) {
296 Class&,
int,
int>::
value,
297 "Should be true for exact match of types on a member function "
298 "and class reference");
301 TEST(IsInvocableRTest, MemberFunctionAndPointer) {
303 Class*,
int,
int>::
value,
304 "Should be true for exact match of types on a member function "
305 "and class pointer");
308 TEST(IsInvocableRTest, DataMemberAndReference) {
311 "Should be true for exact match of types on a data member and "
315 TEST(IsInvocableRTest, DataMemberAndPointer) {
318 "Should be true for exact match of types on a data member and "
322 TEST(IsInvocableRTest, CallableZeroArgs) {
325 "Should be true for exact match for a zero-arg free function");