blob: 8683051b056549008a29dda60036efd7e63679b9 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +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.com8d0a5242013-07-16 16:11:16 +00007#include "PathOpsTestCommon.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00008#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
caryclark@google.comad65a3e2013-04-15 19:13:59 +000022static const size_t sectTestsCount = SK_ARRAY_COUNT(sectTests);
caryclark@google.com07393ca2013-04-08 11:47:37 +000023
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
caryclark@google.comad65a3e2013-04-15 19:13:59 +000029static const size_t noSectTestsCount = SK_ARRAY_COUNT(noSectTests);
caryclark@google.com07393ca2013-04-08 11:47:37 +000030
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
caryclark@google.comad65a3e2013-04-15 19:13:59 +000039static const size_t emptyTestsCount = SK_ARRAY_COUNT(reallyEmpty);
caryclark@google.com07393ca2013-04-08 11:47:37 +000040
41static const SkRect notReallyEmpty[] = {
42 {0, 0, 1, 0},
43 {0, 0, 0, 1},
44 {0, 0, 1, 1},
45};
46
caryclark@google.comad65a3e2013-04-15 19:13:59 +000047static const size_t notEmptyTestsCount = SK_ARRAY_COUNT(notReallyEmpty);
caryclark@google.com07393ca2013-04-08 11:47:37 +000048
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000049DEF_TEST(PathOpsBounds, reporter) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000050 for (size_t index = 0; index < sectTestsCount; ++index) {
51 const SkPathOpsBounds& bounds1 = static_cast<const SkPathOpsBounds&>(sectTests[index][0]);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000052 SkASSERT(ValidBounds(bounds1));
caryclark@google.com07393ca2013-04-08 11:47:37 +000053 const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(sectTests[index][1]);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000054 SkASSERT(ValidBounds(bounds2));
caryclark@google.com07393ca2013-04-08 11:47:37 +000055 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]);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000060 SkASSERT(ValidBounds(bounds1));
caryclark@google.com07393ca2013-04-08 11:47:37 +000061 const SkPathOpsBounds& bounds2 = static_cast<const SkPathOpsBounds&>(noSectTests[index][1]);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000062 SkASSERT(ValidBounds(bounds2));
caryclark@google.com07393ca2013-04-08 11:47:37 +000063 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]);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000084 // SkASSERT(ValidBounds(bounds)); // don't check because test may contain nan
caryclark@google.com07393ca2013-04-08 11:47:37 +000085 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]);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000090 SkASSERT(ValidBounds(bounds));
caryclark@google.com07393ca2013-04-08 11:47:37 +000091 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}