a560ab56961f0aa32a536d872cf4e1f8724b5ef8
[folly.git] / folly / futures / test / WillEqualTest.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
21 using namespace folly;
22
23 TEST(WillEqual, basic) {
24   // both p1 and p2 already fulfilled
25   {
26     Promise<int> p1;
27     Promise<int> p2;
28     p1.setValue(27);
29     p2.setValue(27);
30     auto f1 = p1.getFuture();
31     auto f2 = p2.getFuture();
32     EXPECT_TRUE(f1.willEqual(f2).get());
33   }
34   {
35     Promise<int> p1;
36     Promise<int> p2;
37     p1.setValue(27);
38     p2.setValue(36);
39     auto f1 = p1.getFuture();
40     auto f2 = p2.getFuture();
41     EXPECT_FALSE(f1.willEqual(f2).get());
42   }
43   // both p1 and p2 not yet fulfilled
44   {
45     Promise<int> p1;
46     Promise<int> p2;
47     auto f1 = p1.getFuture();
48     auto f2 = p2.getFuture();
49     auto f3 = f1.willEqual(f2);
50     p1.setValue(27);
51     p2.setValue(27);
52     EXPECT_TRUE(f3.get());
53   }
54   {
55     Promise<int> p1;
56     Promise<int> p2;
57     auto f1 = p1.getFuture();
58     auto f2 = p2.getFuture();
59     auto f3 = f1.willEqual(f2);
60     p1.setValue(27);
61     p2.setValue(36);
62     EXPECT_FALSE(f3.get());
63   }
64   // p1 already fulfilled, p2 not yet fulfilled
65   {
66     Promise<int> p1;
67     Promise<int> p2;
68     p1.setValue(27);
69     auto f1 = p1.getFuture();
70     auto f2 = p2.getFuture();
71     auto f3 = f1.willEqual(f2);
72     p2.setValue(27);
73     EXPECT_TRUE(f3.get());
74   }
75   {
76     Promise<int> p1;
77     Promise<int> p2;
78     p1.setValue(27);
79     auto f1 = p1.getFuture();
80     auto f2 = p2.getFuture();
81     auto f3 = f1.willEqual(f2);
82     p2.setValue(36);
83     EXPECT_FALSE(f3.get());
84   }
85   // p2 already fulfilled, p1 not yet fulfilled
86   {
87     Promise<int> p1;
88     Promise<int> p2;
89     p2.setValue(27);
90     auto f1 = p1.getFuture();
91     auto f2 = p2.getFuture();
92     auto f3 = f1.willEqual(f2);
93     p1.setValue(27);
94     EXPECT_TRUE(f3.get());
95   }
96   {
97     Promise<int> p1;
98     Promise<int> p2;
99     p2.setValue(36);
100     auto f1 = p1.getFuture();
101     auto f2 = p2.getFuture();
102     auto f3 = f1.willEqual(f2);
103     p1.setValue(27);
104     EXPECT_FALSE(f3.get());
105   }
106 }