aboutsummaryrefslogtreecommitdiff
path: root/test/std/experimental/language.support/support.coroutines/end.to.end/fullexpr-dtor.sh.cpp
blob: c7e34fe919e1318f56d516716005c7bb3673b9c5 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// -*- 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>

#include "test_macros.h"

using namespace std::experimental;

int alive = 0;
int ctor_called = 0;
int dtor_called = 0;
void reset() {
  assert(alive == 0);
  alive = 0;
  ctor_called = 0;
  dtor_called = 0;
}
struct Noisy {
  Noisy() { ++alive; ++ctor_called; }
  ~Noisy() { --alive; ++dtor_called; }
#if TEST_STD_VER > 14
  Noisy(Noisy const&) = delete;
#else
  // FIXME: This test depends on copy elision taking place in C++14
  // (pre-c++17 guaranteed copy elision)
  Noisy(Noisy const&);
#endif
};

struct Bug {
  bool await_ready() { return true; }
  void await_suspend(std::experimental::coroutine_handle<>) {}
  Noisy await_resume() { return {}; }
};
struct coro2 {
  struct promise_type {
    suspend_never initial_suspend() { return{}; }
    suspend_never final_suspend() { return{}; }
    coro2 get_return_object() { return{}; }
    void return_void() {}
    Bug yield_value(int) { return {}; }
    void unhandled_exception() {}
  };
};

// Checks that destructors are correctly invoked for the object returned by
// coawait.
coro2 a() {
  reset();
  {
    auto x = co_await Bug{};
    assert(alive == 1);
    assert(ctor_called == 1);
    assert(dtor_called == 0);
  }
  assert(alive == 0);
  assert(dtor_called == 1);
}

coro2 b() {
  reset();
  {
    co_await Bug{};
    assert(ctor_called == 1);
    assert(dtor_called == 1);
    assert(alive == 0);
  }
  assert(ctor_called == 1);
  assert(dtor_called == 1);
  assert(alive == 0);

}

coro2 c() {
  reset();
  {
    auto x = co_yield 42;
    assert(alive == 1);
    assert(ctor_called == 1);
    assert(dtor_called == 0);
  }
  assert(alive == 0);
  assert(ctor_called == 1);
  assert(dtor_called == 1);
}

coro2 d() {
  reset();
  {
    co_yield 42;
    assert(ctor_called == 1);
    assert(dtor_called == 1);
    assert(alive == 0);
  }
  assert(alive == 0);
  assert(ctor_called == 1);
  assert(dtor_called == 1);
}

int main() {
  a();
  b();
  c();
  d();
}