All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DumpUtils_test.cc
Go to the documentation of this file.
1 /**
2  * @file DumpUtils_test.cc
3  * @brief Unit test for utilities in DumpUtils.h
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date February 28, 2017
6  *
7  *
8  */
9 
10 // Boost libraries
11 #define BOOST_TEST_MODULE DumpUtils_test
12 #include <boost/test/unit_test.hpp>
13 
14 // LArSoft libraries
16 
17 // ROOT libraries
18 #include <TVector3.h>
19 
20 // C/C++ standard libraries
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 #include <list>
25 #include <array>
26 #include <sstream>
27 
28 
29 //------------------------------------------------------------------------------
31  std::ostringstream sstr;
32  // BUG the double brace syntax is required to work around clang bug 21629
33  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
34  std::array<float, 5> const array = {{ 1., 2., 3., 4., 6. }};
35  sstr << lar::dump::array<5>(array);
36  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
37  BOOST_TEST
38  (std::string(lar::dump::array<5>(array)) == "{ 1; 2; 3; 4; 6 }");
39 }
40 
42  std::ostringstream sstr;
43  std::vector<float> const array = { 1., 2., 3., 4., 6. };
44  sstr << lar::dump::array<5>(array);
45  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
46  BOOST_TEST
47  (std::string(lar::dump::array<5>(array)) == "{ 1; 2; 3; 4; 6 }");
48 }
49 
51  std::ostringstream sstr;
52  float const array[5] = { 1., 2., 3., 4., 6. };
53  sstr << lar::dump::array<5>(array);
54  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
55  BOOST_TEST
56  (std::string(lar::dump::array<5>(array)) == "{ 1; 2; 3; 4; 6 }");
57 }
58 
60  std::ostringstream sstr;
61  float const array[5] = { 1., 2., 3., 4., 6. };
62  float const* ptr = array;
63  sstr << lar::dump::array<5>(ptr);
64  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
65  BOOST_TEST(std::string(lar::dump::array<5>(ptr)) == "{ 1; 2; 3; 4; 6 }");
66 }
67 
69  std::ostringstream sstr;
70  double array[5] = { 1., 2., 3., 4., 6. };
71  sstr << lar::dump::array<5>(array);
72  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
73  BOOST_TEST
74  (std::string(lar::dump::array<5>(array)) == "{ 1; 2; 3; 4; 6 }");
75 }
76 
77 void ArrayTest_ptr() {
78  std::ostringstream sstr;
79  double array[5] = { 1., 2., 3., 4., 6. };
80  double* ptr = array;
81  sstr << lar::dump::array<5>(ptr);
82  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
83  BOOST_TEST(std::string(lar::dump::array<5>(ptr)) == "{ 1; 2; 3; 4; 6 }");
84 }
85 
86 void ArrayTest() {
87 
92  ArrayTest_ptr();
94 
95 } // ArrayTest()
96 
97 
98 //------------------------------------------------------------------------------
100 
101  std::ostringstream sstr;
102 
103  /* This is the code as in the documentation:
104  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
105  * double const data[5] = { 1., 2., 3., 4., 6. };
106  * std::cout << "Data: " << lar::dump::array<5>(data) << std::endl;
107  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108  * which is promised to return:
109  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110  * Data: { 1; 2; 3; 4; 6 }
111  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112  */
113 
114  double const data[5] = { 1., 2., 3., 4., 6. };
115  std::cout << "Data: " << lar::dump::array<5>(data) << std::endl;
116 
117  sstr << lar::dump::array<5>(data);
118  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
119 
120 } // ArrayDocumentationTest()
121 
122 
123 //------------------------------------------------------------------------------
125  std::ostringstream sstr;
126  std::vector<float> const v = { 1., 2., 3., 4., 6. };
127  sstr << lar::dump::vector(v);
128  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
129  BOOST_TEST
130  (std::string(lar::dump::vector(v)) == "{ 1; 2; 3; 4; 6 }");
131 } // VectorTest_STLvector()
132 
134  std::ostringstream sstr;
135  std::list<float> const v = { 1., 2., 3., 4., 6. };
136  sstr << lar::dump::vector(v);
137  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
138  BOOST_TEST
139  (std::string(lar::dump::vector(v)) == "{ 1; 2; 3; 4; 6 }");
140 } // VectorTest_STLlist()
141 
142 void VectorTest() {
143 
146 
147 } // VectorTest()
148 
149 
150 //------------------------------------------------------------------------------
152 
153  std::ostringstream sstr;
154 
155  /* This is the code as in the documentation:
156  *
157  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
158  * std::vector<double> data = { 1., 2., 3., 4., 6. };
159  * std::cout << "Data: " << lar::dump::vector(data) << std::endl;
160  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161  * which is promised to return:
162  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163  * Data: { 1; 2; 3; 4; 6 }
164  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165  */
166 
167  std::vector<double> data = { 1., 2., 3., 4., 6. };
168  std::cout << "Data: " << lar::dump::vector(data) << std::endl;
169 
170  sstr << lar::dump::array<5>(data);
171  BOOST_TEST(sstr.str() == "{ 1; 2; 3; 4; 6 }");
172 
173 } // VectorDocumentationTest()
174 
175 
176 //------------------------------------------------------------------------------
178  std::ostringstream sstr;
179  TVector3 pos( 1., 2., 4. );
180  sstr << lar::dump::vector3D(pos);
181  BOOST_TEST(sstr.str() == "{ 1; 2; 4 }");
182  BOOST_TEST(std::string(lar::dump::vector3D(pos)) == "{ 1; 2; 4 }");
183 }
184 
186  std::ostringstream sstr;
187  float const array[3] = { 1., 2., 4. };
188  sstr << lar::dump::vector3D(array);
189  BOOST_TEST(sstr.str() == "{ 1; 2; 4 }");
190  BOOST_TEST(std::string(lar::dump::vector3D(array)) == "{ 1; 2; 4 }");
191 }
192 
194  std::ostringstream sstr;
195  float const array[3] = { 1., 2., 4. };
196  float const* ptr = array;
197  sstr << lar::dump::vector3D(ptr);
198  BOOST_TEST(sstr.str() == "{ 1; 2; 4 }");
199  BOOST_TEST(std::string(lar::dump::vector3D(ptr)) == "{ 1; 2; 4 }");
200 }
201 
203  std::ostringstream sstr;
204  double array[3] = { 1., 2., 4. };
205  sstr << lar::dump::vector3D(array);
206  BOOST_TEST(sstr.str() == "{ 1; 2; 4 }");
207  BOOST_TEST(std::string(lar::dump::vector3D(array)) == "{ 1; 2; 4 }");
208 }
209 
211  std::ostringstream sstr;
212  double array[3] = { 1., 2., 4. };
213  double* ptr = array;
214  sstr << lar::dump::vector3D(ptr);
215  BOOST_TEST(sstr.str() == "{ 1; 2; 4 }");
216  BOOST_TEST(std::string(lar::dump::vector3D(ptr)) == "{ 1; 2; 4 }");
217 }
218 
219 
220 void Vector3Dtest() {
221 
227 
228 } // Vector3Dtest()
229 
230 
231 //------------------------------------------------------------------------------
233 
234  std::ostringstream sstr;
235 
236  /* This is the code as in the documentation:
237  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
238  * TVector3 pos;
239  * std::cout << "Position: " << lar::dump::vector3D(pos) << std::endl;
240  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
241  * which is promised to return:
242  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243  * Position: { 0, 0, 0 }
244  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245  */
246 
247  TVector3 pos;
248  std::cout << "Position: " << lar::dump::vector3D(pos) << std::endl;
249 
250  sstr << lar::dump::vector3D(pos);
251  BOOST_TEST(sstr.str() == "{ 0; 0; 0 }");
252 
253 } // Vector3DstreamOutputDocumentationTest()
254 
255 
257 
258  std::ostringstream sstr;
259 
260  /* This is the code as in the documentation:
261  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
262  * TVector3 pos;
263  * std::runtime_error e("Position: " + lar::dump::vector3D(pos));
264  * std::cout << e.what() << std::endl;
265  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
266  * which is promised to return:
267  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
268  * Position: { 0, 0, 0 }
269  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270  */
271 
272  TVector3 pos;
273  std::runtime_error e("Position: " + lar::dump::vector3D(pos));
274  std::cout << e.what() << std::endl;
275 
276  BOOST_TEST(std::string(e.what()) == "Position: { 0; 0; 0 }");
277 
278 } // Vector3DstringConcatDocumentationTest()
279 
280 
282 
283  std::ostringstream sstr;
284 
285  /* This is the code that should have been in the documentation:
286  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
287  * TVector3 pos;
288  * std::string s = "Position: ";
289  * s += lar::dump::vector3D(pos);
290  * std::cout << s << std::endl;
291  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
292  * which is promised to return:
293  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
294  * Position: { 0, 0, 0 }
295  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296  */
297 
298  TVector3 pos;
299  std::string s = "Position: ";
300  s += lar::dump::vector3D(pos);
301  std::cout << s << std::endl;
302 
303  BOOST_TEST(s == "Position: { 0; 0; 0 }");
304 
305 } // Vector3DstringAppendDocumentationTest()
306 
307 
312 } // Vector3DdocumentationTest()
313 
314 
315 //------------------------------------------------------------------------------
316 
317 #include <array>
318 
319 namespace lar {
320  namespace dump {
321 
322  // specialization for 3D std::array<>
323  template <typename T>
324  struct VectorDumper<std::array<T, 3U>> {
325 
326  using Vector_t = std::array<T, 3U>;
327 
328  Vector_t const& a;
329 
330  VectorDumper(Vector_t const& a): a(a) {}
331 
332  template <typename Stream>
333  void operator() (Stream&& out) const
334  { out << "{ " << a[0] << "; " << a[1] << "; " << a[2] << " }"; }
335 
336  }; // struct VectorDumper<array>
337 
338 
339  } // namespace dump
340 } // namespace lar
341 
342 
344 
345  std::ostringstream sstr;
346 
347  std::array<float, 3U> v;
348  v.fill(0.0);
349  sstr << lar::dump::vector3D(v);
350  BOOST_TEST(sstr.str() == "{ 0; 0; 0 }");
351 
352 } // Vector3DspecializationTest()
353 
354 
355 
356 //------------------------------------------------------------------------------
357 BOOST_AUTO_TEST_CASE(DumpArrays_testcase) {
358 
359  ArrayTest();
360 
362 
363 } // BOOST_AUTO_TEST_CASE(DumpArrays_testcase)
364 
365 
366 BOOST_AUTO_TEST_CASE(DumpVectors_testcase) {
367 
368  VectorTest();
369 
371 
372 } // BOOST_AUTO_TEST_CASE(DumpVectors_testcase)
373 
374 
375 BOOST_AUTO_TEST_CASE(Dump3Dvectors_testcase) {
376 
377  Vector3Dtest();
378 
380 
382 
383 } // BOOST_AUTO_TEST_CASE(Dump3Dvectors_testcase)
void ArrayTest_STLvector()
BOOST_AUTO_TEST_CASE(AllTests)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
auto vector3D(Vector3D const &v)
Returns a manipulator which will print the specified vector.
Definition: DumpUtils.h:301
void Vector3Dtest_cptr()
void Vector3Dtest()
void Vector3DstringAppendDocumentationTest()
void ArrayTest_ptr()
void Vector3DdocumentationTest()
void Vector3Dtest_TVector3()
process_name gaushit a
void Vector3DstringConcatDocumentationTest()
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
void VectorDocumentationTest()
Utilities to dump objects into a stream.
then echo ***************************************echo array
Definition: find_fhicl.sh:28
void Vector3DspecializationTest()
void Vector3Dtest_carray()
void ArrayTest()
void operator()(Stream &&out) const
Inserts the content of the stored vector into the specified stream.
Definition: DumpUtils.h:171
void ArrayTest_STLarray()
void ArrayTest_cptr()
void VectorTest()
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
void Vector3Dtest_ptr()
do i e
void ArrayTest_array()
void Vector3DstreamOutputDocumentationTest()
void VectorTest_STLvector()
Manipulator managing the dump of the vector content into a stream.
Definition: DumpUtils.h:155
void Vector3Dtest_array()
void ArrayDocumentationTest()
BEGIN_PROLOG could also be cout
void VectorTest_STLlist()
bnb BNB Stream
void ArrayTest_carray()