~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/base/timeutils_unittest.cc

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2004--2011, Google Inc.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
8
 *  1. Redistributions of source code must retain the above copyright notice,
 
9
 *     this list of conditions and the following disclaimer.
 
10
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 
11
 *     this list of conditions and the following disclaimer in the documentation
 
12
 *     and/or other materials provided with the distribution.
 
13
 *  3. The name of the author may not be used to endorse or promote products
 
14
 *     derived from this software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
20
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
24
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
25
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
#include "talk/base/common.h"
 
29
#include "talk/base/gunit.h"
 
30
#include "talk/base/thread.h"
 
31
#include "talk/base/timeutils.h"
 
32
 
 
33
namespace talk_base {
 
34
 
 
35
TEST(TimeTest, TimeInMs) {
 
36
  uint32 ts_earlier = Time();
 
37
  Thread::SleepMs(100);
 
38
  uint32 ts_now = Time();
 
39
  // Allow for the thread to wakeup ~20ms early.
 
40
  EXPECT_GE(ts_now, ts_earlier + 80);
 
41
  // Make sure the Time is not returning in smaller unit like microseconds.
 
42
  EXPECT_LT(ts_now, ts_earlier + 1000);
 
43
}
 
44
 
 
45
TEST(TimeTest, Comparison) {
 
46
  // Obtain two different times, in known order
 
47
  TimeStamp ts_earlier = Time();
 
48
  Thread::SleepMs(100);
 
49
  TimeStamp ts_now = Time();
 
50
  EXPECT_NE(ts_earlier, ts_now);
 
51
 
 
52
  // Common comparisons
 
53
  EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_now));
 
54
  EXPECT_TRUE( TimeIsLater(       ts_earlier, ts_now));
 
55
  EXPECT_FALSE(TimeIsLaterOrEqual(ts_now,     ts_earlier));
 
56
  EXPECT_FALSE(TimeIsLater(       ts_now,     ts_earlier));
 
57
 
 
58
  // Edge cases
 
59
  EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_earlier));
 
60
  EXPECT_FALSE(TimeIsLater(       ts_earlier, ts_earlier));
 
61
 
 
62
  // Obtain a third time
 
63
  TimeStamp ts_later = TimeAfter(100);
 
64
  EXPECT_NE(ts_now, ts_later);
 
65
  EXPECT_TRUE( TimeIsLater(ts_now,     ts_later));
 
66
  EXPECT_TRUE( TimeIsLater(ts_earlier, ts_later));
 
67
 
 
68
  // Common comparisons
 
69
  EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_now,     ts_later));
 
70
  EXPECT_FALSE(TimeIsBetween(ts_earlier, ts_later,   ts_now));
 
71
  EXPECT_FALSE(TimeIsBetween(ts_now,     ts_earlier, ts_later));
 
72
  EXPECT_TRUE( TimeIsBetween(ts_now,     ts_later,   ts_earlier));
 
73
  EXPECT_TRUE( TimeIsBetween(ts_later,   ts_earlier, ts_now));
 
74
  EXPECT_FALSE(TimeIsBetween(ts_later,   ts_now,     ts_earlier));
 
75
 
 
76
  // Edge cases
 
77
  EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_earlier));
 
78
  EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_later));
 
79
  EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_later,   ts_later));
 
80
 
 
81
  // Earlier of two times
 
82
  EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier));
 
83
  EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_now));
 
84
  EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later));
 
85
  EXPECT_EQ(ts_earlier, TimeMin(ts_now,     ts_earlier));
 
86
  EXPECT_EQ(ts_earlier, TimeMin(ts_later,   ts_earlier));
 
87
 
 
88
  // Later of two times
 
89
  EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier));
 
90
  EXPECT_EQ(ts_now,     TimeMax(ts_earlier, ts_now));
 
91
  EXPECT_EQ(ts_later,   TimeMax(ts_earlier, ts_later));
 
92
  EXPECT_EQ(ts_now,     TimeMax(ts_now,     ts_earlier));
 
93
  EXPECT_EQ(ts_later,   TimeMax(ts_later,   ts_earlier));
 
94
}
 
95
 
 
96
TEST(TimeTest, Intervals) {
 
97
  TimeStamp ts_earlier = Time();
 
98
  TimeStamp ts_later = TimeAfter(500);
 
99
 
 
100
  // We can't depend on ts_later and ts_earlier to be exactly 500 apart
 
101
  // since time elapses between the calls to Time() and TimeAfter(500)
 
102
  EXPECT_LE(500,  TimeDiff(ts_later, ts_earlier));
 
103
  EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later));
 
104
 
 
105
  // Time has elapsed since ts_earlier
 
106
  EXPECT_GE(TimeSince(ts_earlier), 0);
 
107
 
 
108
  // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve
 
109
  EXPECT_LE(TimeUntil(ts_earlier), 0);
 
110
 
 
111
  // ts_later likely hasn't happened yet, so TimeSince could be -ve
 
112
  // but within 500
 
113
  EXPECT_GE(TimeSince(ts_later), -500);
 
114
 
 
115
  // TimeUntil ts_later is at most 500
 
116
  EXPECT_LE(TimeUntil(ts_later), 500);
 
117
}
 
118
 
 
119
TEST(TimeTest, BoundaryComparison) {
 
120
  // Obtain two different times, in known order
 
121
  TimeStamp ts_earlier = static_cast<TimeStamp>(-50);
 
122
  TimeStamp ts_later = ts_earlier + 100;
 
123
  EXPECT_NE(ts_earlier, ts_later);
 
124
 
 
125
  // Common comparisons
 
126
  EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_later));
 
127
  EXPECT_TRUE( TimeIsLater(       ts_earlier, ts_later));
 
128
  EXPECT_FALSE(TimeIsLaterOrEqual(ts_later,   ts_earlier));
 
129
  EXPECT_FALSE(TimeIsLater(       ts_later,   ts_earlier));
 
130
 
 
131
  // Earlier of two times
 
132
  EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier));
 
133
  EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later));
 
134
  EXPECT_EQ(ts_earlier, TimeMin(ts_later,   ts_earlier));
 
135
 
 
136
  // Later of two times
 
137
  EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier));
 
138
  EXPECT_EQ(ts_later,   TimeMax(ts_earlier, ts_later));
 
139
  EXPECT_EQ(ts_later,   TimeMax(ts_later,   ts_earlier));
 
140
 
 
141
  // Interval
 
142
  EXPECT_EQ(100,  TimeDiff(ts_later, ts_earlier));
 
143
  EXPECT_EQ(-100, TimeDiff(ts_earlier, ts_later));
 
144
}
 
145
 
 
146
} // namespace talk_base