Add some new PolyUtils tests.

Also:
* clean up PolyUtils checks to be correct and consistent.
* fix some bugs discovered by the unit tests.

Bug: skia:
Change-Id: I1a8e07d13cb44fecc67344154dc1002f3f910f5d
Reviewed-on: https://skia-review.googlesource.com/138592
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/tests/OffsetSimplePolyTest.cpp b/tests/OffsetSimplePolyTest.cpp
index 547dc9e..0f4f18f 100644
--- a/tests/OffsetSimplePolyTest.cpp
+++ b/tests/OffsetSimplePolyTest.cpp
@@ -7,30 +7,6 @@
 #include "Test.h"
 #include "SkPolyUtils.h"
 
-static bool is_convex(const SkTDArray<SkPoint>& poly) {
-    if (poly.count() < 3) {
-        return false;
-    }
-
-    SkVector v0 = poly[0] - poly[poly.count() - 1];
-    SkVector v1 = poly[1] - poly[poly.count() - 1];
-    SkScalar winding = v0.cross(v1);
-
-    for (int i = 0; i < poly.count()-1; ++i) {
-        int j = i + 1;
-        int k = (i + 2) % poly.count();
-
-        SkVector v0 = poly[j] - poly[i];
-        SkVector v1 = poly[k] - poly[i];
-        SkScalar perpDot = v0.cross(v1);
-        if (winding*perpDot < 0) {
-            return false;
-        }
-    }
-
-    return true;
-}
-
 DEF_TEST(OffsetSimplePoly, reporter) {
     SkTDArray<SkPoint> rrectPoly;
 
@@ -58,18 +34,18 @@
     *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
     *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
     *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
-    REPORTER_ASSERT(reporter, is_convex(rrectPoly));
+    REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.count()));
 
     // inset a little
     SkTDArray<SkPoint> offsetPoly;
-    bool result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 3, &offsetPoly);
+    bool result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 3, &offsetPoly);
     REPORTER_ASSERT(reporter, result);
-    REPORTER_ASSERT(reporter, is_convex(offsetPoly));
+    REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
 
     // inset to rect
-    result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 10, &offsetPoly);
+    result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 10, &offsetPoly);
     REPORTER_ASSERT(reporter, result);
-    REPORTER_ASSERT(reporter, is_convex(offsetPoly));
+    REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
     REPORTER_ASSERT(reporter, offsetPoly.count() == 4);
     if (offsetPoly.count() == 4) {
         REPORTER_ASSERT(reporter, offsetPoly[0].equals(-95, 45));
@@ -80,9 +56,9 @@
 
     // just to full inset
     // fails, but outputs a line segment
-    result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 55, &offsetPoly);
+    result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 55, &offsetPoly);
     REPORTER_ASSERT(reporter, !result);
-    REPORTER_ASSERT(reporter, !is_convex(offsetPoly));
+    REPORTER_ASSERT(reporter, !SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
     REPORTER_ASSERT(reporter, offsetPoly.count() == 2);
     if (offsetPoly.count() == 2) {
         REPORTER_ASSERT(reporter, offsetPoly[0].equals(-50, 0));
@@ -90,7 +66,7 @@
     }
 
     // past full inset
-    result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 75, &offsetPoly);
+    result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 75, &offsetPoly);
     REPORTER_ASSERT(reporter, !result);
 
     // troublesome case
@@ -122,12 +98,13 @@
     *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f);
     *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f);
     *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f);
-    REPORTER_ASSERT(reporter, is_convex(clippedRRectPoly));
+    REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(),
+                                                clippedRRectPoly.count()));
 
-    result = SkOffsetSimplePolygon(&clippedRRectPoly[0], clippedRRectPoly.count(), 32.3699417f,
+    result = SkOffsetSimplePolygon(clippedRRectPoly.begin(), clippedRRectPoly.count(), 32.3699417f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
-    REPORTER_ASSERT(reporter, is_convex(offsetPoly));
+    REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
 
     ////////////////////////////////////////////////////////////////////////////////
     // Concave tests
@@ -145,46 +122,54 @@
     *starPoly.push() = SkPoint::Make(-28.86f, 0.0f);
     *starPoly.push() = SkPoint::Make(-43.30f, -25.0f);
     *starPoly.push() = SkPoint::Make(-14.43f, -25.0f);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(starPoly.begin(), starPoly.count()));
 
     // try a variety of distances
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 0.1f,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 0.1f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 5.665f,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 5.665f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 28,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 28,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
     // down to a point
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 28.866f,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 28.866f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, !result);
 
     // and past
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 50.5f,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 50.5f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, !result);
 
     // and now out
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -0.1f,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -0.1f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -5.6665f,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -5.6665f,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -50,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -50,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
-    result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -100,
+    result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -100,
                                    &offsetPoly);
     REPORTER_ASSERT(reporter, result);
+    REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
 
     SkTDArray<SkPoint> intersectingPoly;
     *intersectingPoly.push() = SkPoint::Make(0.0f, -50.0f);
@@ -201,6 +186,6 @@
     *intersectingPoly.push() = SkPoint::Make(-14.43f, -25.0f);
 
     // SkOffsetSimplePolygon now assumes that the input is simple, so we'll just check for that
-    result = SkIsSimplePolygon(&intersectingPoly[0], intersectingPoly.count());
+    result = SkIsSimplePolygon(intersectingPoly.begin(), intersectingPoly.count());
     REPORTER_ASSERT(reporter, !result);
 }