22 #include "gmock/gmock.h" 23 #include "gtest/gtest.h" 28 namespace base_internal {
31 int Function(
int a,
int b) {
return a -
b; }
33 int Sink(std::unique_ptr<int> p) {
37 std::unique_ptr<int> Factory(
int n) {
38 return make_unique<int>(
n);
44 int operator()(
int a,
int b)
const {
return a -
b; }
47 struct MutableFunctor {
48 int operator()(
int a,
int b) {
return a -
b; }
51 struct EphemeralFunctor {
52 int operator()(
int a,
int b) && {
return a -
b; }
55 struct OverloadedFunctor {
56 template <
typename... Args>
57 std::string operator()(
const Args&... args) & {
58 return StrCat(
"&", args...);
60 template <
typename... Args>
61 std::string operator()(
const Args&... args)
const& {
62 return StrCat(
"const&", args...);
64 template <
typename... Args>
65 std::string operator()(
const Args&... args) && {
66 return StrCat(
"&&", args...);
71 int Method(
int a,
int b) {
return a -
b; }
72 int ConstMethod(
int a,
int b)
const {
return a -
b; }
78 int ConstMethod()
const {
return member; }
87 decltype(
Invoke(std::declval<const F&>())) CallMaybeWithArg(
const F& f) {
92 decltype(
Invoke(std::declval<const F&>(), 42)) CallMaybeWithArg(
const F& f) {
96 TEST(InvokeTest, Function) {
97 EXPECT_EQ(1,
Invoke(Function, 3, 2));
98 EXPECT_EQ(1,
Invoke(&Function, 3, 2));
101 TEST(InvokeTest, NonCopyableArgument) {
102 EXPECT_EQ(42,
Invoke(Sink, make_unique<int>(42)));
105 TEST(InvokeTest, NonCopyableResult) {
106 EXPECT_THAT(
Invoke(Factory, 42), ::testing::Pointee(42));
109 TEST(InvokeTest, VoidResult) {
113 TEST(InvokeTest, ConstFunctor) {
114 EXPECT_EQ(1,
Invoke(ConstFunctor(), 3, 2));
117 TEST(InvokeTest, MutableFunctor) {
119 EXPECT_EQ(1,
Invoke(f, 3, 2));
120 EXPECT_EQ(1,
Invoke(MutableFunctor(), 3, 2));
123 TEST(InvokeTest, EphemeralFunctor) {
126 EXPECT_EQ(1,
Invoke(EphemeralFunctor(), 3, 2));
129 TEST(InvokeTest, OverloadedFunctor) {
131 const OverloadedFunctor& cf = f;
133 EXPECT_EQ(
"&",
Invoke(f));
134 EXPECT_EQ(
"& 42",
Invoke(f,
" 42"));
136 EXPECT_EQ(
"const&",
Invoke(cf));
137 EXPECT_EQ(
"const& 42",
Invoke(cf,
" 42"));
143 TEST(InvokeTest, ReferenceWrapper) {
146 EXPECT_EQ(1,
Invoke(std::cref(cf), 3, 2));
147 EXPECT_EQ(1,
Invoke(std::ref(cf), 3, 2));
148 EXPECT_EQ(1,
Invoke(std::ref(mf), 3, 2));
151 TEST(InvokeTest, MemberFunction) {
152 std::unique_ptr<Class> p(
new Class);
153 std::unique_ptr<const Class> cp(
new Class);
154 EXPECT_EQ(1,
Invoke(&Class::Method, p, 3, 2));
155 EXPECT_EQ(1,
Invoke(&Class::Method, p.get(), 3, 2));
157 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, p, 3, 2));
158 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, p.get(), 3, 2));
159 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, *p, 3, 2));
161 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, cp, 3, 2));
162 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, cp.get(), 3, 2));
163 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, *cp, 3, 2));
165 EXPECT_EQ(1,
Invoke(&Class::Method, make_unique<Class>(), 3, 2));
166 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, make_unique<Class>(), 3, 2));
167 EXPECT_EQ(1,
Invoke(&Class::ConstMethod, make_unique<const Class>(), 3, 2));
170 TEST(InvokeTest, DataMember) {
171 std::unique_ptr<Class> p(
new Class{42});
172 std::unique_ptr<const Class> cp(
new Class{42});
185 TEST(InvokeTest, FlipFlop) {
189 EXPECT_EQ(42,
Invoke(&FlipFlop::ConstMethod, obj));
193 TEST(InvokeTest, SfinaeFriendly) {
194 CallMaybeWithArg(NoOp);
195 EXPECT_THAT(CallMaybeWithArg(Factory), ::testing::Pointee(42));
TEST(NotificationTest, SanityTest)
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
uint128 operator*(uint128 lhs, uint128 rhs)
InvokeT< F, Args... > Invoke(F &&f, Args &&... args)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept