blob: 7d67507207528740fb31763735ebea4b3d0a1f62 [file] [log] [blame]
Alexander Afanasyev85b17b82014-11-10 16:22:05 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyev85b17b82014-11-10 16:22:05 -08004 *
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_TIME_UNIT_TEST_CLOCK_HPP
23#define NDN_TIME_UNIT_TEST_CLOCK_HPP
24
25#include "time-custom-clock.hpp"
26
27namespace ndn {
28namespace time {
29
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080030/**
31 * @brief Traits for UnitTestClock, defining default behavior for different clocks
32 *
33 * The only behavior that is currently controlled by the traits is default start
34 * time. The generic implementation assumes start time to be zero.
35 */
36template<class BaseClock>
37class UnitTestClockTraits
38{
39public:
40 static nanoseconds
41 getDefaultStartTime()
42 {
43 return nanoseconds::zero();
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020044 }
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080045};
46
47/**
48 * @brief Specialization of UnitTestClockTraits for system_clock
49 *
50 * This specialization sets the default start time to 1415684132 seconds
51 * (equivalent to Tue, 11 Nov 2014 05:35:32 UTC if unix epoch is assumed).
52 */
53template<>
54class UnitTestClockTraits<system_clock>
55{
56public:
57 static nanoseconds
58 getDefaultStartTime()
59 {
60 return seconds(1415684132);
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020061 }
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080062};
63
64/**
65 * @brief Clock that can be used in unit tests for time-dependent tests independent of wall clock
66 *
67 * This clock should be explicitly advanced with UnitTestClock<BaseClock>::advance() or set
68 * with UnitTestClock<BaseClock>::setNow() methods.
69 *
70 * @note Default start time is determined by UnitTestClockTraits
71 */
72template<class BaseClock>
73class UnitTestClock : public CustomClock<BaseClock>
74{
75public:
76 explicit
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020077 UnitTestClock(const nanoseconds& startTime = UnitTestClockTraits<BaseClock>::getDefaultStartTime());
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080078
79 /**
80 * @brief Advance unit test clock by @p duration
81 */
82 void
83 advance(const nanoseconds& duration);
84
85 /**
86 * @brief Explicitly set clock to @p timeSinceEpoch
87 */
88 void
89 setNow(const nanoseconds& timeSinceEpoch);
90
91public: // CustomClock<BaseClock>
Davide Pesavento57c07df2016-12-11 18:41:45 -050092 std::string
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020093 getSince() const override;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080094
Davide Pesavento57c07df2016-12-11 18:41:45 -050095 typename BaseClock::time_point
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020096 getNow() const override;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080097
Davide Pesavento57c07df2016-12-11 18:41:45 -050098 boost::posix_time::time_duration
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020099 toPosixDuration(const typename BaseClock::duration& duration) const override;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800100
101private:
102 nanoseconds m_currentTime;
103};
104
Davide Pesavento88a0d812017-08-19 21:31:42 -0400105extern template class UnitTestClock<system_clock>;
106extern template class UnitTestClock<steady_clock>;
107
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800108typedef UnitTestClock<system_clock> UnitTestSystemClock;
109typedef UnitTestClock<steady_clock> UnitTestSteadyClock;
110
111} // namespace time
112} // namespace ndn
113
114#endif // NDN_TIME_UNIT_TEST_CLOCK_HPP