-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use ImplPtr in Timer * Add unittests for Timer Signed-off-by: Michael Carroll <michael@openrobotics.org>
- Loading branch information
Showing
3 changed files
with
110 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <thread> | ||
|
||
#include <ignition/common/Console.hh> | ||
#include <ignition/common/Timer.hh> | ||
|
||
///////////////////////////////////////////////// | ||
TEST(Timer_TEST, Sequence) | ||
{ | ||
ignition::common::Timer t; | ||
EXPECT_FALSE(t.Running()); | ||
EXPECT_DOUBLE_EQ(0.0, t.ElapsedTime().count()); | ||
|
||
// Call stop on a timer that hasn't been started | ||
t.Stop(); | ||
EXPECT_FALSE(t.Running()); | ||
|
||
// Start the timer | ||
t.Start(); | ||
EXPECT_TRUE(t.Running()); | ||
EXPECT_LT(0.0, t.ElapsedTime().count()); | ||
|
||
t.Stop(); | ||
EXPECT_FALSE(t.Running()); | ||
EXPECT_LT(0.0, t.ElapsedTime().count()); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
TEST(Timer_TEST, Elapsed) | ||
{ | ||
ignition::common::Timer t; | ||
t.Start(); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
t.Stop(); | ||
|
||
// Loose margin just to make sure it is close without being flaky | ||
EXPECT_LT(0.0, t.ElapsedTime().count()); | ||
igndbg << "Actual Elapsed: " << t.ElapsedTime().count() << std::endl; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
TEST(Timer_TEST, Copy) | ||
{ | ||
ignition::common::Timer t1; | ||
t1.Start(); | ||
EXPECT_TRUE(t1.Running()); | ||
|
||
ignition::common::Timer t2 = t1; | ||
EXPECT_TRUE(t2.Running()); | ||
|
||
// Stop the original | ||
t1.Stop(); | ||
EXPECT_FALSE(t1.Running()); | ||
EXPECT_TRUE(t2.Running()); | ||
|
||
// Stop the copy | ||
t2.Stop(); | ||
EXPECT_FALSE(t1.Running()); | ||
EXPECT_FALSE(t2.Running()); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
int main(int argc, char **argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |