File: monotone_cubic_spline_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 4,804 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/ash/power/auto_screen_brightness/monotone_cubic_spline.h"

#include <algorithm>
#include <random>

#include "base/check.h"

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

namespace ash {
namespace power {
namespace auto_screen_brightness {

TEST(MonotoneCubicSpline, Interpolation) {
  const std::vector<double> xs = {0,   10,  20,   40,   60,  80,
                                  100, 500, 1000, 2000, 3000};
  const std::vector<double> ys = {0, 5, 10, 15, 20, 25, 30, 40, 60, 80, 1000};

  const std::optional<MonotoneCubicSpline> spline =
      MonotoneCubicSpline::CreateMonotoneCubicSpline(xs, ys);
  DCHECK(spline);
  EXPECT_EQ(spline->GetControlPointsY().size(), xs.size());

  // Spline's control points get their exact values.
  for (size_t i = 0; i < xs.size(); ++i) {
    EXPECT_DOUBLE_EQ(spline->Interpolate(xs[i]), ys[i]);
  }

  // Data points falling out of the range get boundary values.
  EXPECT_DOUBLE_EQ(spline->Interpolate(-0.1), ys[0]);
  EXPECT_DOUBLE_EQ(spline->Interpolate(4000), ys.back());

  // Check interpolation results on non-control points. Results are compared
  // with java implementation of Spline for Android.
  const std::vector<double> ts = {2.2, 4.8, 12.3, 46.4, 70.1, 90.5, 95.8};
  const std::vector<double> expected = {
      1.1,    2.3999999999999995, 6.200916250000001, 16.599999999999998,
      22.525, 28.08849264366124,  29.413985177197368};
  for (size_t i = 0; i < ts.size(); ++i) {
    EXPECT_DOUBLE_EQ(spline->Interpolate(ts[i]), expected[i]);
  }
}

TEST(MonotoneCubicSpline, Monotonicity) {
  const unsigned seed = 1;
  std::default_random_engine generator(seed);
  std::uniform_real_distribution<double> distribution(0.0, 200);

  std::vector<double> xs;
  std::vector<double> ys;
  for (size_t i = 0; i < 10; ++i) {
    xs.push_back(distribution(generator));
    ys.push_back(distribution(generator));
  }

  // Sort xs and ensure they are strictly increasing.
  std::sort(xs.begin(), xs.end());
  for (size_t i = 1; i < xs.size(); ++i) {
    if (xs[i] <= xs[i - 1]) {
      xs[i] = xs[i - 1] + 1;
    }
  }

  std::sort(ys.begin(), ys.end());

  const std::optional<MonotoneCubicSpline> spline =
      MonotoneCubicSpline::CreateMonotoneCubicSpline(xs, ys);
  DCHECK(spline);

  std::vector<double> test_points;
  for (size_t i = 0; i < 1000; ++i) {
    test_points.push_back(distribution(generator));
  }
  std::sort(test_points.begin(), test_points.end());

  for (size_t i = 1; i < test_points.size(); ++i) {
    EXPECT_LE(spline->Interpolate(test_points[i - 1]),
              spline->Interpolate(test_points[i]));
  }
}

TEST(MonotoneCubicSpline, FromStringCorrectFormat) {
  const std::string data("1,10\n2,20\n3,30");
  const std::optional<MonotoneCubicSpline> spline_from_string =
      MonotoneCubicSpline::FromString(data);
  DCHECK(spline_from_string);
  const std::vector<double> xs = {1, 2, 3};
  const std::vector<double> ys = {10, 20, 30};
  const std::optional<MonotoneCubicSpline> expected_spline =
      MonotoneCubicSpline::CreateMonotoneCubicSpline(xs, ys);
  DCHECK(expected_spline);
  EXPECT_EQ(*expected_spline, *spline_from_string);
}

TEST(MonotoneCubicSpline, FromStringTooFewRows) {
  const std::string data("1,10");
  const std::optional<MonotoneCubicSpline> spline_from_string =
      MonotoneCubicSpline::FromString(data);
  EXPECT_FALSE(spline_from_string.has_value());
}

TEST(MonotoneCubicSpline, ToString) {
  const std::vector<double> xs = {1, 2, 3};
  const std::vector<double> ys = {10, 20, 30};
  const std::optional<MonotoneCubicSpline> spline =
      MonotoneCubicSpline::CreateMonotoneCubicSpline(xs, ys);
  DCHECK(spline);
  const std::string string_from_spline = spline->ToString();

  const std::string expected_string("1,10\n2,20\n3,30");

  EXPECT_EQ(expected_string, string_from_spline);
}

TEST(MonotoneCubicSpline, AssignmentAndEquality) {
  const std::vector<double> xs1 = {0,   10,  20,   40,   60,  80,
                                   100, 500, 1000, 2000, 3000};
  const std::vector<double> ys1 = {0, 5, 10, 15, 20, 25, 30, 40, 60, 80, 1000};
  std::optional<MonotoneCubicSpline> spline1 =
      MonotoneCubicSpline::CreateMonotoneCubicSpline(xs1, ys1);

  const std::vector<double> xs2 = {1, 2, 3};
  const std::vector<double> ys2 = {10, 20, 30};
  const std::optional<MonotoneCubicSpline> spline2 =
      MonotoneCubicSpline::CreateMonotoneCubicSpline(xs2, ys2);

  EXPECT_NE(*spline1, *spline2);
  spline1 = spline2;

  EXPECT_EQ(*spline1, *spline2);

  const MonotoneCubicSpline spline3 = *spline1;
  EXPECT_EQ(spline3, spline2);
}

}  // namespace auto_screen_brightness
}  // namespace power
}  // namespace ash