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