gtsam  4.0.0
gtsam
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 <boost/optional.hpp>
24 #include <map>
25 #include <iostream>
26 #include <vector>
27 
28 namespace gtsam {
29 
33 inline bool assert_equal(const Key& expected, const Key& actual, double tol = 0.0) {
34  if(expected != actual) {
35  std::cout << "Not equal:\nexpected: " << expected << "\nactual: " << actual << std::endl;
36  return false;
37  }
38  return true;
39 }
40 
48 template<class V>
49 bool assert_equal(const boost::optional<V>& expected,
50  const boost::optional<V>& actual, double tol = 1e-9) {
51  if (!expected && actual) {
52  std::cout << "expected is boost::none, while actual is not" << std::endl;
53  return false;
54  }
55  if (expected && !actual) {
56  std::cout << "actual is boost::none, while expected is not" << std::endl;
57  return false;
58  }
59  if (!expected && !actual)
60  return true;
61  return assert_equal(*expected, *actual, tol);
62 }
63 
64 template<class V>
65 bool assert_equal(const V& expected, const boost::optional<V>& actual, double tol = 1e-9) {
66  if (!actual) {
67  std::cout << "actual is boost::none" << std::endl;
68  return false;
69  }
70  return assert_equal(expected, *actual, tol);
71 }
72 
73 template<class V>
74 bool assert_equal(const V& expected, const boost::optional<const V&>& actual, double tol = 1e-9) {
75  if (!actual) {
76  std::cout << "actual is boost::none" << std::endl;
77  return false;
78  }
79  return assert_equal(expected, *actual, tol);
80 }
81 
86 template<class V>
87 bool assert_equal(const std::vector<V>& expected, const std::vector<V>& actual, double tol = 1e-9) {
88  bool match = true;
89  if (expected.size() != actual.size())
90  match = false;
91  if(match) {
92  size_t i = 0;
93  for(const V& a: expected) {
94  if (!assert_equal(a, actual[i++], tol)) {
95  match = false;
96  break;
97  }
98  }
99  }
100  if(!match) {
101  std::cout << "expected: " << std::endl;
102  for(const V& a: expected) { std::cout << a << " "; }
103  std::cout << "\nactual: " << std::endl;
104  for(const V& a: actual) { std::cout << a << " "; }
105  std::cout << std::endl;
106  return false;
107  }
108  return true;
109 }
110 
115 template<class V1, class V2>
116 bool assert_container_equal(const std::map<V1,V2>& expected, const std::map<V1,V2>& actual, double tol = 1e-9) {
117  typedef typename std::map<V1,V2> Map;
118  bool match = true;
119  if (expected.size() != actual.size())
120  match = false;
121  typename Map::const_iterator
122  itExp = expected.begin(),
123  itAct = actual.begin();
124  if(match) {
125  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
126  if (!assert_equal(itExp->first, itAct->first, tol) ||
127  !assert_equal(itExp->second, itAct->second, tol)) {
128  match = false;
129  break;
130  }
131  }
132  }
133  if(!match) {
134  std::cout << "expected: " << std::endl;
135  for(const typename Map::value_type& a: expected) {
136  a.first.print("key");
137  a.second.print(" value");
138  }
139  std::cout << "\nactual: " << std::endl;
140  for(const typename Map::value_type& a: actual) {
141  a.first.print("key");
142  a.second.print(" value");
143  }
144  std::cout << std::endl;
145  return false;
146  }
147  return true;
148 }
149 
153 template<class V2>
154 bool assert_container_equal(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual, double tol = 1e-9) {
155  typedef typename std::map<size_t,V2> Map;
156  bool match = true;
157  if (expected.size() != actual.size())
158  match = false;
159  typename Map::const_iterator
160  itExp = expected.begin(),
161  itAct = actual.begin();
162  if(match) {
163  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
164  if (itExp->first != itAct->first ||
165  !assert_equal(itExp->second, itAct->second, tol)) {
166  match = false;
167  break;
168  }
169  }
170  }
171  if(!match) {
172  std::cout << "expected: " << std::endl;
173  for(const typename Map::value_type& a: expected) {
174  std::cout << "Key: " << a.first << std::endl;
175  a.second.print(" value");
176  }
177  std::cout << "\nactual: " << std::endl;
178  for(const typename Map::value_type& a: actual) {
179  std::cout << "Key: " << a.first << std::endl;
180  a.second.print(" value");
181  }
182  std::cout << std::endl;
183  return false;
184  }
185  return true;
186 }
187 
191 template<class V1, class V2>
192 bool assert_container_equal(const std::vector<std::pair<V1,V2> >& expected,
193  const std::vector<std::pair<V1,V2> >& actual, double tol = 1e-9) {
194  typedef typename std::vector<std::pair<V1,V2> > VectorPair;
195  bool match = true;
196  if (expected.size() != actual.size())
197  match = false;
198  typename VectorPair::const_iterator
199  itExp = expected.begin(),
200  itAct = actual.begin();
201  if(match) {
202  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
203  if (!assert_equal(itExp->first, itAct->first, tol) ||
204  !assert_equal(itExp->second, itAct->second, tol)) {
205  match = false;
206  break;
207  }
208  }
209  }
210  if(!match) {
211  std::cout << "expected: " << std::endl;
212  for(const typename VectorPair::value_type& a: expected) {
213  a.first.print( " first ");
214  a.second.print(" second");
215  }
216  std::cout << "\nactual: " << std::endl;
217  for(const typename VectorPair::value_type& a: actual) {
218  a.first.print( " first ");
219  a.second.print(" second");
220  }
221  std::cout << std::endl;
222  return false;
223  }
224  return true;
225 }
226 
227 
231 template<class V>
232 bool assert_container_equal(const V& expected, const V& actual, double tol = 1e-9) {
233  bool match = true;
234  typename V::const_iterator
235  itExp = expected.begin(),
236  itAct = actual.begin();
237  if(match) {
238  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
239  if (!assert_equal(*itExp, *itAct, tol)) {
240  match = false;
241  break;
242  }
243  }
244  if(itExp != expected.end() || itAct != actual.end())
245  match = false;
246  }
247  if(!match) {
248  std::cout << "expected: " << std::endl;
249  for(const typename V::value_type& a: expected) { a.print(" "); }
250  std::cout << "\nactual: " << std::endl;
251  for(const typename V::value_type& a: actual) { a.print(" "); }
252  std::cout << std::endl;
253  return false;
254  }
255  return true;
256 }
257 
262 template<class V2>
263 bool assert_container_equality(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual) {
264  typedef typename std::map<size_t,V2> Map;
265  bool match = true;
266  if (expected.size() != actual.size())
267  match = false;
268  typename Map::const_iterator
269  itExp = expected.begin(),
270  itAct = actual.begin();
271  if(match) {
272  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
273  if (itExp->first != itAct->first || itExp->second != itAct->second) {
274  match = false;
275  break;
276  }
277  }
278  }
279  if(!match) {
280  std::cout << "expected: " << std::endl;
281  for(const typename Map::value_type& a: expected) {
282  std::cout << "Key: " << a.first << std::endl;
283  std::cout << "Value: " << a.second << std::endl;
284  }
285  std::cout << "\nactual: " << std::endl;
286  for(const typename Map::value_type& a: actual) {
287  std::cout << "Key: " << a.first << std::endl;
288  std::cout << "Value: " << a.second << std::endl;
289  }
290  std::cout << std::endl;
291  return false;
292  }
293  return true;
294 }
295 
296 
300 template<class V>
301 bool assert_container_equality(const V& expected, const V& actual) {
302  bool match = true;
303  if (expected.size() != actual.size())
304  match = false;
305  typename V::const_iterator
306  itExp = expected.begin(),
307  itAct = actual.begin();
308  if(match) {
309  for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
310  if (*itExp != *itAct) {
311  match = false;
312  break;
313  }
314  }
315  }
316  if(!match) {
317  std::cout << "expected: " << std::endl;
318  for(const typename V::value_type& a: expected) { std::cout << a << " "; }
319  std::cout << "\nactual: " << std::endl;
320  for(const typename V::value_type& a: actual) { std::cout << a << " "; }
321  std::cout << std::endl;
322  return false;
323  }
324  return true;
325 }
326 
330 inline bool assert_equal(const std::string& expected, const std::string& actual) {
331  if (expected == actual)
332  return true;
333  printf("Not equal:\n");
334  std::cout << "expected: [" << expected << "]\n";
335  std::cout << "actual: [" << actual << "]" << std::endl;
336  return false;
337 }
338 
342 template<class V>
343 bool assert_inequal(const V& expected, const V& actual, double tol = 1e-9) {
344  if (!actual.equals(expected, tol))
345  return true;
346  printf("Erroneously equal:\n");
347  expected.print("expected");
348  actual.print("actual");
349  return false;
350 }
351 
352 } // \namespace gtsam
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:42
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Included from all GTSAM files.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Concept check for values that can be used in unit tests.
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
inequals with an tolerance, prints out message if within tolerance
Definition: Matrix.cpp:62
bool assert_container_equality(const std::map< size_t, V2 > &expected, const std::map< size_t, V2 > &actual)
Function for comparing maps of size_t->testable Types are assumed to have operator ==.
Definition: TestableAssertions.h:263
bool assert_container_equal(const std::map< V1, V2 > &expected, const std::map< V1, V2 > &actual, double tol=1e-9)
Function for comparing maps of testable->testable TODO: replace with more generalized version.
Definition: TestableAssertions.h:116