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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 2 | /* |
| 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 | |
| 22 | #include "util/time-unit-test-clock.hpp" |
| 23 | #include "util/scheduler.hpp" |
| 24 | |
| 25 | #include "boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 26 | #include "../unit-test-time-fixture.hpp" |
| 27 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 29 | #include <thread> |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace tests { |
| 33 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Util) |
| 35 | BOOST_FIXTURE_TEST_SUITE(TestTimeUnitTestClock, UnitTestTimeFixture) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 36 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(SystemClock) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 38 | { |
| 39 | BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(), |
| 40 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 41 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 42 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(), |
| 44 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
| 45 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 46 | steadyClock->advance(1_day); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(time::system_clock::now().time_since_epoch(), |
| 48 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
| 49 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 50 | systemClock->advance(1_day); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 51 | BOOST_CHECK_GT(time::system_clock::now().time_since_epoch(), |
| 52 | time::UnitTestClockTraits<time::system_clock>::getDefaultStartTime()); |
| 53 | |
| 54 | time::system_clock::TimePoint referenceTime = |
| 55 | time::fromUnixTimestamp(time::milliseconds(1390966967032LL)); |
| 56 | BOOST_CHECK_GT(time::system_clock::now(), referenceTime); |
| 57 | |
| 58 | systemClock->setNow(referenceTime.time_since_epoch()); |
| 59 | BOOST_CHECK_EQUAL(time::system_clock::now(), referenceTime); |
| 60 | |
| 61 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(time::system_clock::now()), |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 62 | "1390966967032000000 nanoseconds since unit test beginning"); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 63 | |
| 64 | BOOST_CHECK_EQUAL(time::toIsoString(referenceTime), "20140129T034247.032000"); |
| 65 | BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47"); |
| 66 | BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47"); |
| 67 | |
| 68 | // Unfortunately, not all systems has lv_LV locale installed :( |
| 69 | // BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y. gada %d. %B", |
| 70 | // std::locale("lv_LV.UTF-8")), |
| 71 | // "2014. gada 29. Janvāris"); |
| 72 | |
| 73 | BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y -- %d -- %B", |
| 74 | std::locale("C")), |
| 75 | "2014 -- 29 -- January"); |
| 76 | |
| 77 | BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000"), referenceTime); |
| 78 | BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000Z"), referenceTime); |
| 79 | BOOST_CHECK_EQUAL(time::fromString("2014-01-29 03:42:47"), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 80 | time::fromUnixTimestamp(1390966967_s)); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 81 | |
| 82 | // Unfortunately, not all systems has lv_LV locale installed :( |
| 83 | // BOOST_CHECK_EQUAL(time::fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", |
| 84 | // std::locale("lv_LV.UTF-8")), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 85 | // time::fromUnixTimestamp(1390953600_s)); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 86 | |
| 87 | BOOST_CHECK_EQUAL(time::fromString("2014 -- 29 -- January", "%Y -- %d -- %B", |
| 88 | std::locale("C")), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 89 | time::fromUnixTimestamp(1390953600_s)); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(SteadyClock) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 93 | { |
| 94 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), |
| 95 | time::steady_clock::duration::zero()); |
| 96 | |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 97 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), |
| 99 | time::steady_clock::duration::zero()); |
| 100 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 101 | systemClock->advance(36500_days); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), |
| 103 | time::steady_clock::duration::zero()); |
| 104 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 105 | steadyClock->advance(100_ns); |
| 106 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), 100_ns); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 107 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 108 | steadyClock->advance(100_us); |
| 109 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), 100100_ns); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 110 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 111 | steadyClock->setNow(1_ms); |
| 112 | BOOST_CHECK_EQUAL(time::steady_clock::now().time_since_epoch(), 1000000_ns); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 113 | |
| 114 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(time::steady_clock::now()), |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 115 | "1000000 nanoseconds since unit test beginning"); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 118 | BOOST_AUTO_TEST_CASE(Scheduler) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 119 | { |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 120 | ndn::Scheduler scheduler(io); |
| 121 | |
| 122 | bool hasFired = false; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 123 | scheduler.scheduleEvent(100_s, [&] { hasFired = true; }); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 125 | io.poll(); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(hasFired, false); |
| 127 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 128 | steadyClock->advance(100_s); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 129 | |
| 130 | io.poll(); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(hasFired, true); |
| 132 | } |
| 133 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 134 | BOOST_AUTO_TEST_SUITE_END() // TestTimeUnitTestClock |
| 135 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 136 | |
| 137 | } // namespace tests |
| 138 | } // namespace ndn |