(Wangle) Clean up tests
[folly.git] / folly / futures / test / WindowTest.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 #include <vector>
22
23 using namespace folly;
24
25 TEST(Window, basic) {
26   // int -> Future<int>
27   auto fn = [](std::vector<int> input, size_t window_size, size_t expect) {
28     auto res = reduce(
29       window(
30         input,
31         [](int i) { return makeFuture(i); },
32         2),
33       0,
34       [](int sum, const Try<int>& b) {
35         return sum + *b;
36       }).get();
37     EXPECT_EQ(expect, res);
38   };
39   {
40     // 2 in-flight at a time
41     std::vector<int> input = {1, 2, 3};
42     fn(input, 2, 6);
43   }
44   {
45     // 4 in-flight at a time
46     std::vector<int> input = {1, 2, 3};
47     fn(input, 4, 6);
48   }
49   {
50     // empty inpt
51     std::vector<int> input;
52     fn(input, 1, 0);
53   }
54   {
55     // int -> Future<void>
56     auto res = reduce(
57       window(
58         std::vector<int>({1, 2, 3}),
59         [](int i) { return makeFuture(); },
60         2),
61       0,
62       [](int sum, const Try<void>& b) {
63         EXPECT_TRUE(b.hasValue());
64         return sum + 1;
65       }).get();
66     EXPECT_EQ(3, res);
67   }
68   {
69     // string -> return Future<int>
70     auto res = reduce(
71       window(
72         std::vector<std::string>{"1", "2", "3"},
73         [](std::string s) { return makeFuture<int>(folly::to<int>(s)); },
74         2),
75       0,
76       [](int sum, const Try<int>& b) {
77         return sum + *b;
78       }).get();
79     EXPECT_EQ(6, res);
80   }
81 }