15 #include "absl/functional/bind_front.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "absl/memory/memory.h"
29 char CharAt(
const char* s,
size_t index) {
return s[
index]; }
31 TEST(BindTest, Basics) {
37 TEST(BindTest, Lambda) {
38 auto lambda = [](
int x,
int y,
int z) {
return x +
y +
z; };
52 TEST(BindTest, PerfectForwardingOfBoundArgs) {
63 std::string operator()(
const int&)
const {
return "const&"; }
64 std::string operator()(
int&&)
const {
return "&&"; }
65 std::string operator()(
const int&&)
const {
return "const&&"; }
68 TEST(BindTest, PerfectForwardingOfFreeArgs) {
77 struct NonCopyableFunctor {
78 NonCopyableFunctor() =
default;
79 NonCopyableFunctor(
const NonCopyableFunctor&) =
delete;
80 NonCopyableFunctor& operator=(
const NonCopyableFunctor&) =
delete;
81 const NonCopyableFunctor* operator()()
const {
return this; }
84 TEST(BindTest, RefToFunctor) {
86 NonCopyableFunctor ncf;
88 auto bound_ncf_copy = bound_ncf;
96 TEST(BindTest, StoreByCopy) {
109 NonCopyable(
const NonCopyable&) =
delete;
110 NonCopyable& operator=(
const NonCopyable&) =
delete;
115 const std::string& GetNonCopyableValue(
const NonCopyable&
n) {
return n.value; }
117 TEST(BindTest, StoreByRef) {
118 NonCopyable
s(
"hello");
129 TEST(BindTest, StoreByCRef) {
130 NonCopyable
s(
"hello");
142 std::reference_wrapper<NonCopyable>
n) {
143 return n.get().value;
146 TEST(BindTest, StoreByRefInvokeByWrapper) {
147 NonCopyable
s(
"hello");
158 TEST(BindTest, StoreByPointer) {
159 NonCopyable
s(
"hello");
168 int Sink(std::unique_ptr<int>
p) {
172 std::unique_ptr<int> Factory(
int n) {
return absl::make_unique<int>(
n); }
174 TEST(BindTest, NonCopyableArg) {
179 TEST(BindTest, NonCopyableResult) {
188 struct FalseCopyable {
190 FalseCopyable(
const FalseCopyable& other) :
m(other.
m) {}
191 FalseCopyable(FalseCopyable&& other) :
m(
std::
move(other.
m)) {}
195 int GetMember(FalseCopyable<std::unique_ptr<int>>
x) {
return *
x.m; }
197 TEST(BindTest, WrappedMoveOnly) {
198 FalseCopyable<std::unique_ptr<int>>
x;
199 x.m = absl::make_unique<int>(42);
204 int Plus(
int a,
int b) {
return a +
b; }
206 TEST(BindTest, ConstExpr) {
209 static constexpr
int five = 5;
215 #if !(defined(_MSC_VER) && _MSC_VER < 1910)
216 static constexpr
char data[] =
"DEF";
222 struct ManglingCall {
223 int operator()(
int,
double,
std::string)
const {
return 0; }
226 TEST(BindTest, Mangling) {