running_samples_unittest.cc 2.9 KB

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