-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Test <coroutine> #1215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StephanTLavavej
merged 17 commits into
microsoft:master
from
StephanTLavavej:coroutine_test
Aug 22, 2020
Merged
Test <coroutine> #1215
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d73c3a7
Add P0912R5_coroutine.
StephanTLavavej e723342
Original test case from @joemmett.
joemmett 3b51dfc
clang-format.
StephanTLavavej 13e3d5d
Add braces and newlines.
StephanTLavavej 73c9189
Rename to Task and Promise, add promise_type typedef.
StephanTLavavej eb1e67f
Rename prev to previous (avoiding std::prev confusion).
StephanTLavavej ceb06ec
using namespace std;
StephanTLavavej 27d6977
Use assert.
StephanTLavavej 853a458
Task's move ctor already deletes the copy ctor.
StephanTLavavej 018fd19
Add const.
StephanTLavavej 1fe4d47
Test triangular numbers to do slightly nontrivial work.
StephanTLavavej 60c7087
Comment punctuation.
StephanTLavavej 174226d
Avoid negated test, extract h.promise().previous.
StephanTLavavej fd452a0
Init result{-1000}, rename to pre, assert(h.done()).
StephanTLavavej e0d577f
Track g_tasks_destroyed.
StephanTLavavej 5e755c2
Add banner, guard __cpp_lib_coroutine >= 201902L.
StephanTLavavej 7d30fef
Code review feedback.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <version> // TRANSITION, P0912R5 Library Support For Coroutines | ||
| #if defined(__cpp_lib_coroutine) && __cpp_lib_coroutine >= 201902L // TRANSITION, P0912R5 Library Support For Coroutines | ||
|
|
||
| #include <assert.h> | ||
| #include <coroutine> | ||
| #include <exception> | ||
| using namespace std; | ||
|
|
||
| int g_tasks_destroyed{0}; | ||
|
|
||
| struct Task { | ||
| struct Promise { | ||
| int result{-1000}; | ||
| coroutine_handle<> previous; | ||
|
|
||
| Task get_return_object() { | ||
| return Task{*this}; | ||
| } | ||
|
|
||
| suspend_always initial_suspend() { | ||
| return {}; | ||
| } | ||
|
|
||
| auto final_suspend() noexcept { | ||
| struct Awaiter { | ||
| bool await_ready() noexcept { | ||
| return false; | ||
| } | ||
|
|
||
| void await_resume() noexcept {} | ||
|
|
||
| coroutine_handle<> await_suspend(coroutine_handle<Promise> h) noexcept { | ||
| if (auto& pre = h.promise().previous; pre) { | ||
| return pre; // resume awaiting coroutine | ||
| } | ||
|
|
||
| // If there is no previous coroutine to resume, we've reached the outermost coroutine. | ||
| // Return a noop coroutine to allow control to return back to the caller. | ||
| return noop_coroutine(); | ||
| } | ||
| }; | ||
|
|
||
| return Awaiter{}; | ||
| } | ||
|
|
||
| void return_value(const int v) { | ||
| result = v; | ||
| } | ||
|
|
||
| void unhandled_exception() noexcept { | ||
| terminate(); | ||
| } | ||
| }; | ||
|
|
||
| using promise_type = Promise; | ||
|
|
||
| bool await_ready() const noexcept { | ||
| return false; | ||
| } | ||
|
|
||
| int await_resume() { | ||
| return coro.promise().result; | ||
| } | ||
|
|
||
| auto await_suspend(coroutine_handle<> enclosing) { | ||
| coro.promise().previous = enclosing; | ||
| return coro; // resume ourselves | ||
| } | ||
|
|
||
| Task(Task&& rhs) noexcept : coro(rhs.coro) { | ||
| rhs.coro = nullptr; | ||
| } | ||
|
|
||
| explicit Task(Promise& p) : coro(coroutine_handle<Promise>::from_promise(p)) {} | ||
|
|
||
| ~Task() { | ||
| ++g_tasks_destroyed; | ||
|
|
||
| if (coro) { | ||
| coro.destroy(); | ||
| } | ||
| } | ||
|
|
||
| coroutine_handle<Promise> coro; | ||
| }; | ||
|
|
||
| Task triangular_number(const int n) { | ||
| if (n == 0) { | ||
| co_return 0; | ||
| } | ||
|
|
||
| co_return n + co_await triangular_number(n - 1); | ||
| } | ||
|
|
||
| int main() { | ||
| assert(g_tasks_destroyed == 0); | ||
|
|
||
| { | ||
| Task t = triangular_number(10); | ||
| coroutine_handle<> h = t.coro; | ||
|
|
||
| assert(h == t.coro); | ||
| assert(h); | ||
| assert(!h.done()); | ||
|
|
||
| h(); | ||
|
|
||
| assert(h == t.coro); | ||
| assert(h); | ||
| assert(h.done()); | ||
|
|
||
| assert(g_tasks_destroyed == 10); // triangular_number() called for [0, 9] | ||
|
|
||
| const int val = t.coro.promise().result; | ||
|
|
||
| assert(val == 55); | ||
| } | ||
|
|
||
| assert(g_tasks_destroyed == 11); // triangular_number() called for [0, 10] | ||
| } | ||
|
|
||
| #else // ^^^ test <coroutine> ^^^ / vvv don't test <coroutine> vvv | ||
| int main() {} | ||
| #endif // TRANSITION, P0912R5 Library Support For Coroutines | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.