forked from NVIDIA/stdexec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_env.cpp
More file actions
152 lines (126 loc) · 4.01 KB
/
Copy pathtest_env.cpp
File metadata and controls
152 lines (126 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2021-2022 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <exec/env.hpp>
#include <stdexec/execution.hpp>
#include <test_common/catch2.hpp>
#include <type_traits>
#include <utility>
namespace
{
// Two dummy properties:
constexpr struct Foo
: STDEXEC::__query<Foo>
, STDEXEC::forwarding_query_t
{
using STDEXEC::__query<Foo>::operator();
} foo{};
constexpr struct Bar : STDEXEC::__query<Bar>
{
static constexpr auto query(STDEXEC::forwarding_query_t) noexcept -> bool
{
return true;
}
} bar{};
TEST_CASE("Test make_env works", "[env]")
{
auto e = STDEXEC::prop{foo, 42};
CHECK(foo(e) == 42);
auto e2 = exec::make_env(e, STDEXEC::prop{bar, 43});
CHECK(foo(e2) == 42);
CHECK(bar(e2) == 43);
auto e3 = exec::make_env(e2, STDEXEC::prop{foo, 44});
CHECK(foo(e3) == 44);
CHECK(bar(e3) == 43);
auto e4 = exec::without(e3, foo);
STATIC_REQUIRE(!std::invocable<Foo, decltype(e4)>);
CHECK(bar(e4) == 43);
}
TEST_CASE("without propagates environment move exceptions", "[env]")
{
struct missing_query : STDEXEC::__query<missing_query>
{
using STDEXEC::__query<missing_query>::operator();
};
struct without_move_error
{};
struct throwing_env
{
explicit throwing_env(bool* throw_on_move) noexcept
: throw_on_move_{throw_on_move}
{}
throwing_env(throwing_env const & other) noexcept
: throw_on_move_{other.throw_on_move_}
{}
throwing_env(throwing_env&& other)
: throw_on_move_{other.throw_on_move_}
{
#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
if (*throw_on_move_)
{
throw without_move_error{};
}
#endif
}
int query(Foo) const noexcept
{
return 42;
}
bool* throw_on_move_;
};
bool throw_on_move = false;
throwing_env missing_env{&throw_on_move};
throwing_env queried_env{&throw_on_move};
STATIC_REQUIRE_FALSE(noexcept(exec::without(std::move(missing_env), missing_query{})));
STATIC_REQUIRE_FALSE(noexcept(exec::without(std::move(queried_env), foo)));
STATIC_REQUIRE(noexcept(exec::without(missing_env, missing_query{})));
STATIC_REQUIRE(noexcept(exec::without(queried_env, foo)));
#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
throw_on_move = true;
CHECK_THROWS_AS(exec::without(std::move(missing_env), missing_query{}), without_move_error);
CHECK_THROWS_AS(exec::without(std::move(queried_env), foo), without_move_error);
#endif
struct throwing_copy_error
{};
struct throwing_copy_env
{
explicit throwing_copy_env(bool* throw_on_copy) noexcept
: throw_on_copy_{throw_on_copy}
{}
throwing_copy_env(throwing_copy_env const & other)
: throw_on_copy_{other.throw_on_copy_}
{
#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
if (*throw_on_copy_)
{
throw throwing_copy_error{};
}
#endif
}
throwing_copy_env(throwing_copy_env&&) noexcept = default;
bool* throw_on_copy_;
};
bool throw_on_copy = false;
throwing_copy_env copy_env{&throw_on_copy};
STATIC_REQUIRE(
std::is_same_v<decltype(exec::without(copy_env, missing_query{})), throwing_copy_env&>);
STATIC_REQUIRE(noexcept(exec::without(copy_env, missing_query{})));
#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
throw_on_copy = true;
auto&& result = exec::without(copy_env, missing_query{});
CHECK(&result == ©_env);
#endif
}
} // namespace