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