proxygen
PartialTest.cpp File Reference
#include <memory>
#include <folly/Function.h>
#include <folly/functional/Partial.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

class  Foo
 
struct  RefQualifiers
 
struct  RefQualifiers2
 

Functions

int add3 (int x, int y, int z)
 
 TEST (Partial, Simple)
 
 TEST (Partial, ReferenceArguments)
 
 TEST (Partial, RefQualifiers)
 
 TEST (Partial, RefQualifiers2)
 
std::unique_ptr< int > calc_uptr (std::unique_ptr< int > x, std::unique_ptr< int > y)
 
 TEST (Partial, MoveOnly)
 
 TEST (Partial, WrapInStdFunction)
 
 TEST (Partial, WrapInFollyFunction)
 

Function Documentation

int add3 ( int  x,
int  y,
int  z 
)

Definition at line 26 of file PartialTest.cpp.

Referenced by TEST().

26  {
27  return 100 * x + 10 * y + z;
28 }
Definition: InvokeTest.cpp:58
Definition: InvokeTest.cpp:72
Definition: InvokeTest.cpp:65
std::unique_ptr<int> calc_uptr ( std::unique_ptr< int >  x,
std::unique_ptr< int >  y 
)

Definition at line 110 of file PartialTest.cpp.

References x.

Referenced by TEST().

110  {
111  *x = 100 * *x + 10 * *y;
112  return x;
113 }
const int x
TEST ( Partial  ,
Simple   
)

Definition at line 30 of file PartialTest.cpp.

References add3(), EXPECT_EQ, and folly::partial().

30  {
31  auto p0 = partial(&add3);
32  EXPECT_EQ(123, p0(1, 2, 3));
33 
34  auto p1 = partial(&add3, 2);
35  EXPECT_EQ(234, p1(3, 4));
36 
37  auto p2 = partial(&add3, 3, 4);
38  EXPECT_EQ(345, p2(5));
39 
40  auto p3 = partial(&add3, 4, 5, 6);
41  EXPECT_EQ(456, p3());
42 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int add3(int x, int y, int z)
Definition: PartialTest.cpp:26
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
TEST ( Partial  ,
ReferenceArguments   
)

Definition at line 56 of file PartialTest.cpp.

References EXPECT_EQ, folly::gen::move, and folly::partial().

56  {
57  auto p0 = partial(&Foo::method, Foo{}, 2, 3);
58  int four = 4;
59  EXPECT_EQ(1234, p0(four));
60 
61  auto const p1 = partial(&Foo::constMethod, Foo{}, 3, 4);
62  EXPECT_EQ(2345, p1(5));
63 
64  auto p2 = partial(&Foo::tempMethod, Foo{}, 4, 5);
65  EXPECT_EQ(3456, std::move(p2)(6));
66 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
TEST ( Partial  ,
RefQualifiers   
)

Definition at line 80 of file PartialTest.cpp.

References EXPECT_EQ, folly::gen::move, and folly::partial().

80  {
81  auto p = partial(RefQualifiers{});
82  auto const& pconst = p;
83 
84  EXPECT_EQ(1234, p(2, 3, 4));
85  EXPECT_EQ(2345, pconst(3, 4, 5));
86  EXPECT_EQ(3456, std::move(p)(4, 5, 6));
87 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
TEST ( Partial  ,
RefQualifiers2   
)

Definition at line 101 of file PartialTest.cpp.

References EXPECT_EQ, folly::gen::move, and folly::partial().

101  {
102  auto p = partial(RefQualifiers2{}, 9, 8);
103  auto const& pconst = p;
104 
105  EXPECT_EQ(1984, p(4));
106  EXPECT_EQ(2985, pconst(5));
107  EXPECT_EQ(3986, std::move(p)(6));
108 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
TEST ( Partial  ,
MoveOnly   
)

Definition at line 115 of file PartialTest.cpp.

References calc_uptr(), EXPECT_EQ, EXPECT_FALSE, folly::gen::move, and folly::partial().

115  {
116  auto five = std::make_unique<int>(5);
117  auto six = std::make_unique<int>(6);
118 
119  // create a partial object which holds a pointer to the `calc_uptr` function
120  // and a `unique_ptr<int>` for the first argument
121  auto p = partial(&calc_uptr, std::move(five));
122 
123  // `five` should be moved out of
124  EXPECT_FALSE(five);
125 
126  // call to the partial object as rvalue, which allows the call to consume
127  // captured data (here: the `unique_ptr<int>` storing 5), and pass it
128  // the other `unique_ptr`
129  auto result = std::move(p)(std::move(six));
130 
131  // ...which now should be moved out of
132  EXPECT_FALSE(six);
133 
134  EXPECT_EQ(560, *result);
135 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
std::unique_ptr< int > calc_uptr(std::unique_ptr< int > x, std::unique_ptr< int > y)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Partial  ,
WrapInStdFunction   
)

Definition at line 137 of file PartialTest.cpp.

References add3(), EXPECT_EQ, and folly::partial().

137  {
138  auto p1 = partial(&add3, 2);
139  std::function<int(int, int)> func = p1;
140  EXPECT_EQ(234, func(3, 4));
141 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int add3(int x, int y, int z)
Definition: PartialTest.cpp:26
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
TEST ( Partial  ,
WrapInFollyFunction   
)

Definition at line 143 of file PartialTest.cpp.

References add3(), EXPECT_EQ, and folly::partial().

143  {
144  auto p1 = partial(&add3, 2);
146  EXPECT_EQ(234, func(3, 4));
147 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int add3(int x, int y, int z)
Definition: PartialTest.cpp:26
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119