Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_chrono/simulated_system_clock.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 19 | using namespace std::chrono_literals; |
| 20 | |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 21 | namespace pw::chrono { |
| 22 | namespace { |
| 23 | |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 24 | // We can't control the SystemClock's period configuration, so just in case |
| 25 | // 42 hours cannot be accurately expressed in integer ticks, round the |
Ewout van Bekkum | 959b883 | 2021-03-11 08:55:01 -0800 | [diff] [blame] | 26 | // duration up. |
| 27 | constexpr SystemClock::duration kRoundedArbitraryDuration = |
| 28 | SystemClock::for_at_least(42h); |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 29 | |
| 30 | TEST(SimulatedSystemClock, InitialTime) { |
| 31 | SimulatedSystemClock clock; |
| 32 | |
| 33 | EXPECT_EQ(SystemClock::time_point(SystemClock::duration(0)), clock.now()); |
| 34 | } |
| 35 | |
| 36 | TEST(SimulatedSystemClock, SetTime) { |
| 37 | SimulatedSystemClock clock; |
| 38 | |
| 39 | clock.SetTime(pw::chrono::SystemClock::time_point(kRoundedArbitraryDuration)); |
| 40 | EXPECT_EQ(kRoundedArbitraryDuration, clock.now().time_since_epoch()); |
| 41 | } |
| 42 | |
| 43 | TEST(SimulatedSystemClock, AdvanceTime) { |
| 44 | SimulatedSystemClock clock; |
| 45 | |
| 46 | const SystemClock::time_point before_timestamp = clock.now(); |
| 47 | clock.AdvanceTime(kRoundedArbitraryDuration); |
| 48 | const SystemClock::time_point after_timestamp = clock.now(); |
| 49 | |
| 50 | EXPECT_EQ(kRoundedArbitraryDuration, clock.now().time_since_epoch()); |
| 51 | EXPECT_EQ(kRoundedArbitraryDuration, after_timestamp - before_timestamp); |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | } // namespace pw::chrono |