Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / remoting / base / rate_counter_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/rate_counter.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 // One second window and one sample per second, so rate equals each sample.
13 TEST(RateCounterTest, OneSecondWindow) {
14   RateCounter rate_counter(base::TimeDelta::FromSeconds(1));
15   EXPECT_EQ(0, rate_counter.Rate());
16
17   base::Time now = base::Time::Now();
18   for (size_t i = 0; i < arraysize(kTestValues); ++i) {
19     now += base::TimeDelta::FromSeconds(1);
20     rate_counter.SetCurrentTimeForTest(now);
21     rate_counter.Record(kTestValues[i]);
22     EXPECT_EQ(static_cast<double>(kTestValues[i]), rate_counter.Rate());
23   }
24 }
25
26 // Record all samples instantaneously, so the rate is the total of the samples.
27 TEST(RateCounterTest, OneSecondWindowAllSamples) {
28   RateCounter rate_counter(base::TimeDelta::FromSeconds(1));
29   EXPECT_EQ(0, rate_counter.Rate());
30
31   rate_counter.SetCurrentTimeForTest(base::Time::Now());
32
33   double expected = 0.0;
34   for (size_t i = 0; i < arraysize(kTestValues); ++i) {
35     rate_counter.Record(kTestValues[i]);
36     expected += kTestValues[i];
37   }
38
39   EXPECT_EQ(expected, rate_counter.Rate());
40 }
41
42 // Two second window, one sample per second.  For all but the first sample, the
43 // rate should be the average of it and the preceding one.  For the first it
44 // will be the average of the sample with zero.
45 TEST(RateCounterTest, TwoSecondWindow) {
46   RateCounter rate_counter(base::TimeDelta::FromSeconds(2));
47   EXPECT_EQ(0, rate_counter.Rate());
48
49   base::Time now = base::Time::Now();
50   for (size_t i = 0; i < arraysize(kTestValues); ++i) {
51     now += base::TimeDelta::FromSeconds(1);
52     rate_counter.SetCurrentTimeForTest(now);
53     rate_counter.Record(kTestValues[i]);
54     double expected = kTestValues[i];
55     if (i > 0)
56       expected += kTestValues[i-1];
57     expected /= 2;
58     EXPECT_EQ(expected, rate_counter.Rate());
59   }
60 }
61
62 // Sample over a window one second shorter than the number of samples.
63 // Rate should be the average of all but the first sample.
64 TEST(RateCounterTest, LongWindow) {
65   const size_t kWindowSeconds = arraysize(kTestValues) - 1;
66
67   RateCounter rate_counter(base::TimeDelta::FromSeconds(kWindowSeconds));
68   EXPECT_EQ(0, rate_counter.Rate());
69
70   double expected = 0.0;
71   base::Time now = base::Time::Now();
72   for (size_t i = 0; i < arraysize(kTestValues); ++i) {
73     now += base::TimeDelta::FromSeconds(1);
74     rate_counter.SetCurrentTimeForTest(now);
75     rate_counter.Record(kTestValues[i]);
76     if (i != 0)
77       expected += kTestValues[i];
78   }
79   expected /= kWindowSeconds;
80
81   EXPECT_EQ(expected, rate_counter.Rate());
82 }
83
84 }  // namespace remoting