test_primitives.cpp
Go to the documentation of this file.
1 // Copyright 2023 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 <tuple>
18 
19 #include <range/v3/utility/common_tuple.hpp>
20 
21 #include "beluga/primitives.hpp"
22 
23 namespace {
24 
25 namespace user {
26 
27 struct SimplestPossibleParticle {
28  float state;
29  double weight;
30 };
31 
32 struct ParticleWithMemberExtensions {
33  float state_;
34  double weight_;
35 
36  float& state() { return state_; }
37  [[nodiscard]] float state() const { return state_; }
38  double& weight() { return weight_; }
39  [[nodiscard]] double weight() const { return weight_; }
40 };
41 
42 struct ParticleWithNonMemberExtensions {
43  float s;
44  double w;
45 };
46 
47 /*
48  * The following functions are marked [[maybe_unused]] since the compiler does not
49  * seem to detect that ADL is discovering them. But they are actually used.
50  */
51 
52 [[maybe_unused]] float& state(ParticleWithNonMemberExtensions& p) {
53  return p.s;
54 }
55 [[maybe_unused]] float state(const ParticleWithNonMemberExtensions& p) {
56  return p.s;
57 }
58 [[maybe_unused]] double& weight(ParticleWithNonMemberExtensions& p) {
59  return p.w;
60 }
61 [[maybe_unused]] double weight(const ParticleWithNonMemberExtensions& p) {
62  return p.w;
63 }
64 
65 } // namespace user
66 
67 template <class T>
68 class PrimitivesTest : public testing::Test {};
69 
70 using PrimitivesTestCases = testing::Types<
71  std::tuple<int, beluga::Weight>,
72  ranges::common_tuple<int, beluga::Weight>,
73  user::SimplestPossibleParticle,
74  user::ParticleWithMemberExtensions,
75  user::ParticleWithNonMemberExtensions>;
76 
77 TYPED_TEST_SUITE(PrimitivesTest, PrimitivesTestCases, );
78 
79 TYPED_TEST(PrimitivesTest, Assignment) {
80  auto particle = TypeParam{};
81  beluga::state(particle) = 1;
82  beluga::weight(particle) = 2;
83  ASSERT_EQ(beluga::state(particle), 1);
84  ASSERT_EQ(beluga::weight(particle), 2);
85 }
86 
87 TYPED_TEST(PrimitivesTest, Const) {
88  const auto particle = TypeParam{4, 5};
89  ASSERT_EQ(beluga::state(particle), 4);
90  ASSERT_EQ(beluga::weight(particle), 5);
91 }
92 
93 } // namespace
primitives.hpp
Implementation of library primitives to abstract member access.
beluga::TYPED_TEST_SUITE
TYPED_TEST_SUITE(SparseGridTests, SparseGridTestCases,)
beluga::state
constexpr state_detail::state_fn state
Customization point object for accessing the state of a particle.
Definition: primitives.hpp:163
beluga::weight
constexpr weight_detail::weight_fn weight
Customization point object for accessing the weight of a particle.
Definition: primitives.hpp:264
user
Definition: test_primitives.cpp:25
beluga::TYPED_TEST
TYPED_TEST(SparseGridTests, CanBeConstructedEmpty)
Definition: test_sparse_value_grid.cpp:46


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