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