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
17
18#pragma once
19
20#include <gtsam/base/Testable.h>
22
23#include <functional>
24#include <optional>
25#include <map>
26#include <iostream>
27#include <sstream>
28#include <vector>
29
30namespace gtsam {
31
35inline bool assert_equal(const Key& expected, const Key& actual) {
36 // TODO - why isn't tol used?
37 if(expected != actual) {
38 std::cout << "Not equal:\nexpected: " << expected << "\nactual: " << actual << std::endl;
39 return false;
40 }
41 return true;
42}
43
51template<class V>
52bool assert_equal(const std::optional<V>& expected,
53 const std::optional<V>& actual, double tol = 1e-9) {
54 if (!expected && actual) {
55 std::cout << "expected is {}, while actual is not" << std::endl;
56 return false;
57 }
58 if (expected && !actual) {
59 std::cout << "actual is {}, while expected is not" << std::endl;
60 return false;
61 }
62 if (!expected && !actual)
63 return true;
64 return assert_equal(*expected, *actual, tol);
65}
66
67template<class V>
68bool assert_equal(const V& expected, const std::optional<V>& actual, double tol = 1e-9) {
69 if (!actual) {
70 std::cout << "actual is {}" << std::endl;
71 return false;
72 }
73 return assert_equal(expected, *actual, tol);
74}
75
76template<class V>
77bool assert_equal(const V& expected,
78 const std::optional<std::reference_wrapper<const V>>& actual, double tol = 1e-9) {
79 if (!actual) {
80 std::cout << "actual is std::nullopt" << std::endl;
81 return false;
82 }
83 return assert_equal(expected, *actual.get(), tol);
84}
85
90template<class V1, class V2>
91bool assert_container_equal(const std::map<V1,V2>& expected, const std::map<V1,V2>& actual, double tol = 1e-9) {
92 typedef typename std::map<V1,V2> Map;
93 bool match = true;
94 if (expected.size() != actual.size())
95 match = false;
96 typename Map::const_iterator
97 itExp = expected.begin(),
98 itAct = actual.begin();
99 if(match) {
100 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
101 if (!assert_equal(itExp->first, itAct->first, tol) ||
102 !assert_equal(itExp->second, itAct->second, tol)) {
103 match = false;
104 break;
105 }
106 }
107 }
108 if(!match) {
109 std::cout << "expected: " << std::endl;
110 for(const typename Map::value_type& a: expected) {
111 a.first.print("key");
112 a.second.print(" value");
113 }
114 std::cout << "\nactual: " << std::endl;
115 for(const typename Map::value_type& a: actual) {
116 a.first.print("key");
117 a.second.print(" value");
118 }
119 std::cout << std::endl;
120 return false;
121 }
122 return true;
123}
124
128template<class V2>
129bool assert_container_equal(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual, double tol = 1e-9) {
130 typedef typename std::map<size_t,V2> Map;
131 bool match = true;
132 if (expected.size() != actual.size())
133 match = false;
134 typename Map::const_iterator
135 itExp = expected.begin(),
136 itAct = actual.begin();
137 if(match) {
138 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
139 if (itExp->first != itAct->first ||
140 !assert_equal(itExp->second, itAct->second, tol)) {
141 match = false;
142 break;
143 }
144 }
145 }
146 if(!match) {
147 std::cout << "expected: " << std::endl;
148 for(const typename Map::value_type& a: expected) {
149 std::cout << "Key: " << a.first << std::endl;
150 a.second.print(" value");
151 }
152 std::cout << "\nactual: " << std::endl;
153 for(const typename Map::value_type& a: actual) {
154 std::cout << "Key: " << a.first << std::endl;
155 a.second.print(" value");
156 }
157 std::cout << std::endl;
158 return false;
159 }
160 return true;
161}
162
166template<class V1, class V2>
167bool assert_container_equal(const std::vector<std::pair<V1,V2> >& expected,
168 const std::vector<std::pair<V1,V2> >& actual, double tol = 1e-9) {
169 typedef typename std::vector<std::pair<V1,V2> > VectorPair;
170 bool match = true;
171 if (expected.size() != actual.size())
172 match = false;
173 typename VectorPair::const_iterator
174 itExp = expected.begin(),
175 itAct = actual.begin();
176 if(match) {
177 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
178 if (!assert_equal(itExp->first, itAct->first, tol) ||
179 !assert_equal(itExp->second, itAct->second, tol)) {
180 match = false;
181 break;
182 }
183 }
184 }
185 if(!match) {
186 std::cout << "expected: " << std::endl;
187 for(const typename VectorPair::value_type& a: expected) {
188 a.first.print( " first ");
189 a.second.print(" second");
190 }
191 std::cout << "\nactual: " << std::endl;
192 for(const typename VectorPair::value_type& a: actual) {
193 a.first.print( " first ");
194 a.second.print(" second");
195 }
196 std::cout << std::endl;
197 return false;
198 }
199 return true;
200}
201
202
206template<class V>
207bool assert_container_equal(const V& expected, const V& actual, double tol = 1e-9) {
208 bool match = true;
209 typename V::const_iterator
210 itExp = expected.begin(),
211 itAct = actual.begin();
212 if(match) {
213 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
214 if (!assert_equal(*itExp, *itAct, tol)) {
215 match = false;
216 break;
217 }
218 }
219 if(itExp != expected.end() || itAct != actual.end())
220 match = false;
221 }
222 if(!match) {
223 std::cout << "expected: " << std::endl;
224 for(const typename V::value_type& a: expected) { a.print(" "); }
225 std::cout << "\nactual: " << std::endl;
226 for(const typename V::value_type& a: actual) { a.print(" "); }
227 std::cout << std::endl;
228 return false;
229 }
230 return true;
231}
232
237template<class V2>
238bool assert_container_equality(const std::map<size_t,V2>& expected, const std::map<size_t,V2>& actual) {
239 typedef typename std::map<size_t,V2> Map;
240 bool match = true;
241 if (expected.size() != actual.size())
242 match = false;
243 typename Map::const_iterator
244 itExp = expected.begin(),
245 itAct = actual.begin();
246 if(match) {
247 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
248 if (itExp->first != itAct->first || itExp->second != itAct->second) {
249 match = false;
250 break;
251 }
252 }
253 }
254 if(!match) {
255 std::cout << "expected: " << std::endl;
256 for(const typename Map::value_type& a: expected) {
257 std::cout << "Key: " << a.first << std::endl;
258 std::cout << "Value: " << a.second << std::endl;
259 }
260 std::cout << "\nactual: " << std::endl;
261 for(const typename Map::value_type& a: actual) {
262 std::cout << "Key: " << a.first << std::endl;
263 std::cout << "Value: " << a.second << std::endl;
264 }
265 std::cout << std::endl;
266 return false;
267 }
268 return true;
269}
270
271
275template<class V>
276bool assert_container_equality(const V& expected, const V& actual) {
277 bool match = true;
278 if (expected.size() != actual.size())
279 match = false;
280 typename V::const_iterator
281 itExp = expected.begin(),
282 itAct = actual.begin();
283 if(match) {
284 for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) {
285 if (*itExp != *itAct) {
286 match = false;
287 break;
288 }
289 }
290 }
291 if(!match) {
292 std::cout << "expected: " << std::endl;
293 for(const typename V::value_type& a: expected) { std::cout << a << " "; }
294 std::cout << "\nactual: " << std::endl;
295 for(const typename V::value_type& a: actual) { std::cout << a << " "; }
296 std::cout << std::endl;
297 return false;
298 }
299 return true;
300}
301
305inline bool assert_equal(const std::string& expected, const std::string& actual) {
306 if (expected == actual)
307 return true;
308 printf("Not equal:\n");
309 std::cout << "expected: [" << expected << "]\n";
310 std::cout << "actual: [" << actual << "]" << std::endl;
311 return false;
312}
313
317template<class V>
318bool assert_inequal(const V& expected, const V& actual, double tol = 1e-9) {
319 if (!actual.equals(expected, tol))
320 return true;
321 printf("Erroneously equal:\n");
322 expected.print("expected");
323 actual.print("actual");
324 return false;
325}
326
330template<class V>
331bool assert_stdout_equal(const std::string& expected, const V& actual) {
332 // Redirect output to buffer so we can compare
333 std::stringstream buffer;
334 // Save the original output stream so we can reset later
335 std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());
336
337 // We test against actual std::cout for faithful reproduction
338 std::cout << actual;
339
340 // Get output string and reset stdout
341 std::string actual_ = buffer.str();
342 std::cout.rdbuf(old);
343
344 return assert_equal(expected, actual_);
345}
346
352template <class V>
353bool assert_print_equal(const std::string& expected, const V& actual,
354 const std::string& s = "") {
355 // Redirect output to buffer so we can compare
356 std::stringstream buffer;
357 // Save the original output stream so we can reset later
358 std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());
359
360 // We test against actual std::cout for faithful reproduction
361 actual.print(s);
362
363 // Get output string and reset stdout
364 std::string actual_ = buffer.str();
365 std::cout.rdbuf(old);
366
367 return assert_equal(expected, actual_);
368}
369
370} // \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:331
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:91
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:39
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:238
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
inequals with an tolerance, prints out message if within tolerance
Definition Matrix.cpp:59
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:353
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43