blob: 38e389a1cd49c1afd60adde8418abe100dcfc106 [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
brians343bc112013-02-10 01:53:46 +00005#include "gtest/gtest.h"
Austin Schuhb2ac0562021-09-13 23:23:33 -07006#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +00007
John Park33858a32018-09-28 23:05:48 -07008#include "aos/macros.h"
9#include "aos/util/death_test_log_implementation.h"
brians343bc112013-02-10 01:53:46 +000010
11namespace aos {
12namespace time {
13namespace testing {
14
Austin Schuhe92f7e62019-12-25 13:54:41 -080015namespace chrono = std::chrono;
16
17TEST(TimeTest, FromRate) { EXPECT_EQ(chrono::milliseconds(10), FromRate(100)); }
Brian Silvermandcaa3f72015-11-29 05:32:08 +000018
Austin Schuh793d6b92016-05-01 13:28:14 -070019// Test the monotonic_clock and sleep_until functions.
20TEST(TimeTest, MonotonicClockSleepAndNow) {
21 monotonic_clock::time_point start = monotonic_clock::now();
Austin Schuhe92f7e62019-12-25 13:54:41 -080022 const auto kSleepTime = chrono::milliseconds(500);
Austin Schuh793d6b92016-05-01 13:28:14 -070023 ::std::this_thread::sleep_until(start + kSleepTime);
24 monotonic_clock::time_point end = monotonic_clock::now();
25 EXPECT_GE(end - start, kSleepTime);
Austin Schuhe92f7e62019-12-25 13:54:41 -080026 EXPECT_LT(end - start, kSleepTime + chrono::milliseconds(200));
Austin Schuh793d6b92016-05-01 13:28:14 -070027}
28
Neil Balch229001a2018-01-07 18:22:52 -080029// Test to_timespec for a duration.
30TEST(TimeTest, DurationToTimespec) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080031 struct timespec pos_time = to_timespec(chrono::milliseconds(56262));
Neil Balch229001a2018-01-07 18:22:52 -080032 EXPECT_EQ(pos_time.tv_sec, 56);
33 EXPECT_EQ(pos_time.tv_nsec, 262000000);
34
Austin Schuhe92f7e62019-12-25 13:54:41 -080035 struct timespec neg_time = to_timespec(chrono::milliseconds(-56262));
Neil Balch229001a2018-01-07 18:22:52 -080036 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.
41TEST(TimeTest, TimePointToTimespec) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080042 struct timespec pos_time =
43 to_timespec(::aos::monotonic_clock::epoch() + chrono::seconds(1432423));
Neil Balch229001a2018-01-07 18:22:52 -080044 EXPECT_EQ(pos_time.tv_sec, 1432423);
45 EXPECT_EQ(pos_time.tv_nsec, 0);
46
Austin Schuhe92f7e62019-12-25 13:54:41 -080047 struct timespec neg_time =
48 to_timespec(::aos::monotonic_clock::epoch() - chrono::seconds(1432423));
Neil Balch229001a2018-01-07 18:22:52 -080049 EXPECT_EQ(neg_time.tv_sec, -1432423);
50 EXPECT_EQ(neg_time.tv_nsec, 0);
51}
52
Brian Silverman967e5df2020-02-09 16:43:34 -080053// Tests from_timeval.
54TEST(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 Schuhe92f7e62019-12-25 13:54:41 -080068// Test that << works with numbers with leading 0's.
69TEST(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.
83TEST(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 Schuh3ce0a922020-07-21 21:08:54 -070093 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -080094 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700103 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800104 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700113 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800114 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700123 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800124 }
125}
126
127// Test that << works with min_time.
128TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700135 EXPECT_EQ(monotonic_clock::FromString(s.str()).value(), t);
Austin Schuhe92f7e62019-12-25 13:54:41 -0800136}
137
Austin Schuh40102d22019-12-27 23:30:06 -0800138// Test that << works with the epoch on the realtime clock.
139TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700146 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800147}
148
149// Test that << works with positive time on the realtime clock.
150TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700159 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800160}
161
162// Test that << works with negative time on the realtime clock.
163TEST(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 Schuh3ce0a922020-07-21 21:08:54 -0700172 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800173 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700182 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800183 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700193 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800194 }
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 Schuh3ce0a922020-07-21 21:08:54 -0700204 EXPECT_EQ(realtime_clock::FromString(s.str()).value(), t);
Austin Schuh40102d22019-12-27 23:30:06 -0800205 }
Austin Schuhb2ac0562021-09-13 23:23:33 -0700206
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 Schuh40102d22019-12-27 23:30:06 -0800227}
228
brians343bc112013-02-10 01:53:46 +0000229} // namespace testing
230} // namespace time
231} // namespace aos