time-unit-test-clock.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2024 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_UTIL_TIME_UNIT_TEST_CLOCK_HPP
23 #define NDN_CXX_UTIL_TIME_UNIT_TEST_CLOCK_HPP
24 
26 
27 namespace ndn::time {
28 
35 template<class BaseClock>
37 {
38 public:
39  static nanoseconds
41  {
42  return 0_ns;
43  }
44 };
45 
52 template<>
54 {
55 public:
56  static nanoseconds
58  {
59  return 1415684132_s;
60  }
61 };
62 
71 template<class BaseClock, class ClockTraits = UnitTestClockTraits<BaseClock>>
72 class UnitTestClock : public CustomClock<BaseClock>
73 {
74 public:
75  explicit
76  UnitTestClock(nanoseconds startTime = ClockTraits::getDefaultStartTime());
77 
81  void
83 
87  void
88  setNow(nanoseconds timeSinceEpoch);
89 
90 public:
91  std::string
92  getSince() const override;
93 
94  typename BaseClock::time_point
95  getNow() const override;
96 
97  typename BaseClock::duration
98  toWaitDuration(typename BaseClock::duration d) const override;
99 
100 private:
101  nanoseconds m_currentTime;
102 };
103 
104 extern template class UnitTestClock<system_clock>;
105 extern template class UnitTestClock<steady_clock>;
106 
109 
110 } // namespace ndn::time
111 
112 #endif // NDN_CXX_UTIL_TIME_UNIT_TEST_CLOCK_HPP
Class implementing custom system or steady clock behavior.
Traits for UnitTestClock, defining default behavior for different clocks.
static nanoseconds getDefaultStartTime()
Clock that can be used in unit tests for time-dependent tests independent of wall clock.
void setNow(nanoseconds timeSinceEpoch)
Explicitly set clock to timeSinceEpoch.
std::string getSince() const override
BaseClock::time_point getNow() const override
BaseClock::duration toWaitDuration(typename BaseClock::duration d) const override
void advance(nanoseconds duration)
Advance unit test clock by duration.
UnitTestClock(nanoseconds startTime=ClockTraits::getDefaultStartTime())
System clock.
Definition: time.hpp:200
::boost::chrono::duration< Rep, Period > duration
Definition: time.hpp:38
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54