Home | History | Annotate | Download | only in quic
      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.
      4 
      5 #include "net/quic/quic_time.h"
      6 #include "net/quic/test_tools/mock_clock.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace net {
     10 namespace test {
     11 
     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());
     16 }
     17 
     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());
     22 }
     23 
     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));
     31 
     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());
     38 }
     39 
     40 TEST(QuicTimeDeltaTest, Add) {
     41   EXPECT_EQ(QuicTime::Delta::FromMicroseconds(2000),
     42             QuicTime::Delta::Zero().Add(QuicTime::Delta::FromMilliseconds(2)));
     43 }
     44 
     45 TEST(QuicTimeDeltaTest, Subtract) {
     46   EXPECT_EQ(QuicTime::Delta::FromMicroseconds(1000),
     47             QuicTime::Delta::FromMilliseconds(2).Subtract(
     48                 QuicTime::Delta::FromMilliseconds(1)));
     49 }
     50 
     51 TEST(QuicTimeDeltaTest, NotEqual) {
     52   EXPECT_TRUE(QuicTime::Delta::FromSeconds(0) !=
     53               QuicTime::Delta::FromSeconds(1));
     54   EXPECT_FALSE(QuicTime::Delta::FromSeconds(0) !=
     55                QuicTime::Delta::FromSeconds(0));
     56 }
     57 
     58 class QuicTimeTest : public ::testing::Test {
     59  protected:
     60   MockClock clock_;
     61 };
     62 
     63 TEST_F(QuicTimeTest, Initialized) {
     64   EXPECT_FALSE(QuicTime::Zero().IsInitialized());
     65   EXPECT_TRUE(QuicTime::Zero().Add(
     66       QuicTime::Delta::FromMicroseconds(1)).IsInitialized());
     67 }
     68 
     69 TEST_F(QuicTimeTest, Add) {
     70   QuicTime time_1 = QuicTime::Zero().Add(
     71       QuicTime::Delta::FromMilliseconds(1));
     72   QuicTime time_2 = QuicTime::Zero().Add(
     73       QuicTime::Delta::FromMilliseconds(2));
     74 
     75   QuicTime::Delta diff = time_2.Subtract(time_1);
     76 
     77   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(1), diff);
     78   EXPECT_EQ(1000, diff.ToMicroseconds());
     79   EXPECT_EQ(1, diff.ToMilliseconds());
     80 }
     81 
     82 TEST_F(QuicTimeTest, Subtract) {
     83   QuicTime time_1 = QuicTime::Zero().Add(
     84       QuicTime::Delta::FromMilliseconds(1));
     85   QuicTime time_2 = QuicTime::Zero().Add(
     86       QuicTime::Delta::FromMilliseconds(2));
     87 
     88   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(1), time_2.Subtract(time_1));
     89 }
     90 
     91 TEST_F(QuicTimeTest, SubtractDelta) {
     92   QuicTime time = QuicTime::Zero().Add(
     93       QuicTime::Delta::FromMilliseconds(2));
     94   EXPECT_EQ(QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(1)),
     95             time.Subtract(QuicTime::Delta::FromMilliseconds(1)));
     96 }
     97 
     98 TEST_F(QuicTimeTest, MockClock) {
     99   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
    100 
    101   QuicTime now = clock_.ApproximateNow();
    102   QuicTime time = QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(1000));
    103 
    104   EXPECT_EQ(now, time);
    105 
    106   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
    107   now = clock_.ApproximateNow();
    108 
    109   EXPECT_NE(now, time);
    110 
    111   time = time.Add(QuicTime::Delta::FromMilliseconds(1));
    112   EXPECT_EQ(now, time);
    113 }
    114 
    115 TEST_F(QuicTimeTest, LE) {
    116   const QuicTime zero = QuicTime::Zero();
    117   const QuicTime one  = zero.Add(QuicTime::Delta::FromSeconds(1));
    118   EXPECT_TRUE(zero <= zero);
    119   EXPECT_TRUE(zero <= one);
    120   EXPECT_TRUE(one <= one);
    121   EXPECT_FALSE(one <= zero);
    122 }
    123 
    124 }  // namespace test
    125 }  // namespace net
    126