blob: c5eb6344f0adb17e7e728fe49ee038c1898c32fd [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"
6
John Park33858a32018-09-28 23:05:48 -07007#include "aos/macros.h"
8#include "aos/util/death_test_log_implementation.h"
brians343bc112013-02-10 01:53:46 +00009
10namespace aos {
11namespace time {
12namespace testing {
13
Austin Schuhe92f7e62019-12-25 13:54:41 -080014namespace chrono = std::chrono;
15
16TEST(TimeTest, FromRate) { EXPECT_EQ(chrono::milliseconds(10), FromRate(100)); }
Brian Silvermandcaa3f72015-11-29 05:32:08 +000017
Austin Schuh793d6b92016-05-01 13:28:14 -070018// Test the monotonic_clock and sleep_until functions.
19TEST(TimeTest, MonotonicClockSleepAndNow) {
20 monotonic_clock::time_point start = monotonic_clock::now();
Austin Schuhe92f7e62019-12-25 13:54:41 -080021 const auto kSleepTime = chrono::milliseconds(500);
Austin Schuh793d6b92016-05-01 13:28:14 -070022 ::std::this_thread::sleep_until(start + kSleepTime);
23 monotonic_clock::time_point end = monotonic_clock::now();
24 EXPECT_GE(end - start, kSleepTime);
Austin Schuhe92f7e62019-12-25 13:54:41 -080025 EXPECT_LT(end - start, kSleepTime + chrono::milliseconds(200));
Austin Schuh793d6b92016-05-01 13:28:14 -070026}
27
Neil Balch229001a2018-01-07 18:22:52 -080028// Test to_timespec for a duration.
29TEST(TimeTest, DurationToTimespec) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080030 struct timespec pos_time = to_timespec(chrono::milliseconds(56262));
Neil Balch229001a2018-01-07 18:22:52 -080031 EXPECT_EQ(pos_time.tv_sec, 56);
32 EXPECT_EQ(pos_time.tv_nsec, 262000000);
33
Austin Schuhe92f7e62019-12-25 13:54:41 -080034 struct timespec neg_time = to_timespec(chrono::milliseconds(-56262));
Neil Balch229001a2018-01-07 18:22:52 -080035 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.
40TEST(TimeTest, TimePointToTimespec) {
Austin Schuhe92f7e62019-12-25 13:54:41 -080041 struct timespec pos_time =
42 to_timespec(::aos::monotonic_clock::epoch() + chrono::seconds(1432423));
Neil Balch229001a2018-01-07 18:22:52 -080043 EXPECT_EQ(pos_time.tv_sec, 1432423);
44 EXPECT_EQ(pos_time.tv_nsec, 0);
45
Austin Schuhe92f7e62019-12-25 13:54:41 -080046 struct timespec neg_time =
47 to_timespec(::aos::monotonic_clock::epoch() - chrono::seconds(1432423));
Neil Balch229001a2018-01-07 18:22:52 -080048 EXPECT_EQ(neg_time.tv_sec, -1432423);
49 EXPECT_EQ(neg_time.tv_nsec, 0);
50}
51
Brian Silverman967e5df2020-02-09 16:43:34 -080052// Tests from_timeval.
53TEST(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 Schuhe92f7e62019-12-25 13:54:41 -080067// Test that << works with numbers with leading 0's.
68TEST(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.
82TEST(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");
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");
101 }
102 {
103 const monotonic_clock::time_point t =
104 monotonic_clock::epoch() - chrono::seconds(1) - chrono::nanoseconds(1);
105
106 std::stringstream s;
107 s << t;
108
109 EXPECT_EQ(s.str(), "-1.000000001sec");
110 }
111 {
112 const monotonic_clock::time_point t =
113 monotonic_clock::epoch() - chrono::seconds(2) - chrono::nanoseconds(1);
114
115 std::stringstream s;
116 s << t;
117
118 EXPECT_EQ(s.str(), "-2.000000001sec");
119 }
120}
121
122// Test that << works with min_time.
123TEST(TimeTest, OperatorStreamMinTime) {
124 const monotonic_clock::time_point t = monotonic_clock::min_time;
125
126 std::stringstream s;
127 s << t;
128
129 EXPECT_EQ(s.str(), "-9223372036.854775808sec");
130}
131
Austin Schuh40102d22019-12-27 23:30:06 -0800132// Test that << works with the epoch on the realtime clock.
133TEST(TimeTest, OperatorStreamRealtimeEpoch) {
134 const realtime_clock::time_point t = realtime_clock::epoch();
135
136 std::stringstream s;
137 s << t;
138
139 EXPECT_EQ(s.str(), "1970-01-01_00-00-00.000000000");
140}
141
142// Test that << works with positive time on the realtime clock.
143TEST(TimeTest, OperatorStreamRealtimePositive) {
144 const realtime_clock::time_point t =
145 realtime_clock::epoch() + std::chrono::hours(5 * 24) +
146 std::chrono::seconds(11) + std::chrono::milliseconds(5);
147
148 std::stringstream s;
149 s << t;
150
151 EXPECT_EQ(s.str(), "1970-01-06_00-00-11.005000000");
152}
153
154// Test that << works with negative time on the realtime clock.
155TEST(TimeTest, OperatorStreamRealtimeNegative) {
156 {
157 const realtime_clock::time_point t =
158 realtime_clock::epoch() - std::chrono::nanoseconds(1);
159
160 std::stringstream s;
161 s << t;
162
163 EXPECT_EQ(s.str(), "1969-12-31_23-59-59.999999999");
164 }
165 {
166 const realtime_clock::time_point t =
167 realtime_clock::epoch() - std::chrono::nanoseconds(999999999);
168
169 std::stringstream s;
170 s << t;
171
172 EXPECT_EQ(s.str(), "1969-12-31_23-59-59.000000001");
173 }
174 {
175 const realtime_clock::time_point t = realtime_clock::epoch() -
176 std::chrono::seconds(1) -
177 std::chrono::nanoseconds(999999999);
178
179 std::stringstream s;
180 s << t;
181
182 EXPECT_EQ(s.str(), "1969-12-31_23-59-58.000000001");
183 }
184 {
185 const realtime_clock::time_point t =
186 realtime_clock::epoch() - std::chrono::hours(5 * 24) +
187 std::chrono::seconds(11) - std::chrono::milliseconds(5);
188
189 std::stringstream s;
190 s << t;
191
192 EXPECT_EQ(s.str(), "1969-12-27_00-00-10.995000000");
193 }
194}
195
brians343bc112013-02-10 01:53:46 +0000196} // namespace testing
197} // namespace time
198} // namespace aos