blob: 80d273e3f6ea7c2cb0f8949538f060184979ff9f [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
Austin Schuhe92f7e62019-12-25 13:54:41 -080052// Test that << works with numbers with leading 0's.
53TEST(TimeTest, OperatorStream) {
54 const monotonic_clock::time_point t = monotonic_clock::epoch() +
55 chrono::seconds(1432423) +
56 chrono::milliseconds(15);
57
58 // And confirm that the stream's settings are restored by adding a random
59 // number afterwords.
60 std::stringstream s;
61 s << t << " and number " << 123;
62
63 EXPECT_EQ(s.str(), "1432423.015000000sec and number 123");
64}
65
66// Test that << works with negative numbers.
67TEST(TimeTest, OperatorStreamNegative) {
68 {
69 const monotonic_clock::time_point t = monotonic_clock::epoch() -
70 chrono::seconds(14) +
71 chrono::milliseconds(915);
72
73 std::stringstream s;
74 s << t;
75
76 EXPECT_EQ(s.str(), "-13.085000000sec");
77 }
78 {
79 const monotonic_clock::time_point t =
80 monotonic_clock::epoch() - chrono::nanoseconds(1);
81
82 std::stringstream s;
83 s << t;
84
85 EXPECT_EQ(s.str(), "-0.000000001sec");
86 }
87 {
88 const monotonic_clock::time_point t =
89 monotonic_clock::epoch() - chrono::seconds(1) - chrono::nanoseconds(1);
90
91 std::stringstream s;
92 s << t;
93
94 EXPECT_EQ(s.str(), "-1.000000001sec");
95 }
96 {
97 const monotonic_clock::time_point t =
98 monotonic_clock::epoch() - chrono::seconds(2) - chrono::nanoseconds(1);
99
100 std::stringstream s;
101 s << t;
102
103 EXPECT_EQ(s.str(), "-2.000000001sec");
104 }
105}
106
107// Test that << works with min_time.
108TEST(TimeTest, OperatorStreamMinTime) {
109 const monotonic_clock::time_point t = monotonic_clock::min_time;
110
111 std::stringstream s;
112 s << t;
113
114 EXPECT_EQ(s.str(), "-9223372036.854775808sec");
115}
116
brians343bc112013-02-10 01:53:46 +0000117} // namespace testing
118} // namespace time
119} // namespace aos