OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 // Unit tests for src/core/SkPoint.cpp and its header | 8 // Unit tests for src/core/SkPoint.cpp and its header |
9 | 9 |
10 #include "SkPoint.h" | 10 #include "SkPoint.h" |
11 #include "SkRect.h" | 11 #include "SkRect.h" |
12 #include "Test.h" | 12 #include "Test.h" |
13 | 13 |
14 static void test_casts(skiatest::Reporter* reporter) { | 14 static void test_casts(skiatest::Reporter* reporter) { |
15 SkPoint p = { 0, 0 }; | 15 SkPoint p = { 0, 0 }; |
16 SkRect r = { 0, 0, 0, 0 }; | 16 SkRect r = { 0, 0, 0, 0 }; |
17 | 17 |
18 const SkScalar* pPtr = SkTCast<const SkScalar*>(&p); | 18 const SkScalar* pPtr = SkTCast<const SkScalar*>(&p); |
19 const SkScalar* rPtr = SkTCast<const SkScalar*>(&r); | 19 const SkScalar* rPtr = SkTCast<const SkScalar*>(&r); |
20 | 20 |
21 REPORTER_ASSERT(reporter, p.asScalars() == pPtr); | 21 REPORTER_ASSERT(reporter, p.asScalars() == pPtr); |
22 REPORTER_ASSERT(reporter, r.asScalars() == rPtr); | 22 REPORTER_ASSERT(reporter, r.asScalars() == rPtr); |
23 } | 23 } |
24 | 24 |
| 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(); |
| 33 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength)); |
| 34 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1)); |
| 35 } |
| 36 |
25 // Tests that SkPoint::length() and SkPoint::Length() both return | 37 // Tests that SkPoint::length() and SkPoint::Length() both return |
26 // approximately expectedLength for this (x,y). | 38 // approximately expectedLength for this (x,y). |
27 static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y, | 39 static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y, |
28 SkScalar expectedLength) { | 40 SkScalar expectedLength) { |
29 SkPoint point; | 41 SkPoint point; |
30 point.set(x, y); | 42 point.set(x, y); |
31 SkScalar s1 = point.length(); | 43 SkScalar s1 = point.length(); |
32 SkScalar s2 = SkPoint::Length(x, y); | 44 SkScalar s2 = SkPoint::Length(x, y); |
33 //The following should be exactly the same, but need not be. | 45 //The following should be exactly the same, but need not be. |
34 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 | 46 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 |
35 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2)); | 47 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2)); |
36 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength)); | 48 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength)); |
| 49 |
| 50 test_Normalize(reporter, x, y); |
37 } | 51 } |
38 | 52 |
39 // Tests SkPoint::Normalize() for this (x,y) | 53 // test that we handle very large values correctly. i.e. that we can |
40 static void test_Normalize(skiatest::Reporter* reporter, | 54 // successfully normalize something whose mag overflows a float. |
41 SkScalar x, SkScalar y) { | 55 static void test_overflow(skiatest::Reporter* reporter) { |
42 SkPoint point; | 56 SkPoint pt = { SkFloatToScalar(3.4e38f), SkFloatToScalar(3.4e38f) }; |
43 point.set(x, y); | 57 |
44 SkScalar oldLength = point.length(); | 58 SkScalar length = pt.length(); |
45 SkScalar returned = SkPoint::Normalize(&point); | 59 REPORTER_ASSERT(reporter, !SkScalarIsFinite(length)); |
46 SkScalar newLength = point.length(); | 60 |
47 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength)); | 61 // this should succeed, even though we can't represent length |
48 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1)); | 62 REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1)); |
| 63 |
| 64 // now that pt is normalized, we check its length |
| 65 length = pt.length(); |
| 66 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1)); |
| 67 } |
| 68 |
| 69 // test that we handle very small values correctly. i.e. that we can |
| 70 // report failure if we try to normalize them. |
| 71 static void test_underflow(skiatest::Reporter* reporter) { |
| 72 SkPoint pt = { SkFloatToScalar(1.0e-37f), SkFloatToScalar(1.0e-37f) }; |
| 73 SkPoint copy = pt; |
| 74 |
| 75 REPORTER_ASSERT(reporter, 0 == SkPoint::Normalize(&pt)); |
| 76 REPORTER_ASSERT(reporter, pt == copy); // pt is unchanged |
| 77 |
| 78 REPORTER_ASSERT(reporter, !pt.setLength(SK_Scalar1)); |
| 79 REPORTER_ASSERT(reporter, pt == copy); // pt is unchanged |
49 } | 80 } |
50 | 81 |
51 static void PointTest(skiatest::Reporter* reporter) { | 82 static void PointTest(skiatest::Reporter* reporter) { |
52 test_casts(reporter); | 83 test_casts(reporter); |
53 | 84 |
54 test_length(reporter, SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5)); | 85 static const struct { |
55 test_length(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f), | 86 SkScalar fX; |
56 SK_Scalar1); | 87 SkScalar fY; |
57 test_Normalize(reporter, SkIntToScalar(3), SkIntToScalar(4)); | 88 SkScalar fLength; |
58 test_Normalize(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f)); | 89 } gRec[] = { |
| 90 { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) }, |
| 91 { SkFloatToScalar(0.6f), SkFloatToScalar(0.8f), SK_Scalar1 }, |
| 92 }; |
| 93 |
| 94 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 95 test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength); |
| 96 } |
| 97 |
| 98 test_underflow(reporter); |
| 99 test_overflow(reporter); |
59 } | 100 } |
60 | 101 |
61 #include "TestClassDef.h" | 102 #include "TestClassDef.h" |
62 DEFINE_TESTCLASS("Point", PointTestClass, PointTest) | 103 DEFINE_TESTCLASS("Point", PointTestClass, PointTest) |
OLD | NEW |