9eb74dc55e679a78cdc2d4674befb08dce52e8ad
[folly.git] / folly / futures / test / UnitTest.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 <gtest/gtest.h>
18
19 #include <folly/futures/Future.h>
20 #include <folly/futures/Unit.h>
21
22 using namespace folly;
23
24 std::runtime_error eggs("eggs");
25
26 TEST(Unit, futureDefaultCtor) {
27   Future<Unit>();
28 }
29
30 TEST(Unit, operatorEq) {
31   EXPECT_TRUE(Unit{} == Unit{});
32 }
33
34 TEST(Unit, operatorNe) {
35   EXPECT_FALSE(Unit{} != Unit{});
36 }
37
38 TEST(Unit, promiseSetValue) {
39   Promise<Unit> p;
40   p.setValue();
41 }
42
43 TEST(Unit, liftInt) {
44   using lifted = Unit::Lift<int>;
45   using actual = std::is_same<int, lifted::type>;
46   EXPECT_TRUE(actual::value);
47 }
48
49 TEST(Unit, liftUnit) {
50   using lifted = Unit::Lift<Unit>;
51   using actual = std::is_same<Unit, lifted::type>;
52   EXPECT_TRUE(actual::value);
53 }
54
55 TEST(Unit, liftVoid) {
56   using lifted = Unit::Lift<void>;
57   using actual = std::is_same<Unit, lifted::type>;
58   EXPECT_TRUE(actual::value);
59 }
60
61 TEST(Unit, dropInt) {
62   using dropped = Unit::Drop<int>;
63   using actual = std::is_same<int, dropped::type>;
64   EXPECT_TRUE(actual::value);
65 }
66
67 TEST(Unit, dropUnit) {
68   using dropped = Unit::Drop<Unit>;
69   using actual = std::is_same<void, dropped::type>;
70   EXPECT_TRUE(actual::value);
71 }
72
73 TEST(Unit, dropVoid) {
74   using dropped = Unit::Drop<void>;
75   using actual = std::is_same<void, dropped::type>;
76   EXPECT_TRUE(actual::value);
77 }
78
79 TEST(Unit, futureToUnit) {
80   Future<Unit> fu = makeFuture(42).unit();
81   fu.value();
82   EXPECT_TRUE(makeFuture<int>(eggs).unit().hasException());
83 }
84
85 TEST(Unit, voidFutureToUnit) {
86   Future<Unit> fu = makeFuture().unit();
87   fu.value();
88   EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
89 }
90
91 TEST(Unit, unitFutureToUnitIdentity) {
92   Future<Unit> fu = makeFuture(Unit{}).unit();
93   fu.value();
94   EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
95 }
96
97 TEST(Unit, toUnitWhileInProgress) {
98   Promise<int> p;
99   Future<Unit> fu = p.getFuture().unit();
100   EXPECT_FALSE(fu.isReady());
101   p.setValue(42);
102   EXPECT_TRUE(fu.isReady());
103 }
104
105 TEST(Unit, makeFutureWith) {
106   int count = 0;
107   Future<Unit> fu = makeFutureWith([&]{ count++; });
108   EXPECT_EQ(1, count);
109 }