Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / ratetracker_unittest.cc
1 /*
2  * libjingle
3  * Copyright 2010, 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/gunit.h"
29 #include "talk/base/ratetracker.h"
30
31 namespace talk_base {
32
33 class RateTrackerForTest : public RateTracker {
34  public:
35   RateTrackerForTest() : time_(0) {}
36   virtual uint32 Time() const { return time_; }
37   void AdvanceTime(uint32 delta) { time_ += delta; }
38
39  private:
40   uint32 time_;
41 };
42
43 TEST(RateTrackerTest, TestBasics) {
44   RateTrackerForTest tracker;
45   EXPECT_EQ(0U, tracker.total_units());
46   EXPECT_EQ(0U, tracker.units_second());
47
48   // Add a sample.
49   tracker.Update(1234);
50   // Advance the clock by 100 ms.
51   tracker.AdvanceTime(100);
52   // total_units should advance, but units_second should stay 0.
53   EXPECT_EQ(1234U, tracker.total_units());
54   EXPECT_EQ(0U, tracker.units_second());
55
56   // Repeat.
57   tracker.Update(1234);
58   tracker.AdvanceTime(100);
59   EXPECT_EQ(1234U * 2, tracker.total_units());
60   EXPECT_EQ(0U, tracker.units_second());
61
62   // Advance the clock by 800 ms, so we've elapsed a full second.
63   // units_second should now be filled in properly.
64   tracker.AdvanceTime(800);
65   EXPECT_EQ(1234U * 2, tracker.total_units());
66   EXPECT_EQ(1234U * 2, tracker.units_second());
67
68   // Poll the tracker again immediately. The reported rate should stay the same.
69   EXPECT_EQ(1234U * 2, tracker.total_units());
70   EXPECT_EQ(1234U * 2, tracker.units_second());
71
72   // Do nothing and advance by a second. We should drop down to zero.
73   tracker.AdvanceTime(1000);
74   EXPECT_EQ(1234U * 2, tracker.total_units());
75   EXPECT_EQ(0U, tracker.units_second());
76
77   // Send a bunch of data at a constant rate for 5.5 "seconds".
78   // We should report the rate properly.
79   for (int i = 0; i < 5500; i += 100) {
80     tracker.Update(9876U);
81     tracker.AdvanceTime(100);
82   }
83   EXPECT_EQ(9876U * 10, tracker.units_second());
84
85   // Advance the clock by 500 ms. Since we sent nothing over this half-second,
86   // the reported rate should be reduced by half.
87   tracker.AdvanceTime(500);
88   EXPECT_EQ(9876U * 5, tracker.units_second());
89 }
90
91 }  // namespace talk_base