gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
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>
22
23#include <boost/optional.hpp>
24#include <map>
25#include <iostream>
26#include <sstream>
27#include <vector>
28
29namespace gtsam {
30
34inline bool assert_equal(const Key& expected, const Key& actual, double tol = 0.0) {
35 if(expected != actual) {
36 std::cout << "Not equal:\nexpected: " << expected << "\nactual: " << actual << std::endl;
37 return false;
38 }
39 return true;
40}
41
49template<class V>
50bool assert_equal(const boost::optional<V>& expected,
51 const boost::optional<V>& actual, double tol = 1e-9) {
52 if (!expected && actual) {
53 std::cout << "expected is boost::none, while actual is not" << std::endl;
54 return false;
55 }
56 if (expected && !actual) {
57 std::cout << "actual is boost::none, while expected is not" << std::endl;
58 return false;
59 }
60 if (!expected && !actual)
61 return true;
62 return assert_equal(*expected, *actual, tol);
63}
64
65template<class V>
66bool assert_equal(const V& expected, const boost::optional<V>& actual, double tol = 1e-9) {
67 if (!actual) {
68 std::cout << "actual is boost::none" << std::endl;
69 return false;
70 }
71 return assert_equal(expected, *actual, tol);
72}
73
74template<class V>
75bool assert_equal(const V& expected, const boost::optional<const V&>& actual, double tol = 1e-9) {
76 if (!actual) {
77 std::cout << "actual is boost::none" << std::endl;
78 return false;
79 }
80 return assert_equal(expected, *actual, tol);
81}
82
83#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V42
88template<class V>
89bool GTSAM_DEPRECATED assert_equal(const std::vector<V>& expected, const std::vector<V>& actual, double tol = 1e-9) {
90 bool match = true;
91 if (expected.size() != actual.size())
92 match = false;
93 if(match) {
94 size_t i = 0;
95 for(const V& a: expected) {
96 if (!assert_equal(a, actual[i++], tol)) {
97 match = false;
98 break;
99 }
100 }
101 }
102 if(!match) {
103 std::cout << "expected: " << std::endl;
104 for(const V& a: expected) { std::cout << a << " "; }
105 std::cout << "\nactual: " << std::endl;
106 for(const V& a: actual) { std::cout << a << " "; }
107 std::cout << std::endl;
108 return false;
109 }
110 return true;
111}
112#endif
113
118template<class V1, class V2>
119bool assert_container_equal(const std::map<V1,V2>& expected, const std::map<V1,V2>& actual, double tol = 1e-9) {
120 typedef typename std::map<V1,V2> Map;
121 bool match = true;
122 if (expected.size() != actual.size())
123 match = false;
124 typename Map::const_iterator
125 itExp = expected.begin(),
126 itAct = actual.begin();
127 if(match) {
128 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
129 if (!assert_equal(itExp->first, itAct->first, tol) ||
130 !assert_equal(itExp->second, itAct->second, tol)) {
131 match = false;
132 break;
133 }
134 }
135 }
136 if(!match) {
137 std::cout << "expected: " << std::endl;
138 for(const typename Map::value_type& a: expected) {
139 a.first.print("key");
140 a.second.print(" value");
141 }
142 std::cout << "\nactual: " << std::endl;
143 for(const typename Map::value_type& a: actual) {
144 a.first.print("key");
145 a.second.print(" value");
146 }
147 std::cout << std::endl;
148 return false;
149 }
150 return true;
151}
152
156template<class V2>
157bool assert_container_equal(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual, double tol = 1e-9) {
158 typedef typename std::map<size_t,V2> Map;
159 bool match = true;
160 if (expected.size() != actual.size())
161 match = false;
162 typename Map::const_iterator
163 itExp = expected.begin(),
164 itAct = actual.begin();
165 if(match) {
166 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
167 if (itExp->first != itAct->first ||
168 !assert_equal(itExp->second, itAct->second, tol)) {
169 match = false;
170 break;
171 }
172 }
173 }
174 if(!match) {
175 std::cout << "expected: " << std::endl;
176 for(const typename Map::value_type& a: expected) {
177 std::cout << "Key: " << a.first << std::endl;
178 a.second.print(" value");
179 }
180 std::cout << "\nactual: " << std::endl;
181 for(const typename Map::value_type& a: actual) {
182 std::cout << "Key: " << a.first << std::endl;
183 a.second.print(" value");
184 }
185 std::cout << std::endl;
186 return false;
187 }
188 return true;
189}
190
194template<class V1, class V2>
195bool assert_container_equal(const std::vector<std::pair<V1,V2> >& expected,
196 const std::vector<std::pair<V1,V2> >& actual, double tol = 1e-9) {
197 typedef typename std::vector<std::pair<V1,V2> > VectorPair;
198 bool match = true;
199 if (expected.size() != actual.size())
200 match = false;
201 typename VectorPair::const_iterator
202 itExp = expected.begin(),
203 itAct = actual.begin();
204 if(match) {
205 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
206 if (!assert_equal(itExp->first, itAct->first, tol) ||
207 !assert_equal(itExp->second, itAct->second, tol)) {
208 match = false;
209 break;
210 }
211 }
212 }
213 if(!match) {
214 std::cout << "expected: " << std::endl;
215 for(const typename VectorPair::value_type& a: expected) {
216 a.first.print( " first ");
217 a.second.print(" second");
218 }
219 std::cout << "\nactual: " << std::endl;
220 for(const typename VectorPair::value_type& a: actual) {
221 a.first.print( " first ");
222 a.second.print(" second");
223 }
224 std::cout << std::endl;
225 return false;
226 }
227 return true;
228}
229
230
234template<class V>
235bool assert_container_equal(const V& expected, const V& actual, double tol = 1e-9) {
236 bool match = true;
237 typename V::const_iterator
238 itExp = expected.begin(),
239 itAct = actual.begin();
240 if(match) {
241 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
242 if (!assert_equal(*itExp, *itAct, tol)) {
243 match = false;
244 break;
245 }
246 }
247 if(itExp != expected.end() || itAct != actual.end())
248 match = false;
249 }
250 if(!match) {
251 std::cout << "expected: " << std::endl;
252 for(const typename V::value_type& a: expected) { a.print(" "); }
253 std::cout << "\nactual: " << std::endl;
254 for(const typename V::value_type& a: actual) { a.print(" "); }
255 std::cout << std::endl;
256 return false;
257 }
258 return true;
259}
260
265template<class V2>
266bool assert_container_equality(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual) {
267 typedef typename std::map<size_t,V2> Map;
268 bool match = true;
269 if (expected.size() != actual.size())
270 match = false;
271 typename Map::const_iterator
272 itExp = expected.begin(),
273 itAct = actual.begin();
274 if(match) {
275 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
276 if (itExp->first != itAct->first || itExp->second != itAct->second) {
277 match = false;
278 break;
279 }
280 }
281 }
282 if(!match) {
283 std::cout << "expected: " << std::endl;
284 for(const typename Map::value_type& a: expected) {
285 std::cout << "Key: " << a.first << std::endl;
286 std::cout << "Value: " << a.second << std::endl;
287 }
288 std::cout << "\nactual: " << std::endl;
289 for(const typename Map::value_type& a: actual) {
290 std::cout << "Key: " << a.first << std::endl;
291 std::cout << "Value: " << a.second << std::endl;
292 }
293 std::cout << std::endl;
294 return false;
295 }
296 return true;
297}
298
299
303template<class V>
304bool assert_container_equality(const V& expected, const V& actual) {
305 bool match = true;
306 if (expected.size() != actual.size())
307 match = false;
308 typename V::const_iterator
309 itExp = expected.begin(),
310 itAct = actual.begin();
311 if(match) {
312 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
313 if (*itExp != *itAct) {
314 match = false;
315 break;
316 }
317 }
318 }
319 if(!match) {
320 std::cout << "expected: " << std::endl;
321 for(const typename V::value_type& a: expected) { std::cout << a << " "; }
322 std::cout << "\nactual: " << std::endl;
323 for(const typename V::value_type& a: actual) { std::cout << a << " "; }
324 std::cout << std::endl;
325 return false;
326 }
327 return true;
328}
329
333inline bool assert_equal(const std::string& expected, const std::string& actual) {
334 if (expected == actual)
335 return true;
336 printf("Not equal:\n");
337 std::cout << "expected: [" << expected << "]\n";
338 std::cout << "actual: [" << actual << "]" << std::endl;
339 return false;
340}
341
345template<class V>
346bool assert_inequal(const V& expected, const V& actual, double tol = 1e-9) {
347 if (!actual.equals(expected, tol))
348 return true;
349 printf("Erroneously equal:\n");
350 expected.print("expected");
351 actual.print("actual");
352 return false;
353}
354
358template<class V>
359bool assert_stdout_equal(const std::string& expected, const V& actual) {
360 // Redirect output to buffer so we can compare
361 std::stringstream buffer;
362 // Save the original output stream so we can reset later
363 std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());
364
365 // We test against actual std::cout for faithful reproduction
366 std::cout << actual;
367
368 // Get output string and reset stdout
369 std::string actual_ = buffer.str();
370 std::cout.rdbuf(old);
371
372 return assert_equal(expected, actual_);
373}
374
380template <class V>
381bool assert_print_equal(const std::string& expected, const V& actual,
382 const std::string& s = "") {
383 // Redirect output to buffer so we can compare
384 std::stringstream buffer;
385 // Save the original output stream so we can reset later
386 std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());
387
388 // We test against actual std::cout for faithful reproduction
389 actual.print(s);
390
391 // Get output string and reset stdout
392 std::string actual_ = buffer.str();
393 std::cout.rdbuf(old);
394
395 return assert_equal(expected, actual_);
396}
397
398} // \namespace gtsam
Concept check for values that can be used in unit tests.
Included from all GTSAM files.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
bool assert_stdout_equal(const std::string &expected, const V &actual)
Capture std out via cout stream and compare against string.
Definition TestableAssertions.h:359
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:119
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:43
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:266
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
inequals with an tolerance, prints out message if within tolerance
Definition Matrix.cpp:63
bool assert_print_equal(const std::string &expected, const V &actual, const std::string &s="")
Capture print function output and compare against string.
Definition TestableAssertions.h:381
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100