MRPT  1.9.9
Clock_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/core/Clock.h>
12 #include <chrono>
13 #include <thread>
14 
15 static void test_delay()
16 {
17  const double t0 = mrpt::Clock::toDouble(mrpt::Clock::now());
18  std::this_thread::sleep_for(std::chrono::milliseconds(10));
19  const double t1 = mrpt::Clock::toDouble(mrpt::Clock::now());
20 
21  EXPECT_GT(t1 - t0, 0.008); // ideally, near 0.010
22  EXPECT_LT(t1 - t0, 5.0); // just detect it's not a crazy number
23 }
24 
25 TEST(clock, delay_Realtime)
26 {
27  // Default:
28  test_delay();
29 
30  // Monotonic:
31  mrpt::Clock::setActiveClock(mrpt::Clock::Source::Monotonic);
32  test_delay();
33 
34  // Realtime:
35  mrpt::Clock::setActiveClock(mrpt::Clock::Source::Realtime);
36  test_delay();
37 }
38 
39 TEST(clock, changeSource)
40 {
41  const double t0 = mrpt::Clock::toDouble(mrpt::Clock::now());
42  mrpt::Clock::setActiveClock(mrpt::Clock::Source::Monotonic);
43 
44  std::this_thread::sleep_for(std::chrono::milliseconds(10));
45  const double t1 = mrpt::Clock::toDouble(mrpt::Clock::now());
46  mrpt::Clock::setActiveClock(mrpt::Clock::Source::Realtime);
47 
48  std::this_thread::sleep_for(std::chrono::milliseconds(10));
49  const double t2 = mrpt::Clock::toDouble(mrpt::Clock::now());
50 
51  EXPECT_GT(t1 - t0, 0.008); // ideally, near 0.010
52  EXPECT_LT(t1 - t0, 5.0); // just detect it's not a crazy number
53 
54  EXPECT_GT(t2 - t1, 0.008); // ideally, near 0.010
55  EXPECT_LT(t2 - t1, 5.0); // just detect it's not a crazy number
56 }
57 
58 TEST(clock, checkSynchEpoch)
59 {
60  for (int i = 0; i < 20; i++)
61  {
62  std::this_thread::sleep_for(std::chrono::milliseconds(5));
63  const int64_t err = mrpt::Clock::resetMonotonicToRealTimeEpoch();
64 
65  // it should be a really small number in a regular computer,
66  // but we set the threshold much higher due to spurious errors
67  // when running unit tests in VMs (build farms)
68  EXPECT_LT(std::abs(err), 70000);
69  }
70 }
static double toDouble(const time_point t) noexcept
Converts a timestamp to a UNIX time_t-like number, with fractional part.
Definition: Clock.cpp:106
EXPECT_LT(out.final_rmse, 3.0)
EXPECT_GT(out.final_iters, 10UL)
static void test_delay()
static time_point now() noexcept
Returns the current time using the currently selected Clock source.
Definition: Clock.cpp:94
static void setActiveClock(const Source s)
Changes the selected clock to get time from when calling now().
Definition: Clock.cpp:114
static int64_t resetMonotonicToRealTimeEpoch() noexcept
Monotonic clock might drift over time with respect to Realtime.
Definition: Clock.cpp:124
TEST(clock, delay_Realtime)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020