kokkos-execution 0.0.1
Loading...
Searching...
No Matches
test_env.cpp
Go to the documentation of this file.
1#include <concepts>
2
3#include "gtest/gtest.h"
4
6
8
19
20namespace Tests::Impl {
21
22constexpr struct PropA : public stdexec::__query<PropA> {
24
25constexpr struct PropB : public stdexec::__query<PropB> {
27
28constexpr struct FwdPropC
29 : public stdexec::__query<FwdPropC>
30 , stdexec::forwarding_query_t {
31 using stdexec::__query<FwdPropC>::operator();
33
34constexpr struct FwdPropD
35 : public stdexec::__query<FwdPropD>
36 , stdexec::forwarding_query_t {
37 using stdexec::__query<FwdPropD>::operator();
39
41TEST(forwarding, queryable) {
42 static_assert([]() {
43 auto env = stdexec::env{
44 stdexec::prop{ prop_a, 123},
45 stdexec::prop{fwd_prop_c, 123}
46 };
47
48 static_assert(stdexec::__queryable_with<decltype(env), PropA>);
49 static_assert(stdexec::__queryable_with<decltype(env), FwdPropC>);
50
51 auto fwd_env = stdexec::__fwd_env(stdexec::__fwd_env(env));
52
53 static_assert(
54 std::same_as<
55 decltype(fwd_env),
56 stdexec::__env::__fwd<
57 stdexec::env<stdexec::prop<Tests::Impl::PropA, int>, stdexec::prop<Tests::Impl::FwdPropC, int>> &
58 >
59 >);
60
61 static_assert(!stdexec::__queryable_with<decltype(fwd_env), PropA>);
62 static_assert(stdexec::__queryable_with<decltype(fwd_env), FwdPropC>);
63
64 return true;
65 }());
66}
67
69TEST(upsert, set_prop_in_empty_env) {
70 constexpr auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_a, stdexec::env<>{}, 42);
71
72 static_assert(std::same_as<decltype(new_env), const stdexec::env<stdexec::prop<PropA, int>, stdexec::env<>>>);
73
74 constexpr auto value = prop_a(new_env);
75
76 static_assert(value == 42);
77}
78
80TEST(upsert, replace_prop_in_single_prop) {
81 static_assert([]() {
82 auto env = stdexec::prop{prop_a, 123};
83
84 auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_a, env, 42.);
85
86 auto value = prop_a(new_env);
87
88 static_assert(std::same_as<decltype(new_env), stdexec::prop<Tests::Impl::PropA, double>>);
89
90 return value == 42.;
91 }());
92}
93
95TEST(upsert, replace_prop_in_env_with_single_prop) {
96 static_assert([]() {
97 auto env = stdexec::env{
98 stdexec::prop{prop_a, 123},
99 };
100
101 auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_a, env, 42.);
102
103 auto value = prop_a(new_env);
104
105 static_assert(std::same_as<decltype(new_env), stdexec::env<stdexec::prop<Tests::Impl::PropA, double>>>);
106
107 return value == 42.;
108 }());
109}
110
112TEST(upsert, add_prop_to_another_prop) {
113 static_assert([]() {
114 auto env = stdexec::prop{prop_b, 123};
115
116 auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_a, env, 42.);
117
118 static_assert(std::same_as<
119 decltype(new_env),
120 stdexec::env<stdexec::prop<Tests::Impl::PropA, double>, stdexec::prop<Tests::Impl::PropB, int>>
121 >);
122
123 auto value = prop_a(new_env);
124
125 return value == 42.;
126 }());
127}
128
130TEST(upsert, replace_prop_in_env_with_two_props) {
131 static_assert([]() {
132 auto env = stdexec::env{
133 stdexec::prop{prop_b, 123},
134 stdexec::prop{prop_a, 123},
135 };
136
137 auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_a, env, 42.);
138
139 static_assert(std::same_as<
140 decltype(new_env),
141 stdexec::env<stdexec::prop<Tests::Impl::PropB, int>, stdexec::prop<Tests::Impl::PropA, double>>
142 >);
143
144 auto value = prop_a(new_env);
145
146 return value == 42.;
147 }());
148}
149
151TEST(upsert, replace_prop_in_forwarding_env_with_two_props) {
152 static_assert([]() {
153 auto env = stdexec::__fwd_env(
154 stdexec::env{
155 stdexec::prop{fwd_prop_d, 123},
156 stdexec::prop{fwd_prop_c, 123},
157 });
158
159 auto new_env = Kokkos::Execution::Impl::upsert_in_env(fwd_prop_c, env, 42.);
160
161 static_assert(
162 std::same_as<
163 decltype(new_env),
164 stdexec::__env::__fwd<
165 stdexec::env<stdexec::prop<Tests::Impl::FwdPropD, int>, stdexec::prop<Tests::Impl::FwdPropC, double>>
166 >
167 >);
168
169 auto value = fwd_prop_c(new_env);
170
171 return value == 42.;
172 }());
173}
174
180TEST(upsert, add_prop_to_env_with_multiple_props) {
181 static_assert([]() {
182 auto env = stdexec::env{
183 stdexec::prop{ prop_b, 123},
184 stdexec::prop{fwd_prop_c, 456},
185 };
186
187 auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_a, env, 42.);
188
189 static_assert(
190 std::same_as<
191 decltype(new_env),
192 stdexec::env<
193 stdexec::prop<Tests::Impl::PropA, double>,
194 stdexec::env<stdexec::prop<Tests::Impl::PropB, int>, stdexec::prop<Tests::Impl::FwdPropC, int>>
195 >
196 >);
197
198 return prop_a(new_env) == 42.;
199 }());
200}
201
203TEST(upsert, add_fwd_prop_to_forwarding_env) {
204 static_assert([]() {
205 auto env = stdexec::__fwd_env(
206 stdexec::env{
207 stdexec::prop{fwd_prop_d, 123},
208 });
209
210 auto new_env = Kokkos::Execution::Impl::upsert_in_env(fwd_prop_c, env, 42.);
211
212 static_assert(std::same_as<
213 decltype(new_env),
214 stdexec::env<
215 stdexec::prop<Tests::Impl::FwdPropC, double>,
216 stdexec::__env::__fwd<stdexec::env<stdexec::prop<Tests::Impl::FwdPropD, int>>>
217 >
218 >);
219
220 return fwd_prop_c(new_env) == 42.;
221 }());
222}
223
225TEST(upsert, upsert_with_move_only) {
226 static_assert([]() {
227 auto env = stdexec::prop{prop_a, 123};
228
229 struct MoveOnly : public stdexec::__move_only {
230 int value;
231 constexpr explicit MoveOnly(int value_)
232 : value(value_) {
233 }
234 };
235
236 auto new_env = Kokkos::Execution::Impl::upsert_in_env(prop_b, env, MoveOnly(42));
237
238 static_assert(std::same_as<
239 decltype(new_env),
240 stdexec::env<stdexec::prop<Tests::Impl::PropB, MoveOnly>, stdexec::prop<Tests::Impl::PropA, int>>
241 >);
242
243 return prop_b(new_env).value == 42;
244 }());
245}
246
252TEST(upsert, chain_multiple_upserts) {
253 static_assert([]() {
254 auto env = stdexec::env{
255 stdexec::prop{prop_a, 1.},
256 };
257
259 prop_a,
261 4.);
262
263 static_assert(std::same_as<decltype(new_env), stdexec::env<stdexec::prop<Tests::Impl::PropA, double>>>);
264
265 return prop_a(new_env) == 4.;
266 }());
267}
268
275TEST(upsert, ref_join_upsert) {
276 double fwd_prop_c_value = 42.;
277
278 auto env = stdexec::prop{fwd_prop_c, std::ref(fwd_prop_c_value)};
279 auto env_ref_join = stdexec::__env::__join(stdexec::prop{prop_b, 2}, stdexec::__fwd_env(env));
280 const auto env_ref_join_upsert = Kokkos::Execution::Impl::upsert_in_env(prop_b, env_ref_join, 3.);
281
282 static_assert(std::same_as<
283 decltype(env_ref_join),
284 stdexec::env<
285 stdexec::prop<Tests::Impl::PropB, int>,
286 stdexec::__env::__fwd<stdexec::prop<Tests::Impl::FwdPropC, double &> &>
287 >
288 >);
289
290 static_assert(std::same_as<
291 std::remove_const_t<decltype(env_ref_join_upsert)>,
292 stdexec::env<
293 stdexec::prop<Tests::Impl::PropB, double>,
294 stdexec::__env::__fwd<stdexec::prop<Tests::Impl::FwdPropC, double &> &>
295 >
296 >);
297
298 ASSERT_EQ(prop_b(env_ref_join_upsert), 3.);
299 ASSERT_EQ(fwd_prop_c(env_ref_join_upsert), 42.);
300
301 fwd_prop_c_value = 666.;
302 ASSERT_EQ(fwd_prop_c(env_ref_join_upsert), 666.);
303
304 env.__value = 3.14;
305 ASSERT_EQ(fwd_prop_c(env_ref_join_upsert), 3.14);
306}
307
308} // namespace Tests::Impl
constexpr UpsertInEnvFn upsert_in_env
Definition env.hpp:105
Tests::Impl::FwdPropC fwd_prop_c
Tests::Impl::FwdPropD fwd_prop_d
Tests::Impl::PropA prop_a
Tests::Impl::PropB prop_b
Tests::PropA prop_a