DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


#include "video/quality_threshold.h"

#include "test/gtest.h"

namespace webrtc {
namespace webrtc {

TEST(QualityThresholdTest, BackAndForth) {
  const int kLowThreshold = 0;
  const int kHighThreshold = 1;
  const float kFraction = 0.75f;
  const float kFraction = 0.75f;
  const int kMaxMeasurements = 10;

  QualityThreshold thresh(kLowThreshold, kHighThreshold, kFraction,
                          kMaxMeasurements);


  const int kNeededMeasurements =
      static_cast<int>(kFraction * kMaxMeasurements + 1);
  for (int i = 0; i < kNeededMeasurements; ++i) {
    EXPECT_FALSE(thresh.IsHigh());
    thresh.AddMeasurement(kLowThreshold);
  }
  ASSERT_TRUE(thresh.IsHigh());
  ASSERT_TRUE(thresh.IsHigh());
  for (int i = 0; i < kNeededMeasurements; ++i) {
    EXPECT_FALSE(*thresh.IsHigh());
    thresh.AddMeasurement(kHighThreshold);
  }
  EXPECT_TRUE(*thresh.IsHigh());
  EXPECT_TRUE(*thresh.IsHigh());

  for (int i = 0; i < kNeededMeasurements; ++i) {
    EXPECT_TRUE(*thresh.IsHigh());
    thresh.AddMeasurement(kLowThreshold);
  }
  EXPECT_FALSE(*thresh.IsHigh());
}
}

TEST(QualityThresholdTest, Variance) {
  const int kLowThreshold = 0;
  const int kHighThreshold = 1;
  const float kFraction = 0.8f;
  const float kFraction = 0.8f;
  const int kMaxMeasurements = 10;
  const double kMaxError = 0.01;

  // Previously randomly generated values...
  int values[] = {51, 79, 80, 56, 19, 20, 48, 57, 48, 25, 2, 25, 38, 37, 25};
  int values[] = {51, 79, 80, 56, 19, 20, 48, 57, 48, 25, 2, 25, 38, 37, 25};
  // ...with precomputed variances.
  double variances[] = {476.9, 687.6, 552, 336.4, 278.767, 265.167};

  QualityThreshold thresh(kLowThreshold, kHighThreshold, kFraction,
                          kMaxMeasurements);
                          kMaxMeasurements);

  for (int i = 0; i < kMaxMeasurements; ++i) {
    EXPECT_FALSE(thresh.CalculateVariance());
    thresh.AddMeasurement(values[i]);
  }

  ASSERT_TRUE(thresh.CalculateVariance());
  ASSERT_TRUE(thresh.CalculateVariance());
  EXPECT_NEAR(variances[0], *thresh.CalculateVariance(), kMaxError);
  for (unsigned int i = 1; i < sizeof(variances) / sizeof(double); ++i) {
    thresh.AddMeasurement(values[i + kMaxMeasurements - 1]);
    EXPECT_NEAR(variances[i], *thresh.CalculateVariance(), kMaxError);
  }
  }

  for (int i = 0; i < kMaxMeasurements; ++i) {
    thresh.AddMeasurement(42);
  }
  }
  EXPECT_NEAR(0, *thresh.CalculateVariance(), kMaxError);
}

TEST(QualityThresholdTest, BetweenThresholds) {
  const int kLowThreshold = 0;
  const int kLowThreshold = 0;
  const int kHighThreshold = 2;
  const float kFraction = 0.6f;
  const int kMaxMeasurements = 10;

  const int kBetweenThresholds = (kLowThreshold + kHighThreshold) / 2;
  const int kBetweenThresholds = (kLowThreshold + kHighThreshold) / 2;

  QualityThreshold thresh(kLowThreshold, kHighThreshold, kFraction,
                          kMaxMeasurements);

  for (int i = 0; i < 2 * kMaxMeasurements; ++i) {
  for (int i = 0; i < 2 * kMaxMeasurements; ++i) {
    EXPECT_FALSE(thresh.IsHigh());
    thresh.AddMeasurement(kBetweenThresholds);
  }
  EXPECT_FALSE(thresh.IsHigh());
}
}

TEST(QualityThresholdTest, FractionHigh) {
TEST(QualityThresholdTest, FractionHigh) {
  const int kLowThreshold = 0;
  const int kHighThreshold = 2;
  const float kFraction = 0.75f;
  const int kMaxMeasurements = 10;


  const int kBetweenThresholds = (kLowThreshold + kHighThreshold) / 2;
  const int kNeededMeasurements =
      static_cast<int>(kFraction * kMaxMeasurements + 1);

  QualityThreshold thresh(kLowThreshold, kHighThreshold, kFraction,
  QualityThreshold thresh(kLowThreshold, kHighThreshold, kFraction,
                          kMaxMeasurements);

  for (int i = 0; i < kMaxMeasurements; ++i) {
    EXPECT_FALSE(thresh.FractionHigh(1));
    thresh.AddMeasurement(kBetweenThresholds);
  }


  for (int i = 0; i < kNeededMeasurements; i++) {
    EXPECT_FALSE(thresh.FractionHigh(1));
    thresh.AddMeasurement(kHighThreshold);
  }
  EXPECT_FALSE(thresh.FractionHigh(2));
  EXPECT_FALSE(thresh.FractionHigh(2));
  ASSERT_TRUE(thresh.FractionHigh(1));
  EXPECT_NEAR(*thresh.FractionHigh(1), 1, 0.001);

  for (int i = 0; i < kNeededMeasurements; i++) {
    EXPECT_NEAR(*thresh.FractionHigh(1), 1, 0.001);
    EXPECT_NEAR(*thresh.FractionHigh(1), 1, 0.001);
    thresh.AddMeasurement(kLowThreshold);
  }
  EXPECT_NEAR(
      *thresh.FractionHigh(1),
      static_cast<double>(kNeededMeasurements) / (kNeededMeasurements + 1),
      static_cast<double>(kNeededMeasurements) / (kNeededMeasurements + 1),
      0.001);
}

}  // namespace webrtc
}  // namespace webrtc