-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
instantaneous_realtime_rate_calculator_test.cc
43 lines (35 loc) · 1.4 KB
/
instantaneous_realtime_rate_calculator_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "drake/systems/analysis/instantaneous_realtime_rate_calculator.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace drake {
namespace systems {
namespace {
using ::testing::Return;
class MockTimer : public Timer {
public:
MOCK_METHOD(Timer::duration, Tick, (), (override));
MOCK_METHOD(void, Start, (), (override));
MOCK_METHOD(Timer::duration, Stop, (), (override));
};
// Tests that the InstantaneousRealtimeRateCalculator calculates the correct
// results including startup and rewinding time
GTEST_TEST(InstantaneousRealtimeRateCalculatorTest, BasicTest) {
auto timer = std::make_unique<MockTimer>();
const double SLEEP_S = 0.1;
EXPECT_CALL(*timer, Tick)
.WillRepeatedly(Return(MockTimer::duration(SLEEP_S)));
InstantaneousRealtimeRateCalculator calculator{};
calculator.InjectMockTimer(std::move(timer));
// first call is seed, no result
EXPECT_FALSE(calculator.CalculateRealtimeRate(0.0).has_value());
// next calls return correct value
EXPECT_DOUBLE_EQ(calculator.CalculateRealtimeRate(0.1).value(), 1.0);
EXPECT_DOUBLE_EQ(calculator.CalculateRealtimeRate(0.2).value(), 1.0);
EXPECT_DOUBLE_EQ(calculator.CalculateRealtimeRate(0.4).value(), 2.0);
// reset time
EXPECT_FALSE(calculator.CalculateRealtimeRate(0.0).has_value());
EXPECT_DOUBLE_EQ(calculator.CalculateRealtimeRate(0.1).value(), 1.0);
}
} // namespace
} // namespace systems
} // namespace drake