Home | History | Annotate | Download | only in coroutine.handle.prom
      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 
     11 // UNSUPPORTED: c++98, c++03, c++11
     12 
     13 // <experimental/coroutine>
     14 
     15 // template <class Promise>
     16 // struct coroutine_handle<Promise>;
     17 
     18 // Promise& promise() const
     19 
     20 #include <experimental/coroutine>
     21 #include <type_traits>
     22 #include <memory>
     23 #include <utility>
     24 #include <cstdint>
     25 #include <cassert>
     26 
     27 #include "test_macros.h"
     28 
     29 namespace coro = std::experimental;
     30 
     31 struct MyCoro {
     32   struct promise_type {
     33     void unhandled_exception() {}
     34     void return_void() {}
     35     coro::suspend_never initial_suspend() { return {}; }
     36     coro::suspend_never final_suspend() { return {}; }
     37     MyCoro get_return_object() {
     38       do_runtime_test();
     39       return {};
     40     }
     41     void do_runtime_test() {
     42       // Test that a coroutine_handle<const T> can be created from a const
     43       // promise_type and that it represents the same coroutine as
     44       // coroutine_handle<T>
     45       using CH = coro::coroutine_handle<promise_type>;
     46       using CCH = coro::coroutine_handle<const promise_type>;
     47       const auto &cthis = *this;
     48       CH h = CH::from_promise(*this);
     49       CCH h2 = CCH::from_promise(*this);
     50       CCH h3 = CCH::from_promise(cthis);
     51       assert(&h.promise() == this);
     52       assert(&h2.promise() == this);
     53       assert(&h3.promise() == this);
     54       assert(h.address() == h2.address());
     55       assert(h2.address() == h3.address());
     56     }
     57   };
     58 };
     59 
     60 MyCoro do_runtime_test() {
     61   co_await coro::suspend_never{};
     62 }
     63 
     64 template <class Promise>
     65 void do_test(coro::coroutine_handle<Promise>&& H) {
     66 
     67   // FIXME Add a runtime test
     68   {
     69     ASSERT_SAME_TYPE(decltype(H.promise()), Promise&);
     70     LIBCPP_ASSERT_NOT_NOEXCEPT(H.promise());
     71   }
     72   {
     73     auto const& CH = H;
     74     ASSERT_SAME_TYPE(decltype(CH.promise()), Promise&);
     75     LIBCPP_ASSERT_NOT_NOEXCEPT(CH.promise());
     76   }
     77 }
     78 
     79 int main()
     80 {
     81   do_test(coro::coroutine_handle<int>{});
     82   do_test(coro::coroutine_handle<const int>{});
     83   do_runtime_test();
     84 }
     85