Make fuzzers use cleaner interface

signalBoring() no longer exists.  When the fuzzer runs out of randomness,
it just returns 0.  Fuzzers should not go into infinite loops if this
happens.  do while loops are particularly error-prone.

BUG=skia:

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=3963

Change-Id: Iebcfc14cc6b0a19c5dd015cd39875c81fa44003e
Reviewed-on: https://skia-review.googlesource.com/3963
Commit-Queue: Kevin Lubick <[email protected]>
Reviewed-by: Mike Klein <[email protected]>
diff --git a/fuzz/FuzzGradients.cpp b/fuzz/FuzzGradients.cpp
index 6ed4b7c..df36c7c 100644
--- a/fuzz/FuzzGradients.cpp
+++ b/fuzz/FuzzGradients.cpp
@@ -12,48 +12,27 @@
 #include "SkTLazy.h"
 
 #include <algorithm>
+#include <vector>
 
 const int MAX_COUNT = 400;
 
-bool makeMatrix(Fuzz* fuzz, SkMatrix* m) {
-    SkScalar scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2;
-    if (!fuzz->next(&scaleX) ||
-        !fuzz->next(&skewX)  ||
-        !fuzz->next(&transX) ||
-        !fuzz->next(&skewY)  ||
-        !fuzz->next(&scaleY) ||
-        !fuzz->next(&transY) ||
-        !fuzz->next(&persp0) ||
-        !fuzz->next(&persp1) ||
-        !fuzz->next(&persp2)) {
-        return false;
-    }
-    m->setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2);
-    return true;
+void makeMatrix(Fuzz* fuzz, SkMatrix* m) {
+    m->setAll(fuzz->next<SkScalar>(), fuzz->next<SkScalar>(), fuzz->next<SkScalar>(),
+              fuzz->next<SkScalar>(), fuzz->next<SkScalar>(), fuzz->next<SkScalar>(),
+              fuzz->next<SkScalar>(), fuzz->next<SkScalar>(), fuzz->next<SkScalar>());
 }
 
-bool initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors,
-    std::vector<SkScalar>* pos, SkShader::TileMode* mode) {
-    if (fuzz->remaining() < sizeof(uint32_t)) {
-        return false;
-    }
-    uint32_t count = fuzz->nextRangeU(0, MAX_COUNT);
+void initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors,
+                        std::vector<SkScalar>* pos, SkShader::TileMode* mode) {
+    int count = fuzz->nextRange(0, MAX_COUNT);
 
-    if (fuzz->remaining() < sizeof(uint32_t)) {
-        return false;
-    }
-    *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3));
+    *mode = static_cast<SkShader::TileMode>(fuzz->nextRange(0, 2));
 
     colors->clear();
     pos   ->clear();
-    for (uint32_t i = 0; i < count; i++) {
-        SkColor c;
-        SkScalar s;
-        if (!fuzz->next(&c) || !fuzz->next(&s)) {
-            return false;
-        }
-        colors->push_back(c);
-        pos   ->push_back(s);
+    for (int i = 0; i < count; i++) {
+        colors->push_back(fuzz->next<SkColor>());
+        pos   ->push_back(fuzz->next<SkScalar>());
     }
     if (count) {
         std::sort(pos->begin(), pos->end());
@@ -61,208 +40,150 @@
         (*pos)[count - 1] = 1;
         (*pos)[0]         = 0;
     }
-    return true;
 }
 
 void fuzzLinearGradient(Fuzz* fuzz) {
-        SkScalar a, b, c, d;
-        bool useLocalMatrix, useGlobalMatrix;
-        if (!fuzz->next(&a)               ||
-            !fuzz->next(&b)               ||
-            !fuzz->next(&c)               ||
-            !fuzz->next(&d)               ||
-            !fuzz->next(&useLocalMatrix)  ||
-            !fuzz->next(&useGlobalMatrix)) {
-            return;
-        }
-        SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)};
+    SkPoint pts[2] = {SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>()),
+                      SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>())};
+    bool useLocalMatrix  = fuzz->next<bool>();
+    bool useGlobalMatrix = fuzz->next<bool>();
 
-        std::vector<SkColor> colors;
-        std::vector<SkScalar> pos;
-        SkShader::TileMode mode;
-        if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
-            return;
-        }
+    std::vector<SkColor> colors;
+    std::vector<SkScalar> pos;
+    SkShader::TileMode mode;
+    initGradientParams(fuzz, &colors, &pos, &mode);
 
-        SkPaint p;
-        uint32_t flags;
-        if (!fuzz->next(&flags)) {
-            return;
-        }
+    SkPaint p;
+    uint32_t flags = fuzz->next<uint32_t>();
 
-        SkTLazy<SkMatrix> localMatrix;
-        if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) {
-            return;
-        }
-        p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(),
-            colors.size(), mode, flags, localMatrix.getMaybeNull()));
+    SkTLazy<SkMatrix> localMatrix;
+    if (useLocalMatrix) {
+        makeMatrix(fuzz, localMatrix.init());
+    }
+    p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(),
+        colors.size(), mode, flags, localMatrix.getMaybeNull()));
 
-        sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
-        if (useGlobalMatrix) {
-            SkMatrix gm;
-            if (!makeMatrix(fuzz, &gm)) {
-                return;
-            }
-            SkCanvas* c = surface->getCanvas();
-            c->setMatrix(gm);
-            c->drawPaint(p);
-        } else {
-            surface->getCanvas()->drawPaint(p);
-        }
+    sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
+    if (useGlobalMatrix) {
+        SkMatrix gm;
+        makeMatrix(fuzz, &gm);
+        SkCanvas* c = surface->getCanvas();
+        c->setMatrix(gm);
+        c->drawPaint(p);
+    } else {
+        surface->getCanvas()->drawPaint(p);
+    }
 }
 
 void fuzzRadialGradient(Fuzz* fuzz) {
-        SkScalar a, b, radius;
-        bool useLocalMatrix, useGlobalMatrix;
-        if (!fuzz->next(&a)               ||
-            !fuzz->next(&b)               ||
-            !fuzz->next(&radius)          ||
-            !fuzz->next(&useLocalMatrix)  ||
-            !fuzz->next(&useGlobalMatrix)) {
-            return;
-        }
-        SkPoint center = SkPoint::Make(a,b);
-
-        std::vector<SkColor> colors;
-        std::vector<SkScalar> pos;
-        SkShader::TileMode mode;
-        if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
-            return;
-        }
-
-        SkPaint p;
-        uint32_t flags;
-        if (!fuzz->next(&flags)) {
-            return;
-        }
-
-        SkTLazy<SkMatrix> localMatrix;
-        if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) {
-            return;
-        }
-        p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(),
-            pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull()));
+    SkPoint center = SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>());
+    SkScalar radius      = fuzz->next<SkScalar>();
+    bool useLocalMatrix  = fuzz->next<bool>();
+    bool useGlobalMatrix = fuzz->next<bool>();
 
 
-        sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
-        if (useGlobalMatrix) {
-            SkMatrix gm;
-            if (!makeMatrix(fuzz, &gm)) {
-                return;
-            }
-            SkCanvas* c = surface->getCanvas();
-            c->setMatrix(gm);
-            c->drawPaint(p);
-        } else {
-            surface->getCanvas()->drawPaint(p);
-        }
+    std::vector<SkColor> colors;
+    std::vector<SkScalar> pos;
+    SkShader::TileMode mode;
+    initGradientParams(fuzz, &colors, &pos, &mode);
+
+    SkPaint p;
+    uint32_t flags = fuzz->next<uint32_t>();
+
+    SkTLazy<SkMatrix> localMatrix;
+    if (useLocalMatrix) {
+        makeMatrix(fuzz, localMatrix.init());
+    }
+    p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(),
+        pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull()));
+
+
+    sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
+    if (useGlobalMatrix) {
+        SkMatrix gm;
+        makeMatrix(fuzz, &gm);
+        SkCanvas* c = surface->getCanvas();
+        c->setMatrix(gm);
+        c->drawPaint(p);
+    } else {
+        surface->getCanvas()->drawPaint(p);
+    }
 }
 
 void fuzzTwoPointConicalGradient(Fuzz* fuzz) {
-        SkScalar a, b, startRadius, c, d, endRadius;
-        bool useLocalMatrix, useGlobalMatrix;
-        if (!fuzz->next(&a)               ||
-            !fuzz->next(&b)               ||
-            !fuzz->next(&startRadius)     ||
-            !fuzz->next(&c)               ||
-            !fuzz->next(&d)               ||
-            !fuzz->next(&endRadius)       ||
-            !fuzz->next(&useLocalMatrix)  ||
-            !fuzz->next(&useGlobalMatrix)) {
-            return;
-        }
-        SkPoint start = SkPoint::Make(a, b);
-        SkPoint end = SkPoint::Make(c, d);
+    SkPoint start = SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>());
+    SkPoint end = SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>());
+    SkScalar startRadius = fuzz->next<SkScalar>();
+    SkScalar endRadius   = fuzz->next<SkScalar>();
+    bool useLocalMatrix  = fuzz->next<bool>();
+    bool useGlobalMatrix = fuzz->next<bool>();
 
-        std::vector<SkColor> colors;
-        std::vector<SkScalar> pos;
-        SkShader::TileMode mode;
-        if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
-            return;
-        }
+    std::vector<SkColor> colors;
+    std::vector<SkScalar> pos;
+    SkShader::TileMode mode;
+    initGradientParams(fuzz, &colors, &pos, &mode);
 
-        SkPaint p;
-        uint32_t flags;
-        if (!fuzz->next(&flags)) {
-            return;
-        }
+    SkPaint p;
+    uint32_t flags = fuzz->next<uint32_t>();
 
-        SkTLazy<SkMatrix> localMatrix;
-        if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) {
-            return;
-        }
-        p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius,
-            end, endRadius, colors.data(), pos.data(), colors.size(), mode,
-            flags, localMatrix.getMaybeNull()));
+    SkTLazy<SkMatrix> localMatrix;
+    if (useLocalMatrix) {
+        makeMatrix(fuzz, localMatrix.init());
+    }
+    p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius,
+        end, endRadius, colors.data(), pos.data(), colors.size(), mode,
+        flags, localMatrix.getMaybeNull()));
 
-        sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
-        if (useGlobalMatrix) {
-            SkMatrix gm;
-            if (!makeMatrix(fuzz, &gm)) {
-                return;
-            }
-            SkCanvas* c = surface->getCanvas();
-            c->setMatrix(gm);
-            c->drawPaint(p);
-        } else {
-            surface->getCanvas()->drawPaint(p);
-        }
+    sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
+    if (useGlobalMatrix) {
+        SkMatrix gm;
+        makeMatrix(fuzz, &gm);
+        SkCanvas* c = surface->getCanvas();
+        c->setMatrix(gm);
+        c->drawPaint(p);
+    } else {
+        surface->getCanvas()->drawPaint(p);
+    }
 }
 
 void fuzzSweepGradient(Fuzz* fuzz) {
-        SkScalar cx, cy;
-        bool useLocalMatrix, useGlobalMatrix;
-        if (!fuzz->next(&cx)              ||
-            !fuzz->next(&cy)              ||
-            !fuzz->next(&useLocalMatrix)  ||
-            !fuzz->next(&useGlobalMatrix)) {
-            return;
-        }
+    SkScalar cx = fuzz->next<SkScalar>();
+    SkScalar cy = fuzz->next<SkScalar>();
+    bool useLocalMatrix  = fuzz->next<bool>();
+    bool useGlobalMatrix = fuzz->next<bool>();
 
-        std::vector<SkColor> colors;
-        std::vector<SkScalar> pos;
-        SkShader::TileMode mode;
-        if (!initGradientParams(fuzz, &colors, &pos, &mode)) {
-            return;
-        }
+    std::vector<SkColor> colors;
+    std::vector<SkScalar> pos;
+    SkShader::TileMode mode;
+    initGradientParams(fuzz, &colors, &pos, &mode);
 
-        SkPaint p;
-        if (useLocalMatrix) {
-            SkMatrix m;
-            if (!makeMatrix(fuzz, &m)) {
-                return;
-            }
-            uint32_t flags;
-            if (!fuzz->next(&flags)) {
-                return;
-            }
-            p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
-                pos.data(), colors.size(), flags, &m));
-        } else {
-            p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
-                pos.data(), colors.size()));
-        }
+    SkPaint p;
+    if (useLocalMatrix) {
+        SkMatrix m;
+        makeMatrix(fuzz, &m);
+        uint32_t flags = fuzz->next<uint32_t>();
 
+        p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
+            pos.data(), colors.size(), flags, &m));
+    } else {
+        p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
+            pos.data(), colors.size()));
+    }
 
-        sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
-        if (useGlobalMatrix) {
-            SkMatrix gm;
-            if (!makeMatrix(fuzz, &gm)) {
-                return;
-            }
-            SkCanvas* c = surface->getCanvas();
-            c->setMatrix(gm);
-            c->drawPaint(p);
-        } else {
-            surface->getCanvas()->drawPaint(p);
-        }
+    sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
+    if (useGlobalMatrix) {
+        SkMatrix gm;
+        makeMatrix(fuzz, &gm);
+        SkCanvas* c = surface->getCanvas();
+        c->setMatrix(gm);
+        c->drawPaint(p);
+    } else {
+        surface->getCanvas()->drawPaint(p);
+    }
 }
 
 DEF_FUZZ(Gradients, fuzz) {
-    uint8_t i;
-    if (!fuzz->next(&i)) {
-        return;
-    }
+    uint8_t i = fuzz->next<uint8_t>();
 
     switch(i) {
         case 0: