aboutsummaryrefslogtreecommitdiff
path: root/test/std/experimental/language.support/support.coroutines/end.to.end/bool_await_suspend.sh.cpp
blob: e51ac67f0fefb6d1cb3143f8e84973db0ae920d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11
// REQUIRES: fcoroutines-ts

// FIXME: When run under UBSAN this test hits an assertion inside Clang
// XFAIL: ubsan

// RUN: %build -fcoroutines-ts
// RUN: %run

#include <experimental/coroutine>
#include <cassert>

using namespace std::experimental;

struct coro_t {
  struct promise_type {
    coro_t get_return_object() {
      return coroutine_handle<promise_type>::from_promise(*this);
    }
    suspend_never initial_suspend() { return {}; }
    suspend_never final_suspend() { return {}; }
    void return_void(){}
    void unhandled_exception() {}
  };
  coro_t(coroutine_handle<promise_type> hh) : h(hh) {}
  coroutine_handle<promise_type> h;
};

struct NoSuspend {
  bool await_ready() { return false; }
  void await_resume() {}
  template <typename F> bool await_suspend(F) { return false; }
};

struct DoSuspend {
  bool await_ready() { return false; }
  void await_resume() {}
  template <typename F> bool await_suspend(F) { return true; }
};

bool f_started, f_resumed = false;
coro_t f() {
  f_started = true;
  co_await DoSuspend{};
  f_resumed = true;
}

bool g_started, g_resumed = false;
coro_t g() {
  g_started = true;
  co_await NoSuspend{};
  g_resumed = true;
}

int main() {
  assert(!f_started && !f_resumed && !g_started && !g_resumed);
  auto fret = f();
  assert(f_started && !f_resumed);
  fret.h.destroy();
  assert(f_started && !f_resumed);
  g();
  assert(g_started && g_resumed);
}