Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Unified Diff: trunk/tests/PointTest.cpp

Issue 14838006: change setLength and Normalize to handle when mag2 overflows a float, but the (Closed) Base URL: http://skia.googlecode.com/svn/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« trunk/src/core/SkPoint.cpp ('K') | « trunk/src/core/SkPoint.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: trunk/tests/PointTest.cpp
===================================================================
--- trunk/tests/PointTest.cpp (revision 8976)
+++ trunk/tests/PointTest.cpp (working copy)
@@ -22,6 +22,18 @@
REPORTER_ASSERT(reporter, r.asScalars() == rPtr);
}
+// Tests SkPoint::Normalize() for this (x,y)
+static void test_Normalize(skiatest::Reporter* reporter,
+ SkScalar x, SkScalar y) {
+ SkPoint point;
+ point.set(x, y);
+ SkScalar oldLength = point.length();
+ SkScalar returned = SkPoint::Normalize(&point);
+ SkScalar newLength = point.length();
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
+}
+
// Tests that SkPoint::length() and SkPoint::Length() both return
// approximately expectedLength for this (x,y).
static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y,
@@ -34,28 +46,57 @@
//See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
+
+ test_Normalize(reporter, x, y);
}
-// Tests SkPoint::Normalize() for this (x,y)
-static void test_Normalize(skiatest::Reporter* reporter,
- SkScalar x, SkScalar y) {
- SkPoint point;
- point.set(x, y);
- SkScalar oldLength = point.length();
- SkScalar returned = SkPoint::Normalize(&point);
- SkScalar newLength = point.length();
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
+// test that we handle very large values correctly. i.e. that we can
+// successfully normalize something whose mag overflows a float.
+static void test_overflow(skiatest::Reporter* reporter) {
+ SkPoint pt = { SkFloatToScalar(3.4e38f), SkFloatToScalar(3.4e38f) };
+
+ SkScalar length = pt.length();
+ REPORTER_ASSERT(reporter, !SkScalarIsFinite(length));
+
+ // this should succeed, even though we can't represent length
+ REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1));
+
+ // now that pt is normalized, we check its length
+ length = pt.length();
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1));
}
+// test that we handle very small values correctly. i.e. that we can
+// report failure if we try to normalize them.
+static void test_underflow(skiatest::Reporter* reporter) {
+ SkPoint pt = { SkFloatToScalar(1.0e-37f), SkFloatToScalar(1.0e-37f) };
+ SkPoint copy = pt;
+
+ REPORTER_ASSERT(reporter, 0 == SkPoint::Normalize(&pt));
+ REPORTER_ASSERT(reporter, pt == copy); // pt is unchanged
+
+ REPORTER_ASSERT(reporter, !pt.setLength(SK_Scalar1));
+ REPORTER_ASSERT(reporter, pt == copy); // pt is unchanged
+}
+
static void PointTest(skiatest::Reporter* reporter) {
test_casts(reporter);
- test_length(reporter, SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5));
- test_length(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f),
- SK_Scalar1);
- test_Normalize(reporter, SkIntToScalar(3), SkIntToScalar(4));
- test_Normalize(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f));
+ static const struct {
+ SkScalar fX;
+ SkScalar fY;
+ SkScalar fLength;
+ } gRec[] = {
+ { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) },
+ { SkFloatToScalar(0.6f), SkFloatToScalar(0.8f), SK_Scalar1 },
+ };
+
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
+ test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength);
+ }
+
+ test_underflow(reporter);
+ test_overflow(reporter);
}
#include "TestClassDef.h"
« trunk/src/core/SkPoint.cpp ('K') | « trunk/src/core/SkPoint.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698