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