Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 22 | #ifndef NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP |
| 23 | #define NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 51 | * (equivalent to Tue 11 Nov 2014 05:35:32 UTC, if Unix epoch is assumed). |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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 | */ |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 72 | template<class BaseClock, class ClockTraits = UnitTestClockTraits<BaseClock>> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 73 | class UnitTestClock : public CustomClock<BaseClock> |
| 74 | { |
| 75 | public: |
| 76 | explicit |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 77 | UnitTestClock(nanoseconds startTime = ClockTraits::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 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 83 | advance(nanoseconds duration); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * @brief Explicitly set clock to @p timeSinceEpoch |
| 87 | */ |
| 88 | void |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 89 | setNow(nanoseconds timeSinceEpoch); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 90 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 91 | public: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 92 | 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 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 95 | 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 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 98 | typename BaseClock::duration |
| 99 | toWaitDuration(typename BaseClock::duration d) const override; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | nanoseconds m_currentTime; |
| 103 | }; |
| 104 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 105 | extern template class UnitTestClock<system_clock>; |
| 106 | extern template class UnitTestClock<steady_clock>; |
| 107 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 108 | using UnitTestSystemClock = UnitTestClock<system_clock>; |
| 109 | using UnitTestSteadyClock = UnitTestClock<steady_clock>; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 110 | |
| 111 | } // namespace time |
| 112 | } // namespace ndn |
| 113 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 114 | #endif // NDN_UTIL_TIME_UNIT_TEST_CLOCK_HPP |