Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PathOpsDVectorTest.cpp
Go to the documentation of this file.
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 */
13#include "tests/Test.h"
14
15#include <array>
16#include <cmath>
17#include <cstddef>
18#include <limits>
19
20static 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
30static const size_t tests_count = std::size(tests);
31
32DEF_TEST(PathOpsDVector, reporter) {
33 for (size_t index = 0; index < tests_count - 1; ++index) {
34 SkDVector v1 = tests[index + 1] - tests[index];
36 SkDVector v2 = tests[index] - tests[index + 1];
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);
54 double v1Cross = v1.cross(v1);
55 REPORTER_ASSERT(reporter, v1Cross == 0);
56 }
57}
58
59DEF_TEST(SkDVector_normalize, reporter) {
60 // See also SkVx_normalize
61 auto assertDoublesEqual = [&](double left, double 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
77DEF_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
reporter
static const size_t tests_count
static const SkDPoint tests[]
bool ValidVector(const SkDVector &v)
#define SkASSERT(cond)
Definition SkAssert.h:116
bool approximately_equal(double x, double y)
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:107
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
Vec2Value v2
double length() const
double cross(const SkDVector &a) const
SkVector asSkVector() const
double dot(const SkDVector &a) const
SkDVector & normalize()
double lengthSquared() const
float fX
x-axis value
float fY
y-axis value