Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / bandwidthsmoother_unittest.cc
1 /*
2  * libjingle
3  * Copyright 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 <limits.h>
29
30 #include "talk/base/bandwidthsmoother.h"
31 #include "talk/base/gunit.h"
32
33 namespace talk_base {
34
35 static const int kTimeBetweenIncrease = 10;
36 static const double kPercentIncrease = 1.1;
37 static const size_t kSamplesCountToAverage = 2;
38 static const double kMinSampleCountPercent = 1.0;
39
40 TEST(BandwidthSmootherTest, TestSampleIncrease) {
41   BandwidthSmoother mon(1000,  // initial_bandwidth_guess
42                         kTimeBetweenIncrease,
43                         kPercentIncrease,
44                         kSamplesCountToAverage,
45                         kMinSampleCountPercent);
46
47   int bandwidth_sample = 1000;
48   EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation());
49   bandwidth_sample =
50       static_cast<int>(bandwidth_sample * kPercentIncrease);
51   EXPECT_FALSE(mon.Sample(9, bandwidth_sample));
52   EXPECT_TRUE(mon.Sample(10, bandwidth_sample));
53   EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation());
54   int next_expected_est =
55       static_cast<int>(bandwidth_sample * kPercentIncrease);
56   bandwidth_sample *= 2;
57   EXPECT_TRUE(mon.Sample(20, bandwidth_sample));
58   EXPECT_EQ(next_expected_est, mon.get_bandwidth_estimation());
59 }
60
61 TEST(BandwidthSmootherTest, TestSampleIncreaseFromZero) {
62   BandwidthSmoother mon(0,  // initial_bandwidth_guess
63                         kTimeBetweenIncrease,
64                         kPercentIncrease,
65                         kSamplesCountToAverage,
66                         kMinSampleCountPercent);
67
68   const int kBandwidthSample = 1000;
69   EXPECT_EQ(0, mon.get_bandwidth_estimation());
70   EXPECT_FALSE(mon.Sample(9, kBandwidthSample));
71   EXPECT_TRUE(mon.Sample(10, kBandwidthSample));
72   EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
73 }
74
75 TEST(BandwidthSmootherTest, TestSampleDecrease) {
76   BandwidthSmoother mon(1000,  // initial_bandwidth_guess
77                         kTimeBetweenIncrease,
78                         kPercentIncrease,
79                         kSamplesCountToAverage,
80                         kMinSampleCountPercent);
81
82   const int kBandwidthSample = 999;
83   EXPECT_EQ(1000, mon.get_bandwidth_estimation());
84   EXPECT_FALSE(mon.Sample(1, kBandwidthSample));
85   EXPECT_EQ(1000, mon.get_bandwidth_estimation());
86   EXPECT_TRUE(mon.Sample(2, kBandwidthSample));
87   EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
88 }
89
90 TEST(BandwidthSmootherTest, TestSampleTooFewSamples) {
91   BandwidthSmoother mon(1000,  // initial_bandwidth_guess
92                         kTimeBetweenIncrease,
93                         kPercentIncrease,
94                         10,  // 10 samples.
95                         0.5);  // 5 min samples.
96
97   const int kBandwidthSample = 500;
98   EXPECT_EQ(1000, mon.get_bandwidth_estimation());
99   EXPECT_FALSE(mon.Sample(1, kBandwidthSample));
100   EXPECT_FALSE(mon.Sample(2, kBandwidthSample));
101   EXPECT_FALSE(mon.Sample(3, kBandwidthSample));
102   EXPECT_FALSE(mon.Sample(4, kBandwidthSample));
103   EXPECT_EQ(1000, mon.get_bandwidth_estimation());
104   EXPECT_TRUE(mon.Sample(5, kBandwidthSample));
105   EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
106 }
107
108 TEST(BandwidthSmootherTest, TestSampleRollover) {
109   const int kHugeBandwidth = 2000000000;  // > INT_MAX/1.1
110   BandwidthSmoother mon(kHugeBandwidth,
111                         kTimeBetweenIncrease,
112                         kPercentIncrease,
113                         kSamplesCountToAverage,
114                         kMinSampleCountPercent);
115
116   EXPECT_FALSE(mon.Sample(10, INT_MAX));
117   EXPECT_FALSE(mon.Sample(11, INT_MAX));
118   EXPECT_EQ(kHugeBandwidth, mon.get_bandwidth_estimation());
119 }
120
121 TEST(BandwidthSmootherTest, TestSampleNegative) {
122   BandwidthSmoother mon(1000,  // initial_bandwidth_guess
123                         kTimeBetweenIncrease,
124                         kPercentIncrease,
125                         kSamplesCountToAverage,
126                         kMinSampleCountPercent);
127
128   EXPECT_FALSE(mon.Sample(10, -1));
129   EXPECT_FALSE(mon.Sample(11, -1));
130   EXPECT_EQ(1000, mon.get_bandwidth_estimation());
131 }
132
133 }  // namespace talk_base