seq_test.cc
Go to the documentation of this file.
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 
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 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
grpc_core::Big::YesItIsUnused
void YesItIsUnused() const
Definition: seq_test.cc:72
C
#define C(x)
Definition: abseil-cpp/absl/hash/internal/city_test.cc:49
grpc_core
Definition: call_metric_recorder.h:31
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
seq.h
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
xds_interop_client.int
int
Definition: xds_interop_client.py:113
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc_core::Big::x
int x[256]
Definition: seq_test.cc:71
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
memory_diff.cur
def cur
Definition: memory_diff.py:83
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::Big
Definition: seq_test.cc:70
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
A
Definition: miscompile_with_no_unique_address_test.cc:23
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
main
int main(int argc, char **argv)
Definition: seq_test.cc:104
grpc_core::Immediate
promise_detail::Immediate< T > Immediate(T value)
Definition: promise/promise.h:73
grpc_core::SeqIter
promise_detail::BasicSeqIter< promise_detail::SeqTraits, Factory, Argument, Iter > SeqIter(Iter begin, Iter end, Argument argument, Factory factory)
Definition: seq.h:79
grpc_core::Seq
promise_detail::Seq< Functors... > Seq(Functors... functors)
Definition: seq.h:62
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:10