blob: 97116b04b941ec43c4dce46884ee1c2e07299d35 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/time/time.h"
brians343bc112013-02-10 01:53:46 +00002
Austin Schuh793d6b92016-05-01 13:28:14 -07003#include <thread>
4
Austin Schuh60e77942022-05-16 17:48:24 -07005#include "glog/logging.h"
6#include "gtest/gtest.h"
brians343bc112013-02-10 01:53:46 +00007
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "aos/macros.h"
9#include "aos/util/death_test_log_implementation.h"
10
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace aos::time::testing {
brians343bc112013-02-10 01:53:46 +000012
Austin Schuhe92f7e62019-12-25 13:54:41 -080013namespace chrono = std::chrono;
14
15TEST(TimeTest, FromRate) { EXPECT_EQ(chrono::milliseconds(10), FromRate(100)); }
Brian Silvermandcaa3f72015-11-29 05:32:08 +000016
Austin Schuh793d6b92016-05-01 13:28:14 -070017// Test the monotonic_clock and sleep_until functions.
18TEST(TimeTest, MonotonicClockSleepAndNow) {
19 monotonic_clock::time_point start = monotonic_clock::now();
Austin Schuhe92f7e62019-12-25 13:54:41 -080020 const auto kSleepTime = chrono::milliseconds(500);
Austin Schuh793d6b92016-05-01 13:28:14 -070021 ::std::this_thread::sleep_until(start + kSleepTime);
22 monotonic_clock::time_point end = monotonic_clock::now();
23 EXPECT_GE(end - start, kSleepTime);
Austin Schuhe92f7e62019-12-25 13:54:41 -080024 EXPECT_LT(end - start, kSleepTime + chrono::milliseconds(200));
Austin Schuh793d6b92016-05-01 13:28:14 -070025}
26
Neil Balch229001a2018-01-07 18:22:52 -080027// Test to_timespec for a duration.
28TEST(TimeTest, DurationToTimespec) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080029 struct timespec pos_time = to_timespec(chrono::milliseconds(56262));
Neil Balch229001a2018-01-07 18:22:52 -080030 EXPECT_EQ(pos_time.tv_sec, 56);
31 EXPECT_EQ(pos_time.tv_nsec, 262000000);
32
Austin Schuhe92f7e62019-12-25 13:54:41 -080033 struct timespec neg_time = to_timespec(chrono::milliseconds(-56262));
Neil Balch229001a2018-01-07 18:22:52 -080034 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.
39TEST(TimeTest, TimePointToTimespec) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080040 struct timespec pos_time =
41 to_timespec(::aos::monotonic_clock::epoch() + chrono::seconds(1432423));
Neil Balch229001a2018-01-07 18:22:52 -080042 EXPECT_EQ(pos_time.tv_sec, 1432423);
43 EXPECT_EQ(pos_time.tv_nsec, 0);
44
Austin Schuhe92f7e62019-12-25 13:54:41 -080045 struct timespec neg_time =
46 to_timespec(::aos::monotonic_clock::epoch() - chrono::seconds(1432423));
Neil Balch229001a2018-01-07 18:22:52 -080047 EXPECT_EQ(neg_time.tv_sec, -1432423);
48 EXPECT_EQ(neg_time.tv_nsec, 0);
49}
50
Brian Silverman967e5df2020-02-09 16:43:34 -080051// Tests from_timeval.
52TEST(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 Schuhe92f7e62019-12-25 13:54:41 -080066// Test that << works with numbers with leading 0's.
67TEST(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.
81TEST(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 Schuh3ce0a922020-07-21 21:08:54 -070091 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -080092 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700101 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800102 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700111 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800112 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700121 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800122 }
123}
124
125// Test that << works with min_time.
126TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700133 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800134}
135
Austin Schuh40102d22019-12-27 23:30:06 -0800136// Test that << works with the epoch on the realtime clock.
137TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700144 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800145}
146
147// Test that << works with positive time on the realtime clock.
148TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700157 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800158}
159
160// Test that << works with negative time on the realtime clock.
161TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700170 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800171 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700180 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800181 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700191 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800192 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700202 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800203 }
Austin Schuhb2ac0562021-09-13 23:23:33 -0700204
205 {
206 const realtime_clock::time_point t = realtime_clock::min_time;
207
208 std::stringstream s;
209 s << t;
210
Philipp Schraderd0d264d2023-12-20 11:11:35 -0800211 // min_time happens to be unrepresentable because of rounding and signed
212 // integer overflow.
213 EXPECT_EQ(s.str(), "(unrepresentable realtime -9223372036854775808)");
Austin Schuhb2ac0562021-09-13 23:23:33 -0700214 }
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 Schuh40102d22019-12-27 23:30:06 -0800226}
227
Eric Schmiedeberg42273f42023-12-14 13:48:45 -0700228// Test that ToString works for monotonic and realtime time points.
229TEST(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 Pleinesf63bde82024-01-13 15:59:33 -0800236} // namespace aos::time::testing