Debian Codegraph @

test/core/promise/seq_test.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/promise/seq.h"
16
17 #include <string>
18 #include <vector>
19
20 #include "absl/types/variant.h"
21 #include "gtest/gtest.h"
22
23 namespace grpc_core {
24
25 TEST(SeqTest, Immediate) {
26 EXPECT_EQ(Seq([] { return 3; })(), 3);
27 }
28
29 TEST(SeqTest, OneThen) {
30 auto initial = [] { return 3; };
31 auto then = [](int i) { return [i]() { return i + 4; }; };
32 EXPECT_EQ(Seq(initial, then)(), Poll<int>(7));
33 }
34
35 TEST(SeqTest, TwoTypedThens) {
36 struct A {};
37 struct B {};
38 struct C {};
39 auto initial = [] { return A{}; };
40 auto next1 = [](A) { return []() { return B{}; }; };
41 auto next2 = [](B) { return []() { return C{}; }; };
42 EXPECT_FALSE(absl::holds_alternative<Pending>(Seq(initial, next1, next2)()));
43 }
44
45 /* This does not compile, but is useful for testing error messages generated
46 TEST(SeqTest, MisTypedThen) {
47 struct A {};
48 struct B {};
49 auto initial = [] { return A{}; };
50 auto next = [](B) { return []() { return B{}; }; };
51 Seq(initial, next)().take();
52 }
53 */
54
55 TEST(SeqTest, TwoThens) {
56 auto initial = [] { return std::string("a"); };
57 auto next1 = [](std::string i) { return [i]() { return i + "b"; }; };
58 auto next2 = [](std::string i) { return [i]() { return i + "c"; }; };
59 EXPECT_EQ(Seq(initial, next1, next2)(), Poll<std::string>("abc"));
60 }
61
62 TEST(SeqTest, ThreeThens) {
63 EXPECT_EQ(Seq([] { return std::string("a"); },
64 [](std::string i) { return [i]() { return i + "b"; }; },
65 [](std::string i) { return [i]() { return i + "c"; }; },
66 [](std::string i) { return [i]() { return i + "d"; }; })(),
67 Poll<std::string>("abcd"));
68 }
69
70 struct Big {
71 int x[256];
72 void YesItIsUnused() const {}
73 };
74
75 TEST(SeqTest, SaneSizes) {
76 auto x = Big();
77 auto p1 = Seq(
78 [x] {
79 x.YesItIsUnused();
80 return 1;
81 },
82 [](int) {
83 auto y = Big();
84 return [y]() {
85 y.YesItIsUnused();
86 return 2;
87 };
88 });
89 EXPECT_GE(sizeof(p1), sizeof(Big));
90 EXPECT_LT(sizeof(p1), 2 * sizeof(Big));
91 }
92
93 TEST(SeqIterTest, Accumulate) {
94 std::vector<int> v{1, 2, 3, 4, 5};
95 EXPECT_EQ(SeqIter(v.begin(), v.end(), 0,
96 [](int cur, int next) {
97 return [cur, next]() { return cur + next; };
98 })(),
99 Poll<int>(15));
100 }
101
102 } // namespace grpc_core
103
104 int main(int argc, char** argv) {
105 ::testing::InitGoogleTest(&argc, argv);
106 return RUN_ALL_TESTS();
107 }