1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_time.h"
6 #include "net/quic/test_tools/mock_clock.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(QuicTimeDeltaTest
, Zero
) {
13 EXPECT_TRUE(QuicTime::Delta::Zero().IsZero());
14 EXPECT_FALSE(QuicTime::Delta::Zero().IsInfinite());
15 EXPECT_FALSE(QuicTime::Delta::FromMilliseconds(1).IsZero());
18 TEST(QuicTimeDeltaTest
, Infinite
) {
19 EXPECT_TRUE(QuicTime::Delta::Infinite().IsInfinite());
20 EXPECT_FALSE(QuicTime::Delta::Zero().IsInfinite());
21 EXPECT_FALSE(QuicTime::Delta::FromMilliseconds(1).IsInfinite());
24 TEST(QuicTimeDeltaTest
, FromTo
) {
25 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(1),
26 QuicTime::Delta::FromMicroseconds(1000));
27 EXPECT_EQ(QuicTime::Delta::FromSeconds(1),
28 QuicTime::Delta::FromMilliseconds(1000));
29 EXPECT_EQ(QuicTime::Delta::FromSeconds(1),
30 QuicTime::Delta::FromMicroseconds(1000000));
32 EXPECT_EQ(1, QuicTime::Delta::FromMicroseconds(1000).ToMilliseconds());
33 EXPECT_EQ(2, QuicTime::Delta::FromMilliseconds(2000).ToSeconds());
34 EXPECT_EQ(1000, QuicTime::Delta::FromMilliseconds(1).ToMicroseconds());
35 EXPECT_EQ(1, QuicTime::Delta::FromMicroseconds(1000).ToMilliseconds());
36 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(2000).ToMicroseconds(),
37 QuicTime::Delta::FromSeconds(2).ToMicroseconds());
40 TEST(QuicTimeDeltaTest
, Add
) {
41 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(2000),
42 QuicTime::Delta::Zero().Add(QuicTime::Delta::FromMilliseconds(2)));
45 TEST(QuicTimeDeltaTest
, Subtract
) {
46 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(1000),
47 QuicTime::Delta::FromMilliseconds(2).Subtract(
48 QuicTime::Delta::FromMilliseconds(1)));
51 TEST(QuicTimeDeltaTest
, Multiply
) {
53 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(4000),
54 QuicTime::Delta::FromMilliseconds(2).Multiply(i
));
56 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(4000),
57 QuicTime::Delta::FromMilliseconds(2).Multiply(d
));
60 TEST(QuicTimeDeltaTest
, Max
) {
61 EXPECT_EQ(QuicTime::Delta::FromMicroseconds(2000),
62 QuicTime::Delta::Max(QuicTime::Delta::FromMicroseconds(1000),
63 QuicTime::Delta::FromMicroseconds(2000)));
66 TEST(QuicTimeDeltaTest
, NotEqual
) {
67 EXPECT_TRUE(QuicTime::Delta::FromSeconds(0) !=
68 QuicTime::Delta::FromSeconds(1));
69 EXPECT_FALSE(QuicTime::Delta::FromSeconds(0) !=
70 QuicTime::Delta::FromSeconds(0));
73 class QuicTimeTest
: public ::testing::Test
{
78 TEST_F(QuicTimeTest
, Initialized
) {
79 EXPECT_FALSE(QuicTime::Zero().IsInitialized());
80 EXPECT_TRUE(QuicTime::Zero().Add(
81 QuicTime::Delta::FromMicroseconds(1)).IsInitialized());
84 TEST_F(QuicTimeTest
, Add
) {
85 QuicTime time_1
= QuicTime::Zero().Add(
86 QuicTime::Delta::FromMilliseconds(1));
87 QuicTime time_2
= QuicTime::Zero().Add(
88 QuicTime::Delta::FromMilliseconds(2));
90 QuicTime::Delta diff
= time_2
.Subtract(time_1
);
92 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(1), diff
);
93 EXPECT_EQ(1000, diff
.ToMicroseconds());
94 EXPECT_EQ(1, diff
.ToMilliseconds());
97 TEST_F(QuicTimeTest
, Subtract
) {
98 QuicTime time_1
= QuicTime::Zero().Add(
99 QuicTime::Delta::FromMilliseconds(1));
100 QuicTime time_2
= QuicTime::Zero().Add(
101 QuicTime::Delta::FromMilliseconds(2));
103 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(1), time_2
.Subtract(time_1
));
106 TEST_F(QuicTimeTest
, SubtractDelta
) {
107 QuicTime time
= QuicTime::Zero().Add(
108 QuicTime::Delta::FromMilliseconds(2));
109 EXPECT_EQ(QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(1)),
110 time
.Subtract(QuicTime::Delta::FromMilliseconds(1)));
113 TEST_F(QuicTimeTest
, Max
) {
114 QuicTime time_1
= QuicTime::Zero().Add(
115 QuicTime::Delta::FromMilliseconds(1));
116 QuicTime time_2
= QuicTime::Zero().Add(
117 QuicTime::Delta::FromMilliseconds(2));
119 EXPECT_EQ(time_2
, QuicTime::Max(time_1
, time_2
));
122 TEST_F(QuicTimeTest
, MockClock
) {
123 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
125 QuicTime now
= clock_
.ApproximateNow();
126 QuicTime time
= QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(1000));
128 EXPECT_EQ(now
, time
);
130 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
131 now
= clock_
.ApproximateNow();
133 EXPECT_NE(now
, time
);
135 time
= time
.Add(QuicTime::Delta::FromMilliseconds(1));
136 EXPECT_EQ(now
, time
);
139 TEST_F(QuicTimeTest
, LE
) {
140 const QuicTime zero
= QuicTime::Zero();
141 const QuicTime one
= zero
.Add(QuicTime::Delta::FromSeconds(1));
142 EXPECT_TRUE(zero
<= zero
);
143 EXPECT_TRUE(zero
<= one
);
144 EXPECT_TRUE(one
<= one
);
145 EXPECT_FALSE(one
<= zero
);