coroutine.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // coroutine.cpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Disable autolinking for unit tests.
  11. #if !defined(BOOST_ALL_NO_LIB)
  12. #define BOOST_ALL_NO_LIB 1
  13. #endif // !defined(BOOST_ALL_NO_LIB)
  14. // Test that header file is self-contained.
  15. #include <boost/asio/coroutine.hpp>
  16. #include "unit_test.hpp"
  17. // Must come after all other headers.
  18. #include <boost/asio/yield.hpp>
  19. //------------------------------------------------------------------------------
  20. // Coroutine completes via yield break.
  21. void yield_break_coro(boost::asio::coroutine& coro)
  22. {
  23. reenter (coro)
  24. {
  25. yield return;
  26. yield break;
  27. }
  28. }
  29. void yield_break_test()
  30. {
  31. boost::asio::coroutine coro;
  32. BOOST_ASIO_CHECK(!coro.is_complete());
  33. yield_break_coro(coro);
  34. BOOST_ASIO_CHECK(!coro.is_complete());
  35. yield_break_coro(coro);
  36. BOOST_ASIO_CHECK(coro.is_complete());
  37. }
  38. //------------------------------------------------------------------------------
  39. // Coroutine completes via return.
  40. void return_coro(boost::asio::coroutine& coro)
  41. {
  42. reenter (coro)
  43. {
  44. return;
  45. }
  46. }
  47. void return_test()
  48. {
  49. boost::asio::coroutine coro;
  50. return_coro(coro);
  51. BOOST_ASIO_CHECK(coro.is_complete());
  52. }
  53. //------------------------------------------------------------------------------
  54. // Coroutine completes via exception.
  55. void exception_coro(boost::asio::coroutine& coro)
  56. {
  57. reenter (coro)
  58. {
  59. throw 1;
  60. }
  61. }
  62. void exception_test()
  63. {
  64. boost::asio::coroutine coro;
  65. try { exception_coro(coro); } catch (int) {}
  66. BOOST_ASIO_CHECK(coro.is_complete());
  67. }
  68. //------------------------------------------------------------------------------
  69. // Coroutine completes by falling off the end.
  70. void fall_off_end_coro(boost::asio::coroutine& coro)
  71. {
  72. reenter (coro)
  73. {
  74. }
  75. }
  76. void fall_off_end_test()
  77. {
  78. boost::asio::coroutine coro;
  79. fall_off_end_coro(coro);
  80. BOOST_ASIO_CHECK(coro.is_complete());
  81. }
  82. //------------------------------------------------------------------------------
  83. BOOST_ASIO_TEST_SUITE
  84. (
  85. "coroutine",
  86. BOOST_ASIO_TEST_CASE(yield_break_test)
  87. BOOST_ASIO_TEST_CASE(return_test)
  88. BOOST_ASIO_TEST_CASE(exception_test)
  89. BOOST_ASIO_TEST_CASE(fall_off_end_test)
  90. )