| 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_samples.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace remoting { |
| 13 |
| 14 typedef void (*TestFunction)(size_t i, RunningSamples& samples); |
| 15 |
| 16 static const int64_t kTestValues[] = { 10, 20, 30, 10, 25, 16, 15 }; |
| 17 |
| 18 // Test framework that verifies average() and max() at beginning, iterates |
| 19 // through all elements and meanwhile calls your own test function |
| 20 static void TestFramework(int windowSize, TestFunction testFn) { |
| 21 RunningSamples samples(windowSize); |
| 22 EXPECT_EQ(0, samples.Average()); |
| 23 EXPECT_EQ(0, samples.Max()); |
| 24 |
| 25 for (size_t i = 0; i < arraysize(kTestValues); ++i) { |
| 26 samples.Record(kTestValues[i]); |
| 27 testFn(i, samples); |
| 28 } |
| 29 } |
| 30 |
| 31 // Average across a single element, i.e. just return the most recent. |
| 32 TEST(RunningSamplesTest, AverageOneElementWindow) { |
| 33 TestFramework(1, [](size_t i, RunningSamples& samples) { |
| 34 EXPECT_EQ(static_cast<double>(kTestValues[i]), samples.Average()); |
| 35 }); |
| 36 } |
| 37 |
| 38 // Average the two most recent elements. |
| 39 TEST(RunningSamplesTest, AverageTwoElementWindow) { |
| 40 TestFramework(2, [](size_t i, RunningSamples& samples) { |
| 41 double expected = kTestValues[i]; |
| 42 if (i > 0) |
| 43 expected = (expected + kTestValues[i-1]) / 2; |
| 44 |
| 45 EXPECT_EQ(expected, samples.Average()); |
| 46 }); |
| 47 } |
| 48 |
| 49 // Average across all the elements if the window size exceeds the element count. |
| 50 TEST(RunningSamplesTest, AverageLongWindow) { |
| 51 TestFramework(arraysize(kTestValues) + 1, |
| 52 [](size_t i, RunningSamples& samples) { |
| 53 double expected = 0.0; |
| 54 for (size_t j = 0; j <= i; ++j) |
| 55 expected += kTestValues[j]; |
| 56 expected /= i + 1; |
| 57 |
| 58 EXPECT_EQ(expected, samples.Average()); |
| 59 }); |
| 60 } |
| 61 |
| 62 // Max of a single element, i.e. just return the most recent. |
| 63 TEST(RunningSamplesTest, MaxOneElementWindow) { |
| 64 TestFramework(1, [](size_t i, RunningSamples& samples) { |
| 65 EXPECT_EQ(static_cast<double>(kTestValues[i]), samples.Max()); |
| 66 }); |
| 67 } |
| 68 |
| 69 // Max of the two most recent elements. |
| 70 TEST(RunningSamplesTest, MaxTwoElementWindow) { |
| 71 TestFramework(2, [](size_t i, RunningSamples& samples) { |
| 72 double expected = kTestValues[i]; |
| 73 if (i > 0) |
| 74 expected = expected > kTestValues[i-1] ? expected : kTestValues[i-1]; |
| 75 |
| 76 EXPECT_EQ(expected, samples.Max()); |
| 77 }); |
| 78 } |
| 79 |
| 80 // Max of all the elements if the window size exceeds the element count. |
| 81 TEST(RunningSamplesTest, MaxLongWindow) { |
| 82 TestFramework(arraysize(kTestValues) + 1, |
| 83 [](size_t i, RunningSamples& samples) { |
| 84 int64_t expected = -1; |
| 85 for (size_t j = 0; j <= i; ++j) |
| 86 expected = expected > kTestValues[j] ? expected : kTestValues[j]; |
| 87 |
| 88 EXPECT_EQ(expected, samples.Max()); |
| 89 }); |
| 90 } |
| 91 |
| 92 } // namespace remoting |
| OLD | NEW |