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