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 111e9b8
Show file tree
Hide file tree
Showing 2 changed files with 358 additions and 0 deletions.
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
124 changes: 124 additions & 0 deletions tesseract_common/test/tesseract_common_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,46 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
#include <tesseract_common/any_poly.h>
#include <tesseract_common/kinematic_limits.h>
#include <tesseract_common/yaml_utils.h>
#include <tesseract_common/collision_margin_data.h>

/** @brief Resource locator implementation using a provided function to locate file resources */
class TestResourceLocator : public tesseract_common::ResourceLocator
{
public:
using Ptr = std::shared_ptr<TestResourceLocator>;
using ConstPtr = std::shared_ptr<const TestResourceLocator>;

~TestResourceLocator() override = default;

tesseract_common::Resource::Ptr locateResource(const std::string& url) const override final
{
std::string mod_url = url;
if (url.find("package://tesseract_common") == 0)
{
mod_url.erase(0, strlen("package://tesseract_common"));
size_t pos = mod_url.find('/');
if (pos == std::string::npos)
return nullptr;

std::string package = mod_url.substr(0, pos);
mod_url.erase(0, pos);

tesseract_common::fs::path file_path(__FILE__);
std::string package_path = file_path.parent_path().parent_path().string();

if (package_path.empty())
return nullptr;

mod_url = package_path + mod_url;
}

if (!tesseract_common::fs::path(mod_url).is_complete())
return nullptr;

return std::make_shared<tesseract_common::SimpleLocatedResource>(
url, mod_url, std::make_shared<TestResourceLocator>(*this));
}
};

TEST(TesseractCommonUnit, isNumeric) // NOLINT
{
Expand Down Expand Up @@ -251,6 +291,14 @@ TEST(TesseractCommonUnit, bytesResource) // NOLINT
EXPECT_EQ(bytes_resource->getResourceContents().size(), data.size());
}

TEST(TesseractCommonUnit, fileToString) // NOLINT
{
tesseract_common::ResourceLocator::Ptr locator = std::make_shared<TestResourceLocator>();
tesseract_common::Resource::Ptr resource = locator->locateResource("package://tesseract_common/package.xml");
std::string data = tesseract_common::fileToString(tesseract_common::fs::path(resource->getFilePath()));
EXPECT_FALSE(data.empty());
}

TEST(TesseractCommonUnit, ManipulatorInfo) // NOLINT
{
// Empty tcp
Expand Down Expand Up @@ -2122,6 +2170,82 @@ TEST(TesseractCommonUnit, concat) // NOLINT
EXPECT_TRUE(c.tail(3).isApprox(b));
}

TEST(TesseractCommonUnit, TestAllowedCollisionEntriesCompare) // NOLINT
{
tesseract_common::AllowedCollisionMatrix acm1;
acm1.addAllowedCollision("link1", "link2", "test");

tesseract_common::AllowedCollisionMatrix acm2;
acm2.addAllowedCollision("link1", "link2", "test");

EXPECT_TRUE(acm1.getAllAllowedCollisions() == acm2.getAllAllowedCollisions());

acm2.addAllowedCollision("link1", "link3", "test");
EXPECT_FALSE(acm1.getAllAllowedCollisions() == acm2.getAllAllowedCollisions());

acm2.clearAllowedCollisions();
acm2.addAllowedCollision("link1", "link3", "test");
EXPECT_FALSE(acm1.getAllAllowedCollisions() == acm2.getAllAllowedCollisions());

acm2.clearAllowedCollisions();
acm2.addAllowedCollision("link1", "link2", "do_not_match");
EXPECT_FALSE(acm1.getAllAllowedCollisions() == acm2.getAllAllowedCollisions());
}

TEST(TesseractCommonUnit, CollisionMarginDataCompare) // NOLINT
{
{ // EQUAL Default
tesseract_common::CollisionMarginData margin_data1;
tesseract_common::CollisionMarginData margin_data2;

EXPECT_TRUE(margin_data1 == margin_data2);
}

{ // EQUAL with pair data
tesseract_common::CollisionMarginData margin_data1;
margin_data1.setPairCollisionMargin("link_1", "link_2", 1);
tesseract_common::CollisionMarginData margin_data2;
margin_data2.setPairCollisionMargin("link_1", "link_2", 1);

EXPECT_TRUE(margin_data1 == margin_data2);
}

{ // Not EQUAL Default
tesseract_common::CollisionMarginData margin_data1(0.1);
tesseract_common::CollisionMarginData margin_data2(0.2);

EXPECT_FALSE(margin_data1 == margin_data2);
}

{ // Not EQUAL with pair data
tesseract_common::CollisionMarginData margin_data1;
margin_data1.setPairCollisionMargin("link_1", "link_2", 1);
tesseract_common::CollisionMarginData margin_data2;
margin_data2.setPairCollisionMargin("link_1", "link_2", 1);
margin_data2.setPairCollisionMargin("link_1", "link_3", 1);

EXPECT_FALSE(margin_data1 == margin_data2);
}

{ // Not EQUAL with pair data
tesseract_common::CollisionMarginData margin_data1;
margin_data1.setPairCollisionMargin("link_1", "link_2", 1);
tesseract_common::CollisionMarginData margin_data2;
margin_data2.setPairCollisionMargin("link_1", "link_2", 2);

EXPECT_FALSE(margin_data1 == margin_data2);
}

{ // Not EQUAL with pair data
tesseract_common::CollisionMarginData margin_data1;
margin_data1.setPairCollisionMargin("link_1", "link_2", 1);
tesseract_common::CollisionMarginData margin_data2;
margin_data2.setPairCollisionMargin("link_1", "link_3", 1);

EXPECT_FALSE(margin_data1 == margin_data2);
}
}

int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit 111e9b8

Please sign in to comment.