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.
5 #include "remoting/base/running_average.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 static const int64 kTestValues
[] = { 10, 20, 30, 10, 25, 16, 15 };
12 // Average across a single element, i.e. just return the most recent.
13 TEST(RunningAverageTest
, OneElementWindow
) {
14 RunningAverage
running_average(1);
15 EXPECT_EQ(0, running_average
.Average());
17 for (size_t i
= 0; i
< arraysize(kTestValues
); ++i
) {
18 running_average
.Record(kTestValues
[i
]);
19 EXPECT_EQ(static_cast<double>(kTestValues
[i
]), running_average
.Average());
23 // Average the two most recent elements.
24 TEST(RunningAverageTest
, TwoElementWindow
) {
25 RunningAverage
running_average(2);
26 EXPECT_EQ(0, running_average
.Average());
28 for (size_t i
= 0; i
< arraysize(kTestValues
); ++i
) {
29 running_average
.Record(kTestValues
[i
]);
31 double expected
= kTestValues
[i
];
33 expected
= (expected
+ kTestValues
[i
-1]) / 2;
35 EXPECT_EQ(expected
, running_average
.Average());
39 // Average across all the elements if the window size exceeds the element count.
40 TEST(RunningAverageTest
, LongWindow
) {
41 RunningAverage
running_average(arraysize(kTestValues
) + 1);
42 EXPECT_EQ(0, running_average
.Average());
44 for (size_t i
= 0; i
< arraysize(kTestValues
); ++i
) {
45 running_average
.Record(kTestValues
[i
]);
47 double expected
= 0.0;
48 for (size_t j
= 0; j
<= i
; ++j
)
49 expected
+= kTestValues
[j
];
52 EXPECT_EQ(expected
, running_average
.Average());
56 } // namespace remoting