From 5239c673aed49c511acd72d06c129edc32d1a614 Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Wed, 17 Apr 2013 08:09:11 -0700 Subject: [PATCH] Fix bug 1789: SDL_IntersectRect intersection with empty rect does not set result to empty; add test coverage to Rect suite --- src/video/SDL_rect.c | 86 +++++++++++++++++++++++++++++++------- test/testautomation_rect.c | 17 +++++++- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/src/video/SDL_rect.c b/src/video/SDL_rect.c index 23894dc3d..045d3f1cf 100644 --- a/src/video/SDL_rect.c +++ b/src/video/SDL_rect.c @@ -29,8 +29,13 @@ SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B) { int Amin, Amax, Bmin, Bmax; - if (!A || !B) { - // TODO error message + if (!A) { + SDL_InvalidParamError("A"); + return SDL_FALSE; + } + + if (!B) { + SDL_InvalidParamError("B"); 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; - if (!A || !B || !result) { - // TODO error message + if (!A) { + SDL_InvalidParamError("A"); + return SDL_FALSE; + } + + if (!B) { + SDL_InvalidParamError("B"); + return SDL_FALSE; + } + + if (!result) { + SDL_InvalidParamError("result"); return SDL_FALSE; } /* Special cases for empty rects */ if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) { + result->w = 0; + result->h = 0; 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; - if (!A || !B || !result) { + if (!A) { + SDL_InvalidParamError("A"); + return; + } + + if (!B) { + SDL_InvalidParamError("B"); + return; + } + + if (!result) { + SDL_InvalidParamError("result"); return; } @@ -171,12 +199,12 @@ SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip, int x, y, i; if (!points) { - /* TODO error message */ + SDL_InvalidParamError("points"); return SDL_FALSE; } if (count < 1) { - /* TODO error message */ + SDL_InvalidParamError("count"); return SDL_FALSE; } @@ -298,8 +326,28 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2, int recty2; int outcode1, outcode2; - if (!rect || !X1 || !Y1 || !X2 || !Y2) { - // TODO error message + if (!rect) { + 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; } @@ -418,18 +466,28 @@ SDL_GetSpanEnclosingRect(int width, int height, int span_y1, span_y2; int rect_y1, rect_y2; - if (width < 1 || height < 1) { - // TODO error message + if (width < 1) { + SDL_InvalidParamError("width"); + return SDL_FALSE; + } + + if (height < 1) { + SDL_InvalidParamError("height"); + return SDL_FALSE; + } + + if (!rects) { + SDL_InvalidParamError("rects"); return SDL_FALSE; } - if (!rects || !span) { - // TODO error message + if (!span) { + SDL_InvalidParamError("span"); return SDL_FALSE; } if (numrects < 1) { - // TODO error message + SDL_InvalidParamError("numrects"); return SDL_FALSE; } diff --git a/test/testautomation_rect.c b/test/testautomation_rect.c index 98ac8424f..333c973ed 100644 --- a/test/testautomation_rect.c +++ b/test/testautomation_rect.c @@ -614,8 +614,11 @@ int rect_testIntersectRectEmpty (void *arg) SDL_Rect rectB; SDL_Rect result; SDL_bool intersection; + SDL_bool empty; // Rect A empty + result.w = SDLTest_RandomIntegerInRange(1, 100); + result.h = SDLTest_RandomIntegerInRange(1, 100); refRectA.x = SDLTest_RandomIntegerInRange(1, 100); refRectA.y = SDLTest_RandomIntegerInRange(1, 100); refRectA.w = SDLTest_RandomIntegerInRange(1, 100); @@ -627,8 +630,12 @@ int rect_testIntersectRectEmpty (void *arg) rectB = refRectB; intersection = SDL_IntersectRect(&rectA, &rectB, &result); _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 + result.w = SDLTest_RandomIntegerInRange(1, 100); + result.h = SDLTest_RandomIntegerInRange(1, 100); refRectA.x = SDLTest_RandomIntegerInRange(1, 100); refRectA.y = SDLTest_RandomIntegerInRange(1, 100); refRectA.w = SDLTest_RandomIntegerInRange(1, 100); @@ -640,8 +647,12 @@ int rect_testIntersectRectEmpty (void *arg) rectB = refRectB; intersection = SDL_IntersectRect(&rectA, &rectB, &result); _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 + result.w = SDLTest_RandomIntegerInRange(1, 100); + result.h = SDLTest_RandomIntegerInRange(1, 100); refRectA.x = SDLTest_RandomIntegerInRange(1, 100); refRectA.y = SDLTest_RandomIntegerInRange(1, 100); refRectA.w = SDLTest_RandomIntegerInRange(1, 100); @@ -655,8 +666,10 @@ int rect_testIntersectRectEmpty (void *arg) rectB = refRectB; intersection = SDL_IntersectRect(&rectA, &rectB, &result); _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; } /*!