aboutsummaryrefslogtreecommitdiff
path: root/pw_chrono/system_clock_facade_test.cc
blob: af17a6549742dc32f9decc69202dd73f4ab353de (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include <chrono>

#include "gtest/gtest.h"
#include "pw_chrono/system_clock.h"
#include "pw_preprocessor/util.h"

using namespace std::chrono_literals;

namespace pw::chrono {
namespace {

extern "C" {

// Functions defined in system_clock_facade_test_c.c which call the API from C.
pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_CallNow();
pw_chrono_SystemClock_Duration pw_chrono_SystemClock_CallTimeElapsed(
    pw_chrono_SystemClock_TimePoint last_time,
    pw_chrono_SystemClock_TimePoint current_time);

pw_chrono_SystemClock_Nanoseconds pw_chrono_SystemClock_CallDurationToNsFloor(
    pw_chrono_SystemClock_Duration ticks);

}  // extern "C"

// While testing that the clock ticks (i.e. moves forward) we want to ensure a
// failure can be reported instead of deadlocking the test until it passes.
// Given that there isn't really a good heuristic for this we instead make some
// wild assumptions to bound the maximum busy loop iterations.
// - Assume our clock is < 6Ghz
// - Assume we can check the clock in a single cycle
// - Wait for up to 1/10th of a second @ 6Ghz, this may be a long period on a
//   slower (i.e. real) machine.
constexpr uint64_t kMaxIterations = 6'000'000'000 / 10;

TEST(SystemClock, Now) {
  const SystemClock::time_point start_time = SystemClock::now();
  // Verify the clock moves forward.
  bool clock_moved_forward = false;
  for (uint64_t i = 0; i < kMaxIterations; ++i) {
    if (SystemClock::now() > start_time) {
      clock_moved_forward = true;
      break;
    }
  }
  EXPECT_TRUE(clock_moved_forward);
}

TEST(VirtualSystemClock, Now) {
  auto& clock = VirtualSystemClock::RealClock();
  const SystemClock::time_point start_time = clock.now();
  // Verify the clock moves forward.
  bool clock_moved_forward = false;
  for (uint64_t i = 0; i < kMaxIterations; ++i) {
    if (clock.now() > start_time) {
      clock_moved_forward = true;
      break;
    }
  }
  EXPECT_TRUE(clock_moved_forward);
}

TEST(SystemClock, NowInC) {
  const pw_chrono_SystemClock_TimePoint start_time =
      pw_chrono_SystemClock_CallNow();
  // Verify the clock moves forward.
  bool clock_moved_forward = false;
  for (uint64_t i = 0; i < kMaxIterations; ++i) {
    if (pw_chrono_SystemClock_CallNow().duration_since_epoch.ticks >
        start_time.duration_since_epoch.ticks) {
      clock_moved_forward = true;
      break;
    }
  }
  EXPECT_TRUE(clock_moved_forward);
}

TEST(SystemClock, TimeElapsedInC) {
  const pw_chrono_SystemClock_TimePoint first = pw_chrono_SystemClock_CallNow();
  const pw_chrono_SystemClock_TimePoint last = pw_chrono_SystemClock_CallNow();
  static_assert(SystemClock::is_monotonic);
  EXPECT_GE(0, pw_chrono_SystemClock_CallTimeElapsed(last, first).ticks);
}

TEST(SystemClock, DurationCastInC) {
  // We can't control the SystemClock's period configuration, so just in case
  // 42 hours cannot be accurately expressed in integer ticks, round the
  // duration w/ floor.
  static constexpr auto kRoundedArbitraryDuration =
      std::chrono::floor<SystemClock::duration>(42h);
  static constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
      PW_SYSTEM_CLOCK_H_FLOOR(42);
  EXPECT_EQ(
      std::chrono::floor<std::chrono::nanoseconds>(kRoundedArbitraryDuration)
          .count(),
      pw_chrono_SystemClock_CallDurationToNsFloor(
          kRoundedArbitraryDurationInC));
}

}  // namespace
}  // namespace pw::chrono