overload operator <<, and add unittest

release/4.3a0
mxie32 2019-06-14 14:57:56 -04:00
parent 7b41007941
commit 9ac72a017b
3 changed files with 55 additions and 22 deletions

View File

@ -133,25 +133,32 @@ bool compare(std::pair<Key, Vector>& lhs, std::pair<Key, Vector>& rhs) {
return lhs.first < rhs.first; return lhs.first < rhs.first;
} }
void VectorValues::print(const string& str, ostream& operator<<(ostream& ss, const VectorValues& v) {
const KeyFormatter& formatter) const { ss << "VectorValues: "
cout << str << ": " << size() << " elements\n"; << ": " << v.size() << " elements\n";
// Change print depending on whether we are using TBB // Change print depending on whether we are using TBB
#ifdef GTSAM_USE_TBB #ifdef GTSAM_USE_TBB
std::vector<std::pair<Key, Vector>> vec; std::vector<std::pair<Key, Vector>> vec;
vec.reserve(size()); vec.reserve(v.size());
for (const value_type& key_value : *this) { for (const auto& key_value : v) {
vec.push_back(std::make_pair(key_value.first, key_value.second)); vec.push_back(std::make_pair(key_value.first, key_value.second));
} }
sort(vec.begin(), vec.end(), compare); sort(vec.begin(), vec.end(), compare);
for (const auto& key_value : vec) for (const auto& key_value : vec)
cout << " " << formatter(key_value.first) << ": " ss << " " << key_value.first << ": " << key_value.second.transpose()
<< key_value.second.transpose() << "\n"; << "\n";
#else #else
for (const value_type& key_value : *this) for (const auto& key_value : v)
cout << " " << formatter(key_value.first) << ": " ss << " " << key_value.first << ": " << key_value.second.transpose()
<< key_value.second.transpose() << "\n"; << "\n";
#endif #endif
return ss;
}
/* ************************************************************************* */
void VectorValues::print(const string& str,
const KeyFormatter& formatter) const {
cout << *this;
cout.flush(); cout.flush();
} }

View File

@ -29,6 +29,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <iostream>
namespace gtsam { namespace gtsam {
@ -228,6 +229,11 @@ namespace gtsam {
*/ */
const_iterator find(Key j) const { return values_.find(j); } const_iterator find(Key j) const { return values_.find(j); }
/**
* overload operator << to print to stringstream
*/
friend std::ostream& operator<<(std::ostream& ss, const VectorValues& v);
/** print required by Testable for unit testing */ /** print required by Testable for unit testing */
void print(const std::string& str = "VectorValues: ", void print(const std::string& str = "VectorValues: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const; const KeyFormatter& formatter = DefaultKeyFormatter) const;

View File

@ -24,6 +24,8 @@
#include <boost/assign/list_of.hpp> #include <boost/assign/list_of.hpp>
#include <boost/range/adaptor/map.hpp> #include <boost/range/adaptor/map.hpp>
#include <sstream>
using namespace std; using namespace std;
using namespace boost::assign; using namespace boost::assign;
using boost::adaptors::map_keys; using boost::adaptors::map_keys;
@ -228,6 +230,24 @@ TEST(VectorValues, vector_sub)
EXPECT(assert_equal(expected, vv.vector(dims))); EXPECT(assert_equal(expected, vv.vector(dims)));
} }
/* ************************************************************************* */
TEST(VectorValues, print)
{
VectorValues vv;
vv.insert(0, (Vector(1) << 1).finished());
vv.insert(1, Vector2(2, 3));
vv.insert(2, Vector2(4, 5));
vv.insert(5, Vector2(6, 7));
vv.insert(7, Vector2(8, 9));
string expected =
"VectorValues: : 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n";
stringstream actual;
actual << vv;
EXPECT(expected == actual.str());
}
/* ************************************************************************* */ /* ************************************************************************* */
int main() { TestResult tr; return TestRegistry::runAllTests(tr); } int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
/* ************************************************************************* */ /* ************************************************************************* */