xref: /aosp_15_r20/external/skia/tests/PathOpsDVectorTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #include "include/core/SkPoint.h"
8 #include "include/core/SkScalar.h"
9 #include "include/core/SkTypes.h"
10 #include "src/pathops/SkPathOpsPoint.h"
11 #include "src/pathops/SkPathOpsTypes.h"
12 #include "tests/PathOpsTestCommon.h"
13 #include "tests/Test.h"
14 
15 #include <array>
16 #include <cmath>
17 #include <cstddef>
18 #include <limits>
19 
20 static const SkDPoint tests[] = {
21     {0, 0},
22     {1, 0},
23     {0, 1},
24     {2, 1},
25     {1, 2},
26     {1, 1},
27     {2, 2}
28 };
29 
30 static const size_t tests_count = std::size(tests);
31 
DEF_TEST(PathOpsDVector,reporter)32 DEF_TEST(PathOpsDVector, reporter) {
33     for (size_t index = 0; index < tests_count - 1; ++index) {
34         SkDVector v1 = tests[index + 1] - tests[index];
35         SkASSERT(ValidVector(v1));
36         SkDVector v2 = tests[index] - tests[index + 1];
37         SkASSERT(ValidVector(v2));
38         v1 += v2;
39         REPORTER_ASSERT(reporter, v1.fX == 0 && v1.fY == 0);
40         v2 -= static_cast<decltype(v2)&>(v2);
41         REPORTER_ASSERT(reporter, v2.fX == 0 && v2.fY == 0);
42         v1 = tests[index + 1] - tests[index];
43         v1 /= 2;
44         v1 *= 2;
45         v1 -= tests[index + 1] - tests[index];
46         REPORTER_ASSERT(reporter, v1.fX == 0 && v1.fY == 0);
47         SkVector sv = v1.asSkVector();
48         REPORTER_ASSERT(reporter, sv.fX == 0 && sv.fY == 0);
49         v1 = tests[index + 1] - tests[index];
50         double lenSq = v1.lengthSquared();
51         double v1Dot = v1.dot(v1);
52         REPORTER_ASSERT(reporter, lenSq == v1Dot);
53         REPORTER_ASSERT(reporter, approximately_equal(sqrt(lenSq), v1.length()));
54         double v1Cross = v1.cross(v1);
55         REPORTER_ASSERT(reporter, v1Cross == 0);
56     }
57 }
58 
DEF_TEST(SkDVector_normalize,reporter)59 DEF_TEST(SkDVector_normalize, reporter) {
60     // See also SkVx_normalize
61     auto assertDoublesEqual = [&](double left, double right) {
62         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(left, right), "%f != %f", left, right);
63     };
64     SkDVector first{1.2, 3.4};
65     first.normalize();
66     REPORTER_ASSERT(reporter, first.isFinite());
67     assertDoublesEqual(first.fX, 0.332820);
68     assertDoublesEqual(first.fY, 0.942990);
69 
70     SkDVector second{2.3, -4.5};
71     second.normalize();
72     REPORTER_ASSERT(reporter, second.isFinite());
73     assertDoublesEqual(second.fX,  0.455111);
74     assertDoublesEqual(second.fY, -0.890435);
75 }
76 
DEF_TEST(SkDVector_normalize_infinity_and_nan,reporter)77 DEF_TEST(SkDVector_normalize_infinity_and_nan, reporter) {
78     // See also SkVx_normalize_infinity_and_nan
79     SkDVector first{0, 0};
80     first.normalize();
81     REPORTER_ASSERT(reporter, !first.isFinite());
82     REPORTER_ASSERT(reporter, std::isnan(first.fX), "%f is not nan", first.fX);
83     REPORTER_ASSERT(reporter, std::isnan(first.fY), "%f is not nan", first.fY);
84 
85     SkDVector second{std::numeric_limits<double>::max(),
86                      std::numeric_limits<double>::max()};
87     second.normalize();
88     REPORTER_ASSERT(reporter, second.isFinite());
89     REPORTER_ASSERT(reporter, second.fX == 0, "%f != 0", second.fX);
90     REPORTER_ASSERT(reporter, second.fY == 0, "%f != 0", second.fY);
91 }
92 
93