• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 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 "api/neteq/tick_timer.h"
12 
13 #include <memory>
14 
15 #include "test/gmock.h"
16 #include "test/gtest.h"
17 
18 namespace webrtc {
19 
20 // Verify that the default value for ms_per_tick is 10.
TEST(TickTimer,DefaultMsPerTick)21 TEST(TickTimer, DefaultMsPerTick) {
22   TickTimer tt;
23   EXPECT_EQ(10, tt.ms_per_tick());
24 }
25 
TEST(TickTimer,CustomMsPerTick)26 TEST(TickTimer, CustomMsPerTick) {
27   TickTimer tt(17);
28   EXPECT_EQ(17, tt.ms_per_tick());
29 }
30 
TEST(TickTimer,Increment)31 TEST(TickTimer, Increment) {
32   TickTimer tt;
33   EXPECT_EQ(0u, tt.ticks());
34   tt.Increment();
35   EXPECT_EQ(1u, tt.ticks());
36 
37   for (int i = 0; i < 17; ++i) {
38     tt.Increment();
39   }
40   EXPECT_EQ(18u, tt.ticks());
41 
42   tt.Increment(17);
43   EXPECT_EQ(35u, tt.ticks());
44 }
45 
TEST(TickTimer,WrapAround)46 TEST(TickTimer, WrapAround) {
47   TickTimer tt;
48   tt.Increment(UINT64_MAX);
49   EXPECT_EQ(UINT64_MAX, tt.ticks());
50   tt.Increment();
51   EXPECT_EQ(0u, tt.ticks());
52 }
53 
TEST(TickTimer,Stopwatch)54 TEST(TickTimer, Stopwatch) {
55   TickTimer tt;
56   // Increment it a "random" number of steps.
57   tt.Increment(17);
58 
59   std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
60   ASSERT_TRUE(sw);
61 
62   EXPECT_EQ(0u, sw->ElapsedTicks());  // Starts at zero.
63   EXPECT_EQ(0u, sw->ElapsedMs());
64   tt.Increment();
65   EXPECT_EQ(1u, sw->ElapsedTicks());  // Increases with the TickTimer.
66   EXPECT_EQ(10u, sw->ElapsedMs());
67 }
68 
TEST(TickTimer,StopwatchWrapAround)69 TEST(TickTimer, StopwatchWrapAround) {
70   TickTimer tt;
71   tt.Increment(UINT64_MAX);
72 
73   std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
74   ASSERT_TRUE(sw);
75 
76   tt.Increment();
77   EXPECT_EQ(0u, tt.ticks());
78   EXPECT_EQ(1u, sw->ElapsedTicks());
79   EXPECT_EQ(10u, sw->ElapsedMs());
80 
81   tt.Increment();
82   EXPECT_EQ(1u, tt.ticks());
83   EXPECT_EQ(2u, sw->ElapsedTicks());
84   EXPECT_EQ(20u, sw->ElapsedMs());
85 }
86 
TEST(TickTimer,StopwatchMsOverflow)87 TEST(TickTimer, StopwatchMsOverflow) {
88   TickTimer tt;
89   std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
90   ASSERT_TRUE(sw);
91 
92   tt.Increment(UINT64_MAX / 10);
93   EXPECT_EQ(UINT64_MAX, sw->ElapsedMs());
94 
95   tt.Increment();
96   EXPECT_EQ(UINT64_MAX, sw->ElapsedMs());
97 
98   tt.Increment(UINT64_MAX - tt.ticks());
99   EXPECT_EQ(UINT64_MAX, tt.ticks());
100   EXPECT_EQ(UINT64_MAX, sw->ElapsedMs());
101 }
102 
TEST(TickTimer,StopwatchWithCustomTicktime)103 TEST(TickTimer, StopwatchWithCustomTicktime) {
104   const int kMsPerTick = 17;
105   TickTimer tt(kMsPerTick);
106   std::unique_ptr<TickTimer::Stopwatch> sw = tt.GetNewStopwatch();
107   ASSERT_TRUE(sw);
108 
109   EXPECT_EQ(0u, sw->ElapsedMs());
110   tt.Increment();
111   EXPECT_EQ(static_cast<uint64_t>(kMsPerTick), sw->ElapsedMs());
112 }
113 
TEST(TickTimer,Countdown)114 TEST(TickTimer, Countdown) {
115   TickTimer tt;
116   // Increment it a "random" number of steps.
117   tt.Increment(4711);
118 
119   std::unique_ptr<TickTimer::Countdown> cd = tt.GetNewCountdown(17);
120   ASSERT_TRUE(cd);
121 
122   EXPECT_FALSE(cd->Finished());
123   tt.Increment();
124   EXPECT_FALSE(cd->Finished());
125 
126   tt.Increment(16);  // Total increment is now 17.
127   EXPECT_TRUE(cd->Finished());
128 
129   // Further increments do not change the state.
130   tt.Increment();
131   EXPECT_TRUE(cd->Finished());
132   tt.Increment(1234);
133   EXPECT_TRUE(cd->Finished());
134 }
135 }  // namespace webrtc
136