- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / base / running_average_unittest.cc
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 "remoting/base/running_average.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace remoting {
9
10 static const int64 kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 };
11
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());
16
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());
20   }
21 }
22
23 // Average the two most recent elements.
24 TEST(RunningAverageTest, TwoElementWindow) {
25   RunningAverage running_average(2);
26   EXPECT_EQ(0, running_average.Average());
27
28   for (size_t i = 0; i < arraysize(kTestValues); ++i) {
29     running_average.Record(kTestValues[i]);
30
31     double expected = kTestValues[i];
32     if (i > 0)
33       expected = (expected + kTestValues[i-1]) / 2;
34
35     EXPECT_EQ(expected, running_average.Average());
36   }
37 }
38
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());
43
44   for (size_t i = 0; i < arraysize(kTestValues); ++i) {
45     running_average.Record(kTestValues[i]);
46
47     double expected = 0.0;
48     for (size_t j = 0; j <= i; ++j)
49       expected += kTestValues[j];
50     expected /= i + 1;
51
52     EXPECT_EQ(expected, running_average.Average());
53   }
54 }
55
56 }  // namespace remoting