From 5a5fe58595e881965c1a395885165eaccf2d44c5 Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Fri, 3 May 2013 15:59:39 +0000 Subject: change setLength and Normalize to handle when mag2 overflows a float, but the actual lenght does not. R=caryclark@google.com Review URL: https://codereview.chromium.org/14838006 git-svn-id: http://skia.googlecode.com/svn/trunk@8988 2bbb7eff-a529-9590-31e7-b0007b416f81 --- tests/PointTest.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'tests/PointTest.cpp') diff --git a/tests/PointTest.cpp b/tests/PointTest.cpp index b8f4398c20..9d4bdfd843 100644 --- a/tests/PointTest.cpp +++ b/tests/PointTest.cpp @@ -22,6 +22,18 @@ static void test_casts(skiatest::Reporter* reporter) { 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 @@ static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y, //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" -- cgit v1.2.3