File: least_squares_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 (106 lines) | stat: -rw-r--r-- 3,743 bytes parent folder | download | duplicates (10)
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
// Copyright 2018 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/least_squares_predictor.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/prediction/input_predictor_unittest_helpers.h"

namespace ui {
namespace test {

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

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

  void SetUp() override {
    predictor_ = std::make_unique<LeastSquaresPredictor>();
  }
};

TEST_F(LSQPredictorTest, ShouldHasPrediction) {
  LeastSquaresPredictor predictor;
  for (size_t i = 0; i < LeastSquaresPredictor::kSize; i++) {
    // First |kSize| point do not have prediction available.
    EXPECT_FALSE(predictor.HasPrediction());
    InputPredictor::InputData data = {gfx::PointF(1, 1),
                                      FromMilliseconds(8 * i)};
    predictor.Update(data);
  }
  EXPECT_TRUE(predictor.HasPrediction());
}

// Tests the lest squares filter behavior.
// The data set is generated by a "known to work" quadratic fit.
TEST_F(LSQPredictorTest, PredictedValue) {
  std::vector<double> x = {22, 58, 102, 108.094};
  std::vector<double> y = {100, 100, 100, 100};
  std::vector<double> t = {13, 21, 37, 42};
  ValidatePredictor(x, y, t);

  x = {100, 100, 101, 104.126};
  y = {120, 280, 600, 1364.93};
  t = {101, 126, 148, 180};
  ValidatePredictor(x, y, t);
}

// Tests the LSQ predictor predict constant velocity.
TEST_F(LSQPredictorTest, PredictLinearValue) {
  std::vector<double> x = {0, 4, 10, 15, 20, 28, 30, 38};
  std::vector<double> y = {30, 34, 40, 45, 50, 58, 60, 68};
  std::vector<double> t = {0, 4, 10, 15, 20, 28, 30, 38};
  ValidatePredictor(x, y, t);
}

// Tests the LSQ predictor predict quadratic value correctly.
TEST_F(LSQPredictorTest, PredictQuadraticValue) {
  std::vector<double> x = {2, 8, 18, 32, 50};
  std::vector<double> y = {100, 400, 900, 1600, 2500};
  std::vector<double> t = {8, 16, 24, 32, 40};
  ValidatePredictor(x, y, t);
}

// Tests that lsq predictor will not crash when given constant time stamp.
TEST_F(LSQPredictorTest, ConstantTimeStampNotCrash) {
  std::vector<double> x = {100, 101, 102};
  std::vector<double> y = {101, 102, 103};
  std::vector<double> t = {0, 0, 0};
  for (size_t i = 0; i < t.size(); i++) {
    InputPredictor::InputData data = {gfx::PointF(x[i], y[i]),
                                      FromMilliseconds(t[i])};
    predictor_->Update(data);
  }
  // Expect false because the matrix is singular
  // and the predictor cannot compute a prediction
  EXPECT_FALSE(predictor_->GeneratePrediction(FromMilliseconds(42)));

  x = {100, 100, 100};
  y = {100, 100, 100};
  t = {100, 100, 100};
  for (size_t i = 0; i < t.size(); i++) {
    InputPredictor::InputData data = {gfx::PointF(x[i], y[i]),
                                      FromMilliseconds(t[i])};
    predictor_->Update(data);
  }
  EXPECT_TRUE(predictor_->GeneratePrediction(FromMilliseconds(142)));
}

// Tests the LSQ predictor produce the time interval correctly.
TEST_F(LSQPredictorTest, TimeInterval) {
  EXPECT_EQ(predictor_->TimeInterval(), kExpectedDefaultTimeInterval);
  std::vector<double> x = {0, 4, 10};
  std::vector<double> y = {30, 34, 40};
  std::vector<double> t = {0, 4, 10};
  for (size_t i = 0; i < t.size(); i++) {
    InputPredictor::InputData data = {gfx::PointF(x[i], y[i]),
                                      FromMilliseconds(t[i])};
    predictor_->Update(data);
  }
  EXPECT_EQ(predictor_->TimeInterval(), base::Milliseconds((t[2] - t[0]) / 2));
}

}  // namespace test
}  // namespace ui