2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <folly/futures/Future.h>
21 using namespace folly;
23 std::runtime_error eggs("eggs");
25 TEST(Unit, futureDefaultCtor) {
29 TEST(Unit, operatorEq) {
30 EXPECT_TRUE(Unit{} == Unit{});
33 TEST(Unit, operatorNe) {
34 EXPECT_FALSE(Unit{} != Unit{});
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);
43 TEST(Unit, promiseSetValue) {
49 using Lifted = Unit::Lift<int>;
50 EXPECT_FALSE(Lifted::value);
51 auto v = std::is_same<int, Lifted::type>::value;
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;
63 using dropped = typename Unit::Drop<int>;
64 EXPECT_FALSE(dropped::value);
65 EXPECT_TRUE((std::is_same<int, dropped::type>::value));
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));
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));
80 TEST(Unit, futureToUnit) {
81 Future<Unit> fu = makeFuture(42).unit();
83 EXPECT_TRUE(makeFuture<int>(eggs).unit().hasException());
86 TEST(Unit, voidFutureToUnit) {
87 Future<Unit> fu = makeFuture().unit();
89 EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
92 TEST(Unit, unitFutureToUnitIdentity) {
93 Future<Unit> fu = makeFuture(Unit{}).unit();
95 EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
98 TEST(Unit, toUnitWhileInProgress) {
100 Future<Unit> fu = p.getFuture().unit();
101 EXPECT_FALSE(fu.isReady());
103 EXPECT_TRUE(fu.isReady());
106 TEST(Unit, makeFutureWith) {
108 Future<Unit> fu = makeFutureWith([&]{ count++; });