From d1e1634b52a63eb1ee9ef32d2f310d0cf65a175e Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Tue, 8 Mar 2011 18:13:50 +0000 Subject: [PATCH] Added a version of assert_container_equal for maps --- gtsam/base/TestableAssertions.h | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/gtsam/base/TestableAssertions.h b/gtsam/base/TestableAssertions.h index c518f4755..4968720fb 100644 --- a/gtsam/base/TestableAssertions.h +++ b/gtsam/base/TestableAssertions.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include #include #include @@ -38,7 +39,7 @@ bool assert_equal(const Index& expected, const Index& actual, double tol = 0.0) /** * Version of assert_equals to work with vectors - * TODO: replace this with a more general approach to handle multiple types of containers + * @DEPRECIATED: use container equals instead */ template bool assert_equal(const std::vector& expected, const std::vector& actual, double tol = 1e-9) { @@ -65,6 +66,45 @@ bool assert_equal(const std::vector& expected, const std::vector& actual, return true; } +/** + * Function for comparing maps of testable->testable + * TODO: replace with more generalized version + */ +template +bool assert_container_equal(const std::map& expected, const std::map& actual, double tol = 1e-9) { + typedef typename std::map Map; + bool match = true; + if (expected.size() != actual.size()) + match = false; + typename Map::const_iterator + itExp = expected.begin(), + itAct = actual.begin(); + if(match) { + for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) { + if (!assert_equal(itExp->first, itAct->first, tol) || + !assert_equal(itExp->second, itAct->second, tol)) { + match = false; + break; + } + } + } + if(!match) { + std::cout << "expected: "; + BOOST_FOREACH(const typename Map::value_type& a, expected) { + a.first.print("key"); + a.second.print(" value"); + } + std::cout << "\nactual: "; + BOOST_FOREACH(const typename Map::value_type& a, actual) { + a.first.print("key"); + a.second.print(" value"); + } + std::cout << std::endl; + return false; + } + return true; +} + /** * General function for comparing containers of testable objects */