SourceXtractorPlusPlus  0.15
Please provide a description of the project.
TestUtils.h
Go to the documentation of this file.
1 
24 #ifndef _COMPAREIMAGES_H
25 #define _COMPAREIMAGES_H
26 
27 #include "SEUtils/IsClose.h"
28 
29 namespace SourceXtractor {
30 
31 template <typename T, typename U>
32 boost::test_tools::predicate_result compareImages(
33  const T& ref, const U& val, double atol = 1e-8, double rtol = 1e-5) {
34  boost::test_tools::predicate_result res(true);
35 
36  if (ref->getWidth() != val->getWidth() || ref->getHeight() != val->getHeight()) {
37  res = false;
38  res.message() << "Images do not match in size: "
39  << ref->getWidth() << 'x' << ref->getHeight() << " vs "
40  << val->getWidth() << 'x' << val->getHeight();
41  }
42 
43  int w = ref->getWidth(), h = ref->getHeight();
44  auto ref_chunk = ref->getChunk(0, 0, w, h);
45  auto val_chunk = val->getChunk(0, 0, w, h);
46 
47  for (int x = 0; x < ref->getWidth(); ++x) {
48  for (int y = 0; y < ref->getHeight(); ++y) {
49  auto expected = ref_chunk->getValue(x, y);
50  auto value = val_chunk->getValue(x, y);
51  if (!isClose(expected, value, atol, rtol)) {
52  res = false;
53  res.message() << "Not matching values at position " << x << "," << y
54  << ": " << expected << " != " << value << "\n";
55  }
56  }
57  }
58 
59  return res;
60 }
61 
62 template <typename T, typename U>
63 boost::test_tools::predicate_result compareCollections(
64  const T& ref, const U& val, double atol = 1e-8, double rtol = 1e-5) {
65  boost::test_tools::predicate_result res(true);
66 
67  auto ref_i = std::begin(ref);
68  auto val_i = std::begin(val);
69  int i = 0;
70 
71  while (ref_i != std::end(ref) && val_i != std::end(val)) {
72 
73  if (!isClose(*ref_i, *val_i, atol, rtol)) {
74  res = false;
75  res.message() << "Not matching values at position " << i << ": " << *ref_i << " != " << *val_i << "\n";
76  }
77 
78  ++ref_i;
79  ++val_i;
80  ++i;
81  }
82 
83  if (ref_i != std::end(ref) || val_i != std::end(val)) {
84  res = false;
85  res.message() << "The sequences have different length!" << "\n";
86  }
87 
88  return res;
89 }
90 
91 boost::test_tools::predicate_result checkIsClose(double ref, const double val, double atol = 1e-8, double rtol = 1e-5) {
92  boost::test_tools::predicate_result res(true);
93  if (!isClose(ref, val, atol, rtol)) {
94  res = false;
95  res.message() << "Values not close enough: " << ref << " " << val << " with absolute tolerance " << atol
96  << " and relative " << rtol << "\n";
97  }
98  return res;
99 }
100 
101 } // end SourceXtractor
102 
103 #endif // _COMPAREIMAGES_H
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T atol(T... args)
T begin(T... args)
T end(T... args)
bool isClose(double a, double b, double atol=1e-8, double rtol=1e-5)
Definition: IsClose.h:28
boost::test_tools::predicate_result checkIsClose(double ref, const double val, double atol=1e-8, double rtol=1e-5)
Definition: TestUtils.h:91
boost::test_tools::predicate_result compareCollections(const T &ref, const U &val, double atol=1e-8, double rtol=1e-5)
Definition: TestUtils.h:63
boost::test_tools::predicate_result compareImages(const T &ref, const U &val, double atol=1e-8, double rtol=1e-5)
Definition: TestUtils.h:32
T ref(T... args)