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 "Test.h"
10
11static const SkRect sectTests[][2] = {
12    {{2, 0, 4, 1}, {4, 0, 6, 1}},
13    {{2, 0, 4, 1}, {3, 0, 5, 1}},
14    {{2, 0, 4, 1}, {3, 0, 5, 0}},
15    {{2, 0, 4, 1}, {3, 1, 5, 2}},
16    {{2, 1, 4, 2}, {1, 0, 5, 3}},
17    {{2, 1, 5, 3}, {3, 1, 4, 2}},
18    {{2, 0, 4, 1}, {3, 0, 3, 0}},  // intersecting an empty bounds is OK
19    {{2, 0, 4, 1}, {4, 1, 5, 2}},  // touching just on a corner is OK
20};
21
22static const size_t sectTestsCount = SK_ARRAY_COUNT(sectTests);
23
24static const SkRect noSectTests[][2] = {
25    {{2, 0, 4, 1}, {5, 0, 6, 1}},
26    {{2, 0, 4, 1}, {3, 2, 5, 2}},
27};
28
29static const size_t noSectTestsCount = SK_ARRAY_COUNT(noSectTests);
30
31static const SkRect reallyEmpty[] = {
32    {0, 0, 0, 0},
33    {1, 1, 1, 0},
34    {1, 1, 0, 1},
35    {1, 1, 0, 0},
36    {1, 2, 3, SK_ScalarNaN},
37};
38
39static const size_t emptyTestsCount = SK_ARRAY_COUNT(reallyEmpty);
40
41static const SkRect notReallyEmpty[] = {
42    {0, 0, 1, 0},
43    {0, 0, 0, 1},
44    {0, 0, 1, 1},
45};
46
47static const size_t notEmptyTestsCount = SK_ARRAY_COUNT(notReallyEmpty);
48
49DEF_TEST(PathOpsBounds, reporter) {
50    for (size_t index = 0; index < sectTestsCount; ++index) {
51        const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(sectTests[index][0]);
52        SkASSERT(ValidBounds(bounds1));
53        const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(sectTests[index][1]);
54        SkASSERT(ValidBounds(bounds2));
55        bool touches = SkPathOpsBounds::Intersects(bounds1, bounds2);
56        REPORTER_ASSERT(reporter, touches);
57    }
58    for (size_t index = 0; index < noSectTestsCount; ++index) {
59        const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(noSectTests[index][0]);
60        SkASSERT(ValidBounds(bounds1));
61        const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(noSectTests[index][1]);
62        SkASSERT(ValidBounds(bounds2));
63        bool touches = SkPathOpsBounds::Intersects(bounds1, bounds2);
64        REPORTER_ASSERT(reporter, !touches);
65    }
66    SkPathOpsBounds bounds;
67    bounds.setEmpty();
68    bounds.add(1, 2, 3, 4);
69    SkPathOpsBounds expected;
70    expected.set(0, 0, 3, 4);
71    REPORTER_ASSERT(reporter, bounds == expected);
72    bounds.setEmpty();
73    SkPathOpsBounds ordinal;
74    ordinal.set(1, 2, 3, 4);
75    bounds.add(ordinal);
76    REPORTER_ASSERT(reporter, bounds == expected);
77    SkPoint topLeft = {0, 0};
78    bounds.setPointBounds(topLeft);
79    SkPoint botRight = {3, 4};
80    bounds.add(botRight);
81    REPORTER_ASSERT(reporter, bounds == expected);
82    for (size_t index = 0; index < emptyTestsCount; ++index) {
83        const SkPathOpsBounds& bounds = static_cast<const SkPathOpsBounds&>(reallyEmpty[index]);
84        // SkASSERT(ValidBounds(bounds));  // don't check because test may contain nan
85        bool empty = bounds.isReallyEmpty();
86        REPORTER_ASSERT(reporter, empty);
87    }
88    for (size_t index = 0; index < notEmptyTestsCount; ++index) {
89        const SkPathOpsBounds& bounds = static_cast<const SkPathOpsBounds&>(notReallyEmpty[index]);
90        SkASSERT(ValidBounds(bounds));
91        bool empty = bounds.isReallyEmpty();
92        REPORTER_ASSERT(reporter, !empty);
93    }
94    const SkPoint curvePts[] = {{0, 0}, {1, 2}, {3, 4}, {5, 6}};
95    bounds.setLineBounds(curvePts);
96    expected.set(0, 0, 1, 2);
97    REPORTER_ASSERT(reporter, bounds == expected);
98    (bounds.*SetCurveBounds[1])(curvePts);
99    REPORTER_ASSERT(reporter, bounds == expected);
100    bounds.setQuadBounds(curvePts);
101    expected.set(0, 0, 3, 4);
102    REPORTER_ASSERT(reporter, bounds == expected);
103    (bounds.*SetCurveBounds[2])(curvePts);
104    REPORTER_ASSERT(reporter, bounds == expected);
105    bounds.setCubicBounds(curvePts);
106    expected.set(0, 0, 5, 6);
107    REPORTER_ASSERT(reporter, bounds == expected);
108    (bounds.*SetCurveBounds[3])(curvePts);
109    REPORTER_ASSERT(reporter, bounds == expected);
110}
111