Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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_TIME_UNIT_TEST_CLOCK_HPP |
| 23 | #define NDN_TIME_UNIT_TEST_CLOCK_HPP |
| 24 | |
| 25 | #include "time-custom-clock.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace time { |
| 29 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 30 | /** |
| 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 | */ |
| 36 | template<class BaseClock> |
| 37 | class UnitTestClockTraits |
| 38 | { |
| 39 | public: |
| 40 | static nanoseconds |
| 41 | getDefaultStartTime() |
| 42 | { |
| 43 | return nanoseconds::zero(); |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 44 | } |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 45 | }; |
| 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 | */ |
| 53 | template<> |
| 54 | class UnitTestClockTraits<system_clock> |
| 55 | { |
| 56 | public: |
| 57 | static nanoseconds |
| 58 | getDefaultStartTime() |
| 59 | { |
| 60 | return seconds(1415684132); |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 61 | } |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 62 | }; |
| 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 | */ |
| 72 | template<class BaseClock> |
| 73 | class UnitTestClock : public CustomClock<BaseClock> |
| 74 | { |
| 75 | public: |
| 76 | explicit |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 77 | UnitTestClock(const nanoseconds& startTime = UnitTestClockTraits<BaseClock>::getDefaultStartTime()); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 78 | |
| 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 | |
| 91 | public: // CustomClock<BaseClock> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 92 | virtual std::string |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 93 | getSince() const override; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 94 | |
| 95 | virtual typename BaseClock::time_point |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 96 | getNow() const override; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 97 | |
| 98 | virtual boost::posix_time::time_duration |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame^] | 99 | toPosixDuration(const typename BaseClock::duration& duration) const override; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | nanoseconds m_currentTime; |
| 103 | }; |
| 104 | |
| 105 | typedef UnitTestClock<system_clock> UnitTestSystemClock; |
| 106 | typedef UnitTestClock<steady_clock> UnitTestSteadyClock; |
| 107 | |
| 108 | } // namespace time |
| 109 | } // namespace ndn |
| 110 | |
| 111 | #endif // NDN_TIME_UNIT_TEST_CLOCK_HPP |