Index: remoting/base/rate_counter_unittest.cc |
diff --git a/remoting/base/rate_counter_unittest.cc b/remoting/base/rate_counter_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f1ccd76d2e9f824806945e16bd7cb0b020364701 |
--- /dev/null |
+++ b/remoting/base/rate_counter_unittest.cc |
@@ -0,0 +1,58 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/base/rate_counter.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace remoting { |
+ |
+static const int64 kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 }; |
+ |
+TEST(RateCounterTest, OneSecondWindow) { |
alexeypa (please no reviews)
2013/03/18 19:39:39
nit: Add the case when Record() is never called.
Wez
2013/03/19 23:23:14
Done.
|
+ base::Time now = base::Time::Now(); |
+ |
+ RateCounter rate_counter(base::TimeDelta::FromSeconds(1)); |
+ for (size_t i = 0; i < arraysize(kTestValues); ++i) { |
+ now += base::TimeDelta::FromSeconds(1); |
+ rate_counter.SetCurrentTimeForTest(now); |
+ rate_counter.Record(kTestValues[i]); |
+ EXPECT_EQ(static_cast<double>(kTestValues[i]), rate_counter.Rate()); |
+ } |
+} |
+ |
+TEST(RateCounterTest, TwoSecondWindow) { |
+ base::Time now = base::Time::Now(); |
+ |
+ RateCounter rate_counter(base::TimeDelta::FromSeconds(2)); |
+ for (size_t i = 0; i < arraysize(kTestValues); ++i) { |
+ now += base::TimeDelta::FromSeconds(1); |
+ rate_counter.SetCurrentTimeForTest(now); |
+ rate_counter.Record(kTestValues[i]); |
+ if (i > 0) { |
+ double expected = kTestValues[i] + kTestValues[i-1]; |
+ expected /= 2; |
+ EXPECT_EQ(expected, rate_counter.Rate()); |
alexeypa (please no reviews)
2013/03/18 19:39:39
nit: Verify the result of Rate() when |i| is 0 as
Wez
2013/03/19 23:23:14
Done.
|
+ } |
+ } |
+} |
+ |
+TEST(RateCounterTest, LongWindow) { |
+ base::Time now = base::Time::Now(); |
+ |
+ const size_t kWindowSeconds = arraysize(kTestValues) + 1; |
alexeypa (please no reviews)
2013/03/18 19:39:39
I guess it is better to have kWindowsSeconds = arr
Wez
2013/03/19 23:23:14
I've reworded the comments and updated this test t
|
+ |
+ RateCounter rate_counter(base::TimeDelta::FromSeconds(kWindowSeconds)); |
+ double expected = 0.0; |
+ for (size_t i = 0; i < arraysize(kTestValues); ++i) { |
+ now += base::TimeDelta::FromSeconds(1); |
+ rate_counter.SetCurrentTimeForTest(now); |
+ rate_counter.Record(kTestValues[i]); |
+ expected += kTestValues[i]; |
+ } |
+ expected /= kWindowSeconds; |
+ |
+ EXPECT_EQ(expected, rate_counter.Rate()); |
+} |
+ |
+} // namespace remoting |