test_normalize.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 <gtest/gtest.h>
16 
17 #include <cmath>
18 #include <execution>
19 #include <tuple>
20 #include <vector>
21 
23 #include "beluga/primitives.hpp"
24 
25 namespace {
26 
27 TEST(NormalizeAction, DefaultExecutionPolicy) {
28  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
29  input |= beluga::actions::normalize(2.0);
30  ASSERT_EQ(input.front(), std::make_tuple(5, 2.0));
31 }
32 
33 TEST(NormalizeAction, SequencedExecutionPolicy) {
34  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
35  input |= beluga::actions::normalize(std::execution::seq, 2.0);
36  ASSERT_EQ(input.front(), std::make_tuple(5, 2.0));
37 }
38 
39 TEST(NormalizeAction, ParallelExecutionPolicy) {
40  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
41  input |= beluga::actions::normalize(std::execution::par, 2.0);
42  ASSERT_EQ(input.front(), std::make_tuple(5, 2.0));
43 }
44 
45 TEST(NormalizeAction, DefaultFactor) {
46  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
47  input |= beluga::actions::normalize(std::execution::seq);
48  ASSERT_EQ(input.front(), std::make_tuple(5, 1.0));
49 }
50 
51 TEST(NormalizeAction, DefaultFactorAndExecutionPolicy) {
52  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
54  ASSERT_EQ(input.front(), std::make_tuple(5, 1.0));
55 }
56 
57 TEST(NormalizeAction, EmptyInputRange) {
58  auto input = std::vector<std::tuple<int, beluga::Weight>>{};
59  input |= beluga::actions::normalize(2.0);
60  ASSERT_TRUE(input.empty());
61 }
62 
63 TEST(NormalizeAction, MultipleParticles) {
64  auto input = std::vector{
65  std::make_tuple(5, beluga::Weight(4.0)), //
66  std::make_tuple(8, beluga::Weight(2.0)), //
67  std::make_tuple(3, beluga::Weight(6.0))};
68  input |= beluga::actions::normalize(2.0);
69  ASSERT_EQ(input.size(), 3);
70  ASSERT_EQ(input[0], std::make_tuple(5, 2.0));
71  ASSERT_EQ(input[1], std::make_tuple(8, 1.0));
72  ASSERT_EQ(input[2], std::make_tuple(3, 3.0));
73 }
74 
75 TEST(NormalizeAction, MultipleElements) {
76  auto input = std::vector{4.0, 2.0, 6.0};
77  input |= beluga::actions::normalize(2.0);
78  ASSERT_EQ(input.size(), 3);
79  ASSERT_EQ(input[0], 2.0);
80  ASSERT_EQ(input[1], 1.0);
81  ASSERT_EQ(input[2], 3.0);
82 }
83 
84 TEST(NormalizeAction, ZeroFactor) {
85  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
86  input |= beluga::actions::normalize(0.0);
87  ASSERT_TRUE(std::isinf(beluga::weight(input.front())));
88 }
89 
90 TEST(NormalizeAction, NegativeFactor) {
91  auto input = std::vector{std::make_tuple(5, beluga::Weight(4.0))};
92  input |= beluga::actions::normalize(-2.0);
93  ASSERT_EQ(input.front(), std::make_tuple(5, beluga::Weight(-2.0)));
94 }
95 
96 } // namespace
primitives.hpp
Implementation of library primitives to abstract member access.
normalize.hpp
Implementation of the normalize range adaptor object.
beluga::weight
constexpr weight_detail::weight_fn weight
Customization point object for accessing the weight of a particle.
Definition: primitives.hpp:264
beluga::Numeric
Helper for creating strongly typed numeric types.
Definition: strongly_typed_numeric.hpp:38
beluga::actions::normalize
constexpr ranges::actions::action_closure< detail::normalize_fn > normalize
Definition: normalize.hpp:162
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


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