John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/time/time.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 3 | #include <thread> |
| 4 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/macros.h" |
| 8 | #include "aos/util/death_test_log_implementation.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | namespace time { |
| 12 | namespace testing { |
| 13 | |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 14 | namespace chrono = std::chrono; |
| 15 | |
| 16 | TEST(TimeTest, FromRate) { EXPECT_EQ(chrono::milliseconds(10), FromRate(100)); } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 17 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 18 | // Test the monotonic_clock and sleep_until functions. |
| 19 | TEST(TimeTest, MonotonicClockSleepAndNow) { |
| 20 | monotonic_clock::time_point start = monotonic_clock::now(); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 21 | const auto kSleepTime = chrono::milliseconds(500); |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 22 | ::std::this_thread::sleep_until(start + kSleepTime); |
| 23 | monotonic_clock::time_point end = monotonic_clock::now(); |
| 24 | EXPECT_GE(end - start, kSleepTime); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 25 | EXPECT_LT(end - start, kSleepTime + chrono::milliseconds(200)); |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 28 | // Test to_timespec for a duration. |
| 29 | TEST(TimeTest, DurationToTimespec) { |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 30 | struct timespec pos_time = to_timespec(chrono::milliseconds(56262)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 31 | EXPECT_EQ(pos_time.tv_sec, 56); |
| 32 | EXPECT_EQ(pos_time.tv_nsec, 262000000); |
| 33 | |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 34 | struct timespec neg_time = to_timespec(chrono::milliseconds(-56262)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 35 | EXPECT_EQ(neg_time.tv_sec, -56); |
| 36 | EXPECT_EQ(neg_time.tv_nsec, -262000000); |
| 37 | } |
| 38 | |
| 39 | // Test to_timespec for a time_point. |
| 40 | TEST(TimeTest, TimePointToTimespec) { |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 41 | struct timespec pos_time = |
| 42 | to_timespec(::aos::monotonic_clock::epoch() + chrono::seconds(1432423)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 43 | EXPECT_EQ(pos_time.tv_sec, 1432423); |
| 44 | EXPECT_EQ(pos_time.tv_nsec, 0); |
| 45 | |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 46 | struct timespec neg_time = |
| 47 | to_timespec(::aos::monotonic_clock::epoch() - chrono::seconds(1432423)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 48 | EXPECT_EQ(neg_time.tv_sec, -1432423); |
| 49 | EXPECT_EQ(neg_time.tv_nsec, 0); |
| 50 | } |
| 51 | |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 52 | // Test that << works with numbers with leading 0's. |
| 53 | TEST(TimeTest, OperatorStream) { |
| 54 | const monotonic_clock::time_point t = monotonic_clock::epoch() + |
| 55 | chrono::seconds(1432423) + |
| 56 | chrono::milliseconds(15); |
| 57 | |
| 58 | // And confirm that the stream's settings are restored by adding a random |
| 59 | // number afterwords. |
| 60 | std::stringstream s; |
| 61 | s << t << " and number " << 123; |
| 62 | |
| 63 | EXPECT_EQ(s.str(), "1432423.015000000sec and number 123"); |
| 64 | } |
| 65 | |
| 66 | // Test that << works with negative numbers. |
| 67 | TEST(TimeTest, OperatorStreamNegative) { |
| 68 | { |
| 69 | const monotonic_clock::time_point t = monotonic_clock::epoch() - |
| 70 | chrono::seconds(14) + |
| 71 | chrono::milliseconds(915); |
| 72 | |
| 73 | std::stringstream s; |
| 74 | s << t; |
| 75 | |
| 76 | EXPECT_EQ(s.str(), "-13.085000000sec"); |
| 77 | } |
| 78 | { |
| 79 | const monotonic_clock::time_point t = |
| 80 | monotonic_clock::epoch() - chrono::nanoseconds(1); |
| 81 | |
| 82 | std::stringstream s; |
| 83 | s << t; |
| 84 | |
| 85 | EXPECT_EQ(s.str(), "-0.000000001sec"); |
| 86 | } |
| 87 | { |
| 88 | const monotonic_clock::time_point t = |
| 89 | monotonic_clock::epoch() - chrono::seconds(1) - chrono::nanoseconds(1); |
| 90 | |
| 91 | std::stringstream s; |
| 92 | s << t; |
| 93 | |
| 94 | EXPECT_EQ(s.str(), "-1.000000001sec"); |
| 95 | } |
| 96 | { |
| 97 | const monotonic_clock::time_point t = |
| 98 | monotonic_clock::epoch() - chrono::seconds(2) - chrono::nanoseconds(1); |
| 99 | |
| 100 | std::stringstream s; |
| 101 | s << t; |
| 102 | |
| 103 | EXPECT_EQ(s.str(), "-2.000000001sec"); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Test that << works with min_time. |
| 108 | TEST(TimeTest, OperatorStreamMinTime) { |
| 109 | const monotonic_clock::time_point t = monotonic_clock::min_time; |
| 110 | |
| 111 | std::stringstream s; |
| 112 | s << t; |
| 113 | |
| 114 | EXPECT_EQ(s.str(), "-9223372036.854775808sec"); |
| 115 | } |
| 116 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 117 | // Test that << works with the epoch on the realtime clock. |
| 118 | TEST(TimeTest, OperatorStreamRealtimeEpoch) { |
| 119 | const realtime_clock::time_point t = realtime_clock::epoch(); |
| 120 | |
| 121 | std::stringstream s; |
| 122 | s << t; |
| 123 | |
| 124 | EXPECT_EQ(s.str(), "1970-01-01_00-00-00.000000000"); |
| 125 | } |
| 126 | |
| 127 | // Test that << works with positive time on the realtime clock. |
| 128 | TEST(TimeTest, OperatorStreamRealtimePositive) { |
| 129 | const realtime_clock::time_point t = |
| 130 | realtime_clock::epoch() + std::chrono::hours(5 * 24) + |
| 131 | std::chrono::seconds(11) + std::chrono::milliseconds(5); |
| 132 | |
| 133 | std::stringstream s; |
| 134 | s << t; |
| 135 | |
| 136 | EXPECT_EQ(s.str(), "1970-01-06_00-00-11.005000000"); |
| 137 | } |
| 138 | |
| 139 | // Test that << works with negative time on the realtime clock. |
| 140 | TEST(TimeTest, OperatorStreamRealtimeNegative) { |
| 141 | { |
| 142 | const realtime_clock::time_point t = |
| 143 | realtime_clock::epoch() - std::chrono::nanoseconds(1); |
| 144 | |
| 145 | std::stringstream s; |
| 146 | s << t; |
| 147 | |
| 148 | EXPECT_EQ(s.str(), "1969-12-31_23-59-59.999999999"); |
| 149 | } |
| 150 | { |
| 151 | const realtime_clock::time_point t = |
| 152 | realtime_clock::epoch() - std::chrono::nanoseconds(999999999); |
| 153 | |
| 154 | std::stringstream s; |
| 155 | s << t; |
| 156 | |
| 157 | EXPECT_EQ(s.str(), "1969-12-31_23-59-59.000000001"); |
| 158 | } |
| 159 | { |
| 160 | const realtime_clock::time_point t = realtime_clock::epoch() - |
| 161 | std::chrono::seconds(1) - |
| 162 | std::chrono::nanoseconds(999999999); |
| 163 | |
| 164 | std::stringstream s; |
| 165 | s << t; |
| 166 | |
| 167 | EXPECT_EQ(s.str(), "1969-12-31_23-59-58.000000001"); |
| 168 | } |
| 169 | { |
| 170 | const realtime_clock::time_point t = |
| 171 | realtime_clock::epoch() - std::chrono::hours(5 * 24) + |
| 172 | std::chrono::seconds(11) - std::chrono::milliseconds(5); |
| 173 | |
| 174 | std::stringstream s; |
| 175 | s << t; |
| 176 | |
| 177 | EXPECT_EQ(s.str(), "1969-12-27_00-00-10.995000000"); |
| 178 | } |
| 179 | } |
| 180 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 181 | } // namespace testing |
| 182 | } // namespace time |
| 183 | } // namespace aos |