Edge_test.cc
Go to the documentation of this file.
1 /**
2  * @file Edge_test.cc
3  * @brief Simple test on a recob::Edge object
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date July 6, 2017
6  * @version 1.0
7  *
8  * This test simply creates recob::Edge objects and verifies that the values it
9  * can access are the right ones.
10  *
11  * See http://www.boost.org/libs/test for the Boost test library home page.
12  *
13  * Timing:
14  * version 1.0: ~1.5" (debug mode)
15  */
16 
17 // C/C++ standard library
18 #include <array>
19 #include <iostream>
20 #include <algorithm> // std::is_sorted(), std::lower_bound()
21 #include <stdexcept> // std::runtime_error
22 
23 // Boost libraries
24 /*
25  * Boost Magic: define the name of the module;
26  * and do that before the inclusion of Boost unit test headers
27  * because it will change what they provide.
28  * Among the those, there is a main() function and some wrapping catching
29  * unhandled exceptions and considering them test failures, and probably more.
30  * This also makes fairly complicate to receive parameters from the command line
31  * (for example, a random seed).
32  */
33 #define BOOST_TEST_MODULE ( hit_test )
34 #include "boost/test/unit_test.hpp"
35 
36 
37 // LArSoft libraries
40 
41 
42 
43 //------------------------------------------------------------------------------
44 //--- Test code
45 //
47 
48  /*
49  * Tested interface:
50  *
51  * /// Default constructor (all invalid IDs).
52  * Edge() = default;
53  *
54  * /// Returns the length of this edge [cm].
55  * double Length() const;
56  *
57  * /// Returns the ID of the SpacePoint this edge emanates from.
58  * SpacePointID_t FirstPointID() const;
59  *
60  * /// Returns the ID of the SpacePoint this edge ends on.
61  * SpacePointID_t SecondPointID() const;
62  *
63  * /// Returns the ID of this edge.
64  * ID_t ID() const;
65  *
66  * /// Streaming operator: prints the edge `a` into the specified stream.
67  * std::ostream& operator << (std::ostream & o, const Edge& a);
68  *
69  * /// Comparison operator: strict ordering of edge by ID.
70  * inline bool operator< (const Edge& a, const Edge& b);
71  *
72  * /// Comparison operator: strict ordering of edge by ID.
73  * inline bool operator< (const Edge& e, Edge::ID_t id);
74  *
75  * /// Comparison operator: strict ordering of edge by ID.
76  * inline bool operator< (Edge::ID_t id, const Edge& e);
77  *
78  */
79 
80  recob::Edge const e;
81 
82  constexpr auto EinvID = recob::Edge::InvalidID;
83  constexpr auto PinvID = recob::SpacePoint::InvalidID;
84 
85  BOOST_TEST(e.ID() == EinvID);
86  BOOST_TEST(e.FirstPointID() == PinvID);
87  BOOST_TEST(e.SecondPointID() == PinvID);
88  BOOST_TEST(e.Length() == 0.0); // exactly so
89 
90  BOOST_TEST(!(e < e));
91  BOOST_TEST(!(e < e.ID()));
92  BOOST_TEST(!(e.ID() < e));
93 
94  std::cout << "Printout of a default-constructed edge: " << e << std::endl;
95 
96 } // EdgeTestDefaultConstructor()
97 
98 
100 
101  /*
102  * Tested interface:
103  *
104  * /// Constructor: assigns all values.
105  * Edge(
106  * const double length,
107  * SpacePointID_t firstPointID, SpacePointID_t secondPointID,
108  * ID_t id = InvalidID
109  * );
110  *
111  * /// Returns the length of this edge [cm].
112  * double Length() const;
113  *
114  * /// Returns the ID of the SpacePoint this edge emanates from.
115  * SpacePointID_t FirstPointID() const;
116  *
117  * /// Returns the ID of the SpacePoint this edge ends on.
118  * SpacePointID_t SecondPointID() const;
119  *
120  * /// Returns the ID of this edge.
121  * ID_t ID() const;
122  *
123  * /// Streaming operator: prints the edge `a` into the specified stream.
124  * std::ostream& operator << (std::ostream & o, const Edge& a);
125  *
126  * /// Comparison operator: strict ordering of edge by ID.
127  * inline bool operator< (const Edge& a, const Edge& b);
128  *
129  * /// Comparison operator: strict ordering of edge by ID.
130  * inline bool operator< (const Edge& e, Edge::ID_t id);
131  *
132  * /// Comparison operator: strict ordering of edge by ID.
133  * inline bool operator< (Edge::ID_t id, const Edge& e);
134  *
135  */
136 
137  recob::Edge const e(3.0, 5, 10, 3);
138 
139  BOOST_TEST(e.ID() == 3);
140  BOOST_TEST(e.FirstPointID() == 5);
141  BOOST_TEST(e.SecondPointID() == 10);
142  BOOST_TEST(e.Length() == 3.0); // exactly so
143 
144  BOOST_TEST(!(e < e));
145  BOOST_TEST(!(e < e.ID()));
146  BOOST_TEST(!(e.ID() < e));
147 
148  std::cout << "Printout of a value-constructed edge: " << e << std::endl;
149 
150  recob::Edge o(3.0, 5, 10, 4);
151 
152  BOOST_TEST( (e < o));
153  BOOST_TEST(!(o < e));
154  BOOST_TEST( (e < o.ID()));
155  BOOST_TEST(!(o.ID() < e));
156 
157 } // EdgeTestValueConstructor()
158 
159 
161 
162  /*
163  * Tested interface:
164  *
165  * /// Default constructor (all invalid IDs).
166  * Edge() = default;
167  *
168  * /// Constructor: assigns all values.
169  * Edge(
170  * const double length,
171  * SpacePointID_t firstPointID, SpacePointID_t secondPointID,
172  * ID_t id = InvalidID
173  * );
174  *
175  * /// Constructor: uses the specified spacepoints.
176  * Edge(
177  * SpacePoint const& firstPoint, SpacePoint const& secondPoint,
178  * ID_t id = InvalidID
179  * );
180  *
181  * /// Returns the length of this edge [cm].
182  * double Length() const;
183  *
184  * /// Returns the ID of the SpacePoint this edge emanates from.
185  * SpacePointID_t FirstPointID() const;
186  *
187  * /// Returns the ID of the SpacePoint this edge ends on.
188  * SpacePointID_t SecondPointID() const;
189  *
190  * /// Returns the ID of this edge.
191  * ID_t ID() const;
192  *
193  * /// Streaming operator: prints the edge `a` into the specified stream.
194  * std::ostream& operator << (std::ostream & o, const Edge& a);
195  *
196  * /// Comparison operator: strict ordering of edge by ID.
197  * inline bool operator< (const Edge& a, const Edge& b);
198  *
199  * /// Comparison operator: strict ordering of edge by ID.
200  * inline bool operator< (const Edge& e, Edge::ID_t id);
201  *
202  * /// Comparison operator: strict ordering of edge by ID.
203  * inline bool operator< (Edge::ID_t id, const Edge& e);
204  *
205  */
206 
207  // BUG the double brace syntax is required to work around clang bug 21629
208  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
209  std::array<double, 3U> const error = {{ 0.1, 0.1, 0.1 }};
210  std::array<double, 3U> point;
211 
212  // BUG the double brace syntax is required to work around clang bug 21629
213  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
214  point = {{ 1.0, 1.0, 1.0 }};
215  recob::SpacePoint const p1(point.data(), error.data(), 1.0, 0);
216 
217  // BUG the double brace syntax is required to work around clang bug 21629
218  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
219  point = {{ 4.0, 5.0, 13.0 }};
220  recob::SpacePoint const p2(point.data(), error.data(), 1.0, 1);
221 
222  recob::Edge const e(p1, p2, 0);
223 
224  BOOST_TEST(e.ID() == 0);
225  BOOST_TEST(e.FirstPointID() == 0);
226  BOOST_TEST(e.SecondPointID() == 1);
227  BOOST_CHECK_CLOSE(e.Length(), 13.0, 1e-4); // tolerance: 10^-6
228 
229  std::cout << "Printout of a spacepoint-constructed edge: " << e << std::endl;
230 
231 } // EdgeTestSpacePointConstructor()
232 
233 
234 //------------------------------------------------------------------------------
236 
237  // prepare the space points for the test, already sorted
238  // BUG the double brace syntax is required to work around clang bug 21629
239  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
240  std::array<double, 3U> const error = {{ 0.1, 0.1, 0.1 }};
241  std::array<double, 3U> const point = {{ 1.0, 1.0, 1.0 }};
242  std::vector<recob::SpacePoint> points;
243  for (unsigned int i = 0; i < 10; ++i)
244  points.emplace_back(point.data(), error.data(), 1.0, i);
245 
246  // prepare the edge with two of the space points
247  recob::Edge const edge(points[3], points[6], 0);
248 
249  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250  if (!std::is_sorted(points.begin(), points.end()))
251  throw std::runtime_error("Space points not sorted!");
252 
253  // find the first space point
254  auto const iFirstPoint = std::lower_bound
255  (points.begin(), points.end(), edge.FirstPointID());
256 
257  if ((iFirstPoint == points.end()) || (iFirstPoint->ID() != edge.FirstPointID())) {
258  throw std::runtime_error
259  ("First point not found: ID=" + std::to_string(edge.FirstPointID()));
260  }
261  recob::SpacePoint const& firstPoint = *iFirstPoint;
262 
263  // find the second space point
264  auto const iSecondPoint = std::lower_bound
265  (points.begin(), points.end(), edge.SecondPointID());
266 
267  if ((iSecondPoint == points.end()) || (iSecondPoint->ID() != edge.SecondPointID())) {
268  throw std::runtime_error
269  ("Second point not found: ID=" + std::to_string(edge.SecondPointID()));
270  }
271  recob::SpacePoint const& secondPoint = *iSecondPoint;
272  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273 
274  BOOST_TEST(&firstPoint == &points[3]);
275  BOOST_TEST(&secondPoint == &points[6]);
276 
277 
278 } // EdgeClassDocumentationTest()
279 
280 
281 //------------------------------------------------------------------------------
282 //--- registration of tests
283 //
284 // Boost needs now to know which tests we want to run.
285 // Tests are "automatically" registered, hence the BOOST_AUTO_TEST_CASE()
286 // macro name. The argument is the name of the test; each step may have a
287 // number of checks and it will fail if any of them does.
288 //
289 
294 } // EdgeTests
295 
296 BOOST_AUTO_TEST_CASE(EdgeDocumentationTests) {
298 }
An object to define a "edge" which is used to connect space points in a triangulation algorithm...
BOOST_AUTO_TEST_CASE(EdgeTests)
Definition: Edge_test.cc:290
void EdgeTestValueConstructor()
Definition: Edge_test.cc:99
error
Definition: include.cc:26
static constexpr ID_t InvalidID
Special value for an invalid edge ID.
Definition: Edge.h:72
double Length() const
Returns the length of this edge [cm].
Definition: Edge.h:108
void EdgeTestSpacePointConstructor()
Definition: Edge_test.cc:160
void EdgeTestDefaultConstructor()
Definition: Edge_test.cc:46
SpacePointID_t FirstPointID() const
Returns the ID of the SpacePoint this edge emanates from.
Definition: Edge.h:112
const double e
static constexpr ID_t InvalidID
Special value for an invalid ID.
Definition: SpacePoint.h:29
void EdgeClassDocumentationTest()
Definition: Edge_test.cc:235
ID_t ID() const
Returns the ID of this edge.
Definition: Edge.h:120
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
QTextStream & endl(QTextStream &s)
Edge is an object containing the results of a Principal Components Analysis of a group of space point...
Definition: Edge.h:61
SpacePointID_t SecondPointID() const
Returns the ID of the SpacePoint this edge ends on.
Definition: Edge.h:116