test_reweight.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/intersperse.hpp>
25 
28 #include "beluga/primitives.hpp"
30 
31 namespace {
32 
33 TEST(ReweightAction, DefaultExecutionPolicy) {
34  auto input = std::vector{std::make_tuple(5, beluga::Weight(2.0))};
35  input |= beluga::actions::reweight([](int value) { return value; });
36  ASSERT_EQ(input.front(), std::make_tuple(5, 10.0));
37 }
38 
39 TEST(ReweightAction, SequencedExecutionPolicy) {
40  auto input = std::vector{std::make_tuple(5, beluga::Weight(2.0))};
41  input |= beluga::actions::reweight(std::execution::seq, [](int value) { return value; });
42  ASSERT_EQ(input.front(), std::make_tuple(5, 10.0));
43 }
44 
45 TEST(ReweightAction, ParallelExecutionPolicy) {
46  auto input = std::vector{std::make_tuple(5, beluga::Weight(2.0))};
47  input |= beluga::actions::reweight(std::execution::par, [](int value) { return value; });
48  ASSERT_EQ(input.front(), std::make_tuple(5, 10.0));
49 }
50 
51 TEST(ReweightAction, Composition) {
52  auto input = std::vector{std::make_tuple(4, beluga::Weight(0.5)), std::make_tuple(4, beluga::Weight(1.0))};
53  input |= beluga::actions::reweight([](int value) { return value; }) | //
54  ranges::views::intersperse(std::make_tuple(5, beluga::Weight(1.0))) | //
56  auto weights = input | beluga::views::weights | ranges::to<std::vector>;
57  ASSERT_THAT(weights, testing::ElementsAre(2, 1, 4));
58 }
59 
60 TEST(ReweightAction, StatefulModel) {
61  auto input = std::vector{std::make_tuple(4, beluga::Weight(0.5)), std::make_tuple(4, beluga::Weight(1.0))};
62  auto model = [value = 0](int) mutable { return value++; };
63  input |= ranges::views::intersperse(std::make_tuple(5, beluga::Weight(1.0))) | //
65  beluga::actions::reweight(std::ref(model));
66  auto weights = input | beluga::views::weights | ranges::to<std::vector>;
67  ASSERT_THAT(weights, testing::ElementsAre(0, 1, 2));
68  ASSERT_EQ(model(0), 3); // the model was passed by reference
69 }
70 
71 } // 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::reweight
constexpr detail::reweight_fn reweight
Definition: reweight.hpp:115
beluga::Numeric
Helper for creating strongly typed numeric types.
Definition: strongly_typed_numeric.hpp:38
reweight.hpp
Implementation of the reweight range adaptor object.
assign.hpp
Implementation of the assign range adaptor object.
beluga::views::weights
constexpr auto weights
Definition: particles.hpp:34
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


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