Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 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 | #include "util/time-unit-test-clock.hpp" |
| 23 | #include "util/scheduler.hpp" |
| 24 | |
| 25 | #include "boost-test.hpp" |
| 26 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 27 | #include <thread> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace tests { |
| 31 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 32 | BOOST_AUTO_TEST_SUITE(UtilTimeUnitTestClock) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 33 | |
| 34 | class UnitTestTimeFixture |
| 35 | { |
| 36 | public: |
| 37 | UnitTestTimeFixture() |
| 38 | : steadyClock(make_shared<time::UnitTestSteadyClock>()) |
| 39 | , systemClock(make_shared<time::UnitTestSystemClock>()) |
| 40 | { |
| 41 | time::setCustomClocks(steadyClock, systemClock); |
| 42 | } |
| 43 | |
| 44 | ~UnitTestTimeFixture() |
| 45 | { |
| 46 | time::setCustomClocks(nullptr, nullptr); |
| 47 | } |
| 48 | |
| 49 | public: |
| 50 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 51 | shared_ptr<time::UnitTestSystemClock> systemClock; |
| 52 | }; |
| 53 | |
| 54 | BOOST_FIXTURE_TEST_CASE(SystemClock, UnitTestTimeFixture) |
| 55 | { |
| 56 | BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(), |
| 57 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 58 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 59 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(), |
| 61 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
| 62 | |
| 63 | steadyClock->advance(time::days(1)); |
| 64 | BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(), |
| 65 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
| 66 | |
| 67 | systemClock->advance(time::days(1)); |
| 68 | BOOST_CHECK_GT(time::system_clock::now().time_since_epoch(), |
| 69 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
| 70 | |
| 71 | time::system_clock::TimePoint referenceTime = |
| 72 | time::fromUnixTimestamp(time::milliseconds(1390966967032LL)); |
| 73 | BOOST_CHECK_GT(time::system_clock::now(), referenceTime); |
| 74 | |
| 75 | systemClock->setNow(referenceTime.time_since_epoch()); |
| 76 | BOOST_CHECK_EQUAL(time::system_clock::now(), referenceTime); |
| 77 | |
| 78 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(time::system_clock::now()), |
| 79 | "1390966967032000000 nanoseconds since unit test clock advancements"); |
| 80 | |
| 81 | BOOST_CHECK_EQUAL(time::toIsoString(referenceTime), "20140129T034247.032000"); |
| 82 | BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47"); |
| 83 | BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47"); |
| 84 | |
| 85 | // Unfortunately, not all systems has lv_LV locale installed :( |
| 86 | // BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y. gada %d. %B", |
| 87 | // std::locale("lv_LV.UTF-8")), |
| 88 | // "2014. gada 29. Janvāris"); |
| 89 | |
| 90 | BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y -- %d -- %B", |
| 91 | std::locale("C")), |
| 92 | "2014 -- 29 -- January"); |
| 93 | |
| 94 | BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000"), referenceTime); |
| 95 | BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000Z"), referenceTime); |
| 96 | BOOST_CHECK_EQUAL(time::fromString("2014-01-29 03:42:47"), |
| 97 | time::fromUnixTimestamp(time::seconds(1390966967))); |
| 98 | |
| 99 | // Unfortunately, not all systems has lv_LV locale installed :( |
| 100 | // BOOST_CHECK_EQUAL(time::fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", |
| 101 | // std::locale("lv_LV.UTF-8")), |
| 102 | // time::fromUnixTimestamp(time::seconds(1390953600))); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(time::fromString("2014 -- 29 -- January", "%Y -- %d -- %B", |
| 105 | std::locale("C")), |
| 106 | time::fromUnixTimestamp(time::seconds(1390953600))); |
| 107 | } |
| 108 | |
| 109 | BOOST_FIXTURE_TEST_CASE(SteadyClock, UnitTestTimeFixture) |
| 110 | { |
| 111 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), |
| 112 | time::steady_clock::duration::zero()); |
| 113 | |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 114 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), |
| 116 | time::steady_clock::duration::zero()); |
| 117 | |
| 118 | systemClock->advance(time::days(36500)); |
| 119 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), |
| 120 | time::steady_clock::duration::zero()); |
| 121 | |
| 122 | steadyClock->advance(time::nanoseconds(100)); |
| 123 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), time::nanoseconds(100)); |
| 124 | |
| 125 | steadyClock->advance(time::microseconds(100)); |
| 126 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), time::nanoseconds(100100)); |
| 127 | |
| 128 | steadyClock->setNow(time::nanoseconds(100)); |
| 129 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), time::nanoseconds(100)); |
| 130 | |
| 131 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(time::steady_clock::now()), |
| 132 | "100 nanoseconds since unit test clock advancements"); |
| 133 | } |
| 134 | |
| 135 | BOOST_FIXTURE_TEST_CASE(Scheduler, UnitTestTimeFixture) |
| 136 | { |
| 137 | boost::asio::io_service io; |
| 138 | ndn::Scheduler scheduler(io); |
| 139 | |
| 140 | bool hasFired = false; |
| 141 | scheduler.scheduleEvent(time::seconds(100), [&] { hasFired = true; }); |
| 142 | |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 143 | io.poll(); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(hasFired, false); |
| 145 | |
| 146 | steadyClock->advance(time::seconds(100)); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 147 | |
| 148 | io.poll(); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(hasFired, true); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_SUITE_END() |
| 153 | |
| 154 | } // namespace tests |
| 155 | } // namespace ndn |