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 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 52 | // Tests from_timeval. |
| 53 | TEST(TimeTest, TimevalToTimePoint) { |
| 54 | struct timeval pos_time; |
| 55 | pos_time.tv_sec = 1432423; |
| 56 | pos_time.tv_usec = 0; |
| 57 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1432423), |
| 58 | from_timeval(pos_time)); |
| 59 | |
| 60 | struct timeval neg_time; |
| 61 | neg_time.tv_sec = -1432423; |
| 62 | neg_time.tv_usec = 0; |
| 63 | EXPECT_EQ(::aos::monotonic_clock::epoch() - chrono::seconds(1432423), |
| 64 | from_timeval(neg_time)); |
| 65 | } |
| 66 | |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 67 | // Test that << works with numbers with leading 0's. |
| 68 | TEST(TimeTest, OperatorStream) { |
| 69 | const monotonic_clock::time_point t = monotonic_clock::epoch() + |
| 70 | chrono::seconds(1432423) + |
| 71 | chrono::milliseconds(15); |
| 72 | |
| 73 | // And confirm that the stream's settings are restored by adding a random |
| 74 | // number afterwords. |
| 75 | std::stringstream s; |
| 76 | s << t << " and number " << 123; |
| 77 | |
| 78 | EXPECT_EQ(s.str(), "1432423.015000000sec and number 123"); |
| 79 | } |
| 80 | |
| 81 | // Test that << works with negative numbers. |
| 82 | TEST(TimeTest, OperatorStreamNegative) { |
| 83 | { |
| 84 | const monotonic_clock::time_point t = monotonic_clock::epoch() - |
| 85 | chrono::seconds(14) + |
| 86 | chrono::milliseconds(915); |
| 87 | |
| 88 | std::stringstream s; |
| 89 | s << t; |
| 90 | |
| 91 | EXPECT_EQ(s.str(), "-13.085000000sec"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 92 | EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 93 | } |
| 94 | { |
| 95 | const monotonic_clock::time_point t = |
| 96 | monotonic_clock::epoch() - chrono::nanoseconds(1); |
| 97 | |
| 98 | std::stringstream s; |
| 99 | s << t; |
| 100 | |
| 101 | EXPECT_EQ(s.str(), "-0.000000001sec"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 102 | EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 103 | } |
| 104 | { |
| 105 | const monotonic_clock::time_point t = |
| 106 | monotonic_clock::epoch() - chrono::seconds(1) - chrono::nanoseconds(1); |
| 107 | |
| 108 | std::stringstream s; |
| 109 | s << t; |
| 110 | |
| 111 | EXPECT_EQ(s.str(), "-1.000000001sec"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 112 | EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 113 | } |
| 114 | { |
| 115 | const monotonic_clock::time_point t = |
| 116 | monotonic_clock::epoch() - chrono::seconds(2) - chrono::nanoseconds(1); |
| 117 | |
| 118 | std::stringstream s; |
| 119 | s << t; |
| 120 | |
| 121 | EXPECT_EQ(s.str(), "-2.000000001sec"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 122 | EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | // Test that << works with min_time. |
| 127 | TEST(TimeTest, OperatorStreamMinTime) { |
| 128 | const monotonic_clock::time_point t = monotonic_clock::min_time; |
| 129 | |
| 130 | std::stringstream s; |
| 131 | s << t; |
| 132 | |
| 133 | EXPECT_EQ(s.str(), "-9223372036.854775808sec"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 134 | EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t); |
Austin Schuh | e92f7e6 | 2019-12-25 13:54:41 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 137 | // Test that << works with the epoch on the realtime clock. |
| 138 | TEST(TimeTest, OperatorStreamRealtimeEpoch) { |
| 139 | const realtime_clock::time_point t = realtime_clock::epoch(); |
| 140 | |
| 141 | std::stringstream s; |
| 142 | s << t; |
| 143 | |
| 144 | EXPECT_EQ(s.str(), "1970-01-01_00-00-00.000000000"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 145 | EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Test that << works with positive time on the realtime clock. |
| 149 | TEST(TimeTest, OperatorStreamRealtimePositive) { |
| 150 | const realtime_clock::time_point t = |
| 151 | realtime_clock::epoch() + std::chrono::hours(5 * 24) + |
| 152 | std::chrono::seconds(11) + std::chrono::milliseconds(5); |
| 153 | |
| 154 | std::stringstream s; |
| 155 | s << t; |
| 156 | |
| 157 | EXPECT_EQ(s.str(), "1970-01-06_00-00-11.005000000"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 158 | EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Test that << works with negative time on the realtime clock. |
| 162 | TEST(TimeTest, OperatorStreamRealtimeNegative) { |
| 163 | { |
| 164 | const realtime_clock::time_point t = |
| 165 | realtime_clock::epoch() - std::chrono::nanoseconds(1); |
| 166 | |
| 167 | std::stringstream s; |
| 168 | s << t; |
| 169 | |
| 170 | EXPECT_EQ(s.str(), "1969-12-31_23-59-59.999999999"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 171 | EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 172 | } |
| 173 | { |
| 174 | const realtime_clock::time_point t = |
| 175 | realtime_clock::epoch() - std::chrono::nanoseconds(999999999); |
| 176 | |
| 177 | std::stringstream s; |
| 178 | s << t; |
| 179 | |
| 180 | EXPECT_EQ(s.str(), "1969-12-31_23-59-59.000000001"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 181 | EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 182 | } |
| 183 | { |
| 184 | const realtime_clock::time_point t = realtime_clock::epoch() - |
| 185 | std::chrono::seconds(1) - |
| 186 | std::chrono::nanoseconds(999999999); |
| 187 | |
| 188 | std::stringstream s; |
| 189 | s << t; |
| 190 | |
| 191 | EXPECT_EQ(s.str(), "1969-12-31_23-59-58.000000001"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 192 | EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 193 | } |
| 194 | { |
| 195 | const realtime_clock::time_point t = |
| 196 | realtime_clock::epoch() - std::chrono::hours(5 * 24) + |
| 197 | std::chrono::seconds(11) - std::chrono::milliseconds(5); |
| 198 | |
| 199 | std::stringstream s; |
| 200 | s << t; |
| 201 | |
| 202 | EXPECT_EQ(s.str(), "1969-12-27_00-00-10.995000000"); |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 203 | EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 207 | } // namespace testing |
| 208 | } // namespace time |
| 209 | } // namespace aos |