| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "remoting/base/running_average.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace remoting { | |
| 13 | |
| 14 static const int64_t kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 }; | |
| 15 | |
| 16 // Average across a single element, i.e. just return the most recent. | |
| 17 TEST(RunningAverageTest, OneElementWindow) { | |
| 18 RunningAverage running_average(1); | |
| 19 EXPECT_EQ(0, running_average.Average()); | |
| 20 | |
| 21 for (size_t i = 0; i < arraysize(kTestValues); ++i) { | |
| 22 running_average.Record(kTestValues[i]); | |
| 23 EXPECT_EQ(static_cast<double>(kTestValues[i]), running_average.Average()); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 // Average the two most recent elements. | |
| 28 TEST(RunningAverageTest, TwoElementWindow) { | |
| 29 RunningAverage running_average(2); | |
| 30 EXPECT_EQ(0, running_average.Average()); | |
| 31 | |
| 32 for (size_t i = 0; i < arraysize(kTestValues); ++i) { | |
| 33 running_average.Record(kTestValues[i]); | |
| 34 | |
| 35 double expected = kTestValues[i]; | |
| 36 if (i > 0) | |
| 37 expected = (expected + kTestValues[i-1]) / 2; | |
| 38 | |
| 39 EXPECT_EQ(expected, running_average.Average()); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Average across all the elements if the window size exceeds the element count. | |
| 44 TEST(RunningAverageTest, LongWindow) { | |
| 45 RunningAverage running_average(arraysize(kTestValues) + 1); | |
| 46 EXPECT_EQ(0, running_average.Average()); | |
| 47 | |
| 48 for (size_t i = 0; i < arraysize(kTestValues); ++i) { | |
| 49 running_average.Record(kTestValues[i]); | |
| 50 | |
| 51 double expected = 0.0; | |
| 52 for (size_t j = 0; j <= i; ++j) | |
| 53 expected += kTestValues[j]; | |
| 54 expected /= i + 1; | |
| 55 | |
| 56 EXPECT_EQ(expected, running_average.Average()); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 } // namespace remoting | |
| OLD | NEW |