Reverted commit D3755446
[folly.git] / folly / test / PartialTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <memory>
18
19 #include <folly/Partial.h>
20 #include <gtest/gtest.h>
21
22 using folly::partial;
23
24 int add3(int x, int y, int z) {
25   return 100 * x + 10 * y + z;
26 }
27
28 TEST(Partial, Simple) {
29   auto p0 = partial(&add3);
30   EXPECT_EQ(123, p0(1, 2, 3));
31
32   auto p1 = partial(&add3, 2);
33   EXPECT_EQ(234, p1(3, 4));
34
35   auto p2 = partial(&add3, 3, 4);
36   EXPECT_EQ(345, p2(5));
37
38   auto p3 = partial(&add3, 4, 5, 6);
39   EXPECT_EQ(456, p3());
40 }
41
42 struct Foo {
43   int method(int& x, int& y, int& z) {
44     return 1000 + 100 * x + 10 * y + z;
45   }
46   int constMethod(int const& x, int const& y, int const& z) const {
47     return 2000 + 100 * x + 10 * y + z;
48   }
49   int tempMethod(int&& x, int&& y, int&& z) {
50     return 3000 + 100 * x + 10 * y + z;
51   }
52 };
53
54 TEST(Partial, ReferenceArguments) {
55   auto p0 = partial(&Foo::method, Foo{}, 2, 3);
56   int four = 4;
57   EXPECT_EQ(1234, p0(four));
58
59   auto const p1 = partial(&Foo::constMethod, Foo{}, 3, 4);
60   EXPECT_EQ(2345, p1(5));
61
62   auto p2 = partial(&Foo::tempMethod, Foo{}, 4, 5);
63   EXPECT_EQ(3456, std::move(p2)(6));
64 }
65
66 struct RefQualifiers {
67   int operator()(int x, int y, int z) & {
68     return 1000 + 100 * x + 10 * y + z;
69   }
70   int operator()(int x, int y, int z) const& {
71     return 2000 + 100 * x + 10 * y + z;
72   }
73   int operator()(int x, int y, int z) && {
74     return 3000 + 100 * x + 10 * y + z;
75   }
76 };
77
78 TEST(Partial, RefQualifiers) {
79   auto p = partial(RefQualifiers{});
80   auto const& pconst = p;
81
82   EXPECT_EQ(1234, p(2, 3, 4));
83   EXPECT_EQ(2345, pconst(3, 4, 5));
84   EXPECT_EQ(3456, std::move(p)(4, 5, 6));
85 }
86
87 struct RefQualifiers2 {
88   int operator()(int& x, int const& y, int z) & {
89     return 1000 + 100 * x + 10 * y + z;
90   }
91   int operator()(int const& x, int y, int z) const& {
92     return 2000 + 100 * x + 10 * y + z;
93   }
94   int operator()(int&& x, int const& y, int z) && {
95     return 3000 + 100 * x + 10 * y + z;
96   }
97 };
98
99 TEST(Partial, RefQualifiers2) {
100   auto p = partial(RefQualifiers2{}, 9, 8);
101   auto const& pconst = p;
102
103   EXPECT_EQ(1984, p(4));
104   EXPECT_EQ(2985, pconst(5));
105   EXPECT_EQ(3986, std::move(p)(6));
106 }
107
108 std::unique_ptr<int> calc_uptr(std::unique_ptr<int> x, std::unique_ptr<int> y) {
109   *x = 100 * *x + 10 * *y;
110   return x;
111 }
112
113 TEST(Partial, MoveOnly) {
114   auto five = std::make_unique<int>(5);
115   auto six = std::make_unique<int>(6);
116
117   // create a partial object which holds a pointer to the `calc_uptr` function
118   // and a `unique_ptr<int>` for the first argument
119   auto p = partial(&calc_uptr, std::move(five));
120
121   // `five` should be moved out of
122   EXPECT_FALSE(five);
123
124   // call to the partial object as rvalue, which allows the call to consume
125   // captured data (here: the `unique_ptr<int>` storing 5), and pass it
126   // the other `unique_ptr`
127   auto result = std::move(p)(std::move(six));
128
129   // ...which now should be moved out of
130   EXPECT_FALSE(six);
131
132   EXPECT_EQ(560, *result);
133 }