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 39 TEST(RollingAccumulatorTest, ZeroSamples) { 40 RollingAccumulator<int> accum(10); 41 42 EXPECT_EQ(0U, accum.count()); 43 EXPECT_DOUBLE_EQ(0.0, accum.ComputeMean()); 44 EXPECT_DOUBLE_EQ(0.0, accum.ComputeVariance()); 45 EXPECT_EQ(0, accum.ComputeMin()); 46 EXPECT_EQ(0, accum.ComputeMax()); 47 } 48 49 TEST(RollingAccumulatorTest, SomeSamples) { 50 RollingAccumulator<int> accum(10); 51 for (int i = 0; i < 4; ++i) { 52 accum.AddSample(i); 53 } 54 55 EXPECT_EQ(4U, accum.count()); 56 EXPECT_EQ(6, accum.ComputeSum()); 57 EXPECT_DOUBLE_EQ(1.5, accum.ComputeMean()); 58 EXPECT_NEAR(2.26666, accum.ComputeWeightedMean(kLearningRate), 0.01); 59 EXPECT_DOUBLE_EQ(1.25, accum.ComputeVariance()); 60 EXPECT_EQ(0, accum.ComputeMin()); 61 EXPECT_EQ(3, accum.ComputeMax()); 62 } 63 64 TEST(RollingAccumulatorTest, RollingSamples) { 65 RollingAccumulator<int> accum(10); 66 for (int i = 0; i < 12; ++i) { 67 accum.AddSample(i); 68 } 69 70 EXPECT_EQ(10U, accum.count()); 71 EXPECT_EQ(65, accum.ComputeSum()); 72 EXPECT_DOUBLE_EQ(6.5, accum.ComputeMean()); 73 EXPECT_NEAR(10.0, accum.ComputeWeightedMean(kLearningRate), 0.01); 74 EXPECT_NEAR(9.0, accum.ComputeVariance(), 1.0); 75 EXPECT_EQ(2, accum.ComputeMin()); 76 EXPECT_EQ(11, accum.ComputeMax()); 77 } 78 79 TEST(RollingAccumulatorTest, ResetSamples) { 80 RollingAccumulator<int> accum(10); 81 82 for (int i = 0; i < 10; ++i) { 83 accum.AddSample(100); 84 } 85 EXPECT_EQ(10U, accum.count()); 86 EXPECT_DOUBLE_EQ(100.0, accum.ComputeMean()); 87 EXPECT_EQ(100, accum.ComputeMin()); 88 EXPECT_EQ(100, accum.ComputeMax()); 89 90 accum.Reset(); 91 EXPECT_EQ(0U, accum.count()); 92 93 for (int i = 0; i < 5; ++i) { 94 accum.AddSample(i); 95 } 96 97 EXPECT_EQ(5U, accum.count()); 98 EXPECT_EQ(10, accum.ComputeSum()); 99 EXPECT_DOUBLE_EQ(2.0, accum.ComputeMean()); 100 EXPECT_EQ(0, accum.ComputeMin()); 101 EXPECT_EQ(4, accum.ComputeMax()); 102 } 103 104 TEST(RollingAccumulatorTest, RollingSamplesDouble) { 105 RollingAccumulator<double> accum(10); 106 for (int i = 0; i < 23; ++i) { 107 accum.AddSample(5 * i); 108 } 109 110 EXPECT_EQ(10u, accum.count()); 111 EXPECT_DOUBLE_EQ(875.0, accum.ComputeSum()); 112 EXPECT_DOUBLE_EQ(87.5, accum.ComputeMean()); 113 EXPECT_NEAR(105.049, accum.ComputeWeightedMean(kLearningRate), 0.1); 114 EXPECT_NEAR(229.166667, accum.ComputeVariance(), 25); 115 EXPECT_DOUBLE_EQ(65.0, accum.ComputeMin()); 116 EXPECT_DOUBLE_EQ(110.0, accum.ComputeMax()); 117 } 118 119 TEST(RollingAccumulatorTest, ComputeWeightedMeanCornerCases) { 120 RollingAccumulator<int> accum(10); 121 EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(kLearningRate)); 122 EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(0.0)); 123 EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(1.1)); 124 125 for (int i = 0; i < 8; ++i) { 126 accum.AddSample(i); 127 } 128 129 EXPECT_DOUBLE_EQ(3.5, accum.ComputeMean()); 130 EXPECT_DOUBLE_EQ(3.5, accum.ComputeWeightedMean(0)); 131 EXPECT_DOUBLE_EQ(3.5, accum.ComputeWeightedMean(1.1)); 132 EXPECT_NEAR(6.0, accum.ComputeWeightedMean(kLearningRate), 0.1); 133 } 134 135 } // namespace talk_base 136