EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HelpersTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HelpersTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
13 
14 #include <bitset>
15 
16 using namespace Acts::VectorHelpers;
17 
18 namespace Acts {
19 namespace Test {
20 
21 BOOST_AUTO_TEST_SUITE(Utilities)
22 
23 BOOST_AUTO_TEST_CASE(bitset_to_matrix_to_bitset) {
24  Eigen::Matrix<int, 4, 3> mat;
25  mat << 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0;
26 
27  std::bitset<4 * 3> act = matrixToBitset(mat);
28  std::bitset<4 * 3> exp{"101001011010"};
29 
30  BOOST_CHECK_EQUAL(exp, act);
31 
32  Eigen::Matrix<int, 4, 3> cnv;
33  cnv = bitsetToMatrix<decltype(cnv)>(act);
34 
35  BOOST_CHECK_EQUAL(mat, cnv);
36 }
37 
38 struct MyStruct {
39  double phi() const { return 42; }
40 };
41 
42 BOOST_AUTO_TEST_CASE(phi_helper_test) {
43  Vector3D v(0, 1, 0);
44  CHECK_CLOSE_ABS(phi(v), M_PI / 2., 1e-9);
45 
46  // should work with dynamic types as well
47  ActsVectorXd v2{3};
48  v2 << 0, 1, 0;
49  CHECK_CLOSE_ABS(phi(v2), M_PI / 2., 1e-9);
50 
51  MyStruct s;
52  BOOST_CHECK_EQUAL(phi(s), 42u);
53 }
54 
55 BOOST_AUTO_TEST_CASE(perp_helper_test) {
56  Vector3D v(1, 2, 3);
57  CHECK_CLOSE_ABS(perp(v), std::sqrt(1 + 2 * 2), 1e-9);
58 
59  // should work with dynamic types as well
60  ActsVectorXd v2{3};
61  v2 << 1, 2, 3;
62  CHECK_CLOSE_ABS(perp(v2), std::sqrt(1 + 2 * 2), 1e-9);
63 }
64 
65 BOOST_AUTO_TEST_CASE(theta_eta_test_helper) {
66  Vector3D v(1, 2, 3);
67  CHECK_CLOSE_ABS(theta(v), 0.640522, 1e-5);
68  CHECK_CLOSE_ABS(eta(v), 1.10359, 1e-5);
69 
70  // should work with dynamic types as well
71  ActsVectorXd v2{3};
72  v2 << 1, 2, 3;
73  CHECK_CLOSE_ABS(theta(v2), 0.640522, 1e-5);
74  CHECK_CLOSE_ABS(eta(v2), 1.10359, 1e-5);
75 }
76 
77 BOOST_AUTO_TEST_CASE(cross_test_helper) {
78  {
79  Vector3D v(1, 2, 3);
81  mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
82 
83  ActsMatrixD<3, 3> act = cross(mat, v);
85  exp << -2, -1, 0, 4, 2, 0, -2, -1, 0;
86 
87  CHECK_CLOSE_ABS(act, exp, 1e-9);
88  }
89 
90  // should work with dynamic types as well
91  {
92  ActsVectorXd v{3};
93  v << 1, 2, 3;
94  ActsMatrixXd mat{3, 3};
95  mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
96 
97  ActsMatrixXd act = cross(mat, v);
98  ActsMatrixXd exp{3, 3};
99  exp << -2, -1, 0, 4, 2, 0, -2, -1, 0;
100 
101  BOOST_CHECK(act.isApprox(exp, 1e-9));
102  }
103 }
104 
105 BOOST_AUTO_TEST_CASE(toString_test_helper) {
106  ActsMatrixD<3, 3> mat;
107  mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
108  std::string out;
109  out = toString(mat);
110  BOOST_CHECK(out.size() > 0);
111 
112  Translation3D trl{Vector3D{1, 2, 3}};
113  out = toString(trl);
114  BOOST_CHECK(out.size() > 0);
115 
116  Transform3D trf;
117  trf = trl;
118  out = toString(trf);
119  BOOST_CHECK(out.size() > 0);
120 }
121 
122 BOOST_AUTO_TEST_CASE(shared_vector_helper_test) {
123  {
124  std::vector<std::shared_ptr<int>> vec;
125  vec = {std::make_shared<int>(5), std::make_shared<int>(9),
126  std::make_shared<int>(26), std::make_shared<int>(18473)};
127 
128  std::vector<int*> unpacked = unpack_shared_vector(vec);
129 
130  std::vector<int*> exp = {
131  vec[0].get(),
132  vec[1].get(),
133  vec[2].get(),
134  vec[3].get(),
135  };
136 
137  BOOST_CHECK_EQUAL_COLLECTIONS(unpacked.begin(), unpacked.end(), exp.begin(),
138  exp.end());
139  }
140 
141  // same for const
142  {
143  std::vector<std::shared_ptr<const int>> vec;
144  vec = {std::make_shared<const int>(5), std::make_shared<const int>(9),
145  std::make_shared<const int>(26), std::make_shared<const int>(18473)};
146 
147  std::vector<const int*> unpacked = unpack_shared_vector(vec);
148 
149  std::vector<const int*> exp = {
150  vec[0].get(),
151  vec[1].get(),
152  vec[2].get(),
153  vec[3].get(),
154  };
155 
156  BOOST_CHECK_EQUAL_COLLECTIONS(unpacked.begin(), unpacked.end(), exp.begin(),
157  exp.end());
158  }
159 }
160 
161 BOOST_AUTO_TEST_CASE(VectorHelpersPosition) {
162  Vector4D pos4 = Vector4D::Constant(-1);
163  pos4[ePos0] = 1;
164  pos4[ePos1] = 2;
165  pos4[ePos2] = 3;
166  BOOST_CHECK_EQUAL(position(pos4), Vector3D(1, 2, 3));
167 
168  FreeVector params = FreeVector::Constant(-1);
169  params[eFreePos0] = 1;
170  params[eFreePos1] = 2;
171  params[eFreePos2] = 3;
172  BOOST_CHECK_EQUAL(position(params), Vector3D(1, 2, 3));
173 }
174 
175 template <size_t I>
176 struct functor {
177  static constexpr size_t invoke() { return I * I * I; }
178 };
179 
180 BOOST_AUTO_TEST_CASE(test_matrix_dimension_switch) {
181  constexpr size_t imax = 20;
182  for (size_t i = 0; i < imax; i++) {
183  size_t val = template_switch<functor, 0, imax>(i);
184  BOOST_CHECK_EQUAL(val, i * i * i);
185  }
186 }
187 
188 BOOST_AUTO_TEST_SUITE_END()
189 } // namespace Test
190 } // namespace Acts