Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/base/common.h"
     12 #include "webrtc/base/gunit.h"
     13 #include "webrtc/base/thread.h"
     14 #include "webrtc/base/timeutils.h"
     15 
     16 namespace rtc {
     17 
     18 TEST(TimeTest, TimeInMs) {
     19   uint32 ts_earlier = Time();
     20   Thread::SleepMs(100);
     21   uint32 ts_now = Time();
     22   // Allow for the thread to wakeup ~20ms early.
     23   EXPECT_GE(ts_now, ts_earlier + 80);
     24   // Make sure the Time is not returning in smaller unit like microseconds.
     25   EXPECT_LT(ts_now, ts_earlier + 1000);
     26 }
     27 
     28 TEST(TimeTest, Comparison) {
     29   // Obtain two different times, in known order
     30   TimeStamp ts_earlier = Time();
     31   Thread::SleepMs(100);
     32   TimeStamp ts_now = Time();
     33   EXPECT_NE(ts_earlier, ts_now);
     34 
     35   // Common comparisons
     36   EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_now));
     37   EXPECT_TRUE( TimeIsLater(       ts_earlier, ts_now));
     38   EXPECT_FALSE(TimeIsLaterOrEqual(ts_now,     ts_earlier));
     39   EXPECT_FALSE(TimeIsLater(       ts_now,     ts_earlier));
     40 
     41   // Edge cases
     42   EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_earlier));
     43   EXPECT_FALSE(TimeIsLater(       ts_earlier, ts_earlier));
     44 
     45   // Obtain a third time
     46   TimeStamp ts_later = TimeAfter(100);
     47   EXPECT_NE(ts_now, ts_later);
     48   EXPECT_TRUE( TimeIsLater(ts_now,     ts_later));
     49   EXPECT_TRUE( TimeIsLater(ts_earlier, ts_later));
     50 
     51   // Common comparisons
     52   EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_now,     ts_later));
     53   EXPECT_FALSE(TimeIsBetween(ts_earlier, ts_later,   ts_now));
     54   EXPECT_FALSE(TimeIsBetween(ts_now,     ts_earlier, ts_later));
     55   EXPECT_TRUE( TimeIsBetween(ts_now,     ts_later,   ts_earlier));
     56   EXPECT_TRUE( TimeIsBetween(ts_later,   ts_earlier, ts_now));
     57   EXPECT_FALSE(TimeIsBetween(ts_later,   ts_now,     ts_earlier));
     58 
     59   // Edge cases
     60   EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_earlier));
     61   EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_later));
     62   EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_later,   ts_later));
     63 
     64   // Earlier of two times
     65   EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier));
     66   EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_now));
     67   EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later));
     68   EXPECT_EQ(ts_earlier, TimeMin(ts_now,     ts_earlier));
     69   EXPECT_EQ(ts_earlier, TimeMin(ts_later,   ts_earlier));
     70 
     71   // Later of two times
     72   EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier));
     73   EXPECT_EQ(ts_now,     TimeMax(ts_earlier, ts_now));
     74   EXPECT_EQ(ts_later,   TimeMax(ts_earlier, ts_later));
     75   EXPECT_EQ(ts_now,     TimeMax(ts_now,     ts_earlier));
     76   EXPECT_EQ(ts_later,   TimeMax(ts_later,   ts_earlier));
     77 }
     78 
     79 TEST(TimeTest, Intervals) {
     80   TimeStamp ts_earlier = Time();
     81   TimeStamp ts_later = TimeAfter(500);
     82 
     83   // We can't depend on ts_later and ts_earlier to be exactly 500 apart
     84   // since time elapses between the calls to Time() and TimeAfter(500)
     85   EXPECT_LE(500,  TimeDiff(ts_later, ts_earlier));
     86   EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
     87 
     88   // Time has elapsed since ts_earlier
     89   EXPECT_GE(TimeSince(ts_earlier), 0);
     90 
     91   // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
     92   EXPECT_LE(TimeUntil(ts_earlier), 0);
     93 
     94   // ts_later likely hasn't happened yet, so TimeSince could be -ve
     95   // but within 500
     96   EXPECT_GE(TimeSince(ts_later), -500);
     97 
     98   // TimeUntil ts_later is at most 500
     99   EXPECT_LE(TimeUntil(ts_later), 500);
    100 }
    101 
    102 TEST(TimeTest, BoundaryComparison) {
    103   // Obtain two different times, in known order
    104   TimeStamp ts_earlier = static_cast<TimeStamp>(-50);
    105   TimeStamp ts_later = ts_earlier + 100;
    106   EXPECT_NE(ts_earlier, ts_later);
    107 
    108   // Common comparisons
    109   EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_later));
    110   EXPECT_TRUE( TimeIsLater(       ts_earlier, ts_later));
    111   EXPECT_FALSE(TimeIsLaterOrEqual(ts_later,   ts_earlier));
    112   EXPECT_FALSE(TimeIsLater(       ts_later,   ts_earlier));
    113 
    114   // Earlier of two times
    115   EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier));
    116   EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later));
    117   EXPECT_EQ(ts_earlier, TimeMin(ts_later,   ts_earlier));
    118 
    119   // Later of two times
    120   EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier));
    121   EXPECT_EQ(ts_later,   TimeMax(ts_earlier, ts_later));
    122   EXPECT_EQ(ts_later,   TimeMax(ts_later,   ts_earlier));
    123 
    124   // Interval
    125   EXPECT_EQ(100,  TimeDiff(ts_later, ts_earlier));
    126   EXPECT_EQ(-100, TimeDiff(ts_earlier, ts_later));
    127 }
    128 
    129 TEST(TimeTest, DISABLED_CurrentTmTime) {
    130   struct tm tm;
    131   int microseconds;
    132 
    133   time_t before = ::time(NULL);
    134   CurrentTmTime(&tm, &microseconds);
    135   time_t after = ::time(NULL);
    136 
    137   // Assert that 'tm' represents a time between 'before' and 'after'.
    138   // mktime() uses local time, so we have to compensate for that.
    139   time_t local_delta = before - ::mktime(::gmtime(&before));  // NOLINT
    140   time_t t = ::mktime(&tm) + local_delta;
    141 
    142   EXPECT_TRUE(before <= t && t <= after);
    143   EXPECT_TRUE(0 <= microseconds && microseconds < 1000000);
    144 }
    145 
    146 class TimestampWrapAroundHandlerTest : public testing::Test {
    147  public:
    148   TimestampWrapAroundHandlerTest() {}
    149 
    150  protected:
    151   TimestampWrapAroundHandler wraparound_handler_;
    152 };
    153 
    154 TEST_F(TimestampWrapAroundHandlerTest, Unwrap) {
    155   uint32 ts = 0xfffffff2;
    156   int64 unwrapped_ts = ts;
    157   EXPECT_EQ(ts, wraparound_handler_.Unwrap(ts));
    158   ts = 2;
    159   unwrapped_ts += 0x10;
    160   EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts));
    161   ts = 0xfffffff2;
    162   unwrapped_ts += 0xfffffff0;
    163   EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts));
    164   ts = 0;
    165   unwrapped_ts += 0xe;
    166   EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts));
    167 }
    168 
    169 }  // namespace rtc
    170