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#define DEBUG_TEST 1
9
10#include "Simplify.h"
11
12namespace SimplifyFindNextTest {
13
14#include "Simplify.cpp"
15
16} // end of SimplifyFindNextTest namespace
17
18#include "Intersection_Tests.h"
19
20static const SimplifyFindNextTest::Segment* testCommon(
21        int contourWinding, int spanWinding, int startIndex, int endIndex,
22        SkTArray<SimplifyFindNextTest::Contour>& contours) {
23    SkTDArray<SimplifyFindNextTest::Contour*> contourList;
24    makeContourList(contours, contourList, false, false);
25    addIntersectTs(contourList[0], contourList[0]);
26    if (contours.count() > 1) {
27        SkASSERT(contours.count() == 2);
28        addIntersectTs(contourList[0], contourList[1]);
29        addIntersectTs(contourList[1], contourList[1]);
30    }
31    fixOtherTIndex(contourList);
32    SimplifyFindNextTest::Segment& segment = contours[0].debugSegments()[0];
33    SkPoint pts[2];
34    pts[0] = segment.xyAtT(&segment.span(endIndex));
35    int nextStart = startIndex;
36    int nextEnd = endIndex;
37    SkTDArray<SimplifyFindNextTest::Span*> chaseArray;
38    bool unsortable = false;
39    SimplifyFindNextTest::Segment* next = segment.findNextWinding(chaseArray,
40            nextStart, nextEnd, unsortable);
41    pts[1] = next->xyAtT(&next->span(nextStart));
42    SkASSERT(pts[0] == pts[1]);
43    return next;
44}
45
46static void test(const SkPath& path) {
47    SkTArray<SimplifyFindNextTest::Contour> contours;
48    SimplifyFindNextTest::EdgeBuilder builder(path, contours);
49    int contourWinding = 0;
50    int spanWinding = 1;
51    int start = 0;
52    int end = 1;
53    testCommon(contourWinding, spanWinding, start, end, contours);
54}
55
56static void test(const SkPath& path, int start, int end) {
57    SkTArray<SimplifyFindNextTest::Contour> contours;
58    SimplifyFindNextTest::EdgeBuilder builder(path, contours);
59    int contourWinding = 0;
60    int spanWinding = 1;
61    testCommon(contourWinding, spanWinding, start, end, contours);
62}
63
64static void testLine1() {
65    SkPath path;
66    path.moveTo(2,0);
67    path.lineTo(1,1);
68    path.lineTo(0,0);
69    path.close();
70    test(path);
71}
72
73static void addInnerCWTriangle(SkPath& path) {
74    path.moveTo(3,0);
75    path.lineTo(4,1);
76    path.lineTo(2,1);
77    path.close();
78}
79
80#if DEBUG_UNUSED
81static void addInnerCCWTriangle(SkPath& path) {
82    path.moveTo(3,0);
83    path.lineTo(2,1);
84    path.lineTo(4,1);
85    path.close();
86}
87#endif
88
89static void addOuterCWTriangle(SkPath& path) {
90    path.moveTo(3,0);
91    path.lineTo(6,2);
92    path.lineTo(0,2);
93    path.close();
94}
95
96#if DEBUG_UNUSED
97static void addOuterCCWTriangle(SkPath& path) {
98    path.moveTo(3,0);
99    path.lineTo(0,2);
100    path.lineTo(6,2);
101    path.close();
102}
103#endif
104
105static void testLine2() {
106    SkPath path;
107    addInnerCWTriangle(path);
108    addOuterCWTriangle(path);
109    test(path, 0, 3);
110}
111
112static void testLine3() {
113    SkPath path;
114    addInnerCWTriangle(path);
115    addOuterCWTriangle(path);
116    test(path, 3, 0);
117}
118
119static void testLine4() {
120    SkPath path;
121    addInnerCWTriangle(path);
122    addOuterCWTriangle(path);
123    test(path, 3, 2);
124}
125
126static void (*tests[])() = {
127    testLine1,
128    testLine2,
129    testLine3,
130    testLine4,
131};
132
133static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
134
135static void (*firstTest)() = 0;
136static bool skipAll = false;
137
138void SimplifyFindNext_Test() {
139    if (skipAll) {
140        return;
141    }
142    size_t index = 0;
143    if (firstTest) {
144        while (index < testCount && tests[index] != firstTest) {
145            ++index;
146        }
147    }
148    bool firstTestComplete = false;
149    for ( ; index < testCount; ++index) {
150        (*tests[index])();
151        firstTestComplete = true;
152    }
153}
154