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