Skip to content

Commit

Permalink
Improve tesseract_common unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Armstrong committed Apr 10, 2023
1 parent d00ae86 commit 7a8c468
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tesseract_common/src/joint_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ bool JointTrajectory::operator==(const JointTrajectory& other) const

bool JointTrajectory::operator!=(const JointTrajectory& rhs) const { return !operator==(rhs); }

// LCOV_EXCL_START

///////////////
// Iterators //
///////////////
Expand Down Expand Up @@ -121,7 +123,7 @@ JointTrajectory::const_reference JointTrajectory::at(size_type n) const { return
JointTrajectory::pointer JointTrajectory::data() { return states.data(); }
JointTrajectory::const_pointer JointTrajectory::data() const { return states.data(); }
JointTrajectory::reference JointTrajectory::operator[](size_type pos) { return states[pos]; }
JointTrajectory::const_reference JointTrajectory::operator[](size_type pos) const { return states[pos]; };
JointTrajectory::const_reference JointTrajectory::operator[](size_type pos) const { return states[pos]; }

///////////////
// Modifiers //
Expand Down Expand Up @@ -164,6 +166,8 @@ void JointTrajectory::emplace_back(Args&&... args)
void JointTrajectory::pop_back() { states.pop_back(); }
void JointTrajectory::swap(std::vector<value_type>& other) { states.swap(other); }

// LCOV_EXCL_STOP

template <class Archive>
void JointTrajectory::serialize(Archive& ar, const unsigned int version) // NOLINT
{
Expand Down
234 changes: 234 additions & 0 deletions tesseract_common/test/tesseract_common_serialization_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,240 @@ TEST(TesseractCommonSerializeUnit, VectorXd) // NOLINT
}
}

TEST(TesseractCommonSerializeUnit, VectorXi) // NOLINT
{
{ // Serialize empty object
Eigen::VectorXi ev;
{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::VectorXi nev;
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}
}

// Serialize to object which already has data
for (int i = 0; i < 5; ++i)
{
Eigen::VectorXi ev = Eigen::VectorXi::Random(6);

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::VectorXi nev = Eigen::VectorXi::Random(6);
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev == nev);
}

// Serialize to object which already has data and different size
for (int i = 0; i < 5; ++i)
{
Eigen::VectorXi ev = Eigen::VectorXi::Random(6);

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::VectorXi nev = Eigen::VectorXi::Random(3);
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev == nev);
}

// Default use case
for (int i = 0; i < 5; ++i)
{
Eigen::VectorXi ev = Eigen::VectorXi::Random(6);

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::VectorXi nev;
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_xi_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev == nev);
}
}

TEST(TesseractCommonSerializeUnit, Vector3d) // NOLINT
{
{ // Serialize empty object
Eigen::Vector3d ev;
{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_3d_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::Vector3d nev;
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_3d_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}
}

// Serialize to object which already has data
for (int i = 0; i < 3; ++i)
{
Eigen::Vector3d ev = Eigen::Vector3d::Random();

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_3d_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::Vector3d nev = Eigen::Vector3d::Random();
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_3d_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev.isApprox(nev, 1e-5));
}

// Default use case
for (int i = 0; i < 3; ++i)
{
Eigen::Vector3d ev = Eigen::Vector3d::Random();

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_3d_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::Vector3d nev;
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_3d_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev.isApprox(nev, 1e-5));
}
}

TEST(TesseractCommonSerializeUnit, Vector4d) // NOLINT
{
{ // Serialize empty object
Eigen::Vector4d ev;
{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_4d_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::Vector4d nev;
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_4d_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}
}

// Serialize to object which already has data
for (int i = 0; i < 4; ++i)
{
Eigen::Vector4d ev = Eigen::Vector4d::Random();

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_4d_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::Vector4d nev = Eigen::Vector4d::Random();
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_4d_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev.isApprox(nev, 1e-5));
}

// Default use case
for (int i = 0; i < 4; ++i)
{
Eigen::Vector4d ev = Eigen::Vector4d::Random();

{
std::ofstream os(tesseract_common::getTempPath() + "eigen_vector_4d_boost.xml");
boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(ev);
}

Eigen::Vector4d nev;
{
std::ifstream ifs(tesseract_common::getTempPath() + "eigen_vector_4d_boost.xml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);

// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(nev);
}

EXPECT_TRUE(ev.isApprox(nev, 1e-5));
}
}

TEST(TesseractCommonSerializeUnit, MatrixX2d) // NOLINT
{
{ // Serialize empty
Expand Down
Loading

0 comments on commit 7a8c468

Please sign in to comment.