1 /*
2 * Copyright 2011 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
8 #include "include/core/SkPoint.h"
9 #include "include/core/SkRect.h"
10 #include "include/core/SkScalar.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkDebug.h"
13 #include "src/core/SkPointPriv.h"
14 #include "tests/Test.h"
15
16 #include <cfloat>
17 #include <cstdint>
18 #include <cstring>
19 #include <string>
20
test_casts(skiatest::Reporter * reporter)21 static void test_casts(skiatest::Reporter* reporter) {
22 SkPoint p = { 0, 0 };
23 SkRect r = { 0, 0, 0, 0 };
24
25 const SkScalar* pPtr = reinterpret_cast<const SkScalar*>(&p);
26 const SkScalar* rPtr = reinterpret_cast<const SkScalar*>(&r);
27
28 REPORTER_ASSERT(reporter, SkPointPriv::AsScalars(p) == pPtr);
29 REPORTER_ASSERT(reporter, r.asScalars() == rPtr);
30 }
31
32 // Tests SkPoint::Normalize() for this (x,y)
test_Normalize(skiatest::Reporter * reporter,SkScalar x,SkScalar y)33 static void test_Normalize(skiatest::Reporter* reporter,
34 SkScalar x, SkScalar y) {
35 SkPoint point;
36 point.set(x, y);
37 SkScalar oldLength = point.length();
38 SkScalar returned = SkPoint::Normalize(&point);
39 SkScalar newLength = point.length();
40 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
41 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
42 }
43
test_normalize_cannormalize_consistent(skiatest::Reporter * reporter)44 static void test_normalize_cannormalize_consistent(skiatest::Reporter* reporter) {
45 const SkScalar values[] = { 1, 1e18f, 1e20f, 1e38f, SK_ScalarInfinity, SK_ScalarNaN };
46
47 for (SkScalar val : values) {
48 const SkScalar variants[] = { val, -val, SkScalarInvert(val), -SkScalarInvert(val) };
49
50 for (SkScalar v : variants) {
51 const SkPoint pts[] = { { 0, v }, { v, 0 }, { 1, v }, { v, 1 }, { v, v } };
52
53 for (SkPoint p : pts) {
54 bool can = SkPointPriv::CanNormalize(p.fX, p.fY);
55 bool nor = p.normalize();
56 REPORTER_ASSERT(reporter, can == nor);
57 }
58 }
59 }
60 }
61
62 // Tests that SkPoint::length() and SkPoint::Length() both return
63 // approximately expectedLength for this (x,y).
test_length(skiatest::Reporter * reporter,SkScalar x,SkScalar y,SkScalar expectedLength)64 static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y,
65 SkScalar expectedLength) {
66 SkPoint point;
67 point.set(x, y);
68 SkScalar s1 = point.length();
69 SkScalar s2 = SkPoint::Length(x, y);
70 //The following should be exactly the same, but need not be.
71 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
72 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
73 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
74
75 test_Normalize(reporter, x, y);
76 }
77
78 // Ugh. Windows compiler can dive into other .cpp files, and sometimes
79 // notices that I will generate an overflow... which is exactly the point
80 // of this test!
81 //
82 // To avoid this warning, I need to convince the compiler that I might not
83 // use that big value, hence this hacky helper function: reporter is
84 // ALWAYS non-null. (shhhhhh, don't tell the compiler that).
get_value(skiatest::Reporter * reporter,T value)85 template <typename T> T get_value(skiatest::Reporter* reporter, T value) {
86 return reporter ? value : 0;
87 }
88
89 // On linux gcc, 32bit, we are seeing the compiler propagate up the value
90 // of SkPoint::length() as a double (which we use sometimes to avoid overflow
91 // during the computation), even though the signature says float (SkScalar).
92 //
93 // force_as_float is meant to capture our latest technique (horrible as
94 // it is) to force the value to be a float, so we can test whether it was
95 // finite or not.
force_as_float(skiatest::Reporter * reporter,float value)96 static float force_as_float(skiatest::Reporter* reporter, float value) {
97 uint32_t storage;
98 memcpy(&storage, &value, 4);
99 // even the pair of memcpy calls are not sufficient, since those seem to
100 // be no-op'd, so we add a runtime tests (just like get_value) to force
101 // the compiler to give us an actual float.
102 if (nullptr == reporter) {
103 storage = ~storage;
104 }
105 memcpy(&value, &storage, 4);
106 return value;
107 }
108
109 // test that we handle very large values correctly. i.e. that we can
110 // successfully normalize something whose mag overflows a float.
test_overflow(skiatest::Reporter * reporter)111 static void test_overflow(skiatest::Reporter* reporter) {
112 SkScalar bigFloat = get_value(reporter, 3.4e38f);
113 SkPoint pt = { bigFloat, bigFloat };
114
115 SkScalar length = pt.length();
116 length = force_as_float(reporter, length);
117
118 // expect this to be non-finite, but dump the results if not.
119 if (SkIsFinite(length)) {
120 SkDebugf("length(%g, %g) == %g\n", pt.fX, pt.fY, length);
121 REPORTER_ASSERT(reporter, !SkIsFinite(length));
122 }
123
124 // this should succeed, even though we can't represent length
125 REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1));
126
127 // now that pt is normalized, we check its length
128 length = pt.length();
129 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1));
130 }
131
DEF_TEST(Point,reporter)132 DEF_TEST(Point, reporter) {
133 test_casts(reporter);
134
135 static const struct {
136 SkScalar fX;
137 SkScalar fY;
138 SkScalar fLength;
139 } gRec[] = {
140 { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) },
141 { 0.6f, 0.8f, SK_Scalar1 },
142 };
143
144 for (size_t i = 0; i < std::size(gRec); ++i) {
145 test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength);
146 }
147
148 test_overflow(reporter);
149 test_normalize_cannormalize_consistent(reporter);
150 }
151
DEF_TEST(Point_setLengthFast,reporter)152 DEF_TEST(Point_setLengthFast, reporter) {
153 // Scale a (1,1) point to a bunch of different lengths,
154 // making sure the slow and fast paths are within 0.1%.
155 const float tests[] = { 1.0f, 0.0f, 1.0e-37f, 3.4e38f, 42.0f, 0.00012f };
156
157 const SkPoint kOne = {1.0f, 1.0f};
158 for (unsigned i = 0; i < std::size(tests); i++) {
159 SkPoint slow = kOne, fast = kOne;
160
161 slow.setLength(tests[i]);
162 SkPointPriv::SetLengthFast(&fast, tests[i]);
163
164 if (slow.length() < FLT_MIN && fast.length() < FLT_MIN) continue;
165
166 SkScalar ratio = slow.length() / fast.length();
167 REPORTER_ASSERT(reporter, ratio > 0.999f);
168 REPORTER_ASSERT(reporter, ratio < 1.001f);
169 }
170 }
171