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