TestableAssertions.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <gtsam/base/Testable.h>
21 #include <gtsam/global_includes.h>
22 
23 #include <functional>
24 #include <optional>
25 #include <map>
26 #include <iostream>
27 #include <sstream>
28 #include <vector>
29 
30 namespace gtsam {
31 
35 inline bool assert_equal(const Key& expected, const Key& actual, double tol = 0.0) {
36  if(expected != actual) {
37  std::cout << "Not equal:\nexpected: " << expected << "\nactual: " << actual << std::endl;
38  return false;
39  }
40  return true;
41 }
42 
50 template<class V>
51 bool assert_equal(const std::optional<V>& expected,
52  const std::optional<V>& actual, double tol = 1e-9) {
53  if (!expected && actual) {
54  std::cout << "expected is {}, while actual is not" << std::endl;
55  return false;
56  }
57  if (expected && !actual) {
58  std::cout << "actual is {}, while expected is not" << std::endl;
59  return false;
60  }
61  if (!expected && !actual)
62  return true;
63  return assert_equal(*expected, *actual, tol);
64 }
65 
66 template<class V>
67 bool assert_equal(const V& expected, const std::optional<V>& actual, double tol = 1e-9) {
68  if (!actual) {
69  std::cout << "actual is {}" << std::endl;
70  return false;
71  }
72  return assert_equal(expected, *actual, tol);
73 }
74 
75 template<class V>
76 bool assert_equal(const V& expected,
77  const std::optional<std::reference_wrapper<const V>>& actual, double tol = 1e-9) {
78  if (!actual) {
79  std::cout << "actual is std::nullopt" << std::endl;
80  return false;
81  }
82  return assert_equal(expected, *actual.get(), tol);
83 }
84 
89 template<class V1, class V2>
90 bool assert_container_equal(const std::map<V1,V2>& expected, const std::map<V1,V2>& actual, double tol = 1e-9) {
91  typedef typename std::map<V1,V2> Map;
92  bool match = true;
93  if (expected.size() != actual.size())
94  match = false;
95  typename Map::const_iterator
96  itExp = expected.begin(),
97  itAct = actual.begin();
98  if(match) {
99  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
100  if (!assert_equal(itExp->first, itAct->first, tol) ||
101  !assert_equal(itExp->second, itAct->second, tol)) {
102  match = false;
103  break;
104  }
105  }
106  }
107  if(!match) {
108  std::cout << "expected: " << std::endl;
109  for(const typename Map::value_type& a: expected) {
110  a.first.print("key");
111  a.second.print(" value");
112  }
113  std::cout << "\nactual: " << std::endl;
114  for(const typename Map::value_type& a: actual) {
115  a.first.print("key");
116  a.second.print(" value");
117  }
118  std::cout << std::endl;
119  return false;
120  }
121  return true;
122 }
123 
127 template<class V2>
128 bool assert_container_equal(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual, double tol = 1e-9) {
129  typedef typename std::map<size_t,V2> Map;
130  bool match = true;
131  if (expected.size() != actual.size())
132  match = false;
133  typename Map::const_iterator
134  itExp = expected.begin(),
135  itAct = actual.begin();
136  if(match) {
137  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
138  if (itExp->first != itAct->first ||
139  !assert_equal(itExp->second, itAct->second, tol)) {
140  match = false;
141  break;
142  }
143  }
144  }
145  if(!match) {
146  std::cout << "expected: " << std::endl;
147  for(const typename Map::value_type& a: expected) {
148  std::cout << "Key: " << a.first << std::endl;
149  a.second.print(" value");
150  }
151  std::cout << "\nactual: " << std::endl;
152  for(const typename Map::value_type& a: actual) {
153  std::cout << "Key: " << a.first << std::endl;
154  a.second.print(" value");
155  }
156  std::cout << std::endl;
157  return false;
158  }
159  return true;
160 }
161 
165 template<class V1, class V2>
166 bool assert_container_equal(const std::vector<std::pair<V1,V2> >& expected,
167  const std::vector<std::pair<V1,V2> >& actual, double tol = 1e-9) {
168  typedef typename std::vector<std::pair<V1,V2> > VectorPair;
169  bool match = true;
170  if (expected.size() != actual.size())
171  match = false;
172  typename VectorPair::const_iterator
173  itExp = expected.begin(),
174  itAct = actual.begin();
175  if(match) {
176  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
177  if (!assert_equal(itExp->first, itAct->first, tol) ||
178  !assert_equal(itExp->second, itAct->second, tol)) {
179  match = false;
180  break;
181  }
182  }
183  }
184  if(!match) {
185  std::cout << "expected: " << std::endl;
186  for(const typename VectorPair::value_type& a: expected) {
187  a.first.print( " first ");
188  a.second.print(" second");
189  }
190  std::cout << "\nactual: " << std::endl;
191  for(const typename VectorPair::value_type& a: actual) {
192  a.first.print( " first ");
193  a.second.print(" second");
194  }
195  std::cout << std::endl;
196  return false;
197  }
198  return true;
199 }
200 
201 
205 template<class V>
206 bool assert_container_equal(const V& expected, const V& actual, double tol = 1e-9) {
207  bool match = true;
208  typename V::const_iterator
209  itExp = expected.begin(),
210  itAct = actual.begin();
211  if(match) {
212  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
213  if (!assert_equal(*itExp, *itAct, tol)) {
214  match = false;
215  break;
216  }
217  }
218  if(itExp != expected.end() || itAct != actual.end())
219  match = false;
220  }
221  if(!match) {
222  std::cout << "expected: " << std::endl;
223  for(const typename V::value_type& a: expected) { a.print(" "); }
224  std::cout << "\nactual: " << std::endl;
225  for(const typename V::value_type& a: actual) { a.print(" "); }
226  std::cout << std::endl;
227  return false;
228  }
229  return true;
230 }
231 
236 template<class V2>
237 bool assert_container_equality(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual) {
238  typedef typename std::map<size_t,V2> Map;
239  bool match = true;
240  if (expected.size() != actual.size())
241  match = false;
242  typename Map::const_iterator
243  itExp = expected.begin(),
244  itAct = actual.begin();
245  if(match) {
246  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
247  if (itExp->first != itAct->first || itExp->second != itAct->second) {
248  match = false;
249  break;
250  }
251  }
252  }
253  if(!match) {
254  std::cout << "expected: " << std::endl;
255  for(const typename Map::value_type& a: expected) {
256  std::cout << "Key: " << a.first << std::endl;
257  std::cout << "Value: " << a.second << std::endl;
258  }
259  std::cout << "\nactual: " << std::endl;
260  for(const typename Map::value_type& a: actual) {
261  std::cout << "Key: " << a.first << std::endl;
262  std::cout << "Value: " << a.second << std::endl;
263  }
264  std::cout << std::endl;
265  return false;
266  }
267  return true;
268 }
269 
270 
274 template<class V>
275 bool assert_container_equality(const V& expected, const V& actual) {
276  bool match = true;
277  if (expected.size() != actual.size())
278  match = false;
279  typename V::const_iterator
280  itExp = expected.begin(),
281  itAct = actual.begin();
282  if(match) {
283  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
284  if (*itExp != *itAct) {
285  match = false;
286  break;
287  }
288  }
289  }
290  if(!match) {
291  std::cout << "expected: " << std::endl;
292  for(const typename V::value_type& a: expected) { std::cout << a << " "; }
293  std::cout << "\nactual: " << std::endl;
294  for(const typename V::value_type& a: actual) { std::cout << a << " "; }
295  std::cout << std::endl;
296  return false;
297  }
298  return true;
299 }
300 
304 inline bool assert_equal(const std::string& expected, const std::string& actual) {
305  if (expected == actual)
306  return true;
307  printf("Not equal:\n");
308  std::cout << "expected: [" << expected << "]\n";
309  std::cout << "actual: [" << actual << "]" << std::endl;
310  return false;
311 }
312 
316 template<class V>
317 bool assert_inequal(const V& expected, const V& actual, double tol = 1e-9) {
318  if (!actual.equals(expected, tol))
319  return true;
320  printf("Erroneously equal:\n");
321  expected.print("expected");
322  actual.print("actual");
323  return false;
324 }
325 
329 template<class V>
330 bool assert_stdout_equal(const std::string& expected, const V& actual) {
331  // Redirect output to buffer so we can compare
332  std::stringstream buffer;
333  // Save the original output stream so we can reset later
334  std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());
335 
336  // We test against actual std::cout for faithful reproduction
337  std::cout << actual;
338 
339  // Get output string and reset stdout
340  std::string actual_ = buffer.str();
341  std::cout.rdbuf(old);
342 
343  return assert_equal(expected, actual_);
344 }
345 
351 template <class V>
352 bool assert_print_equal(const std::string& expected, const V& actual,
353  const std::string& s = "") {
354  // Redirect output to buffer so we can compare
355  std::stringstream buffer;
356  // Save the original output stream so we can reset later
357  std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());
358 
359  // We test against actual std::cout for faithful reproduction
360  actual.print(s);
361 
362  // Get output string and reset stdout
363  std::string actual_ = buffer.str();
364  std::cout.rdbuf(old);
365 
366  return assert_equal(expected, actual_);
367 }
368 
369 } // \namespace gtsam
bool assert_stdout_equal(const std::string &expected, const V &actual)
bool match(const T &xpr, std::string ref, std::string str_xpr="")
Concept check for values that can be used in unit tests.
bool assert_print_equal(const std::string &expected, const V &actual, const std::string &s="")
Matrix expected
Definition: testMatrix.cpp:971
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
bool assert_container_equality(const std::map< size_t, V2 > &expected, const std::map< size_t, V2 > &actual)
Included from all GTSAM files.
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
Definition: Matrix.cpp:60
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
bool assert_container_equal(const std::map< V1, V2 > &expected, const std::map< V1, V2 > &actual, double tol=1e-9)
traits
Definition: chartTesting.h:28
const G double tol
Definition: Group.h:86
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46