Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "frc/Timer.h" // NOLINT(build/include_order) |
| 6 | |
| 7 | #include "frc/simulation/SimHooks.h" |
| 8 | #include "gtest/gtest.h" |
| 9 | |
| 10 | using namespace frc; |
| 11 | |
| 12 | namespace { |
| 13 | class TimerTest : public ::testing::Test { |
| 14 | protected: |
| 15 | void SetUp() override { |
| 16 | frc::sim::PauseTiming(); |
| 17 | frc::sim::RestartTiming(); |
| 18 | } |
| 19 | |
| 20 | void TearDown() override { frc::sim::ResumeTiming(); } |
| 21 | }; |
| 22 | |
| 23 | } // namespace |
| 24 | |
| 25 | TEST_F(TimerTest, StartStop) { |
| 26 | Timer timer; |
| 27 | |
| 28 | // Verify timer is initialized as stopped |
| 29 | EXPECT_EQ(timer.Get(), 0_s); |
| 30 | frc::sim::StepTiming(500_ms); |
| 31 | EXPECT_EQ(timer.Get(), 0_s); |
| 32 | |
| 33 | // Verify timer increments after it's started |
| 34 | timer.Start(); |
| 35 | frc::sim::StepTiming(500_ms); |
| 36 | EXPECT_EQ(timer.Get(), 500_ms); |
| 37 | |
| 38 | // Verify timer stops incrementing after it's stopped |
| 39 | timer.Stop(); |
| 40 | frc::sim::StepTiming(500_ms); |
| 41 | EXPECT_EQ(timer.Get(), 500_ms); |
| 42 | } |
| 43 | |
| 44 | TEST_F(TimerTest, Reset) { |
| 45 | Timer timer; |
| 46 | timer.Start(); |
| 47 | |
| 48 | // Advance timer to 500 ms |
| 49 | EXPECT_EQ(timer.Get(), 0_s); |
| 50 | frc::sim::StepTiming(500_ms); |
| 51 | EXPECT_EQ(timer.Get(), 500_ms); |
| 52 | |
| 53 | // Verify timer reports 0 ms after reset |
| 54 | timer.Reset(); |
| 55 | EXPECT_EQ(timer.Get(), 0_s); |
| 56 | |
| 57 | // Verify timer continues incrementing |
| 58 | frc::sim::StepTiming(500_ms); |
| 59 | EXPECT_EQ(timer.Get(), 500_ms); |
| 60 | |
| 61 | // Verify timer doesn't start incrementing after reset if it was stopped |
| 62 | timer.Stop(); |
| 63 | timer.Reset(); |
| 64 | frc::sim::StepTiming(500_ms); |
| 65 | EXPECT_EQ(timer.Get(), 0_ms); |
| 66 | } |
| 67 | |
| 68 | TEST_F(TimerTest, HasElapsed) { |
| 69 | Timer timer; |
| 70 | |
| 71 | // Verify 0 ms has elapsed since timer hasn't started |
| 72 | EXPECT_TRUE(timer.HasElapsed(0_s)); |
| 73 | |
| 74 | // Verify timer doesn't report elapsed time when stopped |
| 75 | frc::sim::StepTiming(500_ms); |
| 76 | EXPECT_FALSE(timer.HasElapsed(400_ms)); |
| 77 | |
| 78 | timer.Start(); |
| 79 | |
| 80 | // Verify timer reports >= 400 ms has elapsed after multiple calls |
| 81 | frc::sim::StepTiming(500_ms); |
| 82 | EXPECT_TRUE(timer.HasElapsed(400_ms)); |
| 83 | EXPECT_TRUE(timer.HasElapsed(400_ms)); |
| 84 | } |
| 85 | |
| 86 | TEST_F(TimerTest, AdvanceIfElapsed) { |
| 87 | Timer timer; |
| 88 | |
| 89 | // Verify 0 ms has elapsed since timer hasn't started |
| 90 | EXPECT_TRUE(timer.AdvanceIfElapsed(0_s)); |
| 91 | |
| 92 | // Verify timer doesn't report elapsed time when stopped |
| 93 | frc::sim::StepTiming(500_ms); |
| 94 | EXPECT_FALSE(timer.AdvanceIfElapsed(400_ms)); |
| 95 | |
| 96 | timer.Start(); |
| 97 | |
| 98 | // Verify timer reports >= 400 ms has elapsed for only first call |
| 99 | frc::sim::StepTiming(500_ms); |
| 100 | EXPECT_TRUE(timer.AdvanceIfElapsed(400_ms)); |
| 101 | EXPECT_FALSE(timer.AdvanceIfElapsed(400_ms)); |
| 102 | |
| 103 | // Verify timer reports >= 400 ms has elapsed for two calls |
| 104 | frc::sim::StepTiming(1_s); |
| 105 | EXPECT_TRUE(timer.AdvanceIfElapsed(400_ms)); |
| 106 | EXPECT_TRUE(timer.AdvanceIfElapsed(400_ms)); |
| 107 | EXPECT_FALSE(timer.AdvanceIfElapsed(400_ms)); |
| 108 | } |
| 109 | |
| 110 | TEST_F(TimerTest, GetFPGATimestamp) { |
| 111 | auto start = frc::Timer::GetFPGATimestamp(); |
| 112 | frc::sim::StepTiming(500_ms); |
| 113 | auto end = frc::Timer::GetFPGATimestamp(); |
| 114 | EXPECT_EQ(start + 500_ms, end); |
| 115 | } |