Skip to content

Commit 9e727c5

Browse files
authored
factored the optional_sender into a header shared by multiple tests (#233)
1 parent 82f2c42 commit 9e727c5

File tree

3 files changed

+70
-75
lines changed

3 files changed

+70
-75
lines changed

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

0 commit comments

Comments
 (0)