-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
realtime_rate_calculator_test.cc
48 lines (38 loc) · 1.65 KB
/
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
44
45
46
47
48
#include "drake/systems/analysis/realtime_rate_calculator.h"
#include <gtest/gtest.h>
namespace drake {
namespace systems {
namespace {
using internal::RealtimeRateCalculator;
class MockTimer final : public Timer {
public:
void Start() final {}
double Tick() final { return 0.1; }
};
// Tests that the RealtimeRateCalculator calculates the correct
// results including startup and rewinding time.
GTEST_TEST(RealtimeRateCalculatorTest, BasicTest) {
RealtimeRateCalculator calculator{};
calculator.InjectMockTimer(std::make_unique<MockTimer>());
// First (seed) call returns nullopt.
EXPECT_FALSE(calculator.UpdateAndRecalculate(0.0).has_value());
// Next calls return correct value.
EXPECT_DOUBLE_EQ(calculator.UpdateAndRecalculate(0.1).value(), 1.0);
EXPECT_DOUBLE_EQ(calculator.UpdateAndRecalculate(0.2).value(), 1.0);
EXPECT_DOUBLE_EQ(calculator.UpdateAndRecalculate(0.4).value(), 2.0);
// Reset time and observe another nullopt result.
EXPECT_FALSE(calculator.UpdateAndRecalculate(0.0).has_value());
EXPECT_DOUBLE_EQ(calculator.UpdateAndRecalculate(0.1).value(), 1.0);
// Calling UpdateAndRecalcuate() again with the same sim time will
// return 0.0 realtime rate.
EXPECT_DOUBLE_EQ(calculator.UpdateAndRecalculate(0.1).value(), 0.0);
// RealtimeRateCalculator should reset properly on call to Reset().
calculator.Reset();
// First call to UpdateAndRecalculate() after Reset() returns nullopt
// and calculation resumes on the next call.
EXPECT_FALSE(calculator.UpdateAndRecalculate(0.2).has_value());
EXPECT_DOUBLE_EQ(calculator.UpdateAndRecalculate(0.3).value(), 1.0);
}
} // namespace
} // namespace systems
} // namespace drake