Skip to content

Commit

Permalink
Added tests for the AnimationTime component (#433)
Browse files Browse the repository at this point in the history
Also using nanoseconds for serialization instead of milliseconds

Signed-off-by: Ashton Larkin <[email protected]>
  • Loading branch information
adlarkin authored Nov 2, 2020
1 parent 6f32258 commit 020a017
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/ignition/gazebo/components/Actor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <ignition/msgs/actor.pb.h>

#include <chrono>
#include <ratio>
#include <string>

#include <sdf/Actor.hh>
Expand Down Expand Up @@ -50,7 +51,7 @@ namespace serializers
public: static std::ostream &Serialize(std::ostream &_out,
const std::chrono::steady_clock::duration &_time)
{
_out << std::chrono::duration_cast<std::chrono::milliseconds>(
_out << std::chrono::duration_cast<std::chrono::nanoseconds>(
_time).count();
return _out;
}
Expand All @@ -64,7 +65,7 @@ namespace serializers
{
int64_t time;
_in >> time;
_time = std::chrono::duration<int64_t, std::milli>(time);
_time = std::chrono::duration<int64_t, std::nano>(time);
return _in;
}
};
Expand Down
27 changes: 27 additions & 0 deletions test/integration/components.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ TEST_F(ComponentsTest, AnimationName)
EXPECT_EQ("comp3", comp3.Data());
}

/////////////////////////////////////////////////
TEST_F(ComponentsTest, AnimationTime)
{
auto start = std::chrono::steady_clock::now();
auto end1 = start + std::chrono::seconds(5);
auto end2 = start + std::chrono::seconds(10);

// Create components
auto comp1 = components::AnimationTime(end1 - start);
auto comp2 = components::AnimationTime(end2 - start);

// Equality operators
EXPECT_NE(comp1, comp2);
EXPECT_FALSE(comp1 == comp2);
EXPECT_TRUE(comp1 != comp2);

// Stream operators
std::ostringstream ostr;
comp1.Serialize(ostr);
EXPECT_EQ("5000000000", ostr.str());

std::istringstream istr(ostr.str());
components::AnimationTime comp3;
comp3.Deserialize(istr);
EXPECT_EQ(comp1, comp3);
}

/////////////////////////////////////////////////
TEST_F(ComponentsTest, AirPressureSensor)
{
Expand Down

0 comments on commit 020a017

Please sign in to comment.