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