task_of_value.pass.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. #include <experimental/task>
  12. #include <string>
  13. #include <vector>
  14. #include <memory>
  15. #include "../sync_wait.hpp"
  16. void test_returning_move_only_type()
  17. {
  18. auto move_only_async =
  19. [](bool x) -> std::experimental::task<std::unique_ptr<int>> {
  20. if (x) {
  21. auto p = std::make_unique<int>(123);
  22. co_return p; // Should be implicit std::move(p) here.
  23. }
  24. co_return std::make_unique<int>(456);
  25. };
  26. assert(*sync_wait(move_only_async(true)) == 123);
  27. assert(*sync_wait(move_only_async(false)) == 456);
  28. }
  29. void test_co_return_with_curly_braces()
  30. {
  31. auto t = []() -> std::experimental::task<std::tuple<int, std::string>>
  32. {
  33. co_return { 123, "test" };
  34. }();
  35. auto result = sync_wait(std::move(t));
  36. assert(std::get<0>(result) == 123);
  37. assert(std::get<1>(result) == "test");
  38. }
  39. void test_co_return_by_initialiser_list()
  40. {
  41. auto t = []() -> std::experimental::task<std::vector<int>>
  42. {
  43. co_return { 2, 10, -1 };
  44. }();
  45. auto result = sync_wait(std::move(t));
  46. assert(result.size() == 3);
  47. assert(result[0] == 2);
  48. assert(result[1] == 10);
  49. assert(result[2] == -1);
  50. }
  51. int main()
  52. {
  53. test_returning_move_only_type();
  54. test_co_return_with_curly_braces();
  55. test_co_return_by_initialiser_list();
  56. return 0;
  57. }