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 "pw_unit_test/framework.h"
18
19 using namespace std::chrono_literals;
20
21 namespace pw::chrono {
22 namespace {
23
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
26 // duration up.
27 constexpr SystemClock::duration kRoundedArbitraryDuration =
28 SystemClock::for_at_least(42h);
29
TEST(SimulatedSystemClock,InitialTime)30 TEST(SimulatedSystemClock, InitialTime) {
31 SimulatedSystemClock clock;
32
33 EXPECT_EQ(SystemClock::time_point(SystemClock::duration(0)), clock.now());
34 }
35
TEST(SimulatedSystemClock,SetTime)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
TEST(SimulatedSystemClock,AdvanceTime)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
56