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