• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "talk/base/gunit.h"
29 #include "talk/base/rollingaccumulator.h"
30 
31 namespace talk_base {
32 
33 namespace {
34 
35 const double kLearningRate = 0.5;
36 
37 }  // namespace
38 
TEST(RollingAccumulatorTest,ZeroSamples)39 TEST(RollingAccumulatorTest, ZeroSamples) {
40   RollingAccumulator<int> accum(10);
41 
42   EXPECT_EQ(0U, accum.count());
43   EXPECT_EQ(0, accum.ComputeMean());
44   EXPECT_EQ(0, accum.ComputeVariance());
45 }
46 
TEST(RollingAccumulatorTest,SomeSamples)47 TEST(RollingAccumulatorTest, SomeSamples) {
48   RollingAccumulator<int> accum(10);
49   for (int i = 0; i < 4; ++i) {
50     accum.AddSample(i);
51   }
52 
53   EXPECT_EQ(4U, accum.count());
54   EXPECT_EQ(6, accum.ComputeSum());
55   EXPECT_EQ(1, accum.ComputeMean());
56   EXPECT_EQ(2, accum.ComputeWeightedMean(kLearningRate));
57   EXPECT_EQ(1, accum.ComputeVariance());
58 }
59 
TEST(RollingAccumulatorTest,RollingSamples)60 TEST(RollingAccumulatorTest, RollingSamples) {
61   RollingAccumulator<int> accum(10);
62   for (int i = 0; i < 12; ++i) {
63     accum.AddSample(i);
64   }
65 
66   EXPECT_EQ(10U, accum.count());
67   EXPECT_EQ(65, accum.ComputeSum());
68   EXPECT_EQ(6, accum.ComputeMean());
69   EXPECT_EQ(10, accum.ComputeWeightedMean(kLearningRate));
70   EXPECT_NEAR(9, accum.ComputeVariance(), 1);
71 }
72 
TEST(RollingAccumulatorTest,RollingSamplesDouble)73 TEST(RollingAccumulatorTest, RollingSamplesDouble) {
74   RollingAccumulator<double> accum(10);
75   for (int i = 0; i < 23; ++i) {
76     accum.AddSample(5 * i);
77   }
78 
79   EXPECT_EQ(10u, accum.count());
80   EXPECT_DOUBLE_EQ(875.0, accum.ComputeSum());
81   EXPECT_DOUBLE_EQ(87.5, accum.ComputeMean());
82   EXPECT_NEAR(105.049, accum.ComputeWeightedMean(kLearningRate), 0.1);
83   EXPECT_NEAR(229.166667, accum.ComputeVariance(), 25);
84 }
85 
TEST(RollingAccumulatorTest,ComputeWeightedMeanCornerCases)86 TEST(RollingAccumulatorTest, ComputeWeightedMeanCornerCases) {
87   RollingAccumulator<int> accum(10);
88   EXPECT_EQ(0, accum.ComputeWeightedMean(kLearningRate));
89   EXPECT_EQ(0, accum.ComputeWeightedMean(0.0));
90   EXPECT_EQ(0, accum.ComputeWeightedMean(1.1));
91 
92   for (int i = 0; i < 8; ++i) {
93     accum.AddSample(i);
94   }
95 
96   EXPECT_EQ(3, accum.ComputeMean());
97   EXPECT_EQ(3, accum.ComputeWeightedMean(0));
98   EXPECT_EQ(3, accum.ComputeWeightedMean(1.1));
99   EXPECT_EQ(6, accum.ComputeWeightedMean(kLearningRate));
100 }
101 
102 }  // namespace talk_base
103