Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cartographer/common/rate_timer.h"
00018
00019 #include "gtest/gtest.h"
00020
00021 namespace cartographer {
00022 namespace common {
00023 namespace {
00024
00025 TEST(RateTimerTest, ComputeRate) {
00026 RateTimer<> rate_timer(common::FromSeconds(1.));
00027 common::Time time = common::FromUniversal(42);
00028 for (int i = 0; i < 100; ++i) {
00029 rate_timer.Pulse(time);
00030 time += common::FromSeconds(0.1);
00031 }
00032 EXPECT_NEAR(10., rate_timer.ComputeRate(), 1e-3);
00033 }
00034
00035 struct SimulatedClock {
00036 using rep = std::chrono::steady_clock::rep;
00037 using period = std::chrono::steady_clock::period;
00038 using duration = std::chrono::steady_clock::duration;
00039 using time_point = std::chrono::steady_clock::time_point;
00040
00041 static time_point time;
00042 static time_point now() noexcept { return time; }
00043 };
00044
00045 SimulatedClock::time_point SimulatedClock::time;
00046
00047 TEST(RateTimerTest, ComputeWallTimeRateRatio) {
00048 common::Time time = common::FromUniversal(42);
00049 RateTimer<SimulatedClock> rate_timer(common::FromSeconds(1.));
00050 for (int i = 0; i < 100; ++i) {
00051 rate_timer.Pulse(time);
00052 time += common::FromSeconds(0.1);
00053 SimulatedClock::time +=
00054 std::chrono::duration_cast<SimulatedClock::duration>(
00055 std::chrono::duration<double>(0.05));
00056 }
00057 EXPECT_NEAR(2., rate_timer.ComputeWallTimeRateRatio(), 1e-3);
00058 }
00059
00060 }
00061 }
00062 }