8000 New print assertions by varunagrawal · Pull Request #601 · borglab/gtsam · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

New print assertions #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions gtsam/base/TestableAssertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/optional.hpp>
#include <map>
#include <iostream>
#include <sstream>
#include <vector>

namespace gtsam {
Expand Down Expand Up @@ -349,4 +350,44 @@ bool assert_inequal(const V& expected, const V& actual, double tol = 1e-9) {
return false;
}

/**
* Capture std out via cout stream and compare against string.
*/
template<class V>
bool assert_stdout_equal(const std::string& expected, const V& actual) {
// Redirect output to buffer so we can compare
std::stringstream buffer;
// Save the original output stream so we can reset later
std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());

// We test against actual std::cout for faithful reproduction
std::cout << actual;

// Get output string and reset stdout
std::string actual_ = buffer.str();
std::cout.rdbuf(old);

return assert_equal(expected, actual_);
}

/**
* Capture print function output and compare against string.
*/
template<class V>
bool assert_print_equal(const std::string& expected, const V& actual) {
// Redirect output to buffer so we can compare
std::stringstream buffer;
// Save the original output stream so we can reset later
std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());

// We test against actual std::cout for faithful reproduction
actual.print();

// Get output string and reset stdout
std::string actual_ = buffer.str();
std::cout.rdbuf(old);

return assert_equal(expected, actual_);
}

} // \namespace gtsam
11 changes: 11 additions & 0 deletions gtsam/geometry/tests/testCal3_S2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <CppUnitLite/TestHarness.h>
#include <gtsam/base/Testable.h>
#include <gtsam/base/TestableAssertions.h>
#include <gtsam/base/numericalDerivative.h>
#include <gtsam/geometry/Cal3_S2.h>

Expand Down Expand Up @@ -127,6 +128,16 @@ TEST(Cal3_S2, between) {

}

/* ************************************************************************* */
TEST(Cal3_S2, Print) {
Cal3_S2 cal(5, 5, 5, 5, 5);
std::stringstream os;
os << "{fx: " << cal.fx() << ", fy: " << cal.fy() << ", s:" << cal.skew() << ", px:" << cal.px()
<< ", py:" << cal.py() << "}";

EXPECT(assert_stdout_equal(os.str(), cal));
}

/* ************************************************************************* */
int main() {
TestResult tr;
Expand Down
26 changes: 4 additions & 22 deletions gtsam/geometry/tests/testPose3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <gtsam/geometry/Pose2.h>
#include <gtsam/base/testLie.h>
#include <gtsam/base/lieProxies.h>
#include <gtsam/base/TestableAssertions.h>

#include <boost/assign/std/vector.hpp> // for operator +=
using namespace boost::assign;
Expand Down Expand Up @@ -1028,32 +1029,13 @@ TEST(Pose3, Create) {
}

/* ************************************************************************* */
TEST(Pose3, print) {
std::stringstream redirectStream;
std::streambuf* ssbuf = redirectStream.rdbuf();
std::streambuf* oldbuf = std::cout.rdbuf();
// redirect cout to redirectStream
std::cout.rdbuf(ssbuf);

TEST(Pose3, Print) {
Pose3 pose(Rot3::identity(), Point3(1, 2, 3));
// output is captured to redirectStream
pose.print();

// Generate the expected output
std::stringstream expected;
Point3 translation(1, 2, 3);

// Add expected rotation
expected << "R: [\n\t1, 0, 0;\n\t0, 1, 0;\n\t0, 0, 1\n]\n";
expected << "t: 1 2 3\n";

// reset cout to the original stream
std::cout.rdbuf(oldbuf);

// Get substring corresponding to translation part
std::string actual = redirectStream.str();
std::string expected = "R: [\n\t1, 0, 0;\n\t0, 1, 0;\n\t0, 0, 1\n]\nt: 1 2 3\n";

CHECK_EQUAL(expected.str(), actual);
EXPECT(assert_print_equal(expected, pose));
}

/* ************************************************************************* */
Expand Down
13 changes: 2 additions & 11 deletions gtsam/nonlinear/tests/testFunctorizedFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <CppUnitLite/TestHarness.h>
#include <gtsam/base/Testable.h>
#include <gtsam/base/TestableAssertions.h>
#include <gtsam/inference/Symbol.h>
#include <gtsam/nonlinear/FunctorizedFactor.h>
#include <gtsam/nonlinear/factorTesting.h>
Expand Down Expand Up @@ -115,16 +116,6 @@ TEST(FunctorizedFactor, Print) {
auto factor =
MakeFunctorizedFactor<Matrix>(key, X, model, MultiplyFunctor(multiplier));

// redirect output to buffer so we can compare
stringstream buffer;
streambuf *old = cout.rdbuf(buffer.rdbuf());

factor.print();

// get output string and reset stdout
string actual = buffer.str();
cout.rdbuf(old);

string expected =
" keys = { X0 }\n"
" noise model: unit (9) \n"
Expand All @@ -135,7 +126,7 @@ TEST(FunctorizedFactor, Print) {
"]\n"
" noise model sigmas: 1 1 1 1 1 1 1 1 1\n";

CHECK_EQUAL(expected, actual);
EXPECT(assert_print_equal(expected, factor));
}

/* ************************************************************************* */
Expand Down
10 changes: 1 addition & 9 deletions gtsam/nonlinear/tests/testValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,15 +595,7 @@ TEST(Values, Demangle) {
values.insert(key1, v);
string expected = "Values with 1 values:\nValue v1: (Eigen::Matrix<double, 1, 3, 1, 1, 3>)\n[\n 5, 6, 7\n]\n\n";

stringstream buffer;
streambuf * old = cout.rdbuf(buffer.rdbuf());

values.print();

string actual = buffer.str();
cout.rdbuf(old);

EXPECT(assert_equal(expected, actual));
EXPECT(assert_print_equal(expected, values));
}

/* ************************************************************************* */
Expand Down
0