blob: 4a8ffd9b5369e85fd72b6e16b26bf0999b7338a8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
epoger@google.com1fd56dc2011-06-15 18:04:58 +00007// Unit tests for src/core/SkPoint.cpp and its header
8
Cary Clarkdf429f32017-11-08 11:44:31 -05009#include "SkPointPriv.h"
reed@google.comc7d0ea32013-03-08 16:07:54 +000010#include "SkRect.h"
epoger@google.com1fd56dc2011-06-15 18:04:58 +000011#include "Test.h"
12
reed@google.comc7d0ea32013-03-08 16:07:54 +000013static void test_casts(skiatest::Reporter* reporter) {
14 SkPoint p = { 0, 0 };
15 SkRect r = { 0, 0, 0, 0 };
16
Mike Kleine72e7732018-06-14 14:41:22 -040017 const SkScalar* pPtr = reinterpret_cast<const SkScalar*>(&p);
18 const SkScalar* rPtr = reinterpret_cast<const SkScalar*>(&r);
reed@google.comc7d0ea32013-03-08 16:07:54 +000019
Cary Clarkdf429f32017-11-08 11:44:31 -050020 REPORTER_ASSERT(reporter, SkPointPriv::AsScalars(p) == pPtr);
reed@google.comc7d0ea32013-03-08 16:07:54 +000021 REPORTER_ASSERT(reporter, r.asScalars() == rPtr);
22}
23
epoger@google.com1fd56dc2011-06-15 18:04:58 +000024// Tests SkPoint::Normalize() for this (x,y)
25static void test_Normalize(skiatest::Reporter* reporter,
26 SkScalar x, SkScalar y) {
27 SkPoint point;
28 point.set(x, y);
29 SkScalar oldLength = point.length();
30 SkScalar returned = SkPoint::Normalize(&point);
31 SkScalar newLength = point.length();
bungeman@google.comb7b5d932012-08-24 19:53:58 +000032 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
epoger@google.com1fd56dc2011-06-15 18:04:58 +000033 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
34}
35
Mike Reed1aced502018-08-16 15:52:28 -040036static void test_normalize_cannormalize_consistent(skiatest::Reporter* reporter) {
37 const SkScalar values[] = { 1, 1e18f, 1e20f, 1e38f, SK_ScalarInfinity, SK_ScalarNaN };
38
39 for (SkScalar val : values) {
40 const SkScalar variants[] = { val, -val, SkScalarInvert(val), -SkScalarInvert(val) };
41
42 for (SkScalar v : variants) {
43 const SkPoint pts[] = { { 0, v }, { v, 0 }, { 1, v }, { v, 1 }, { v, v } };
44
45 for (SkPoint p : pts) {
46 bool can = SkPointPriv::CanNormalize(p.fX, p.fY);
47 bool nor = p.normalize();
48 REPORTER_ASSERT(reporter, can == nor);
49 }
50 }
51 }
52}
53
reed@google.com5a5fe582013-05-03 15:59:39 +000054// Tests that SkPoint::length() and SkPoint::Length() both return
55// approximately expectedLength for this (x,y).
56static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y,
57 SkScalar expectedLength) {
58 SkPoint point;
59 point.set(x, y);
60 SkScalar s1 = point.length();
61 SkScalar s2 = SkPoint::Length(x, y);
62 //The following should be exactly the same, but need not be.
63 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
64 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
65 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +000066
reed@google.com5a5fe582013-05-03 15:59:39 +000067 test_Normalize(reporter, x, y);
68}
69
reed@google.com25720b42013-05-03 16:30:44 +000070// Ugh. Windows compiler can dive into other .cpp files, and sometimes
71// notices that I will generate an overflow... which is exactly the point
72// of this test!
73//
74// To avoid this warning, I need to convince the compiler that I might not
75// use that big value, hence this hacky helper function: reporter is
76// ALWAYS non-null. (shhhhhh, don't tell the compiler that).
77template <typename T> T get_value(skiatest::Reporter* reporter, T value) {
78 return reporter ? value : 0;
79}
80
reed@google.comfcc9ca02013-05-06 15:59:51 +000081// On linux gcc, 32bit, we are seeing the compiler propagate up the value
82// of SkPoint::length() as a double (which we use sometimes to avoid overflow
83// during the computation), even though the signature says float (SkScalar).
84//
85// force_as_float is meant to capture our latest technique (horrible as
86// it is) to force the value to be a float, so we can test whether it was
87// finite or not.
88static float force_as_float(skiatest::Reporter* reporter, float value) {
89 uint32_t storage;
90 memcpy(&storage, &value, 4);
91 // even the pair of memcpy calls are not sufficient, since those seem to
92 // be no-op'd, so we add a runtime tests (just like get_value) to force
93 // the compiler to give us an actual float.
halcanary96fcdcc2015-08-27 07:41:13 -070094 if (nullptr == reporter) {
reed@google.comfcc9ca02013-05-06 15:59:51 +000095 storage = ~storage;
96 }
97 memcpy(&value, &storage, 4);
98 return value;
99}
100
reed@google.com5a5fe582013-05-03 15:59:39 +0000101// test that we handle very large values correctly. i.e. that we can
102// successfully normalize something whose mag overflows a float.
103static void test_overflow(skiatest::Reporter* reporter) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000104 SkScalar bigFloat = get_value(reporter, 3.4e38f);
reed@google.com25720b42013-05-03 16:30:44 +0000105 SkPoint pt = { bigFloat, bigFloat };
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000106
reed@google.com5a5fe582013-05-03 15:59:39 +0000107 SkScalar length = pt.length();
reed@google.comfcc9ca02013-05-06 15:59:51 +0000108 length = force_as_float(reporter, length);
109
reed@google.comdc9cdf82013-05-03 18:11:00 +0000110 // expect this to be non-finite, but dump the results if not.
111 if (SkScalarIsFinite(length)) {
112 SkDebugf("length(%g, %g) == %g\n", pt.fX, pt.fY, length);
113 REPORTER_ASSERT(reporter, !SkScalarIsFinite(length));
114 }
reed@google.com5a5fe582013-05-03 15:59:39 +0000115
116 // this should succeed, even though we can't represent length
117 REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1));
118
119 // now that pt is normalized, we check its length
120 length = pt.length();
121 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1));
122}
123
124// test that we handle very small values correctly. i.e. that we can
125// report failure if we try to normalize them.
126static void test_underflow(skiatest::Reporter* reporter) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000127 SkPoint pt = { 1.0e-37f, 1.0e-37f };
reeda8b326c2014-12-09 11:50:32 -0800128 const SkPoint empty = { 0, 0 };
reed@google.com5a5fe582013-05-03 15:59:39 +0000129
130 REPORTER_ASSERT(reporter, 0 == SkPoint::Normalize(&pt));
reeda8b326c2014-12-09 11:50:32 -0800131 REPORTER_ASSERT(reporter, pt == empty);
reed@google.com5a5fe582013-05-03 15:59:39 +0000132
133 REPORTER_ASSERT(reporter, !pt.setLength(SK_Scalar1));
reeda8b326c2014-12-09 11:50:32 -0800134 REPORTER_ASSERT(reporter, pt == empty);
reed@google.com5a5fe582013-05-03 15:59:39 +0000135}
136
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000137DEF_TEST(Point, reporter) {
reed@google.comc7d0ea32013-03-08 16:07:54 +0000138 test_casts(reporter);
139
reed@google.com5a5fe582013-05-03 15:59:39 +0000140 static const struct {
141 SkScalar fX;
142 SkScalar fY;
143 SkScalar fLength;
144 } gRec[] = {
145 { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) },
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000146 { 0.6f, 0.8f, SK_Scalar1 },
reed@google.com5a5fe582013-05-03 15:59:39 +0000147 };
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000148
reed@google.com5a5fe582013-05-03 15:59:39 +0000149 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
150 test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength);
151 }
152
153 test_underflow(reporter);
154 test_overflow(reporter);
Mike Reed1aced502018-08-16 15:52:28 -0400155 test_normalize_cannormalize_consistent(reporter);
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000156}
157
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000158DEF_TEST(Point_setLengthFast, reporter) {
159 // Scale a (1,1) point to a bunch of different lengths,
160 // making sure the slow and fast paths are within 0.1%.
161 const float tests[] = { 1.0f, 0.0f, 1.0e-37f, 3.4e38f, 42.0f, 0.00012f };
162
163 const SkPoint kOne = {1.0f, 1.0f};
164 for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); i++) {
165 SkPoint slow = kOne, fast = kOne;
166
167 slow.setLength(tests[i]);
Cary Clarkdf429f32017-11-08 11:44:31 -0500168 SkPointPriv::SetLengthFast(&fast, tests[i]);
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000169
170 if (slow.length() < FLT_MIN && fast.length() < FLT_MIN) continue;
171
172 SkScalar ratio = slow.length() / fast.length();
173 REPORTER_ASSERT(reporter, ratio > 0.999f);
174 REPORTER_ASSERT(reporter, ratio < 1.001f);
175 }
176}