1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11#include <coroutine>
12#include <cassert>
13#include <memory>
14
15#include "test_macros.h"
16
17struct error_tag { };
18
19template <typename T, typename Error = int>
20struct expected {
21
22 struct Data {
23 Data() : val(), error() { }
24 Data(T v, Error e) : val(v), error(e) { }
25 T val;
26 Error error;
27 };
28 std::shared_ptr<Data> data;
29
30 expected(T val) : data(std::make_shared<Data>(val, Error())) {}
31 expected(error_tag, Error error) : data(std::make_shared<Data>(T(), error)) {}
32 expected(std::shared_ptr<Data> p) : data(p) {}
33
34 struct promise_type {
35 std::shared_ptr<Data> data;
36 expected get_return_object() { data = std::make_shared<Data>(); return {data}; }
37 std::suspend_never initial_suspend() { return {}; }
38 std::suspend_never final_suspend() noexcept { return {}; }
39 void return_value(T v) { data->val = v; data->error = {}; }
40 void unhandled_exception() {}
41 };
42
43 bool await_ready() { return !data->error; }
44 T await_resume() { return data->val; }
45 void await_suspend(std::coroutine_handle<promise_type> h) {
46 h.promise().data->error = data->error;
47 h.destroy();
48 }
49
50 T const& value() { return data->val; }
51 Error const& error() { return data->error; }
52};
53
54expected<int> g() { return {0}; }
55expected<int> h() { return {error_tag{}, 42}; }
56
57extern "C" void print(int);
58
59bool f1_started, f1_resumed = false;
60expected<int> f1() {
61 f1_started = true;
62 (void)(co_await g());
63 f1_resumed = true;
64 co_return 100;
65}
66
67bool f2_started, f2_resumed = false;
68expected<int> f2() {
69 f2_started = true;
70 (void)(co_await h());
71 f2_resumed = true;
72 co_return 200;
73}
74
75int main(int, char**) {
76 auto c1 = f1();
77 assert(f1_started && f1_resumed);
78 assert(c1.value() == 100);
79 assert(c1.error() == 0);
80
81 auto c2 = f2();
82 assert(f2_started && !f2_resumed);
83 assert(c2.value() == 0);
84 assert(c2.error() == 42);
85
86 return 0;
87}
88

source code of libcxx/test/std/language.support/support.coroutines/end.to.end/expected.pass.cpp