blob: cff6bd3842795a6fea9da7fb67412a74697d2ef7 [file] [log] [blame]
caryclark@google.com66560ca2013-04-26 19:51:16 +00001/*
2 * Copyright 2013 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 */
caryclark@google.com66560ca2013-04-26 19:51:16 +00007#include "SkPath.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00008#include "SkPathOps.h"
caryclark@google.com66560ca2013-04-26 19:51:16 +00009#include "SkPoint.h"
10#include "Test.h"
11
12static const SkPoint nonFinitePts[] = {
13 { SK_ScalarInfinity, 0 },
14 { 0, SK_ScalarInfinity },
15 { SK_ScalarInfinity, SK_ScalarInfinity },
16 { SK_ScalarNegativeInfinity, 0},
17 { 0, SK_ScalarNegativeInfinity },
18 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
19 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
20 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
21 { SK_ScalarNaN, 0 },
22 { 0, SK_ScalarNaN },
23 { SK_ScalarNaN, SK_ScalarNaN },
24};
25
26const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
27
28static const SkPoint finitePts[] = {
29 { 0, 0 },
30 { SK_ScalarMax, 0 },
31 { 0, SK_ScalarMax },
32 { SK_ScalarMax, SK_ScalarMax },
33 { SK_ScalarMin, 0 },
34 { 0, SK_ScalarMin },
35 { SK_ScalarMin, SK_ScalarMin },
36};
37
38const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
39
caryclark@google.com570863f2013-09-16 15:55:01 +000040static void failOne(skiatest::Reporter* reporter, int index) {
41 SkPath path;
42 int i = (int) (index % nonFinitePtsCount);
43 int f = (int) (index % finitePtsCount);
44 int g = (int) ((f + 1) % finitePtsCount);
45 switch (index % 13) {
46 case 0: path.lineTo(nonFinitePts[i]); break;
47 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
48 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
49 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
50 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
51 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
52 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
53 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
54 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
55 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
56 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
57 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
58 case 12: path.moveTo(nonFinitePts[i]); break;
59 }
60 SkPath result;
61 result.setFillType(SkPath::kWinding_FillType);
62 bool success = Simplify(path, &result);
63 REPORTER_ASSERT(reporter, !success);
64 REPORTER_ASSERT(reporter, result.isEmpty());
65 REPORTER_ASSERT(reporter, result.getFillType() == SkPath::kWinding_FillType);
66 reporter->bumpTestCount();
67}
68
69static void dontFailOne(skiatest::Reporter* reporter, int index) {
70 SkPath path;
71 int f = (int) (index % finitePtsCount);
72 int g = (int) ((f + 1) % finitePtsCount);
73 switch (index % 11) {
74 case 0: path.lineTo(finitePts[f]); break;
75 case 1: path.quadTo(finitePts[f], finitePts[f]); break;
76 case 2: path.quadTo(finitePts[f], finitePts[g]); break;
77 case 3: path.quadTo(finitePts[g], finitePts[f]); break;
78 case 4: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
79 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
80 case 6: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
81 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
82 case 8: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
83 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
84 case 10: path.moveTo(finitePts[f]); break;
85 }
86 SkPath result;
87 result.setFillType(SkPath::kWinding_FillType);
88 bool success = Simplify(path, &result);
89 REPORTER_ASSERT(reporter, success);
90 REPORTER_ASSERT(reporter, result.getFillType() != SkPath::kWinding_FillType);
91 reporter->bumpTestCount();
92}
93
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000094DEF_TEST(PathOpsSimplifyFail, reporter) {
caryclark@google.com66560ca2013-04-26 19:51:16 +000095 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
caryclark@google.com570863f2013-09-16 15:55:01 +000096 failOne(reporter, index);
caryclark@google.come23b4a42013-04-26 20:41:24 +000097 }
caryclark@google.com66560ca2013-04-26 19:51:16 +000098 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
caryclark@google.com570863f2013-09-16 15:55:01 +000099 dontFailOne(reporter, index);
caryclark@google.com66560ca2013-04-26 19:51:16 +0000100 }
101}
102
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000103DEF_TEST(PathOpsSimplifyFailOne, reporter) {
caryclark@google.com570863f2013-09-16 15:55:01 +0000104 int index = 0;
105 failOne(reporter, index);
106}
107
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000108DEF_TEST(PathOpsSimplifyDontFailOne, reporter) {
caryclark@google.com570863f2013-09-16 15:55:01 +0000109 int index = 6;
110 dontFailOne(reporter, index);
111}