blob: 6eb1955d28dbae89224da60d5a315ec5cd260e7f [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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#include "SkPathOpsCubic.h"
8#include "SkPathOpsLine.h"
9#include "SkPathOpsQuad.h"
10#include "SkPathOpsRect.h"
11#include "Test.h"
12
13static const SkDLine lineTests[] = {
14 {{{2, 1}, {2, 1}}},
15 {{{2, 1}, {1, 1}}},
16 {{{2, 1}, {2, 2}}},
17 {{{1, 1}, {2, 2}}},
18 {{{3, 0}, {2, 1}}},
19 {{{3, 2}, {1, 1}}},
20};
21
22static const SkDQuad quadTests[] = {
23 {{{1, 1}, {2, 1}, {0, 2}}},
24 {{{0, 0}, {1, 1}, {3, 1}}},
25 {{{2, 0}, {1, 1}, {2, 2}}},
26 {{{4, 0}, {0, 1}, {4, 2}}},
27 {{{0, 0}, {0, 1}, {1, 1}}},
28};
29
30static const SkDCubic cubicTests[] = {
31 {{{2, 0}, {3, 1}, {2, 2}, {1, 1}}},
32 {{{3, 1}, {2, 2}, {1, 1}, {2, 0}}},
33 {{{3, 0}, {2, 1}, {3, 2}, {1, 1}}},
34};
35
caryclark@google.comad65a3e2013-04-15 19:13:59 +000036static const size_t lineTests_count = SK_ARRAY_COUNT(lineTests);
37static const size_t quadTests_count = SK_ARRAY_COUNT(quadTests);
38static const size_t cubicTests_count = SK_ARRAY_COUNT(cubicTests);
caryclark@google.com07393ca2013-04-08 11:47:37 +000039
caryclark@google.comad65a3e2013-04-15 19:13:59 +000040static void PathOpsDRectTest(skiatest::Reporter* reporter) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000041 size_t index;
42 SkDRect rect, rect2;
43 for (index = 0; index < lineTests_count; ++index) {
44 const SkDLine& line = lineTests[index];
45 rect.setBounds(line);
caryclark@google.com3b97af52013-04-23 11:56:44 +000046 REPORTER_ASSERT(reporter, rect.fLeft == SkTMin(line[0].fX, line[1].fX));
47 REPORTER_ASSERT(reporter, rect.fTop == SkTMin(line[0].fY, line[1].fY));
48 REPORTER_ASSERT(reporter, rect.fRight == SkTMax(line[0].fX, line[1].fX));
49 REPORTER_ASSERT(reporter, rect.fBottom == SkTMax(line[0].fY, line[1].fY));
caryclark@google.com07393ca2013-04-08 11:47:37 +000050 rect2.set(line[0]);
51 rect2.add(line[1]);
caryclark@google.com3b97af52013-04-23 11:56:44 +000052 REPORTER_ASSERT(reporter, rect2.fLeft == SkTMin(line[0].fX, line[1].fX));
53 REPORTER_ASSERT(reporter, rect2.fTop == SkTMin(line[0].fY, line[1].fY));
54 REPORTER_ASSERT(reporter, rect2.fRight == SkTMax(line[0].fX, line[1].fX));
55 REPORTER_ASSERT(reporter, rect2.fBottom == SkTMax(line[0].fY, line[1].fY));
caryclark@google.com07393ca2013-04-08 11:47:37 +000056 REPORTER_ASSERT(reporter, rect.contains(line[0]));
57 REPORTER_ASSERT(reporter, rect.intersects(&rect2));
58 }
59 for (index = 0; index < quadTests_count; ++index) {
60 const SkDQuad& quad = quadTests[index];
61 rect.setRawBounds(quad);
caryclark@google.com3b97af52013-04-23 11:56:44 +000062 REPORTER_ASSERT(reporter, rect.fLeft == SkTMin(quad[0].fX,
63 SkTMin(quad[1].fX, quad[2].fX)));
64 REPORTER_ASSERT(reporter, rect.fTop == SkTMin(quad[0].fY,
65 SkTMin(quad[1].fY, quad[2].fY)));
66 REPORTER_ASSERT(reporter, rect.fRight == SkTMax(quad[0].fX,
67 SkTMax(quad[1].fX, quad[2].fX)));
68 REPORTER_ASSERT(reporter, rect.fBottom == SkTMax(quad[0].fY,
69 SkTMax(quad[1].fY, quad[2].fY)));
caryclark@google.com07393ca2013-04-08 11:47:37 +000070 rect2.setBounds(quad);
71 REPORTER_ASSERT(reporter, rect.intersects(&rect2));
72 // FIXME: add a recursive box subdivision method to verify that tight bounds is correct
73 SkDPoint leftTop = {rect2.fLeft, rect2.fTop};
74 REPORTER_ASSERT(reporter, rect.contains(leftTop));
75 SkDPoint rightBottom = {rect2.fRight, rect2.fBottom};
76 REPORTER_ASSERT(reporter, rect.contains(rightBottom));
77 }
78 for (index = 0; index < cubicTests_count; ++index) {
79 const SkDCubic& cubic = cubicTests[index];
80 rect.setRawBounds(cubic);
caryclark@google.com3b97af52013-04-23 11:56:44 +000081 REPORTER_ASSERT(reporter, rect.fLeft == SkTMin(cubic[0].fX,
82 SkTMin(cubic[1].fX, SkTMin(cubic[2].fX, cubic[3].fX))));
83 REPORTER_ASSERT(reporter, rect.fTop == SkTMin(cubic[0].fY,
84 SkTMin(cubic[1].fY, SkTMin(cubic[2].fY, cubic[3].fY))));
85 REPORTER_ASSERT(reporter, rect.fRight == SkTMax(cubic[0].fX,
86 SkTMax(cubic[1].fX, SkTMax(cubic[2].fX, cubic[3].fX))));
87 REPORTER_ASSERT(reporter, rect.fBottom == SkTMax(cubic[0].fY,
88 SkTMax(cubic[1].fY, SkTMax(cubic[2].fY, cubic[3].fY))));
caryclark@google.com07393ca2013-04-08 11:47:37 +000089 rect2.setBounds(cubic);
90 REPORTER_ASSERT(reporter, rect.intersects(&rect2));
91 // FIXME: add a recursive box subdivision method to verify that tight bounds is correct
92 SkDPoint leftTop = {rect2.fLeft, rect2.fTop};
93 REPORTER_ASSERT(reporter, rect.contains(leftTop));
94 SkDPoint rightBottom = {rect2.fRight, rect2.fBottom};
95 REPORTER_ASSERT(reporter, rect.contains(rightBottom));
96 }
97}
98
99#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000100DEFINE_TESTCLASS_SHORT(PathOpsDRectTest)