Skip to content

Commit f160abf

Browse files
committed
factored the optional_sender into a header shared by multiple tests
1 parent 82f2c42 commit f160abf

3 files changed

Lines changed: 69 additions & 75 deletions

File tree

tests/beman/execution/exec-stopped-as-error.test.cpp

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <optional>
66
#include <system_error>
77
#include <test/execution.hpp>
8+
#include <test/optional_sender.hpp>
89
#ifdef BEMAN_HAS_MODULES
910
import beman.execution;
1011
import beman.execution.detail;
@@ -18,41 +19,6 @@ import beman.execution.detail;
1819

1920
// ----------------------------------------------------------------------------
2021
namespace {
21-
template <typename T>
22-
struct optional_sender {
23-
using sender_concept = test_std::sender_t;
24-
using completion_signatures = test_std::completion_signatures<test_std::set_value_t(T), test_std::set_stopped_t()>;
25-
26-
optional_sender() = default;
27-
28-
explicit optional_sender(T value) noexcept : opt(value) {}
29-
30-
template <typename, typename...>
31-
static consteval auto get_completion_signatures() noexcept -> completion_signatures {
32-
return {};
33-
}
34-
35-
template <test_std::receiver_of<completion_signatures> Rcvr>
36-
auto connect(Rcvr rcvr) && noexcept -> auto {
37-
struct state {
38-
using operation_state_concept = test_std::operation_state_t;
39-
auto start() & noexcept -> void {
40-
test::use_type<operation_state_concept>(); // make -Werror=unused-local-typedefs happy
41-
if (opt_) {
42-
test_std::set_value(std::move(rcvr_), *opt_);
43-
} else {
44-
test_std::set_stopped(std::move(rcvr_));
45-
}
46-
}
47-
48-
Rcvr rcvr_;
49-
std::optional<T> opt_;
50-
};
51-
return state{rcvr, std::move(opt)};
52-
}
53-
54-
std::optional<T> opt;
55-
};
5622

5723
auto test_stopped_as_error_signatures() -> void {
5824
test_std::sender auto sndr1 = test_std::just(42) | test_std::stopped_as_error(-1);
@@ -61,7 +27,7 @@ auto test_stopped_as_error_signatures() -> void {
6127
auto [i] = test_std::sync_wait(std::move(sndr1)).value();
6228
ASSERT(i == 42);
6329

64-
test_std::sender auto sndr2 = test_std::stopped_as_error(optional_sender<int>{}, 114514);
30+
test_std::sender auto sndr2 = test_std::stopped_as_error(test::optional_sender<int>{}, 114514);
6531
using sigs_of_sndr2 = test_std::completion_signatures_of_t<decltype(sndr2), test_detail::sync_wait_env>;
6632
static_assert(
6733
std::same_as<sigs_of_sndr2,
@@ -75,8 +41,8 @@ auto test_stopped_as_error_signatures() -> void {
7541

7642
auto test_stopped_as_std_error_code() -> void {
7743
try {
78-
auto snd =
79-
test_std::stopped_as_error(optional_sender<int>{}, std::make_error_code(std::errc::operation_canceled));
44+
auto snd = test_std::stopped_as_error(test::optional_sender<int>{},
45+
std::make_error_code(std::errc::operation_canceled));
8046
test_std::sync_wait(std::move(snd));
8147
} catch (const std::system_error& e) {
8248
ASSERT(e.code() == std::errc::operation_canceled);

tests/beman/execution/exec-stopped-as-optional.test.cpp

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <optional>
66
#include <system_error>
77
#include <test/execution.hpp>
8+
#include <test/optional_sender.hpp>
89
#ifdef BEMAN_HAS_MODULES
910
import beman.execution;
1011
import beman.execution.detail;
@@ -18,42 +19,6 @@ import beman.execution.detail;
1819

1920
// ----------------------------------------------------------------------------
2021
namespace {
21-
template <typename T>
22-
struct optional_sender {
23-
using sender_concept = test_std::sender_t;
24-
using completion_signatures = test_std::completion_signatures<test_std::set_value_t(T), test_std::set_stopped_t()>;
25-
26-
optional_sender() = default;
27-
28-
explicit optional_sender(T value) noexcept : opt(value) {}
29-
30-
template <typename, typename...>
31-
static consteval auto get_completion_signatures() noexcept -> completion_signatures {
32-
return {};
33-
}
34-
35-
template <test_std::receiver_of<completion_signatures> Rcvr>
36-
auto connect(Rcvr rcvr) && noexcept -> auto {
37-
struct state {
38-
using operation_state_concept = test_std::operation_state_t;
39-
auto start() & noexcept -> void {
40-
test::use_type<operation_state_concept>(); // make -Werror=unused-local-typedefs happy
41-
if (opt_) {
42-
test_std::set_value(std::move(rcvr_), *opt_);
43-
} else {
44-
test_std::set_stopped(std::move(rcvr_));
45-
}
46-
}
47-
48-
Rcvr rcvr_;
49-
std::optional<T> opt_;
50-
};
51-
return state{rcvr, std::move(opt)};
52-
}
53-
54-
std::optional<T> opt;
55-
};
56-
5722
auto test_stopped_as_optional() -> void {
5823
test_std::sender auto sndr1 = test_std::just(42) | test_std::stopped_as_optional();
5924
using sigs_of_sndr1 = test_std::completion_signatures_of_t<decltype(sndr1), test_detail::sync_wait_env>;
@@ -62,7 +27,7 @@ auto test_stopped_as_optional() -> void {
6227
auto [i] = test_std::sync_wait(std::move(sndr1)).value();
6328
ASSERT(i == 42);
6429

65-
test_std::sender auto sndr2 = test_std::stopped_as_optional(optional_sender<int>{});
30+
test_std::sender auto sndr2 = test_std::stopped_as_optional(test::optional_sender<int>{});
6631
using sigs_of_sndr2 = test_std::completion_signatures_of_t<decltype(sndr2), test_std::detail::sync_wait_env>;
6732
static_assert(
6833
std::same_as<sigs_of_sndr2, test_std::completion_signatures<test_std::set_value_t(std::optional<int>)>>);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// tests/beman/execution/include/optional_sender.hpp -*-C++-*-
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#ifndef INCLUDED_TESTS_BEMAN_EXECUTION_INCLUDE_OPTIONAL_SENDER
5+
#define INCLUDED_TESTS_BEMAN_EXECUTION_INCLUDE_OPTIONAL_SENDER
6+
7+
#include <test/execution.hpp>
8+
#include <optional>
9+
#include <utility>
10+
#ifdef BEMAN_HAS_MODULES
11+
import beman.execution;
12+
import beman.execution.detail;
13+
#else
14+
#include <beman/execution/detail/just.hpp>
15+
#include <beman/execution/detail/receiver.hpp>
16+
#include <beman/execution/detail/sender.hpp>
17+
#include <beman/execution/detail/stopped_as_error.hpp>
18+
#include <beman/execution/detail/sync_wait.hpp>
19+
#endif
20+
21+
// ----------------------------------------------------------------------------
22+
23+
namespace test {
24+
template <typename T>
25+
struct optional_sender {
26+
using sender_concept = test_std::sender_t;
27+
using completion_signatures = test_std::completion_signatures<test_std::set_value_t(T), test_std::set_stopped_t()>;
28+
29+
optional_sender() = default;
30+
31+
explicit optional_sender(T value) noexcept : opt(value) {}
32+
33+
template <typename, typename...>
34+
static consteval auto get_completion_signatures() noexcept -> completion_signatures {
35+
return {};
36+
}
37+
38+
template <test_std::receiver_of<completion_signatures> Rcvr>
39+
auto connect(Rcvr rcvr) && noexcept -> auto {
40+
struct state {
41+
using operation_state_concept = test_std::operation_state_t;
42+
auto start() & noexcept -> void {
43+
test::use_type<operation_state_concept>(); // make -Werror=unused-local-typedefs happy
44+
if (opt_) {
45+
test_std::set_value(std::move(rcvr_), *opt_);
46+
} else {
47+
test_std::set_stopped(std::move(rcvr_));
48+
}
49+
}
50+
51+
Rcvr rcvr_;
52+
std::optional<T> opt_;
53+
};
54+
return state{rcvr, std::move(opt)};
55+
}
56+
57+
std::optional<T> opt;
58+
};
59+
} // namespace test
60+
61+
// ----------------------------------------------------------------------------
62+
63+
#endif

0 commit comments

Comments
 (0)