File: linear_predictor_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (152 lines) | stat: -rw-r--r-- 5,650 bytes parent folder | download | duplicates (9)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/prediction/linear_predictor.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/prediction/input_predictor_unittest_helpers.h"
#include "ui/base/ui_base_features.h"

namespace ui {
namespace test {

class LinearPredictorFirstOrderTest : public InputPredictorTest {
 public:
  explicit LinearPredictorFirstOrderTest() {}

  LinearPredictorFirstOrderTest(const LinearPredictorFirstOrderTest&) = delete;
  LinearPredictorFirstOrderTest& operator=(
      const LinearPredictorFirstOrderTest&) = delete;

  void SetUp() override {
    predictor_ = std::make_unique<LinearPredictor>(
        LinearPredictor::EquationOrder::kFirstOrder);
  }
};

class LinearPredictorSecondOrderTest : public InputPredictorTest {
 public:
  explicit LinearPredictorSecondOrderTest() {}

  LinearPredictorSecondOrderTest(const LinearPredictorSecondOrderTest&) =
      delete;
  LinearPredictorSecondOrderTest& operator=(
      const LinearPredictorSecondOrderTest&) = delete;

  void SetUp() override {
    predictor_ = std::make_unique<LinearPredictor>(
        LinearPredictor::EquationOrder::kSecondOrder);
  }
};

// Test if the output name of the predictor is taking account of the
// equation order
TEST_F(LinearPredictorFirstOrderTest, GetName) {
  EXPECT_EQ(predictor_->GetName(), features::kPredictorNameLinearFirst);
}

// Test if the output name of the predictor is taking account of the
// equation order
TEST_F(LinearPredictorSecondOrderTest, GetName) {
  EXPECT_EQ(predictor_->GetName(), features::kPredictorNameLinearSecond);
}

// Test that the number of events required to compute a prediction is correct
TEST_F(LinearPredictorFirstOrderTest, ShouldHavePrediction) {
  LinearPredictor predictor(LinearPredictor::EquationOrder::kFirstOrder);
  size_t n = static_cast<size_t>(LinearPredictor::EquationOrder::kFirstOrder);
  for (size_t i = 0; i < n; i++) {
    EXPECT_FALSE(predictor.HasPrediction());
    predictor.Update(InputPredictor::InputData());
  }
  EXPECT_TRUE(predictor.HasPrediction());
  predictor.Reset();
  EXPECT_FALSE(predictor.HasPrediction());
}

// Test that the number of events required to compute a prediction is correct
TEST_F(LinearPredictorSecondOrderTest, ShouldHavePrediction) {
  LinearPredictor predictor(LinearPredictor::EquationOrder::kSecondOrder);
  size_t n1 = static_cast<size_t>(LinearPredictor::EquationOrder::kFirstOrder);
  size_t n2 = static_cast<size_t>(LinearPredictor::EquationOrder::kSecondOrder);
  for (size_t i = 0; i < n2; i++) {
    if (i < n1)
      EXPECT_FALSE(predictor.HasPrediction());
    else
      EXPECT_TRUE(predictor.HasPrediction());
    predictor.Update(InputPredictor::InputData());
  }
  EXPECT_TRUE(predictor.HasPrediction());
  predictor.Reset();
  EXPECT_FALSE(predictor.HasPrediction());
}

TEST_F(LinearPredictorFirstOrderTest, PredictedValue) {
  std::vector<double> x = {10, 20};
  std::vector<double> y = {5, 25};
  std::vector<double> t = {17, 33};
  // Compensating 23 ms
  // 1st order prediction at 33 + 23 = 56 ms

  std::vector<double> pred_ts = {56};
  std::vector<double> pred_x = {34.37};
  std::vector<double> pred_y = {53.75};
  ValidatePredictor(x, y, t, pred_ts, pred_x, pred_y);
}

TEST_F(LinearPredictorSecondOrderTest, PredictedValue) {
  std::vector<double> x = {0, 10, 20};
  std::vector<double> y = {0, 5, 25};
  std::vector<double> t = {0, 17, 33};
  // Compensating 23 ms in both results
  // 1st order prediction at 17 + 23 = 40 ms
  // 2nd order prediction at 33 + 23 = 56 ms
  std::vector<double> pred_ts = {40, 56};
  std::vector<double> pred_x = {23.52, 34.98};
  std::vector<double> pred_y = {11.76, 69.55};
  ValidatePredictor(x, y, t, pred_ts, pred_x, pred_y);
}

// Test constant position and constant velocity
TEST_F(LinearPredictorSecondOrderTest, ConstantPositionAndVelocity) {
  std::vector<double> x = {10, 10, 10, 10, 10};  // constant position
  std::vector<double> y = {0, 5, 10, 15, 20};    // constant velocity
  std::vector<double> t = {0, 7, 14, 21, 28};    // regular interval
  // since velocity is constant, acceleration should be 0 which simplifies
  // computations
  // Compensating 10 ms in all results
  std::vector<double> pred_ts = {17, 24, 31, 38};
  std::vector<double> pred_x = {10, 10, 10, 10};
  std::vector<double> pred_y = {12.14, 17.14, 22.14, 27.14};
  ValidatePredictor(x, y, t, pred_ts, pred_x, pred_y);
}

// Test time interval in first order
TEST_F(LinearPredictorFirstOrderTest, TimeInterval) {
  EXPECT_EQ(predictor_->TimeInterval(), kExpectedDefaultTimeInterval);
  std::vector<double> x = {10, 20};
  std::vector<double> y = {5, 25};
  std::vector<double> t = {17, 33};
  size_t n = static_cast<size_t>(LinearPredictor::EquationOrder::kFirstOrder);
  for (size_t i = 0; i < n; i++) {
    predictor_->Update({gfx::PointF(x[i], y[i]), FromMilliseconds(t[i])});
  }
  EXPECT_EQ(predictor_->TimeInterval(), base::Milliseconds(t[1] - t[0]));
}

// Test time interval in second order
TEST_F(LinearPredictorSecondOrderTest, TimeInterval) {
  EXPECT_EQ(predictor_->TimeInterval(), kExpectedDefaultTimeInterval);
  std::vector<double> x = {0, 10, 20};
  std::vector<double> y = {0, 5, 25};
  std::vector<double> t = {0, 17, 33};
  size_t n = static_cast<size_t>(LinearPredictor::EquationOrder::kSecondOrder);
  for (size_t i = 0; i < n; i++) {
    predictor_->Update({gfx::PointF(x[i], y[i]), FromMilliseconds(t[i])});
  }
  EXPECT_EQ(predictor_->TimeInterval(), base::Milliseconds((t[2] - t[0]) / 2));
}

}  // namespace test
}  // namespace ui