e4ef1eb9bbbffb6fa53e394a445852cf8c395df5
[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, futureToUnit) {
63   Future<Unit> fu = makeFuture(42).unit();
64   fu.value();
65   EXPECT_TRUE(makeFuture<int>(eggs).unit().hasException());
66 }
67
68 TEST(Unit, voidFutureToUnit) {
69   Future<Unit> fu = makeFuture().unit();
70   fu.value();
71   EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
72 }
73
74 TEST(Unit, unitFutureToUnitIdentity) {
75   Future<Unit> fu = makeFuture(Unit{}).unit();
76   fu.value();
77   EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
78 }
79
80 TEST(Unit, toUnitWhileInProgress) {
81   Promise<int> p;
82   Future<Unit> fu = p.getFuture().unit();
83   EXPECT_FALSE(fu.isReady());
84   p.setValue(42);
85   EXPECT_TRUE(fu.isReady());
86 }
87
88 TEST(Unit, makeFutureWith) {
89   int count = 0;
90   Future<Unit> fu = makeFutureWith([&]{ count++; });
91   EXPECT_EQ(1, count);
92 }