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