1 /*
2  *  Copyright 2011 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 "webrtc/base/gunit.h"
12 #include "webrtc/base/rollingaccumulator.h"
13 
14 namespace rtc {
15 
16 namespace {
17 
18 const double kLearningRate = 0.5;
19 
20 }  // namespace
21 
TEST(RollingAccumulatorTest,ZeroSamples)22 TEST(RollingAccumulatorTest, ZeroSamples) {
23   RollingAccumulator<int> accum(10);
24 
25   EXPECT_EQ(0U, accum.count());
26   EXPECT_DOUBLE_EQ(0.0, accum.ComputeMean());
27   EXPECT_DOUBLE_EQ(0.0, accum.ComputeVariance());
28   EXPECT_EQ(0, accum.ComputeMin());
29   EXPECT_EQ(0, accum.ComputeMax());
30 }
31 
TEST(RollingAccumulatorTest,SomeSamples)32 TEST(RollingAccumulatorTest, SomeSamples) {
33   RollingAccumulator<int> accum(10);
34   for (int i = 0; i < 4; ++i) {
35     accum.AddSample(i);
36   }
37 
38   EXPECT_EQ(4U, accum.count());
39   EXPECT_EQ(6, accum.ComputeSum());
40   EXPECT_DOUBLE_EQ(1.5, accum.ComputeMean());
41   EXPECT_NEAR(2.26666, accum.ComputeWeightedMean(kLearningRate), 0.01);
42   EXPECT_DOUBLE_EQ(1.25, accum.ComputeVariance());
43   EXPECT_EQ(0, accum.ComputeMin());
44   EXPECT_EQ(3, accum.ComputeMax());
45 }
46 
TEST(RollingAccumulatorTest,RollingSamples)47 TEST(RollingAccumulatorTest, RollingSamples) {
48   RollingAccumulator<int> accum(10);
49   for (int i = 0; i < 12; ++i) {
50     accum.AddSample(i);
51   }
52 
53   EXPECT_EQ(10U, accum.count());
54   EXPECT_EQ(65, accum.ComputeSum());
55   EXPECT_DOUBLE_EQ(6.5, accum.ComputeMean());
56   EXPECT_NEAR(10.0, accum.ComputeWeightedMean(kLearningRate), 0.01);
57   EXPECT_NEAR(9.0, accum.ComputeVariance(), 1.0);
58   EXPECT_EQ(2, accum.ComputeMin());
59   EXPECT_EQ(11, accum.ComputeMax());
60 }
61 
TEST(RollingAccumulatorTest,ResetSamples)62 TEST(RollingAccumulatorTest, ResetSamples) {
63   RollingAccumulator<int> accum(10);
64 
65   for (int i = 0; i < 10; ++i) {
66     accum.AddSample(100);
67   }
68   EXPECT_EQ(10U, accum.count());
69   EXPECT_DOUBLE_EQ(100.0, accum.ComputeMean());
70   EXPECT_EQ(100, accum.ComputeMin());
71   EXPECT_EQ(100, accum.ComputeMax());
72 
73   accum.Reset();
74   EXPECT_EQ(0U, accum.count());
75 
76   for (int i = 0; i < 5; ++i) {
77     accum.AddSample(i);
78   }
79 
80   EXPECT_EQ(5U, accum.count());
81   EXPECT_EQ(10, accum.ComputeSum());
82   EXPECT_DOUBLE_EQ(2.0, accum.ComputeMean());
83   EXPECT_EQ(0, accum.ComputeMin());
84   EXPECT_EQ(4, accum.ComputeMax());
85 }
86 
TEST(RollingAccumulatorTest,RollingSamplesDouble)87 TEST(RollingAccumulatorTest, RollingSamplesDouble) {
88   RollingAccumulator<double> accum(10);
89   for (int i = 0; i < 23; ++i) {
90     accum.AddSample(5 * i);
91   }
92 
93   EXPECT_EQ(10u, accum.count());
94   EXPECT_DOUBLE_EQ(875.0, accum.ComputeSum());
95   EXPECT_DOUBLE_EQ(87.5, accum.ComputeMean());
96   EXPECT_NEAR(105.049, accum.ComputeWeightedMean(kLearningRate), 0.1);
97   EXPECT_NEAR(229.166667, accum.ComputeVariance(), 25);
98   EXPECT_DOUBLE_EQ(65.0, accum.ComputeMin());
99   EXPECT_DOUBLE_EQ(110.0, accum.ComputeMax());
100 }
101 
TEST(RollingAccumulatorTest,ComputeWeightedMeanCornerCases)102 TEST(RollingAccumulatorTest, ComputeWeightedMeanCornerCases) {
103   RollingAccumulator<int> accum(10);
104   EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(kLearningRate));
105   EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(0.0));
106   EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(1.1));
107 
108   for (int i = 0; i < 8; ++i) {
109     accum.AddSample(i);
110   }
111 
112   EXPECT_DOUBLE_EQ(3.5, accum.ComputeMean());
113   EXPECT_DOUBLE_EQ(3.5, accum.ComputeWeightedMean(0));
114   EXPECT_DOUBLE_EQ(3.5, accum.ComputeWeightedMean(1.1));
115   EXPECT_NEAR(6.0, accum.ComputeWeightedMean(kLearningRate), 0.1);
116 }
117 
118 }  // namespace rtc
119