proxygen
NonCopyableLambdaTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present 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 <folly/futures/Future.h>
19 
20 using namespace folly;
21 
22 TEST(NonCopyableLambda, basic) {
23  Promise<int> promise;
24  Future<int> future = promise.getFuture();
25 
26  Future<Unit>().thenValue(std::bind(
27  [](Promise<int>& p2, folly::Unit) mutable { p2.setValue(123); },
28  std::move(promise),
29  std::placeholders::_1));
30 
31  // The previous statement can be simplified in C++14:
32  // Future<Unit>().then([promise = std::move(promise)]() mutable {
33  // promise.setValue(123);
34  // });
35 
36  EXPECT_TRUE(future.isReady());
37  EXPECT_EQ(std::move(future).get(), 123);
38 }
39 
40 TEST(NonCopyableLambda, unique_ptr) {
41  Promise<Unit> promise;
42  auto int_ptr = std::make_unique<int>(1);
43 
44  EXPECT_EQ(*int_ptr, 1);
45 
46  auto future = promise.getFuture().thenValue(std::bind(
47  [](std::unique_ptr<int>& p, folly::Unit) mutable {
48  ++*p;
49  return std::move(p);
50  },
51  std::move(int_ptr),
52  std::placeholders::_1));
53 
54  // The previous statement can be simplified in C++14:
55  // auto future =
56  // promise.getFuture().then([int_ptr = std::move(int_ptr)]() mutable {
57  // ++*int_ptr;
58  // return std::move(int_ptr);
59  // });
60 
61  EXPECT_FALSE(future.isReady());
62  promise.setValue();
63  EXPECT_TRUE(future.isReady());
64  EXPECT_EQ(*std::move(future).get(), 2);
65 }
66 
67 TEST(NonCopyableLambda, Function) {
68  Promise<int> promise;
69 
70  Function<int(int)> callback = [](int x) { return x + 1; };
71 
72  auto future = promise.getFuture().then(std::move(callback));
73  EXPECT_THROW(callback(0), std::bad_function_call);
74 
75  EXPECT_FALSE(future.isReady());
76  promise.setValue(100);
77  EXPECT_TRUE(future.isReady());
78  EXPECT_EQ(std::move(future).get(), 101);
79 }
80 
81 TEST(NonCopyableLambda, FunctionConst) {
82  Promise<int> promise;
83 
84  Function<int(int) const> callback = [](int x) { return x + 1; };
85 
86  auto future = promise.getFuture().then(std::move(callback));
87  EXPECT_THROW(callback(0), std::bad_function_call);
88 
89  EXPECT_FALSE(future.isReady());
90  promise.setValue(100);
91  EXPECT_TRUE(future.isReady());
92  EXPECT_EQ(std::move(future).get(), 101);
93 }
Definition: InvokeTest.cpp:58
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const int x
static void basic()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST(SequencedExecutor, CPUThreadPoolExecutor)