test_propagate.cpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 #include <execution>
19 #include <functional>
20 #include <tuple>
21 #include <vector>
22 
23 #include <range/v3/range/conversion.hpp>
24 #include <range/v3/view/take_exactly.hpp>
25 
28 #include "beluga/primitives.hpp"
30 #include "beluga/views/sample.hpp"
31 
32 namespace {
33 
34 TEST(PropagateAction, DefaultExecutionPolicy) {
35  auto input = std::vector{std::make_tuple(5, beluga::Weight(1.0))};
36  input |= beluga::actions::propagate([](int value) { return ++value; });
37  ASSERT_EQ(input.front(), std::make_tuple(6, 1.0));
38 }
39 
40 TEST(PropagateAction, SequencedExecutionPolicy) {
41  auto input = std::vector{std::make_tuple(5, beluga::Weight(1.0))};
42  input |= beluga::actions::propagate(std::execution::seq, [](int value) { return ++value; });
43  ASSERT_EQ(input.front(), std::make_tuple(6, 1.0));
44 }
45 
46 TEST(PropagateAction, ParallelExecutionPolicy) {
47  auto input = std::vector{std::make_tuple(5, beluga::Weight(1.0))};
48  input |= beluga::actions::propagate(std::execution::par, [](int value) { return ++value; });
49  ASSERT_EQ(input.front(), std::make_tuple(6, 1.0));
50 }
51 
52 TEST(PropagateAction, Composition) {
53  auto input = std::vector{std::make_tuple(5, beluga::Weight(1.0))};
54  input |= beluga::actions::propagate([](int value) { return --value; }) | //
56  ranges::views::take_exactly(5) | //
58  auto states = input | beluga::views::states | ranges::to<std::vector>;
59  ASSERT_THAT(states, testing::ElementsAre(4, 4, 4, 4, 4));
60 }
61 
62 TEST(PropagateAction, StatefulModel) {
63  auto input = std::vector{std::make_tuple(5, beluga::Weight(1.0))};
64  auto model = [value = 0](int) mutable { return value++; };
65  input |= beluga::views::sample | //
66  ranges::views::take_exactly(5) | //
68  beluga::actions::propagate(std::ref(model));
69  auto states = input | beluga::views::states | ranges::to<std::vector>;
70  ASSERT_THAT(states, testing::ElementsAre(0, 1, 2, 3, 4));
71  ASSERT_EQ(model(0), 5); // the model was passed by reference
72 }
73 
74 } // namespace
primitives.hpp
Implementation of library primitives to abstract member access.
particles.hpp
Implementation of views related to particle ranges.
beluga::actions::assign
constexpr detail::assign_fn assign
Definition: assign.hpp:99
beluga::actions::propagate
constexpr detail::propagate_fn propagate
Definition: propagate.hpp:131
beluga::Numeric
Helper for creating strongly typed numeric types.
Definition: strongly_typed_numeric.hpp:38
assign.hpp
Implementation of the assign range adaptor object.
sample.hpp
Implementation of a sample (with replacement) range adaptor object.
propagate.hpp
Implementation of the propagate range adaptor object.
beluga::views::states
constexpr auto states
Definition: particles.hpp:30
beluga::views::sample
constexpr ranges::views::view_closure< detail::sample_fn > sample
Definition: sample.hpp:240
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53