1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/geometry/cubic_bezier.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(CubicBezierTest
, Basic
) {
14 CubicBezier
function(0.25, 0.0, 0.75, 1.0);
16 double epsilon
= 0.00015;
18 EXPECT_NEAR(function
.Solve(0), 0, epsilon
);
19 EXPECT_NEAR(function
.Solve(0.05), 0.01136, epsilon
);
20 EXPECT_NEAR(function
.Solve(0.1), 0.03978, epsilon
);
21 EXPECT_NEAR(function
.Solve(0.15), 0.079780, epsilon
);
22 EXPECT_NEAR(function
.Solve(0.2), 0.12803, epsilon
);
23 EXPECT_NEAR(function
.Solve(0.25), 0.18235, epsilon
);
24 EXPECT_NEAR(function
.Solve(0.3), 0.24115, epsilon
);
25 EXPECT_NEAR(function
.Solve(0.35), 0.30323, epsilon
);
26 EXPECT_NEAR(function
.Solve(0.4), 0.36761, epsilon
);
27 EXPECT_NEAR(function
.Solve(0.45), 0.43345, epsilon
);
28 EXPECT_NEAR(function
.Solve(0.5), 0.5, epsilon
);
29 EXPECT_NEAR(function
.Solve(0.6), 0.63238, epsilon
);
30 EXPECT_NEAR(function
.Solve(0.65), 0.69676, epsilon
);
31 EXPECT_NEAR(function
.Solve(0.7), 0.75884, epsilon
);
32 EXPECT_NEAR(function
.Solve(0.75), 0.81764, epsilon
);
33 EXPECT_NEAR(function
.Solve(0.8), 0.87196, epsilon
);
34 EXPECT_NEAR(function
.Solve(0.85), 0.92021, epsilon
);
35 EXPECT_NEAR(function
.Solve(0.9), 0.96021, epsilon
);
36 EXPECT_NEAR(function
.Solve(0.95), 0.98863, epsilon
);
37 EXPECT_NEAR(function
.Solve(1), 1, epsilon
);
40 // Tests that solving the bezier works with knots with y not in (0, 1).
41 TEST(CubicBezierTest
, UnclampedYValues
) {
42 CubicBezier
function(0.5, -1.0, 0.5, 2.0);
44 double epsilon
= 0.00015;
46 EXPECT_NEAR(function
.Solve(0.0), 0.0, epsilon
);
47 EXPECT_NEAR(function
.Solve(0.05), -0.08954, epsilon
);
48 EXPECT_NEAR(function
.Solve(0.1), -0.15613, epsilon
);
49 EXPECT_NEAR(function
.Solve(0.15), -0.19641, epsilon
);
50 EXPECT_NEAR(function
.Solve(0.2), -0.20651, epsilon
);
51 EXPECT_NEAR(function
.Solve(0.25), -0.18232, epsilon
);
52 EXPECT_NEAR(function
.Solve(0.3), -0.11992, epsilon
);
53 EXPECT_NEAR(function
.Solve(0.35), -0.01672, epsilon
);
54 EXPECT_NEAR(function
.Solve(0.4), 0.12660, epsilon
);
55 EXPECT_NEAR(function
.Solve(0.45), 0.30349, epsilon
);
56 EXPECT_NEAR(function
.Solve(0.5), 0.50000, epsilon
);
57 EXPECT_NEAR(function
.Solve(0.55), 0.69651, epsilon
);
58 EXPECT_NEAR(function
.Solve(0.6), 0.87340, epsilon
);
59 EXPECT_NEAR(function
.Solve(0.65), 1.01672, epsilon
);
60 EXPECT_NEAR(function
.Solve(0.7), 1.11992, epsilon
);
61 EXPECT_NEAR(function
.Solve(0.75), 1.18232, epsilon
);
62 EXPECT_NEAR(function
.Solve(0.8), 1.20651, epsilon
);
63 EXPECT_NEAR(function
.Solve(0.85), 1.19641, epsilon
);
64 EXPECT_NEAR(function
.Solve(0.9), 1.15613, epsilon
);
65 EXPECT_NEAR(function
.Solve(0.95), 1.08954, epsilon
);
66 EXPECT_NEAR(function
.Solve(1.0), 1.0, epsilon
);
69 TEST(CubicBezierTest
, Range
) {
70 double epsilon
= 0.00015;
73 // Derivative is a constant.
74 scoped_ptr
<CubicBezier
> function(
75 new CubicBezier(0.25, (1.0 / 3.0), 0.75, (2.0 / 3.0)));
76 function
->Range(&min
, &max
);
80 // Derivative is linear.
81 function
.reset(new CubicBezier(0.25, -0.5, 0.75, (-1.0 / 6.0)));
82 function
->Range(&min
, &max
);
83 EXPECT_NEAR(min
, -0.225, epsilon
);
86 // Derivative has no real roots.
87 function
.reset(new CubicBezier(0.25, 0.25, 0.75, 0.5));
88 function
->Range(&min
, &max
);
92 // Derivative has exactly one real root.
93 function
.reset(new CubicBezier(0.0, 1.0, 1.0, 0.0));
94 function
->Range(&min
, &max
);
98 // Derivative has one root < 0 and one root > 1.
99 function
.reset(new CubicBezier(0.25, 0.1, 0.75, 0.9));
100 function
->Range(&min
, &max
);
104 // Derivative has two roots in [0,1].
105 function
.reset(new CubicBezier(0.25, 2.5, 0.75, 0.5));
106 function
->Range(&min
, &max
);
108 EXPECT_NEAR(max
, 1.28818, epsilon
);
109 function
.reset(new CubicBezier(0.25, 0.5, 0.75, -1.5));
110 function
->Range(&min
, &max
);
111 EXPECT_NEAR(min
, -0.28818, epsilon
);
114 // Derivative has one root < 0 and one root in [0,1].
115 function
.reset(new CubicBezier(0.25, 0.1, 0.75, 1.5));
116 function
->Range(&min
, &max
);
118 EXPECT_NEAR(max
, 1.10755, epsilon
);
120 // Derivative has one root in [0,1] and one root > 1.
121 function
.reset(new CubicBezier(0.25, -0.5, 0.75, 0.9));
122 function
->Range(&min
, &max
);
123 EXPECT_NEAR(min
, -0.10755, epsilon
);
126 // Derivative has two roots < 0.
127 function
.reset(new CubicBezier(0.25, 0.3, 0.75, 0.633));
128 function
->Range(&min
, &max
);
132 // Derivative has two roots > 1.
133 function
.reset(new CubicBezier(0.25, 0.367, 0.75, 0.7));
134 function
->Range(&min
, &max
);
139 TEST(CubicBezierTest
, Slope
) {
140 CubicBezier
function(0.25, 0.0, 0.75, 1.0);
142 double epsilon
= 0.00015;
144 EXPECT_NEAR(function
.Slope(0), 0, epsilon
);
145 EXPECT_NEAR(function
.Slope(0.05), 0.42170, epsilon
);
146 EXPECT_NEAR(function
.Slope(0.1), 0.69778, epsilon
);
147 EXPECT_NEAR(function
.Slope(0.15), 0.89121, epsilon
);
148 EXPECT_NEAR(function
.Slope(0.2), 1.03184, epsilon
);
149 EXPECT_NEAR(function
.Slope(0.25), 1.13576, epsilon
);
150 EXPECT_NEAR(function
.Slope(0.3), 1.21239, epsilon
);
151 EXPECT_NEAR(function
.Slope(0.35), 1.26751, epsilon
);
152 EXPECT_NEAR(function
.Slope(0.4), 1.30474, epsilon
);
153 EXPECT_NEAR(function
.Slope(0.45), 1.32628, epsilon
);
154 EXPECT_NEAR(function
.Slope(0.5), 1.33333, epsilon
);
155 EXPECT_NEAR(function
.Slope(0.55), 1.32628, epsilon
);
156 EXPECT_NEAR(function
.Slope(0.6), 1.30474, epsilon
);
157 EXPECT_NEAR(function
.Slope(0.65), 1.26751, epsilon
);
158 EXPECT_NEAR(function
.Slope(0.7), 1.21239, epsilon
);
159 EXPECT_NEAR(function
.Slope(0.75), 1.13576, epsilon
);
160 EXPECT_NEAR(function
.Slope(0.8), 1.03184, epsilon
);
161 EXPECT_NEAR(function
.Slope(0.85), 0.89121, epsilon
);
162 EXPECT_NEAR(function
.Slope(0.9), 0.69778, epsilon
);
163 EXPECT_NEAR(function
.Slope(0.95), 0.42170, epsilon
);
164 EXPECT_NEAR(function
.Slope(1), 0, epsilon
);
167 TEST(CubicBezierTest
, InputOutOfRange
) {
168 CubicBezier
simple(0.5, 1.0, 0.5, 1.0);
169 EXPECT_EQ(-2.0, simple
.Solve(-1.0));
170 EXPECT_EQ(1.0, simple
.Solve(2.0));
172 CubicBezier
coincidentEndpoints(0.0, 0.0, 1.0, 1.0);
173 EXPECT_EQ(-1.0, coincidentEndpoints
.Solve(-1.0));
174 EXPECT_EQ(2.0, coincidentEndpoints
.Solve(2.0));
176 CubicBezier
verticalGradient(0.0, 1.0, 1.0, 0.0);
177 EXPECT_EQ(0.0, verticalGradient
.Solve(-1.0));
178 EXPECT_EQ(1.0, verticalGradient
.Solve(2.0));
180 CubicBezier
distinctEndpoints(0.1, 0.2, 0.8, 0.8);
181 EXPECT_EQ(-2.0, distinctEndpoints
.Solve(-1.0));
182 EXPECT_EQ(2.0, distinctEndpoints
.Solve(2.0));
184 CubicBezier
coincidentEndpoint(0.0, 0.0, 0.8, 0.8);
185 EXPECT_EQ(-1.0, coincidentEndpoint
.Solve(-1.0));
186 EXPECT_EQ(2.0, coincidentEndpoint
.Solve(2.0));
188 CubicBezier
threeCoincidentPoints(0.0, 0.0, 0.0, 0.0);
189 EXPECT_EQ(0, threeCoincidentPoints
.Solve(-1.0));
190 EXPECT_EQ(2.0, threeCoincidentPoints
.Solve(2.0));