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