EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RectangleBoundsTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RectangleBoundsTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 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/data/test_case.hpp>
10 #include <boost/test/tools/output_test_stream.hpp>
11 #include <boost/test/unit_test.hpp>
12 
16 
17 #include <algorithm>
18 #include <iostream>
19 #include <limits>
20 
21 namespace utf = boost::unit_test;
22 const double inf = std::numeric_limits<double>::infinity();
23 
24 namespace Acts {
25 
26 namespace Test {
27 void dumpVertices(const RectangleBounds& r) {
28  const auto& v = r.vertices();
29  for (const auto& i : v) {
30  std::cout << "(" << i[0] << ", " << i[1] << ")" << std::endl;
31  }
32 }
33 bool approximatelyEqual(const Vector2D& a, const Vector2D& b) {
34  const double dif0 = std::abs(a[0] - b[0]);
35  const double dif1 = std::abs(a[1] - b[1]);
36  const double tol = 1e-9;
37  return ((dif0 < tol) and (dif1 < tol));
38 }
39 BOOST_AUTO_TEST_SUITE(Surfaces)
40 
41 
42 BOOST_AUTO_TEST_CASE(RectangleBoundsConstruction) {
43  const double halfX(10.), halfY(5.);
44  RectangleBounds twentyByTenRectangle(halfX, halfY);
45  BOOST_CHECK_EQUAL(twentyByTenRectangle.type(),
47  //
48  // nonsensical bounds are also permitted, but maybe should not be
49  const double zeroHalfX(0.), zeroHalfY(0.);
50  const double infHalfX(inf), infHalfY(inf);
51  //
52  // BOOST_TEST_MESSAGE("Initialise with zero dimensions");
53  RectangleBounds zeroDimensionsRectangle(zeroHalfX, zeroHalfY);
54  BOOST_CHECK_EQUAL(zeroDimensionsRectangle.type(),
56  //
57  // BOOST_TEST_MESSAGE("Initialise with infinite dimensions");
58  RectangleBounds infinite(infHalfX, infHalfY);
59  BOOST_CHECK_EQUAL(infinite.type(), Acts::SurfaceBounds::eRectangle);
60 }
61 
63 BOOST_AUTO_TEST_CASE(RectangleBoundsRecreation) {
64  const double halfX(10.), halfY(2.);
65  RectangleBounds original(halfX, halfY);
66  // const bool symmetric(false);
67  auto valvector = original.values();
68  std::array<double, RectangleBounds::eSize> values;
69  std::copy_n(valvector.begin(), RectangleBounds::eSize, values.begin());
70  RectangleBounds recreated(values);
71  BOOST_CHECK_EQUAL(original, recreated);
72 }
73 
74 // Exception tests
75 BOOST_AUTO_TEST_CASE(RadialBoundsException) {
76  const double halfX(10.), halfY(2.);
77 
78  // Negative x half length
79  BOOST_CHECK_THROW(RectangleBounds(-halfX, halfY), std::logic_error);
80 
81  // Negative y half length
82  BOOST_CHECK_THROW(RectangleBounds(halfX, -halfY), std::logic_error);
83 }
84 
86 BOOST_TEST_DECORATOR(*utf::tolerance(1e-10))
87 BOOST_AUTO_TEST_CASE(RectangleBoundsProperties) {
88  const double halfX(10.), halfY(5.);
89  RectangleBounds rect(halfX, halfY);
90  BOOST_CHECK_EQUAL(rect.halfLengthX(), 10.);
91  BOOST_CHECK_EQUAL(rect.halfLengthY(), 5.);
92 
93  CHECK_CLOSE_ABS(rect.min(), Vector2D(-halfX, -halfY), 1e-6);
94  CHECK_CLOSE_ABS(rect.max(), Vector2D(halfX, halfY), 1e-6);
95 
96  const std::vector<Vector2D> coords = {
97  {-10., -5.}, {10., -5.}, {10., 5.}, {-10., 5.}};
98  // equality, ensure ordering is ok
99  const auto& rectVertices = rect.vertices();
100  BOOST_CHECK_EQUAL_COLLECTIONS(coords.cbegin(), coords.cend(),
101  rectVertices.cbegin(), rectVertices.cend());
102  const Vector2D pointA{1.0, 1.0};
103  // distance is signed, from boundary to point. (doesn't seem right, given
104  BoundaryCheck bcheck(true, true);
105  BOOST_CHECK(rect.inside(pointA, bcheck));
106 }
107 BOOST_AUTO_TEST_CASE(RectangleBoundsAssignment) {
108  const double halfX(10.), halfY(2.);
109  RectangleBounds rectA(halfX, halfY);
110  RectangleBounds rectB(0.0, 0.0);
111  rectB = rectA;
112  const auto originalVertices = rectA.vertices();
113  const auto assignedVertices = rectB.vertices();
114  BOOST_CHECK_EQUAL_COLLECTIONS(
115  originalVertices.cbegin(), originalVertices.cend(),
116  assignedVertices.cbegin(), assignedVertices.cend());
117 }
118 
119 BOOST_AUTO_TEST_SUITE_END()
120 } // namespace Test
121 
122 } // namespace Acts