1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "PathOpsExtendedTest.h"
9#include "PathOpsThreadedCommon.h"
10#include "SkBitmap.h"
11#include "SkCanvas.h"
12#include "SkForceLinking.h"
13#include "SkMatrix.h"
14#include "SkPaint.h"
15#include "SkRTConf.h"
16#include "SkStream.h"
17#include "SkTaskGroup.h"
18#include "SkThread.h"
19
20#ifdef SK_BUILD_FOR_MAC
21#include <sys/sysctl.h>
22#endif
23
24__SK_FORCE_IMAGE_DECODER_LINKING;
25
26DEFINE_bool2(runFail, f, false, "run tests known to fail.");
27
28static const char marker[] =
29    "</div>\n"
30    "\n"
31    "<script type=\"text/javascript\">\n"
32    "\n"
33    "var testDivs = [\n";
34
35static const char* opStrs[] = {
36    "kDifference_PathOp",
37    "kIntersect_PathOp",
38    "kUnion_PathOp",
39    "kXor_PathOp",
40    "kReverseDifference_PathOp",
41};
42
43static const char* opSuffixes[] = {
44    "d",
45    "i",
46    "u",
47    "o",
48};
49
50static bool gShowPath = false;
51static bool gComparePathsAssert = true;
52static bool gPathStrAssert = true;
53
54#if DEBUG_SHOW_TEST_NAME
55static void showPathData(const SkPath& path) {
56    SkPath::RawIter iter(path);
57    uint8_t verb;
58    SkPoint pts[4];
59    SkPoint firstPt = {0, 0}, lastPt = {0, 0};
60    bool firstPtSet = false;
61    bool lastPtSet = true;
62    while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
63        switch (verb) {
64            case SkPath::kMove_Verb:
65                if (firstPtSet && lastPtSet && firstPt != lastPt) {
66                    SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
67                            firstPt.fX, firstPt.fY);
68                    lastPtSet = false;
69                }
70                firstPt = pts[0];
71                firstPtSet = true;
72                continue;
73            case SkPath::kLine_Verb:
74                SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
75                        pts[1].fX, pts[1].fY);
76                lastPt = pts[1];
77                lastPtSet = true;
78                break;
79            case SkPath::kQuad_Verb:
80                SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
81                        pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
82                lastPt = pts[2];
83                lastPtSet = true;
84                break;
85            case SkPath::kCubic_Verb:
86                SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
87                        pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
88                        pts[3].fX, pts[3].fY);
89                lastPt = pts[3];
90                lastPtSet = true;
91                break;
92            case SkPath::kClose_Verb:
93                if (firstPtSet && lastPtSet && firstPt != lastPt) {
94                    SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
95                            firstPt.fX, firstPt.fY);
96                }
97                firstPtSet = lastPtSet = false;
98                break;
99            default:
100                SkDEBUGFAIL("bad verb");
101                return;
102        }
103    }
104    if (firstPtSet && lastPtSet && firstPt != lastPt) {
105        SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
106                firstPt.fX, firstPt.fY);
107    }
108}
109#endif
110
111void showOp(const SkPathOp op) {
112    switch (op) {
113        case kDifference_PathOp:
114            SkDebugf("op difference\n");
115            break;
116        case kIntersect_PathOp:
117            SkDebugf("op intersect\n");
118            break;
119        case kUnion_PathOp:
120            SkDebugf("op union\n");
121            break;
122        case kXOR_PathOp:
123            SkDebugf("op xor\n");
124            break;
125        case kReverseDifference_PathOp:
126            SkDebugf("op reverse difference\n");
127            break;
128        default:
129            SkASSERT(0);
130    }
131}
132
133#if DEBUG_SHOW_TEST_NAME
134static char hexorator(int x) {
135    if (x < 10) {
136        return x + '0';
137    }
138    x -= 10;
139    SkASSERT(x < 26);
140    return x + 'A';
141}
142#endif
143
144void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
145#if DEBUG_SHOW_TEST_NAME
146    state->fSerialNo[0] = hexorator(state->fA);
147    state->fSerialNo[1] = hexorator(state->fB);
148    state->fSerialNo[2] = hexorator(state->fC);
149    state->fSerialNo[3] = hexorator(state->fD);
150    state->fSerialNo[4] = hexorator(a);
151    state->fSerialNo[5] = hexorator(b);
152    state->fSerialNo[6] = hexorator(c);
153    state->fSerialNo[7] = hexorator(d);
154    state->fSerialNo[8] = '\0';
155    SkDebugf("%s\n", state->fSerialNo);
156    if (strcmp(state->fSerialNo, state->fKey) == 0) {
157        SkDebugf("%s\n", state->fPathStr);
158    }
159#endif
160}
161
162const int bitWidth = 64;
163const int bitHeight = 64;
164
165static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
166    SkRect larger = one.getBounds();
167    larger.join(two.getBounds());
168    SkScalar largerWidth = larger.width();
169    if (largerWidth < 4) {
170        largerWidth = 4;
171    }
172    SkScalar largerHeight = larger.height();
173    if (largerHeight < 4) {
174        largerHeight = 4;
175    }
176    SkScalar hScale = (bitWidth - 2) / largerWidth;
177    SkScalar vScale = (bitHeight - 2) / largerHeight;
178    scale.reset();
179    scale.preScale(hScale, vScale);
180}
181
182static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
183        int& error2x2) {
184    if (bits.width() == 0) {
185        bits.allocN32Pixels(bitWidth * 2, bitHeight);
186    }
187    SkCanvas canvas(bits);
188    canvas.drawColor(SK_ColorWHITE);
189    SkPaint paint;
190    canvas.save();
191    const SkRect& bounds1 = scaledOne.getBounds();
192    canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
193    canvas.drawPath(scaledOne, paint);
194    canvas.restore();
195    canvas.save();
196    canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
197    canvas.drawPath(scaledTwo, paint);
198    canvas.restore();
199    int errors2 = 0;
200    int errors = 0;
201    for (int y = 0; y < bitHeight - 1; ++y) {
202        uint32_t* addr1 = bits.getAddr32(0, y);
203        uint32_t* addr2 = bits.getAddr32(0, y + 1);
204        uint32_t* addr3 = bits.getAddr32(bitWidth, y);
205        uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
206        for (int x = 0; x < bitWidth - 1; ++x) {
207            // count 2x2 blocks
208            bool err = addr1[x] != addr3[x];
209            if (err) {
210                errors2 += addr1[x + 1] != addr3[x + 1]
211                        && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
212                errors++;
213            }
214        }
215    }
216    error2x2 = errors2;
217    return errors;
218}
219
220static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
221        SkPath& scaledTwo, int& error2x2) {
222    SkMatrix scale;
223    scaleMatrix(one, two, scale);
224    one.transform(scale, &scaledOne);
225    two.transform(scale, &scaledTwo);
226    return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
227}
228
229bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
230    if (!drawPaths) {
231        return true;
232    }
233    const SkRect& bounds1 = one.getBounds();
234    const SkRect& bounds2 = two.getBounds();
235    SkRect larger = bounds1;
236    larger.join(bounds2);
237    SkBitmap bits;
238    char out[256];
239    int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
240    if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
241        return false;
242    }
243    int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
244    if (bitHeight >= (int) sizeof(out)) {
245        return false;
246    }
247    bits.allocN32Pixels(bitWidth * 2, bitHeight);
248    SkCanvas canvas(bits);
249    canvas.drawColor(SK_ColorWHITE);
250    SkPaint paint;
251    canvas.save();
252    canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
253    canvas.drawPath(one, paint);
254    canvas.restore();
255    canvas.save();
256    canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
257    canvas.drawPath(two, paint);
258    canvas.restore();
259    for (int y = 0; y < bitHeight; ++y) {
260        uint32_t* addr1 = bits.getAddr32(0, y);
261        int x;
262        char* outPtr = out;
263        for (x = 0; x < bitWidth; ++x) {
264            *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
265        }
266        *outPtr++ = '|';
267        for (x = bitWidth; x < bitWidth * 2; ++x) {
268            *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
269        }
270        *outPtr++ = '\0';
271        SkDebugf("%s\n", out);
272    }
273    return true;
274}
275
276static int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
277        const SkPath& two, SkBitmap& bitmap) {
278    int errors2x2;
279    SkPath scaledOne, scaledTwo;
280    (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
281    if (errors2x2 == 0) {
282        return 0;
283    }
284    const int MAX_ERRORS = 9;
285    REPORTER_ASSERT(reporter, errors2x2 <= MAX_ERRORS || !gComparePathsAssert);
286    return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
287}
288
289const int gTestFirst = 4;
290static int gTestNo = gTestFirst;
291static SkTDArray<SkPathOp> gTestOp;
292
293static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
294        const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
295        const SkPathOp shapeOp, const SkMatrix& scale) {
296    SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
297    SkString defaultTestName;
298    if (!testName) {
299        defaultTestName.printf("xOp%d%s", gTestNo, opSuffixes[shapeOp]);
300        testName = defaultTestName.c_str();
301    }
302    SkDebugf("static void %s(skiatest::Reporter* reporter, const char* filename) {\n", testName);
303    *gTestOp.append() = shapeOp;
304    ++gTestNo;
305    SkDebugf("    SkPath path, pathB;\n");
306#if DEBUG_SHOW_TEST_NAME
307    SkPathOpsDebug::ShowOnePath(a, "path", false);
308    SkPathOpsDebug::ShowOnePath(b, "pathB", false);
309#endif
310    SkDebugf("    testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
311    SkDebugf("}\n");
312    drawAsciiPaths(scaledOne, scaledTwo, true);
313}
314
315void ShowTestArray() {
316    for (int x = gTestFirst; x < gTestNo; ++x) {
317        SkDebugf("    TEST(xOp%d%s),\n", x, opSuffixes[gTestOp[x - gTestFirst]]);
318    }
319}
320
321SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
322SK_DECLARE_STATIC_MUTEX(compareDebugOut4);
323static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
324        const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
325        const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale) {
326    int errors2x2;
327    (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
328    if (errors2x2 == 0) {
329        if (gShowPath) {
330            showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
331        }
332        return 0;
333    }
334    const int MAX_ERRORS = 8;
335    if (errors2x2 > MAX_ERRORS && gComparePathsAssert) {
336        SkAutoMutexAcquire autoM(compareDebugOut3);
337        SkDebugf("\n*** this test fails ***\n");
338        showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
339        REPORTER_ASSERT(reporter, 0);
340    } else if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) {
341        SkAutoMutexAcquire autoM(compareDebugOut4);
342        showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
343    }
344    return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
345}
346
347// Default values for when reporter->verbose() is false.
348static int testNumber = 55;
349static const char* testName = "pathOpTest";
350
351static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
352    outFile.writeText(testName);
353    outFile.writeDecAsText(testNumber);
354    ++testNumber;
355    if (nameSuffix) {
356        outFile.writeText(nameSuffix);
357    }
358}
359
360static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
361        const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
362#if 0
363    outFile.writeText("<div id=\"");
364    writeTestName(nameSuffix, outFile);
365    outFile.writeText("\">\n");
366    if (pathPrefix) {
367        outFile.writeText(pathPrefix);
368    }
369    outFile.writeText(pathStr);
370    outFile.writeText("</div>\n\n");
371
372    outFile.writeText(marker);
373    outFile.writeText("    ");
374    writeTestName(nameSuffix, outFile);
375    outFile.writeText(",\n\n\n");
376#endif
377    outFile.writeText("static void ");
378    writeTestName(nameSuffix, outFile);
379    outFile.writeText("(skiatest::Reporter* reporter) {\n    SkPath path");
380    if (twoPaths) {
381        outFile.writeText(", pathB");
382    }
383    outFile.writeText(";\n");
384    if (pathPrefix) {
385        outFile.writeText(pathPrefix);
386    }
387    outFile.writeText(pathStr);
388    outFile.writeText("    ");
389    outFile.writeText(testFunction);
390    outFile.writeText("\n}\n\n");
391#if 0
392    outFile.writeText("static void (*firstTest)() = ");
393    writeTestName(nameSuffix, outFile);
394    outFile.writeText(";\n\n");
395
396    outFile.writeText("static struct {\n");
397    outFile.writeText("    void (*fun)();\n");
398    outFile.writeText("    const char* str;\n");
399    outFile.writeText("} tests[] = {\n");
400    outFile.writeText("    TEST(");
401    writeTestName(nameSuffix, outFile);
402    outFile.writeText("),\n");
403#endif
404    outFile.flush();
405}
406
407SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
408bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
409                  const char* pathStr) {
410    SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
411    path.setFillType(fillType);
412#if DEBUG_SHOW_TEST_NAME
413    if (gShowPath) {
414        SkPathOpsDebug::ShowOnePath(path, "path", false);
415    }
416#endif
417    if (!Simplify(path, &out)) {
418        SkDebugf("%s did not expect failure\n", __FUNCTION__);
419        REPORTER_ASSERT(state.fReporter, 0);
420        return false;
421    }
422    if (!state.fReporter->verbose()) {
423        return true;
424    }
425    int result = comparePaths(state.fReporter, NULL, path, out, *state.fBitmap);
426    if (result && gPathStrAssert) {
427        SkAutoMutexAcquire autoM(simplifyDebugOut);
428        char temp[8192];
429        sk_bzero(temp, sizeof(temp));
430        SkMemoryWStream stream(temp, sizeof(temp));
431        const char* pathPrefix = NULL;
432        const char* nameSuffix = NULL;
433        if (fillType == SkPath::kEvenOdd_FillType) {
434            pathPrefix = "    path.setFillType(SkPath::kEvenOdd_FillType);\n";
435            nameSuffix = "x";
436        }
437        const char testFunction[] = "testSimplify(reporter, path);";
438        outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
439        SkDebugf(temp);
440        REPORTER_ASSERT(state.fReporter, 0);
441    }
442    state.fReporter->bumpTestCount();
443    return result == 0;
444}
445
446bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
447#if DEBUG_SHOW_TEST_NAME
448    showPathData(path);
449#endif
450    SkPath out;
451    if (!Simplify(path, &out)) {
452        SkDebugf("%s did not expect failure\n", __FUNCTION__);
453        REPORTER_ASSERT(reporter, 0);
454        return false;
455    }
456    SkBitmap bitmap;
457    int result = comparePaths(reporter, filename, path, out, bitmap);
458    if (result && gPathStrAssert) {
459        REPORTER_ASSERT(reporter, 0);
460    }
461    reporter->bumpTestCount();
462    return result == 0;
463}
464
465#if DEBUG_SHOW_TEST_NAME
466static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
467    SkDebugf("\n");
468    showPathData(a);
469    showOp(shapeOp);
470    showPathData(b);
471}
472#endif
473
474static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
475                 const SkPathOp shapeOp, const char* testName, bool threaded) {
476#if DEBUG_SHOW_TEST_NAME
477    showName(a, b, shapeOp);
478#endif
479    SkPath out;
480    if (!Op(a, b, shapeOp, &out) ) {
481        SkDebugf("%s did not expect failure\n", __FUNCTION__);
482        REPORTER_ASSERT(reporter, 0);
483        return false;
484    }
485    if (threaded && !reporter->verbose()) {
486        return true;
487    }
488    SkPath pathOut, scaledPathOut;
489    SkRegion rgnA, rgnB, openClip, rgnOut;
490    openClip.setRect(-16000, -16000, 16000, 16000);
491    rgnA.setPath(a, openClip);
492    rgnB.setPath(b, openClip);
493    rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
494    rgnOut.getBoundaryPath(&pathOut);
495
496    SkMatrix scale;
497    scaleMatrix(a, b, scale);
498    SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
499    SkPath scaledA, scaledB;
500    scaledA.addPath(a, scale);
501    scaledA.setFillType(a.getFillType());
502    scaledB.addPath(b, scale);
503    scaledB.setFillType(b.getFillType());
504    scaledRgnA.setPath(scaledA, openClip);
505    scaledRgnB.setPath(scaledB, openClip);
506    scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
507    scaledRgnOut.getBoundaryPath(&scaledPathOut);
508    SkBitmap bitmap;
509    SkPath scaledOut;
510    scaledOut.addPath(out, scale);
511    scaledOut.setFillType(out.getFillType());
512    int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
513            a, b, shapeOp, scale);
514    if (result && gPathStrAssert) {
515        REPORTER_ASSERT(reporter, 0);
516    }
517    reporter->bumpTestCount();
518    return result == 0;
519}
520
521bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
522                 const SkPathOp shapeOp, const char* testName) {
523    return innerPathOp(reporter, a, b, shapeOp, testName, false);
524}
525
526bool testPathFailOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
527                 const SkPathOp shapeOp, const char* testName) {
528#if DEBUG_SHOW_TEST_NAME
529    showName(a, b, shapeOp);
530#endif
531    SkPath out;
532    if (Op(a, b, shapeOp, &out) ) {
533        SkDebugf("%s test is expected to fail\n", __FUNCTION__);
534        REPORTER_ASSERT(reporter, 0);
535        return false;
536    }
537    return true;
538}
539
540bool testThreadedPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
541                 const SkPathOp shapeOp, const char* testName) {
542    return innerPathOp(reporter, a, b, shapeOp, testName, true);
543}
544
545SK_DECLARE_STATIC_MUTEX(gMutex);
546
547void initializeTests(skiatest::Reporter* reporter, const char* test) {
548#if 0  // doesn't work yet
549    SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
550    SK_CONF_SET("images.png.suppressDecoderWarnings", true);
551#endif
552    if (reporter->verbose()) {
553        SkAutoMutexAcquire lock(gMutex);
554        testName = test;
555        size_t testNameSize = strlen(test);
556        SkFILEStream inFile("../../experimental/Intersection/op.htm");
557        if (inFile.isValid()) {
558            SkTDArray<char> inData;
559            inData.setCount((int) inFile.getLength());
560            size_t inLen = inData.count();
561            inFile.read(inData.begin(), inLen);
562            inFile.setPath(NULL);
563            char* insert = strstr(inData.begin(), marker);
564            if (insert) {
565                insert += sizeof(marker) - 1;
566                const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
567                testNumber = atoi(numLoc) + 1;
568            }
569        }
570    }
571}
572
573void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
574    const char testFunction[] = "testSimplify(path);";
575    const char* pathPrefix = NULL;
576    const char* nameSuffix = NULL;
577    if (pathFillType == SkPath::kEvenOdd_FillType) {
578        pathPrefix = "    path.setFillType(SkPath::kEvenOdd_FillType);\n";
579        nameSuffix = "x";
580    }
581    SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
582    outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
583}
584
585void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
586    const char testFunction[] = "testOp(path);";
587    SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
588    const char* nameSuffix = opSuffixes[op];
589    SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
590    outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream);
591}
592
593void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
594                void (*firstTest)(skiatest::Reporter* , const char* filename),
595                void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
596    size_t index;
597    if (firstTest) {
598        index = count - 1;
599        while (index > 0 && tests[index].fun != firstTest) {
600            --index;
601        }
602#if DEBUG_SHOW_TEST_NAME
603        SkDebugf("<div id=\"%s\">\n", tests[index].str);
604        SkDebugf("  %s [%s]\n", __FUNCTION__, tests[index].str);
605#endif
606        (*tests[index].fun)(reporter, tests[index].str);
607        if (tests[index].fun == stopTest) {
608            return;
609        }
610    }
611    index = reverse ? count - 1 : 0;
612    size_t last = reverse ? 0 : count - 1;
613    do {
614        if (tests[index].fun != firstTest) {
615    #if DEBUG_SHOW_TEST_NAME
616            SkDebugf("<div id=\"%s\">\n", tests[index].str);
617            SkDebugf("  %s [%s]\n", __FUNCTION__, tests[index].str);
618    #endif
619            (*tests[index].fun)(reporter, tests[index].str);
620        }
621        if (tests[index].fun == stopTest) {
622            SkDebugf("lastTest\n");
623            break;
624        }
625        if (index == last) {
626            break;
627        }
628        index += reverse ? -1 : 1;
629    } while (true);
630}
631