Refactor PathTest.cpp test_isNestedRects and test_isRect.

The existing implementations make it difficult to add new tests
and rely on non-local side effects.

R=bsalomon@google.com

Review URL: https://codereview.chromium.org/24502004

git-svn-id: http://skia.googlecode.com/svn/trunk@11455 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bungeman@google.com 2013-09-25 18:21:39 +00:00
parent 592c535c9e
commit b8d9d5bd29

View File

@ -1281,10 +1281,8 @@ static void test_isRect(skiatest::Reporter* reporter) {
SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f},
{1, 0}, {.5f, 0}};
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1},
{0, 1}, {0, .5f}};
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
@ -1307,40 +1305,61 @@ static void test_isRect(skiatest::Reporter* reporter) {
SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
size_t testLen[] = {
sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6),
sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc),
sizeof(rd), sizeof(re), sizeof(rf),
sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6),
sizeof(f7), sizeof(f8), sizeof(f9), sizeof(fa), sizeof(fb),
sizeof(c1), sizeof(c2)
struct IsRectTest {
SkPoint *fPoints;
size_t fPointCount;
bool fClose;
bool fIsRect;
} tests[] = {
{ r1, SK_ARRAY_COUNT(r1), true, true },
{ r2, SK_ARRAY_COUNT(r2), true, true },
{ r3, SK_ARRAY_COUNT(r3), true, true },
{ r4, SK_ARRAY_COUNT(r4), true, true },
{ r5, SK_ARRAY_COUNT(r5), true, true },
{ r6, SK_ARRAY_COUNT(r6), true, true },
{ r7, SK_ARRAY_COUNT(r7), true, true },
{ r8, SK_ARRAY_COUNT(r8), true, true },
{ r9, SK_ARRAY_COUNT(r9), true, true },
{ ra, SK_ARRAY_COUNT(ra), true, true },
{ rb, SK_ARRAY_COUNT(rb), true, true },
{ rc, SK_ARRAY_COUNT(rc), true, true },
{ rd, SK_ARRAY_COUNT(rd), true, true },
{ re, SK_ARRAY_COUNT(re), true, true },
{ rf, SK_ARRAY_COUNT(rf), true, true },
{ f1, SK_ARRAY_COUNT(f1), true, false },
{ f2, SK_ARRAY_COUNT(f2), true, false },
{ f3, SK_ARRAY_COUNT(f3), true, false },
{ f4, SK_ARRAY_COUNT(f4), true, false },
{ f5, SK_ARRAY_COUNT(f5), true, false },
{ f6, SK_ARRAY_COUNT(f6), true, false },
{ f7, SK_ARRAY_COUNT(f7), true, false },
{ f8, SK_ARRAY_COUNT(f8), true, false },
{ f9, SK_ARRAY_COUNT(f9), true, false },
{ fa, SK_ARRAY_COUNT(fa), true, false },
{ fb, SK_ARRAY_COUNT(fb), true, false },
{ c1, SK_ARRAY_COUNT(c1), false, false },
{ c2, SK_ARRAY_COUNT(c2), false, false },
};
SkPoint* tests[] = {
r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re, rf,
f1, f2, f3, f4, f5, f6, f7, f8, f9, fa, fb,
c1, c2
};
SkPoint* lastPass = rf;
SkPoint* lastClose = fb;
bool fail = false;
bool close = true;
const size_t testCount = sizeof(tests) / sizeof(tests[0]);
const size_t testCount = SK_ARRAY_COUNT(tests);
size_t index;
for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
SkPath path;
path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY);
for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) {
path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY);
path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
for (index = 1; index < tests[testIndex].fPointCount; ++index) {
path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
}
if (close) {
if (tests[testIndex].fClose) {
path.close();
}
REPORTER_ASSERT(reporter, fail ^ path.isRect(0));
REPORTER_ASSERT(reporter, fail ^ path.isRect(NULL, NULL));
REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
if (!fail) {
if (tests[testIndex].fIsRect) {
SkRect computed, expected;
expected.set(tests[testIndex], testLen[testIndex] / sizeof(SkPoint));
expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
REPORTER_ASSERT(reporter, path.isRect(&computed));
REPORTER_ASSERT(reporter, expected == computed);
@ -1348,7 +1367,7 @@ static void test_isRect(skiatest::Reporter* reporter) {
SkPath::Direction direction, cheapDirection;
REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
REPORTER_ASSERT(reporter, isClosed == close);
REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
REPORTER_ASSERT(reporter, direction == cheapDirection);
} else {
SkRect computed;
@ -1363,69 +1382,62 @@ static void test_isRect(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, isClosed == (bool) -1);
REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
}
if (tests[testIndex] == lastPass) {
fail = true;
}
if (tests[testIndex] == lastClose) {
close = false;
}
}
// fail, close then line
SkPath path1;
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
path1.lineTo(1, 0);
REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
REPORTER_ASSERT(reporter, !path1.isRect(NULL));
// fail, move in the middle
path1.reset();
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
if (index == 2) {
path1.moveTo(1, .5f);
}
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
REPORTER_ASSERT(reporter, !path1.isRect(NULL));
// fail, move on the edge
path1.reset();
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
REPORTER_ASSERT(reporter, !path1.isRect(NULL));
// fail, quad
path1.reset();
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
if (index == 2) {
path1.quadTo(1, .5f, 1, .5f);
}
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
REPORTER_ASSERT(reporter, !path1.isRect(NULL));
// fail, cubic
path1.reset();
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
if (index == 2) {
path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
}
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
REPORTER_ASSERT(reporter, !path1.isRect(NULL));
}
static void test_isNestedRects(skiatest::Reporter* reporter) {
@ -1439,10 +1451,8 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, // CCW
{1, 0}, {.5f, 0}};
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, // CW
{0, 1}, {0, .5f}};
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
@ -1461,59 +1471,65 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
size_t testLen[] = {
sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6),
sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc),
sizeof(rd), sizeof(re),
sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6),
sizeof(f7), sizeof(f8),
sizeof(c1), sizeof(c2)
};
SkPoint* tests[] = {
r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re,
f1, f2, f3, f4, f5, f6, f7, f8,
c1, c2
};
SkPath::Direction dirs[] = {
SkPath::kCW_Direction, SkPath::kCW_Direction, SkPath::kCW_Direction,
SkPath::kCW_Direction, SkPath::kCCW_Direction, SkPath::kCCW_Direction,
SkPath::kCCW_Direction, SkPath::kCCW_Direction, SkPath::kCCW_Direction,
SkPath::kCCW_Direction, SkPath::kCW_Direction, SkPath::kCW_Direction,
SkPath::kCCW_Direction, SkPath::kCW_Direction, SkPath::kUnknown_Direction,
SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction,
SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction,
SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction,
};
SkASSERT(SK_ARRAY_COUNT(tests) == SK_ARRAY_COUNT(dirs));
struct IsNestedRectTest {
SkPoint *fPoints;
size_t fPointCount;
SkPath::Direction fDirection;
bool fClose;
bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
} tests[] = {
{ r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
{ r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
{ r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
{ r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
{ r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
{ r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
{ r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
{ r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
{ r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
{ ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
{ rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
{ rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
{ rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
{ re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
const SkPoint* lastPass = re;
const SkPoint* lastClose = f8;
const size_t testCount = sizeof(tests) / sizeof(tests[0]);
{ f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
{ f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
{ f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
{ f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
{ f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
{ f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
{ f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
{ f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
{ c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
{ c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
};
const size_t testCount = SK_ARRAY_COUNT(tests);
size_t index;
for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
bool fail = false;
bool close = true;
for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
SkPath path;
if (rectFirst) {
path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
}
path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY);
for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) {
path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY);
path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
for (index = 1; index < tests[testIndex].fPointCount; ++index) {
path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
}
if (close) {
if (tests[testIndex].fClose) {
path.close();
}
if (!rectFirst) {
path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
}
REPORTER_ASSERT(reporter, fail ^ path.isNestedRects(0));
if (!fail) {
REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
if (tests[testIndex].fIsNestedRect) {
SkRect expected[2], computed[2];
SkPath::Direction expectedDirs[2], computedDirs[2];
SkRect testBounds;
testBounds.set(tests[testIndex], testLen[testIndex] / sizeof(SkPoint));
testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
expected[1] = testBounds;
if (rectFirst) {
@ -1521,19 +1537,13 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
} else {
expectedDirs[0] = SkPath::kCCW_Direction;
}
expectedDirs[1] = dirs[testIndex];
expectedDirs[1] = tests[testIndex].fDirection;
REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
REPORTER_ASSERT(reporter, expected[0] == computed[0]);
REPORTER_ASSERT(reporter, expected[1] == computed[1]);
REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
}
if (tests[testIndex] == lastPass) {
fail = true;
}
if (tests[testIndex] == lastClose) {
close = false;
}
}
// fail, close then line
@ -1542,7 +1552,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
}
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
path1.lineTo(r1[index].fX, r1[index].fY);
}
path1.close();
@ -1550,7 +1560,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
if (!rectFirst) {
path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
}
REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0));
REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
// fail, move in the middle
path1.reset();
@ -1558,7 +1568,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
}
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
if (index == 2) {
path1.moveTo(1, .5f);
}
@ -1568,14 +1578,14 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
if (!rectFirst) {
path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
}
REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0));
REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
// fail, move on the edge
path1.reset();
if (rectFirst) {
path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
}
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
path1.lineTo(r1[index].fX, r1[index].fY);
}
@ -1583,7 +1593,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
if (!rectFirst) {
path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
}
REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0));
REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
// fail, quad
path1.reset();
@ -1591,7 +1601,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
}
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
if (index == 2) {
path1.quadTo(1, .5f, 1, .5f);
}
@ -1601,7 +1611,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
if (!rectFirst) {
path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
}
REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0));
REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
// fail, cubic
path1.reset();
@ -1609,7 +1619,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
}
path1.moveTo(r1[0].fX, r1[0].fY);
for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
if (index == 2) {
path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
}
@ -1619,13 +1629,13 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
if (!rectFirst) {
path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
}
REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0));
REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
// fail, not nested
path1.reset();
path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0));
REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
}
// pass, stroke rect
@ -1635,7 +1645,7 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
strokePaint.setStyle(SkPaint::kStroke_Style);
strokePaint.setStrokeWidth(2);
strokePaint.getFillPath(src, &dst);
REPORTER_ASSERT(reporter, dst.isNestedRects(0));
REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
}
static void write_and_read_back(skiatest::Reporter* reporter,