Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 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_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 | |
| 30 | |
| 31 | /** |
| 32 | * @brief Traits for UnitTestClock, defining default behavior for different clocks |
| 33 | * |
| 34 | * The only behavior that is currently controlled by the traits is default start |
| 35 | * time. The generic implementation assumes start time to be zero. |
| 36 | */ |
| 37 | template<class BaseClock> |
| 38 | class UnitTestClockTraits |
| 39 | { |
| 40 | public: |
| 41 | static nanoseconds |
| 42 | getDefaultStartTime() |
| 43 | { |
| 44 | return nanoseconds::zero(); |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * @brief Specialization of UnitTestClockTraits for system_clock |
| 50 | * |
| 51 | * This specialization sets the default start time to 1415684132 seconds |
| 52 | * (equivalent to Tue, 11 Nov 2014 05:35:32 UTC if unix epoch is assumed). |
| 53 | */ |
| 54 | template<> |
| 55 | class UnitTestClockTraits<system_clock> |
| 56 | { |
| 57 | public: |
| 58 | static nanoseconds |
| 59 | getDefaultStartTime() |
| 60 | { |
| 61 | return seconds(1415684132); |
| 62 | }; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * @brief Clock that can be used in unit tests for time-dependent tests independent of wall clock |
| 67 | * |
| 68 | * This clock should be explicitly advanced with UnitTestClock<BaseClock>::advance() or set |
| 69 | * with UnitTestClock<BaseClock>::setNow() methods. |
| 70 | * |
| 71 | * @note Default start time is determined by UnitTestClockTraits |
| 72 | */ |
| 73 | template<class BaseClock> |
| 74 | class UnitTestClock : public CustomClock<BaseClock> |
| 75 | { |
| 76 | public: |
| 77 | explicit |
| 78 | UnitTestClock(const nanoseconds& startTime = |
| 79 | UnitTestClockTraits<BaseClock>::getDefaultStartTime()); |
| 80 | |
| 81 | /** |
| 82 | * @brief Advance unit test clock by @p duration |
| 83 | */ |
| 84 | void |
| 85 | advance(const nanoseconds& duration); |
| 86 | |
| 87 | /** |
| 88 | * @brief Explicitly set clock to @p timeSinceEpoch |
| 89 | */ |
| 90 | void |
| 91 | setNow(const nanoseconds& timeSinceEpoch); |
| 92 | |
| 93 | public: // CustomClock<BaseClock> |
| 94 | |
| 95 | virtual std::string |
| 96 | getSince() const; |
| 97 | |
| 98 | virtual typename BaseClock::time_point |
| 99 | getNow() const; |
| 100 | |
| 101 | virtual boost::posix_time::time_duration |
| 102 | toPosixDuration(const typename BaseClock::duration& duration) const; |
| 103 | |
| 104 | private: |
| 105 | nanoseconds m_currentTime; |
| 106 | }; |
| 107 | |
| 108 | typedef UnitTestClock<system_clock> UnitTestSystemClock; |
| 109 | typedef UnitTestClock<steady_clock> UnitTestSteadyClock; |
| 110 | |
| 111 | } // namespace time |
| 112 | } // namespace ndn |
| 113 | |
| 114 | #endif // NDN_TIME_UNIT_TEST_CLOCK_HPP |