1/*
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 */
7#include "PathOpsTestCommon.h"
8#include "SkPathOpsBounds.h"
9#include "SkPathOpsCurve.h"
10#include "Test.h"
11
12static const SkRect sectTests[][2] = {
13    {{2, 0, 4, 1}, {4, 0, 6, 1}},
14    {{2, 0, 4, 1}, {3, 0, 5, 1}},
15    {{2, 0, 4, 1}, {3, 0, 5, 0}},
16    {{2, 0, 4, 1}, {3, 1, 5, 2}},
17    {{2, 1, 4, 2}, {1, 0, 5, 3}},
18    {{2, 1, 5, 3}, {3, 1, 4, 2}},
19    {{2, 0, 4, 1}, {3, 0, 3, 0}},  // intersecting an empty bounds is OK
20    {{2, 0, 4, 1}, {4, 1, 5, 2}},  // touching just on a corner is OK
21};
22
23static const size_t sectTestsCount = SK_ARRAY_COUNT(sectTests);
24
25static const SkRect noSectTests[][2] = {
26    {{2, 0, 4, 1}, {5, 0, 6, 1}},
27    {{2, 0, 4, 1}, {3, 2, 5, 2}},
28};
29
30static const size_t noSectTestsCount = SK_ARRAY_COUNT(noSectTests);
31
32DEF_TEST(PathOpsBounds, reporter) {
33    for (size_t index = 0; index < sectTestsCount; ++index) {
34        const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(sectTests[index][0]);
35        SkASSERT(ValidBounds(bounds1));
36        const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(sectTests[index][1]);
37        SkASSERT(ValidBounds(bounds2));
38        bool touches = SkPathOpsBounds::Intersects(bounds1, bounds2);
39        REPORTER_ASSERT(reporter, touches);
40    }
41    for (size_t index = 0; index < noSectTestsCount; ++index) {
42        const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(noSectTests[index][0]);
43        SkASSERT(ValidBounds(bounds1));
44        const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(noSectTests[index][1]);
45        SkASSERT(ValidBounds(bounds2));
46        bool touches = SkPathOpsBounds::Intersects(bounds1, bounds2);
47        REPORTER_ASSERT(reporter, !touches);
48    }
49    SkPathOpsBounds bounds;
50    bounds.setEmpty();
51    bounds.add(1, 2, 3, 4);
52    SkPathOpsBounds expected;
53    expected.set(0, 0, 3, 4);
54    REPORTER_ASSERT(reporter, bounds == expected);
55    bounds.setEmpty();
56    SkPathOpsBounds ordinal;
57    ordinal.set(1, 2, 3, 4);
58    bounds.add(ordinal);
59    REPORTER_ASSERT(reporter, bounds == expected);
60    bounds.setEmpty();
61    SkDPoint botRight = {3, 4};
62    bounds.add(botRight);
63    REPORTER_ASSERT(reporter, bounds == expected);
64    const SkPoint curvePts[] = {{0, 0}, {1, 2}, {3, 4}, {5, 6}};
65    SkDCurve curve;
66    curve.fQuad.set(curvePts);
67    curve.setQuadBounds(curvePts, 1, 0, 1, &bounds);
68    expected.set(0, 0, 3, 4);
69    REPORTER_ASSERT(reporter, bounds == expected);
70    curve.fCubic.set(curvePts);
71    curve.setCubicBounds(curvePts, 1, 0, 1, &bounds);
72    expected.set(0, 0, 5, 6);
73    REPORTER_ASSERT(reporter, bounds == expected);
74}
75