epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 7 | // Unit tests for src/core/SkPoint.cpp and its header |
| 8 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 9 | #include "SkPointPriv.h" |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame] | 10 | #include "SkRect.h" |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 11 | #include "Test.h" |
| 12 | |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame] | 13 | static void test_casts(skiatest::Reporter* reporter) { |
| 14 | SkPoint p = { 0, 0 }; |
| 15 | SkRect r = { 0, 0, 0, 0 }; |
| 16 | |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 17 | const SkScalar* pPtr = reinterpret_cast<const SkScalar*>(&p); |
| 18 | const SkScalar* rPtr = reinterpret_cast<const SkScalar*>(&r); |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame] | 19 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 20 | REPORTER_ASSERT(reporter, SkPointPriv::AsScalars(p) == pPtr); |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame] | 21 | REPORTER_ASSERT(reporter, r.asScalars() == rPtr); |
| 22 | } |
| 23 | |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 24 | // Tests SkPoint::Normalize() for this (x,y) |
| 25 | static 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.com | b7b5d93 | 2012-08-24 19:53:58 +0000 | [diff] [blame] | 32 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength)); |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 33 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1)); |
| 34 | } |
| 35 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 36 | // Tests that SkPoint::length() and SkPoint::Length() both return |
| 37 | // approximately expectedLength for this (x,y). |
| 38 | static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y, |
| 39 | SkScalar expectedLength) { |
| 40 | SkPoint point; |
| 41 | point.set(x, y); |
| 42 | SkScalar s1 = point.length(); |
| 43 | SkScalar s2 = SkPoint::Length(x, y); |
| 44 | //The following should be exactly the same, but need not be. |
| 45 | //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 |
| 46 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2)); |
| 47 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength)); |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 48 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 49 | test_Normalize(reporter, x, y); |
| 50 | } |
| 51 | |
reed@google.com | 25720b4 | 2013-05-03 16:30:44 +0000 | [diff] [blame] | 52 | // Ugh. Windows compiler can dive into other .cpp files, and sometimes |
| 53 | // notices that I will generate an overflow... which is exactly the point |
| 54 | // of this test! |
| 55 | // |
| 56 | // To avoid this warning, I need to convince the compiler that I might not |
| 57 | // use that big value, hence this hacky helper function: reporter is |
| 58 | // ALWAYS non-null. (shhhhhh, don't tell the compiler that). |
| 59 | template <typename T> T get_value(skiatest::Reporter* reporter, T value) { |
| 60 | return reporter ? value : 0; |
| 61 | } |
| 62 | |
reed@google.com | fcc9ca0 | 2013-05-06 15:59:51 +0000 | [diff] [blame] | 63 | // On linux gcc, 32bit, we are seeing the compiler propagate up the value |
| 64 | // of SkPoint::length() as a double (which we use sometimes to avoid overflow |
| 65 | // during the computation), even though the signature says float (SkScalar). |
| 66 | // |
| 67 | // force_as_float is meant to capture our latest technique (horrible as |
| 68 | // it is) to force the value to be a float, so we can test whether it was |
| 69 | // finite or not. |
| 70 | static float force_as_float(skiatest::Reporter* reporter, float value) { |
| 71 | uint32_t storage; |
| 72 | memcpy(&storage, &value, 4); |
| 73 | // even the pair of memcpy calls are not sufficient, since those seem to |
| 74 | // be no-op'd, so we add a runtime tests (just like get_value) to force |
| 75 | // the compiler to give us an actual float. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | if (nullptr == reporter) { |
reed@google.com | fcc9ca0 | 2013-05-06 15:59:51 +0000 | [diff] [blame] | 77 | storage = ~storage; |
| 78 | } |
| 79 | memcpy(&value, &storage, 4); |
| 80 | return value; |
| 81 | } |
| 82 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 83 | // test that we handle very large values correctly. i.e. that we can |
| 84 | // successfully normalize something whose mag overflows a float. |
| 85 | static void test_overflow(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 86 | SkScalar bigFloat = get_value(reporter, 3.4e38f); |
reed@google.com | 25720b4 | 2013-05-03 16:30:44 +0000 | [diff] [blame] | 87 | SkPoint pt = { bigFloat, bigFloat }; |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 88 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 89 | SkScalar length = pt.length(); |
reed@google.com | fcc9ca0 | 2013-05-06 15:59:51 +0000 | [diff] [blame] | 90 | length = force_as_float(reporter, length); |
| 91 | |
reed@google.com | dc9cdf8 | 2013-05-03 18:11:00 +0000 | [diff] [blame] | 92 | // expect this to be non-finite, but dump the results if not. |
| 93 | if (SkScalarIsFinite(length)) { |
| 94 | SkDebugf("length(%g, %g) == %g\n", pt.fX, pt.fY, length); |
| 95 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(length)); |
| 96 | } |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 97 | |
| 98 | // this should succeed, even though we can't represent length |
| 99 | REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1)); |
| 100 | |
| 101 | // now that pt is normalized, we check its length |
| 102 | length = pt.length(); |
| 103 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1)); |
| 104 | } |
| 105 | |
| 106 | // test that we handle very small values correctly. i.e. that we can |
| 107 | // report failure if we try to normalize them. |
| 108 | static void test_underflow(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 109 | SkPoint pt = { 1.0e-37f, 1.0e-37f }; |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 110 | const SkPoint empty = { 0, 0 }; |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 111 | |
| 112 | REPORTER_ASSERT(reporter, 0 == SkPoint::Normalize(&pt)); |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, pt == empty); |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 114 | |
| 115 | REPORTER_ASSERT(reporter, !pt.setLength(SK_Scalar1)); |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 116 | REPORTER_ASSERT(reporter, pt == empty); |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 119 | DEF_TEST(Point, reporter) { |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame] | 120 | test_casts(reporter); |
| 121 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 122 | static const struct { |
| 123 | SkScalar fX; |
| 124 | SkScalar fY; |
| 125 | SkScalar fLength; |
| 126 | } gRec[] = { |
| 127 | { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) }, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 128 | { 0.6f, 0.8f, SK_Scalar1 }, |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 129 | }; |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 130 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 131 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 132 | test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength); |
| 133 | } |
| 134 | |
| 135 | test_underflow(reporter); |
| 136 | test_overflow(reporter); |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 137 | } |
| 138 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 139 | DEF_TEST(Point_setLengthFast, reporter) { |
| 140 | // Scale a (1,1) point to a bunch of different lengths, |
| 141 | // making sure the slow and fast paths are within 0.1%. |
| 142 | const float tests[] = { 1.0f, 0.0f, 1.0e-37f, 3.4e38f, 42.0f, 0.00012f }; |
| 143 | |
| 144 | const SkPoint kOne = {1.0f, 1.0f}; |
| 145 | for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); i++) { |
| 146 | SkPoint slow = kOne, fast = kOne; |
| 147 | |
| 148 | slow.setLength(tests[i]); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 149 | SkPointPriv::SetLengthFast(&fast, tests[i]); |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 150 | |
| 151 | if (slow.length() < FLT_MIN && fast.length() < FLT_MIN) continue; |
| 152 | |
| 153 | SkScalar ratio = slow.length() / fast.length(); |
| 154 | REPORTER_ASSERT(reporter, ratio > 0.999f); |
| 155 | REPORTER_ASSERT(reporter, ratio < 1.001f); |
| 156 | } |
| 157 | } |