proxygen
NonCopyableLambdaTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (NonCopyableLambda, basic)
 
 TEST (NonCopyableLambda, unique_ptr)
 
 TEST (NonCopyableLambda, Function)
 
 TEST (NonCopyableLambda, FunctionConst)
 

Function Documentation

TEST ( NonCopyableLambda  ,
basic   
)

Definition at line 22 of file NonCopyableLambdaTest.cpp.

References folly::netops::bind(), EXPECT_EQ, EXPECT_TRUE, folly::Promise< T >::getFuture(), folly::futures::detail::FutureBase< T >::isReady(), folly::gen::move, and folly::Promise< T >::setValue().

22  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
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
TEST ( NonCopyableLambda  ,
unique_ptr   
)

Definition at line 40 of file NonCopyableLambdaTest.cpp.

References folly::netops::bind(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::Promise< T >::getFuture(), folly::gen::move, and folly::Promise< T >::setValue().

40  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
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 ( NonCopyableLambda  ,
Function   
)

Definition at line 67 of file NonCopyableLambdaTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::Promise< T >::getFuture(), folly::gen::move, folly::Promise< T >::setValue(), and x.

67  {
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 }
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
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
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( NonCopyableLambda  ,
FunctionConst   
)

Definition at line 81 of file NonCopyableLambdaTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::Promise< T >::getFuture(), folly::gen::move, folly::Promise< T >::setValue(), and x.

81  {
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
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
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862