1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94

media / gpu / exponential_moving_average_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/gpu/exponential_moving_average.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace media {
namespace {

// Test ExponentialMovingAverageTest adds the predefined values and checks
// whether the correct mean and standard deviation values are produced.
class ExponentialMovingAverageTest : public testing::Test {
 public:
  ExponentialMovingAverageTest() = default;

  void SetUp() override {
    moving_average_ =
        std::make_unique<ExponentialMovingAverage>(base::Milliseconds(100));
    EXPECT_EQ(base::TimeDelta(), moving_average_->curr_window_size());
    EXPECT_EQ(base::Milliseconds(100), moving_average_->max_window_size());
  }

 protected:
  std::unique_ptr<ExponentialMovingAverage> moving_average_;
};

// Test Cases

// Adding predefined sequence to the moving average filter and checking
// whether the stats are inside expected ranges. The filter is checked
// with two sequences using different window sizes.
TEST_F(ExponentialMovingAverageTest, RunBasicMovingAverageTest) {
  constexpr float kExpectedMeanMin1 = 94.73f;
  constexpr float kExpectedMeanMax1 = 94.74f;
  constexpr float kExpectedStdDevMin1 = 1.72f;
  constexpr float kExpectedStdDevMax1 = 1.73f;
  constexpr float kExpectedMeanMin2 = 103.16f;
  constexpr float kExpectedMeanMax2 = 103.17f;
  constexpr float kExpectedStdDevMin2 = 3.19f;
  constexpr float kExpectedStdDevMax2 = 3.20f;

  base::TimeDelta timestamp = base::Microseconds(0);
  moving_average_->AddValue(100, timestamp);
  timestamp += base::Milliseconds(10);
  moving_average_->AddValue(120, timestamp);
  timestamp += base::Milliseconds(8);
  moving_average_->AddValue(90, timestamp);
  timestamp += base::Milliseconds(12);
  moving_average_->AddValue(115, timestamp);
  timestamp += base::Milliseconds(11);
  moving_average_->AddValue(95, timestamp);
  timestamp += base::Milliseconds(9);
  moving_average_->AddValue(100, timestamp);
  timestamp += base::Milliseconds(10);
  moving_average_->AddValue(120, timestamp);
  timestamp += base::Milliseconds(11);
  moving_average_->AddValue(115, timestamp);
  timestamp += base::Milliseconds(7);
  moving_average_->AddValue(90, timestamp);
  timestamp += base::Milliseconds(11);
  moving_average_->AddValue(85, timestamp);
  timestamp += base::Milliseconds(8);
  moving_average_->AddValue(95, timestamp);

  EXPECT_LT(kExpectedMeanMin1, moving_average_->mean());
  EXPECT_GT(kExpectedMeanMax1, moving_average_->mean());
  EXPECT_LT(kExpectedStdDevMin1, moving_average_->GetStdDeviation());
  EXPECT_GT(kExpectedStdDevMax1, moving_average_->GetStdDeviation());

  moving_average_->update_max_window_size(base::Milliseconds(200));
  EXPECT_EQ(base::Milliseconds(100), moving_average_->curr_window_size());
  EXPECT_EQ(base::Milliseconds(200), moving_average_->max_window_size());

  moving_average_->AddValue(105, timestamp);
  timestamp += base::Milliseconds(11);
  moving_average_->AddValue(90, timestamp);
  timestamp += base::Milliseconds(11);
  moving_average_->AddValue(100, timestamp);
  timestamp += base::Milliseconds(8);
  moving_average_->AddValue(100, timestamp);
  timestamp += base::Milliseconds(10);
  moving_average_->AddValue(105, timestamp);

  EXPECT_LT(kExpectedMeanMin2, moving_average_->mean());
  EXPECT_GT(kExpectedMeanMax2, moving_average_->mean());
  EXPECT_LT(kExpectedStdDevMin2, moving_average_->GetStdDeviation());
  EXPECT_GT(kExpectedStdDevMax2, moving_average_->GetStdDeviation());
}

}  // namespace

}  // namespace media