|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <concepts> |
| 6 | +#include <execution> |
| 7 | +#include <tuple> |
| 8 | +using namespace std; |
| 9 | +using execution::connect, execution::operation_state, execution::receiver_of, execution::sender_of, execution::start, |
| 10 | + execution::typed_sender; |
| 11 | + |
| 12 | +struct Tracker { |
| 13 | + bool set_values_called = false; |
| 14 | + bool set_error_called = false; |
| 15 | + bool set_done_called = false; |
| 16 | +}; |
| 17 | +struct Dummy {}; |
| 18 | +struct CustomError {}; |
| 19 | + |
| 20 | +enum class DoesThrowOnSetValue : bool { No, Yes }; |
| 21 | +template <DoesThrowOnSetValue ThrowOnSetValue, class... Types> |
| 22 | +struct test_receiver { |
| 23 | + constexpr test_receiver(Tracker& tracker) noexcept : _tracker(tracker) {} |
| 24 | + friend constexpr auto tag_invoke(execution::set_done_t, test_receiver rec) noexcept { |
| 25 | + rec._tracker.set_done_called = true; |
| 26 | + } |
| 27 | + friend auto tag_invoke(execution::set_error_t, test_receiver rec, exception_ptr) noexcept { |
| 28 | + rec._tracker.set_error_called = true; |
| 29 | + } |
| 30 | + friend constexpr auto tag_invoke(execution::set_error_t, test_receiver rec, CustomError) noexcept { |
| 31 | + rec._tracker.set_error_called = true; |
| 32 | + } |
| 33 | + friend constexpr auto tag_invoke(execution::set_value_t, test_receiver rec, Types...) noexcept( |
| 34 | + ThrowOnSetValue == DoesThrowOnSetValue::No) { |
| 35 | + rec._tracker.set_values_called = true; |
| 36 | + if constexpr (ThrowOnSetValue == DoesThrowOnSetValue::Yes) { |
| 37 | + throw 42; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + struct fake_operation_state { |
| 42 | + friend constexpr void tag_invoke(execution::start_t, fake_operation_state&) noexcept {} |
| 43 | + }; |
| 44 | + |
| 45 | + Tracker& _tracker; |
| 46 | +}; |
| 47 | +template <class... Types> |
| 48 | +using nothrow_receiver = test_receiver<DoesThrowOnSetValue::No, Types...>; |
| 49 | +template <class... Types> |
| 50 | +using throws_on_set_values_receiver = test_receiver<DoesThrowOnSetValue::Yes, Types...>; |
| 51 | + |
| 52 | +static_assert(receiver_of<nothrow_receiver<int>, int>); |
| 53 | +static_assert(operation_state<nothrow_receiver<int>::fake_operation_state>); |
| 54 | +static_assert(receiver_of<throws_on_set_values_receiver<int>, int>); |
| 55 | +static_assert(operation_state<throws_on_set_values_receiver<int>::fake_operation_state>); |
| 56 | + |
| 57 | +constexpr bool test_just() { |
| 58 | + { |
| 59 | + Tracker tracker; |
| 60 | + const sender_of<int> auto sender = execution::just(5); |
| 61 | + operation_state auto state = connect(sender, nothrow_receiver<int>{tracker}); |
| 62 | + assert(!tracker.set_values_called); |
| 63 | + start(state); |
| 64 | + assert(tracker.set_values_called); |
| 65 | + |
| 66 | + static_assert(!invocable<execution::connect_t, decltype(sender), nothrow_receiver<Dummy>>); |
| 67 | + } |
| 68 | + |
| 69 | + { |
| 70 | + Tracker tracker; |
| 71 | + operation_state auto state = connect(execution::just(5), nothrow_receiver<int>{tracker}); |
| 72 | + assert(!tracker.set_values_called); |
| 73 | + start(state); |
| 74 | + assert(tracker.set_values_called); |
| 75 | + } |
| 76 | + |
| 77 | + { |
| 78 | + Tracker tracker; |
| 79 | + const sender_of<int, Dummy> auto sender = execution::just(5, Dummy{}); |
| 80 | + operation_state auto state = connect(sender, nothrow_receiver<int, Dummy>{tracker}); |
| 81 | + assert(!tracker.set_values_called); |
| 82 | + start(state); |
| 83 | + assert(tracker.set_values_called); |
| 84 | + |
| 85 | + static_assert(!invocable<execution::connect_t, decltype(sender), nothrow_receiver<Dummy, int>>); |
| 86 | + } |
| 87 | + |
| 88 | + if (!is_constant_evaluated()) { |
| 89 | + Tracker tracker; |
| 90 | + operation_state auto state = connect(execution::just(5), throws_on_set_values_receiver<int>{tracker}); |
| 91 | + assert(!tracker.set_values_called); |
| 92 | + assert(!tracker.set_error_called); |
| 93 | + start(state); |
| 94 | + assert(tracker.set_values_called); |
| 95 | + assert(tracker.set_error_called); |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
| 99 | +} |
| 100 | + |
| 101 | +int main() { |
| 102 | + test_just(); |
| 103 | + static_assert(test_just()); |
| 104 | +} |
0 commit comments