blob: 7c356171834f04864a99d61c6a80c9bfdf265988 [file] [log] [blame]
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -07001// 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 Bekkum830d5d12021-01-28 18:58:03 -080019using namespace std::chrono_literals;
20
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070021namespace pw::chrono {
22namespace {
23
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070024// 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 Bekkum959b8832021-03-11 08:55:01 -080026// duration up.
27constexpr SystemClock::duration kRoundedArbitraryDuration =
28 SystemClock::for_at_least(42h);
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070029
30TEST(SimulatedSystemClock, InitialTime) {
31 SimulatedSystemClock clock;
32
33 EXPECT_EQ(SystemClock::time_point(SystemClock::duration(0)), clock.now());
34}
35
36TEST(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
43TEST(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