coroutine-support.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
  3. Boost Software License - Version 1.0 - August 17th, 2003
  4. Permission is hereby granted, free of charge, to any person or organization
  5. obtaining a copy of the software and accompanying documentation covered by
  6. this license (the "Software") to use, reproduce, display, distribute,
  7. execute, and transmit the Software, and to prepare derivative works of the
  8. Software, and to permit third-parties to whom the Software is furnished to
  9. do so, all subject to the following:
  10. The copyright notices in the Software and this entire statement, including
  11. the above license grant, this restriction and the following disclaimer,
  12. must be included in all copies of the Software, in whole or in part, and
  13. all derivative works of the Software, unless such copies or derivative
  14. works are solely in the form of machine-executable object code generated by
  15. a source language processor.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  19. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  20. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. DEALINGS IN THE SOFTWARE.
  23. */
  24. #if defined(__cpp_coroutines)
  25. #include <boost/outcome/coroutine_support.hpp>
  26. #include <boost/outcome/outcome.hpp>
  27. #include <boost/outcome/try.hpp>
  28. #include <boost/test/unit_test.hpp>
  29. #include <boost/test/unit_test_monitor.hpp>
  30. namespace coroutines
  31. {
  32. template <class T> using eager = BOOST_OUTCOME_V2_NAMESPACE::awaitables::eager<T>;
  33. template <class T> using lazy = BOOST_OUTCOME_V2_NAMESPACE::awaitables::lazy<T>;
  34. template <class T, class E = boost::system::error_code> using result = BOOST_OUTCOME_V2_NAMESPACE::result<T, E>;
  35. inline eager<result<int>> eager_int(int x) { co_return x + 1; }
  36. inline lazy<result<int>> lazy_int(int x) { co_return x + 1; }
  37. inline eager<result<int>> eager_error() { co_return boost::system::errc::not_enough_memory; }
  38. inline lazy<result<int>> lazy_error() { co_return boost::system::errc::not_enough_memory; }
  39. inline eager<result<void>> eager_void() { co_return boost::system::errc::not_enough_memory; }
  40. inline lazy<result<void>> lazy_void() { co_return boost::system::errc::not_enough_memory; }
  41. template <class U, class... Args> inline eager<result<std::string>> eager_coawait(U &&f, Args... args)
  42. {
  43. BOOST_OUTCOME_CO_TRY(co_await f(args...));
  44. co_return "hi";
  45. }
  46. template <class U, class... Args> inline lazy<result<std::string>> lazy_coawait(U &&f, Args... args)
  47. {
  48. BOOST_OUTCOME_CO_TRY(co_await f(args...));
  49. co_return "hi";
  50. }
  51. #ifndef BOOST_NO_EXCEPTIONS
  52. struct custom_exception_type
  53. {
  54. };
  55. inline lazy<result<int, boost::exception_ptr>> result_exception(boost::exception_ptr e)
  56. {
  57. boost::rethrow_exception(e);
  58. co_return 5;
  59. }
  60. inline lazy<BOOST_OUTCOME_V2_NAMESPACE::outcome<int>> outcome_exception(boost::exception_ptr e)
  61. {
  62. boost::rethrow_exception(e);
  63. co_return 5;
  64. }
  65. #endif
  66. inline eager<int> eager_int2(int x) { co_return x + 1; }
  67. inline lazy<int> lazy_int2(int x) { co_return x + 1; }
  68. inline eager<void> eager_void2() { co_return; }
  69. inline lazy<void> lazy_void2() { co_return; }
  70. } // namespace coroutines
  71. BOOST_OUTCOME_AUTO_TEST_CASE(works_result_coroutine, "Tests that results are eager and lazy awaitable")
  72. {
  73. using namespace coroutines;
  74. auto eager_await = [](auto t) { return t.await_resume(); };
  75. auto lazy_await = [](auto t) {
  76. t.await_suspend({});
  77. return t.await_resume();
  78. };
  79. BOOST_CHECK(eager_await(eager_int(5)).value() == 6);
  80. BOOST_CHECK(lazy_await(lazy_int(5)).value() == 6);
  81. BOOST_CHECK(eager_await(eager_error()).error() == boost::system::errc::not_enough_memory);
  82. BOOST_CHECK(lazy_await(lazy_error()).error() == boost::system::errc::not_enough_memory);
  83. BOOST_CHECK(eager_await(eager_void()).error() == boost::system::errc::not_enough_memory);
  84. BOOST_CHECK(lazy_await(lazy_void()).error() == boost::system::errc::not_enough_memory);
  85. BOOST_CHECK(eager_await(eager_coawait(eager_int, 5)).value() == "hi");
  86. BOOST_CHECK(lazy_await(lazy_coawait(lazy_int, 5)).value() == "hi");
  87. #ifndef BOOST_NO_EXCEPTIONS
  88. auto e = boost::copy_exception(custom_exception_type());
  89. BOOST_CHECK_THROW(lazy_await(result_exception(e)).value(), custom_exception_type);
  90. BOOST_CHECK_THROW(lazy_await(outcome_exception(e)).value(), custom_exception_type);
  91. #endif
  92. BOOST_CHECK(eager_await(eager_int2(5)) == 6);
  93. BOOST_CHECK(lazy_await(lazy_int2(5)) == 6);
  94. eager_await(eager_void2());
  95. lazy_await(lazy_void2());
  96. }
  97. #else
  98. int main(void)
  99. {
  100. return 0;
  101. }
  102. #endif