loop_test.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/loop.h"
  15. #include <gtest/gtest.h>
  16. #include "src/core/lib/promise/seq.h"
  17. namespace grpc_core {
  18. TEST(LoopTest, CountToFive) {
  19. int i = 0;
  20. Loop([&i]() -> LoopCtl<int> {
  21. i++;
  22. if (i < 5) return Continue();
  23. return i;
  24. })();
  25. EXPECT_EQ(i, 5);
  26. }
  27. TEST(LoopTest, FactoryCountToFive) {
  28. int i = 0;
  29. Loop([&i]() {
  30. return [&i]() -> LoopCtl<int> {
  31. i++;
  32. if (i < 5) return Continue();
  33. return i;
  34. };
  35. })();
  36. EXPECT_EQ(i, 5);
  37. }
  38. TEST(LoopTest, LoopOfSeq) {
  39. auto x =
  40. Loop(Seq([]() { return 42; }, [](int i) -> LoopCtl<int> { return i; }))();
  41. EXPECT_EQ(x, Poll<int>(42));
  42. }
  43. } // namespace grpc_core
  44. int main(int argc, char** argv) {
  45. ::testing::InitGoogleTest(&argc, argv);
  46. return RUN_ALL_TESTS();
  47. }