seq_test.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "src/core/lib/promise/seq.h"
  15. #include <gtest/gtest.h>
  16. namespace grpc_core {
  17. TEST(SeqTest, Immediate) {
  18. EXPECT_EQ(Seq([] { return 3; })(), 3);
  19. }
  20. TEST(SeqTest, OneThen) {
  21. auto initial = [] { return 3; };
  22. auto then = [](int i) { return [i]() { return i + 4; }; };
  23. EXPECT_EQ(Seq(initial, then)(), Poll<int>(7));
  24. }
  25. TEST(SeqTest, TwoTypedThens) {
  26. struct A {};
  27. struct B {};
  28. struct C {};
  29. auto initial = [] { return A{}; };
  30. auto next1 = [](A) { return []() { return B{}; }; };
  31. auto next2 = [](B) { return []() { return C{}; }; };
  32. EXPECT_FALSE(absl::holds_alternative<Pending>(Seq(initial, next1, next2)()));
  33. }
  34. /* This does not compile, but is useful for testing error messages generated
  35. TEST(SeqTest, MisTypedThen) {
  36. struct A {};
  37. struct B {};
  38. auto initial = [] { return A{}; };
  39. auto next = [](B) { return []() { return B{}; }; };
  40. Seq(initial, next)().take();
  41. }
  42. */
  43. TEST(SeqTest, TwoThens) {
  44. auto initial = [] { return std::string("a"); };
  45. auto next1 = [](std::string i) { return [i]() { return i + "b"; }; };
  46. auto next2 = [](std::string i) { return [i]() { return i + "c"; }; };
  47. EXPECT_EQ(Seq(initial, next1, next2)(), Poll<std::string>("abc"));
  48. }
  49. TEST(SeqTest, ThreeThens) {
  50. EXPECT_EQ(Seq([] { return std::string("a"); },
  51. [](std::string i) { return [i]() { return i + "b"; }; },
  52. [](std::string i) { return [i]() { return i + "c"; }; },
  53. [](std::string i) { return [i]() { return i + "d"; }; })(),
  54. Poll<std::string>("abcd"));
  55. }
  56. struct Big {
  57. int x[256];
  58. void YesItIsUnused() const {}
  59. };
  60. TEST(SeqTest, SaneSizes) {
  61. auto x = Big();
  62. auto p1 = Seq(
  63. [x] {
  64. x.YesItIsUnused();
  65. return 1;
  66. },
  67. [](int) {
  68. auto y = Big();
  69. return [y]() {
  70. y.YesItIsUnused();
  71. return 2;
  72. };
  73. });
  74. EXPECT_GE(sizeof(p1), sizeof(Big));
  75. EXPECT_LT(sizeof(p1), 2 * sizeof(Big));
  76. }
  77. TEST(SeqIterTest, Accumulate) {
  78. std::vector<int> v{1, 2, 3, 4, 5};
  79. EXPECT_EQ(SeqIter(v.begin(), v.end(), 0,
  80. [](int cur, int next) {
  81. return [cur, next]() { return cur + next; };
  82. })(),
  83. Poll<int>(15));
  84. }
  85. } // namespace grpc_core
  86. int main(int argc, char** argv) {
  87. ::testing::InitGoogleTest(&argc, argv);
  88. return RUN_ALL_TESTS();
  89. }