Fix bug 1789: SDL_IntersectRect intersection with empty rect does not set result to empty; add test coverage to Rect suite

This commit is contained in:
Andreas Schiffler 2013-04-17 08:09:11 -07:00
parent 5bd319e5b5
commit 56c27c7be9
2 changed files with 87 additions and 16 deletions

View file

@ -29,8 +29,13 @@ SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
{ {
int Amin, Amax, Bmin, Bmax; int Amin, Amax, Bmin, Bmax;
if (!A || !B) { if (!A) {
// TODO error message SDL_InvalidParamError("A");
return SDL_FALSE;
}
if (!B) {
SDL_InvalidParamError("B");
return SDL_FALSE; return SDL_FALSE;
} }
@ -71,13 +76,25 @@ SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{ {
int Amin, Amax, Bmin, Bmax; int Amin, Amax, Bmin, Bmax;
if (!A || !B || !result) { if (!A) {
// TODO error message SDL_InvalidParamError("A");
return SDL_FALSE;
}
if (!B) {
SDL_InvalidParamError("B");
return SDL_FALSE;
}
if (!result) {
SDL_InvalidParamError("result");
return SDL_FALSE; return SDL_FALSE;
} }
/* Special cases for empty rects */ /* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) { if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
result->w = 0;
result->h = 0;
return SDL_FALSE; return SDL_FALSE;
} }
@ -113,7 +130,18 @@ SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{ {
int Amin, Amax, Bmin, Bmax; int Amin, Amax, Bmin, Bmax;
if (!A || !B || !result) { if (!A) {
SDL_InvalidParamError("A");
return;
}
if (!B) {
SDL_InvalidParamError("B");
return;
}
if (!result) {
SDL_InvalidParamError("result");
return; return;
} }
@ -171,12 +199,12 @@ SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
int x, y, i; int x, y, i;
if (!points) { if (!points) {
/* TODO error message */ SDL_InvalidParamError("points");
return SDL_FALSE; return SDL_FALSE;
} }
if (count < 1) { if (count < 1) {
/* TODO error message */ SDL_InvalidParamError("count");
return SDL_FALSE; return SDL_FALSE;
} }
@ -298,8 +326,28 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
int recty2; int recty2;
int outcode1, outcode2; int outcode1, outcode2;
if (!rect || !X1 || !Y1 || !X2 || !Y2) { if (!rect) {
// TODO error message SDL_InvalidParamError("rect");
return SDL_FALSE;
}
if (!X1) {
SDL_InvalidParamError("X1");
return SDL_FALSE;
}
if (!Y1) {
SDL_InvalidParamError("Y1");
return SDL_FALSE;
}
if (!X2) {
SDL_InvalidParamError("X2");
return SDL_FALSE;
}
if (!Y2) {
SDL_InvalidParamError("Y2");
return SDL_FALSE; return SDL_FALSE;
} }
@ -418,18 +466,28 @@ SDL_GetSpanEnclosingRect(int width, int height,
int span_y1, span_y2; int span_y1, span_y2;
int rect_y1, rect_y2; int rect_y1, rect_y2;
if (width < 1 || height < 1) { if (width < 1) {
// TODO error message SDL_InvalidParamError("width");
return SDL_FALSE; return SDL_FALSE;
} }
if (!rects || !span) { if (height < 1) {
// TODO error message SDL_InvalidParamError("height");
return SDL_FALSE;
}
if (!rects) {
SDL_InvalidParamError("rects");
return SDL_FALSE;
}
if (!span) {
SDL_InvalidParamError("span");
return SDL_FALSE; return SDL_FALSE;
} }
if (numrects < 1) { if (numrects < 1) {
// TODO error message SDL_InvalidParamError("numrects");
return SDL_FALSE; return SDL_FALSE;
} }

View file

@ -614,8 +614,11 @@ int rect_testIntersectRectEmpty (void *arg)
SDL_Rect rectB; SDL_Rect rectB;
SDL_Rect result; SDL_Rect result;
SDL_bool intersection; SDL_bool intersection;
SDL_bool empty;
// Rect A empty // Rect A empty
result.w = SDLTest_RandomIntegerInRange(1, 100);
result.h = SDLTest_RandomIntegerInRange(1, 100);
refRectA.x = SDLTest_RandomIntegerInRange(1, 100); refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
refRectA.y = SDLTest_RandomIntegerInRange(1, 100); refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
refRectA.w = SDLTest_RandomIntegerInRange(1, 100); refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
@ -627,8 +630,12 @@ int rect_testIntersectRectEmpty (void *arg)
rectB = refRectB; rectB = refRectB;
intersection = SDL_IntersectRect(&rectA, &rectB, &result); intersection = SDL_IntersectRect(&rectA, &rectB, &result);
_validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL); _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
empty = (SDL_bool)SDL_RectEmpty(&result);
SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
// Rect B empty // Rect B empty
result.w = SDLTest_RandomIntegerInRange(1, 100);
result.h = SDLTest_RandomIntegerInRange(1, 100);
refRectA.x = SDLTest_RandomIntegerInRange(1, 100); refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
refRectA.y = SDLTest_RandomIntegerInRange(1, 100); refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
refRectA.w = SDLTest_RandomIntegerInRange(1, 100); refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
@ -640,8 +647,12 @@ int rect_testIntersectRectEmpty (void *arg)
rectB = refRectB; rectB = refRectB;
intersection = SDL_IntersectRect(&rectA, &rectB, &result); intersection = SDL_IntersectRect(&rectA, &rectB, &result);
_validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL); _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
empty = (SDL_bool)SDL_RectEmpty(&result);
SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
// Rect A and B empty // Rect A and B empty
result.w = SDLTest_RandomIntegerInRange(1, 100);
result.h = SDLTest_RandomIntegerInRange(1, 100);
refRectA.x = SDLTest_RandomIntegerInRange(1, 100); refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
refRectA.y = SDLTest_RandomIntegerInRange(1, 100); refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
refRectA.w = SDLTest_RandomIntegerInRange(1, 100); refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
@ -655,6 +666,8 @@ int rect_testIntersectRectEmpty (void *arg)
rectB = refRectB; rectB = refRectB;
intersection = SDL_IntersectRect(&rectA, &rectB, &result); intersection = SDL_IntersectRect(&rectA, &rectB, &result);
_validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL); _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
empty = (SDL_bool)SDL_RectEmpty(&result);
SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
return TEST_COMPLETED; return TEST_COMPLETED;
} }