test_particle_traits.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 <tuple>
18 #include <utility>
19 
20 #include <range/v3/utility/common_tuple.hpp>
21 
22 #include "beluga/primitives.hpp"
24 
25 namespace {
26 
27 static_assert(!beluga::is_particle_v<int>);
28 static_assert(!beluga::is_particle_v<struct Object>);
29 static_assert(!beluga::is_particle_v<std::tuple<int, int>>);
30 static_assert(!beluga::is_particle_v<std::tuple<beluga::Weight>>);
31 static_assert(!beluga::is_particle_v<std::tuple<beluga::Weight, beluga::Weight>>);
32 
33 namespace user {
34 
35 struct SimplestPossibleParticle {
36  int state;
37  double weight;
38 };
39 
40 struct ParticleWithMemberExtensions {
41  int state_;
42  double weight_;
43 
44  int& state() { return state_; }
45  [[nodiscard]] int state() const { return state_; }
46  double& weight() { return weight_; }
47  [[nodiscard]] double weight() const { return weight_; }
48 };
49 
50 struct ParticleWithNonMemberExtensions {
51  int s;
52  double w;
53 };
54 
55 /*
56  * The following functions are marked [[maybe_unused]] since the compiler does not
57  * seem to detect that ADL is discovering them. But they are actually used.
58  */
59 
60 [[maybe_unused]] int& state(ParticleWithNonMemberExtensions& p) {
61  return p.s;
62 }
63 [[maybe_unused]] int state(const ParticleWithNonMemberExtensions& p) {
64  return p.s;
65 }
66 [[maybe_unused]] double& weight(ParticleWithNonMemberExtensions& p) {
67  return p.w;
68 }
69 [[maybe_unused]] double weight(const ParticleWithNonMemberExtensions& p) {
70  return p.w;
71 }
72 
73 } // namespace user
74 
75 template <class T>
76 class ParticleTraitsTest : public testing::Test {};
77 
78 using ParticleTraitsTestCases = testing::Types<
79  std::tuple<int, beluga::Weight>,
80  std::pair<int, beluga::Weight>,
81  ranges::common_tuple<int, beluga::Weight>,
82  ranges::common_pair<int, beluga::Weight>,
83  user::SimplestPossibleParticle,
84  user::ParticleWithMemberExtensions,
85  user::ParticleWithNonMemberExtensions>;
86 
87 TYPED_TEST_SUITE(ParticleTraitsTest, ParticleTraitsTestCases, );
88 
89 TYPED_TEST(ParticleTraitsTest, MakeFromState) {
90  // Also check that each parameter type is a valid particle.
91  static_assert(beluga::is_particle_v<TypeParam>);
92  auto particle = beluga::make_from_state<TypeParam>(5);
93  ASSERT_EQ(beluga::state(particle), 5);
94  ASSERT_EQ(beluga::weight(particle), 1.0);
95 }
96 
97 } // 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
particle_traits.hpp
Implementation of traits for particle types, see the Particle named requirements.
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