diff --git a/gtsam/base/TestableAssertions.h b/gtsam/base/TestableAssertions.h index 9c7a08c1e..c518f4755 100644 --- a/gtsam/base/TestableAssertions.h +++ b/gtsam/base/TestableAssertions.h @@ -38,6 +38,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 */ template bool assert_equal(const std::vector& expected, const std::vector& actual, double tol = 1e-9) { @@ -64,6 +65,66 @@ bool assert_equal(const std::vector& expected, const std::vector& actual, return true; } +/** + * General function for comparing containers of testable objects + */ +template +bool assert_container_equal(const V& expected, const V& actual, double tol = 1e-9) { + bool match = true; + if (expected.size() != actual.size()) + match = false; + typename V::const_iterator + itExp = expected.begin(), + itAct = actual.begin(); + if(match) { + for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) { + if (!assert_equal(*itExp, *itAct, tol)) { + match = false; + break; + } + } + } + if(!match) { + std::cout << "expected: "; + BOOST_FOREACH(const typename V::value_type& a, expected) { a.print(" "); } + std::cout << "\nactual: "; + BOOST_FOREACH(const typename V::value_type& a, actual) { a.print(" "); } + std::cout << std::endl; + return false; + } + return true; +} + +/** + * General function for comparing containers of objects with operator== + */ +template +bool assert_container_equality(const V& expected, const V& actual, double tol = 1e-9) { + bool match = true; + if (expected.size() != actual.size()) + match = false; + typename V::const_iterator + itExp = expected.begin(), + itAct = actual.begin(); + if(match) { + for (; itExp!=expected.end() && itAct!=actual.end(); ++itExp, ++itAct) { + if (*itExp != *itAct) { + match = false; + break; + } + } + } + if(!match) { + std::cout << "expected: "; + BOOST_FOREACH(const typename V::value_type& a, expected) { std::cout << a << " "; } + std::cout << "\nactual: "; + BOOST_FOREACH(const typename V::value_type& a, actual) { std::cout << a << " "; } + std::cout << std::endl; + return false; + } + return true; +} + /** * Allow for testing inequality */