aboutsummaryrefslogtreecommitdiff
path: root/test/std/experimental/language.support/support.coroutines/end.to.end/await_result.sh.cpp
blob: cca875d1a6ba58820d8c29db846be58b49ae9eb1 (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
// -*- 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

// 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() {
      coroutine_handle<promise_type>{};
      return {};
    }
    suspend_never initial_suspend() { return {}; }
    suspend_never final_suspend() { return {}; }
    void return_void(){}
    static void unhandled_exception() {}
  };
};

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


struct A {
  ~A(){}
  bool await_ready() { return true; }
  int await_resume() { return 42; }
  template <typename F> void await_suspend(F) {}
};

int last_value = -1;
void set_value(int x) {
  last_value = x;
}

coro_t f(int n) {
  if (n == 0) {
    set_value(0);
    co_return;
  }
  int val = co_await A{};
  set_value(42);
}

coro_t g() { B val = co_await B{}; }

int main() {
  last_value = -1;
  f(0);
  assert(last_value == 0);
  f(1);
  assert(last_value == 42);
}