test/test-automation/tests/testrect/testrect.c
author Andreas Schiffler
Fri, 16 Sep 2011 08:26:20 -0700
changeset 5948 05e6bfac079f
parent 5912 98471d04c8db
child 5951 b4c936b80652
permissions -rw-r--r--
Add test cases for SDL_RectEmpty
     1 /**
     2  * Original code: automated SDL rect test written by Edgar Simo "bobbens"
     3  * New/updated tests: aschiffler at ferzkopp dot net
     4  */
     5 
     6 #include <stdio.h>
     7 
     8 #include <SDL/SDL.h>
     9 
    10 #include "../../include/SDL_test.h"
    11 
    12 /* SDL_IntersectRectAndLine */
    13 static const TestCaseReference test1 =
    14 		(TestCaseReference){ "rect_testIntersectRectAndLine", "Tests SDL_IntersectRectAndLine clipping cases", TEST_ENABLED, 0, 0 };
    15 
    16 static const TestCaseReference test2 =
    17 		(TestCaseReference){ "rect_testIntersectRectAndLineInside", "Tests SDL_IntersectRectAndLine with line fully contained in rect", TEST_ENABLED, 0, 0 };
    18 
    19 static const TestCaseReference test3 =
    20 		(TestCaseReference){ "rect_testIntersectRectAndLineOutside", "Tests SDL_IntersectRectAndLine with line fully contained in rect", TEST_ENABLED, 0, 0 };
    21 
    22 static const TestCaseReference test4 =
    23 		(TestCaseReference){ "rect_testIntersectRectAndLineParam", "Negative tests against SDL_IntersectRectAndLine with invalid parameters", TEST_ENABLED, 0, 0 };
    24 
    25 /* SDL_IntersectRect */
    26 static const TestCaseReference test5 =
    27 		(TestCaseReference){ "rect_testIntersectRectInside", "Tests SDL_IntersectRect with B fully contained in A", TEST_ENABLED, 0, 0 };
    28 
    29 static const TestCaseReference test6 =
    30 		(TestCaseReference){ "rect_testIntersectRectOutside", "Tests SDL_IntersectRect with B fully outside of A", TEST_ENABLED, 0, 0 };
    31 
    32 static const TestCaseReference test7 =
    33 		(TestCaseReference){ "rect_testIntersectRectPartial", "Tests SDL_IntersectRect with B partially intersecting A", TEST_ENABLED, 0, 0 };
    34 
    35 static const TestCaseReference test8 =
    36 		(TestCaseReference){ "rect_testIntersectRectPoint", "Tests SDL_IntersectRect with 1x1 sized rectangles", TEST_ENABLED, 0, 0 };
    37 
    38 static const TestCaseReference test9 =
    39 		(TestCaseReference){ "rect_testIntersectRectParam", "Negative tests against SDL_IntersectRect with invalid parameters", TEST_ENABLED, 0, 0 };
    40 
    41 /* SDL_HasIntersection */
    42 static const TestCaseReference test10 =
    43 		(TestCaseReference){ "rect_testHasIntersectionInside", "Tests SDL_HasIntersection with B fully contained in A", TEST_ENABLED, 0, 0 };
    44 
    45 static const TestCaseReference test11 =
    46 		(TestCaseReference){ "rect_testHasIntersectionOutside", "Tests SDL_HasIntersection with B fully outside of A", TEST_ENABLED, 0, 0 };
    47 
    48 static const TestCaseReference test12 =
    49 		(TestCaseReference){ "rect_testHasIntersectionPartial", "Tests SDL_HasIntersection with B partially intersecting A", TEST_ENABLED, 0, 0 };
    50 
    51 static const TestCaseReference test13 =
    52 		(TestCaseReference){ "rect_testHasIntersectionPoint", "Tests SDL_HasIntersection with 1x1 sized rectangles", TEST_ENABLED, 0, 0 };
    53 
    54 static const TestCaseReference test14 =
    55 		(TestCaseReference){ "rect_testHasIntersectionParam", "Negative tests against SDL_HasIntersection with invalid parameters", TEST_ENABLED, 0, 0 };
    56 
    57 /* SDL_EnclosePoints */
    58 static const TestCaseReference test15 =
    59 		(TestCaseReference){ "rect_testEnclosePoints", "Tests SDL_EnclosePoints without clipping", TEST_ENABLED, 0, 0 };
    60 
    61 static const TestCaseReference test16 =
    62 		(TestCaseReference){ "rect_testEnclosePointsWithClipping", "Tests SDL_EnclosePoints with clipping", TEST_ENABLED, 0, 0 };
    63 
    64 static const TestCaseReference test17 =
    65 		(TestCaseReference){ "rect_testEnclosePointsRepeatedInput", "Tests SDL_EnclosePoints with repeated input", TEST_ENABLED, 0, 0 };
    66 
    67 static const TestCaseReference test18 =
    68 		(TestCaseReference){ "rect_testEnclosePointsParam", "Negative tests against SDL_EnclosePoints with invalid parameters", TEST_ENABLED, 0, 0 };
    69 
    70 /* SDL_UnionRect */
    71 static const TestCaseReference test19 =
    72 		(TestCaseReference){ "rect_testUnionRectInside", "Tests SDL_UnionRect where rect B is inside rect A", TEST_ENABLED, 0, 0 };
    73 
    74 static const TestCaseReference test20 =
    75 		(TestCaseReference){ "rect_testUnionRectOutside", "Tests SDL_UnionRect where rect B is outside rect A", TEST_ENABLED, 0, 0 };
    76 
    77 static const TestCaseReference test21 =
    78 		(TestCaseReference){ "rect_testUnionRectParam", "Negative tests against SDL_UnionRect with invalid parameters", TEST_ENABLED, 0, 0 };
    79 
    80 /* SDL_RectEmpty */
    81 static const TestCaseReference test22 =
    82 		(TestCaseReference){ "rect_testRectEmpty", "Tests SDL_RectEmpty with various inputs", TEST_ENABLED, 0, 0 };
    83 
    84 static const TestCaseReference test23 =
    85 		(TestCaseReference){ "rect_testRectEmptyParam", "Negative tests against SDL_RectEmpty with invalid parameters", TEST_ENABLED, 0, 0 };
    86 
    87 /* TODO: SDL_RectEquals */
    88 
    89 /*!
    90  * \brief Test suite for functions that handle simple rectangles including overlaps and merges.
    91  *
    92  * \sa
    93  * http://wiki.libsdl.org/moin.cgi/CategoryRect
    94  */
    95 extern const TestCaseReference *testSuite[] =  {
    96 	&test1, &test2, &test3, &test4, &test5, &test6, &test7, &test8, &test9, &test10, &test11, &test12, &test13, &test14, 
    97 	&test15, &test16, &test17, &test18, &test19, &test20, &test21, &test22, &test23, NULL
    98 };
    99 
   100 TestCaseReference **QueryTestSuite() {
   101 	return (TestCaseReference **)testSuite;
   102 }
   103 
   104 /*!
   105  * \brief Private helper to check SDL_IntersectRectAndLine results
   106  */
   107 void _validateIntersectRectAndLineResults(
   108     SDL_bool intersection, SDL_bool expectedIntersection,
   109     SDL_Rect *rect, SDL_Rect * refRect,
   110     int x1, int y1, int x2, int y2,
   111     int x1Ref, int y1Ref, int x2Ref, int y2Ref)
   112 {
   113     AssertTrue(intersection == expectedIntersection, 
   114         "Incorrect intersection result: expected %s, got %s intersecting rect (%d,%d,%d,%d) with line (%d,%d - %d,%d)\n",
   115         (expectedIntersection == SDL_TRUE) ? "true" : "false",
   116         (intersection == SDL_TRUE) ? "true" : "false",
   117         refRect->x, refRect->y, refRect->w, refRect->h,
   118         x1Ref, y1Ref, x2Ref, y2Ref);
   119     AssertTrue(rect->x == refRect->x && rect->y == refRect->y && rect->w == refRect->w && rect->h == refRect->h,
   120         "Source rectangle was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   121         rect->x, rect->y, rect->w, rect->h,
   122         refRect->x, refRect->y, refRect->w, refRect->h);
   123     AssertTrue(x1 == x1Ref && y1 == y1Ref && x2 == x2Ref && y2 == y2Ref,
   124         "Line was incorrectly clipped or modified: got (%d,%d - %d,%d) expected (%d,%d - %d,%d)",
   125         x1, y1, x2, y2,
   126         x1Ref, y1Ref, x2Ref, y2Ref);
   127 }
   128 
   129 /*!
   130  * \brief Tests SDL_IntersectRectAndLine() clipping cases
   131  *
   132  * \sa
   133  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
   134  */
   135 int rect_testIntersectRectAndLine (void *arg)
   136 {
   137     SDL_Rect refRect = { 0, 0, 32, 32 };
   138     SDL_Rect rect;
   139     int x1, y1;
   140     int x2, y2;
   141     SDL_bool intersected;
   142 
   143     int xLeft = -RandomIntegerInRange(1, refRect.w);
   144     int xRight = refRect.w + RandomIntegerInRange(1, refRect.w);
   145     int yTop = -RandomIntegerInRange(1, refRect.h);
   146     int yBottom = refRect.h + RandomIntegerInRange(1, refRect.h);
   147 
   148     x1 = xLeft;
   149     y1 = 15;
   150     x2 = xRight;
   151     y2 = 15;
   152     rect = refRect;
   153     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   154     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 0, 15, 31, 15);
   155 
   156     x1 = 15;
   157     y1 = yTop;
   158     x2 = 15;
   159     y2 = yBottom;
   160     rect = refRect;
   161     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   162     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 15, 0, 15, 31);
   163 
   164     x1 = -refRect.w;
   165     y1 = -refRect.h;
   166     x2 = 2*refRect.w;
   167     y2 = 2*refRect.h;
   168     rect = refRect;
   169     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   170      _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 0, 0, 31, 31);
   171 
   172     x1 = 2*refRect.w;
   173     y1 = 2*refRect.h;
   174     x2 = -refRect.w;
   175     y2 = -refRect.h;
   176     rect = refRect;
   177     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   178     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 31, 31, 0, 0);
   179 
   180     x1 = -1;
   181     y1 = 32;
   182     x2 = 32;
   183     y2 = -1;
   184     rect = refRect;
   185     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   186     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 0, 31, 31, 0);
   187 
   188     x1 = 32;
   189     y1 = -1;
   190     x2 = -1;
   191     y2 = 32;
   192     rect = refRect;
   193     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   194     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 31, 0, 0, 31);
   195 }
   196 
   197 /*!
   198  * \brief Tests SDL_IntersectRectAndLine() non-clipping case line inside
   199  *
   200  * \sa
   201  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
   202  */
   203 int rect_testIntersectRectAndLineInside (void *arg)
   204 {
   205     SDL_Rect refRect = { 0, 0, 32, 32 };
   206     SDL_Rect rect;
   207     int x1, y1;
   208     int x2, y2;
   209     SDL_bool intersected;
   210 
   211     int xmin = refRect.x;
   212     int xmax = refRect.x + refRect.w - 1;
   213     int ymin = refRect.y;
   214     int ymax = refRect.y + refRect.h - 1;
   215     int x1Ref = RandomIntegerInRange(xmin + 1, xmax - 1);
   216     int y1Ref = RandomIntegerInRange(ymin + 1, ymax - 1);
   217     int x2Ref = RandomIntegerInRange(xmin + 1, xmax - 1);
   218     int y2Ref = RandomIntegerInRange(ymin + 1, ymax - 1);
   219 
   220     x1 = x1Ref;
   221     y1 = y1Ref;
   222     x2 = x2Ref;
   223     y2 = y2Ref;
   224     rect = refRect;
   225     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   226     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, x2Ref, y2Ref);
   227 
   228     x1 = x1Ref;
   229     y1 = y1Ref;
   230     x2 = xmax;
   231     y2 = ymax;
   232     rect = refRect;
   233     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   234     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, xmax, ymax);
   235 
   236     x1 = xmin;
   237     y1 = ymin;
   238     x2 = x2Ref;
   239     y2 = y2Ref;
   240     rect = refRect;
   241     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   242     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, xmin, ymin, x2Ref, y2Ref);
   243 
   244     x1 = xmin;
   245     y1 = ymin;
   246     x2 = xmax;
   247     y2 = ymax;
   248     rect = refRect;
   249     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   250     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, xmin, ymin, xmax, ymax);
   251 
   252     x1 = xmin;
   253     y1 = ymax;
   254     x2 = xmax;
   255     y2 = ymin;
   256     rect = refRect;
   257     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   258     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, xmin, ymax, xmax, ymin);
   259 }
   260 
   261 
   262 /*!
   263  * \brief Tests SDL_IntersectRectAndLine() non-clipping cases outside
   264  *
   265  * \sa
   266  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
   267  */
   268 int rect_testIntersectRectAndLineOutside (void *arg)
   269 {
   270     SDL_Rect refRect = { 0, 0, 32, 32 };
   271     SDL_Rect rect;
   272     int x1, y1;
   273     int x2, y2;
   274     SDL_bool intersected;
   275 
   276     int xLeft = -RandomIntegerInRange(1, refRect.w);
   277     int xRight = refRect.w + RandomIntegerInRange(1, refRect.w);
   278     int yTop = -RandomIntegerInRange(1, refRect.h);
   279     int yBottom = refRect.h + RandomIntegerInRange(1, refRect.h);
   280 
   281     x1 = xLeft;
   282     y1 = 0;
   283     x2 = xLeft;
   284     y2 = 31;
   285     rect = refRect;
   286     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   287     _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, xLeft, 0, xLeft, 31);
   288 
   289     x1 = xRight;
   290     y1 = 0;
   291     x2 = xRight;
   292     y2 = 31;
   293     rect = refRect;
   294     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   295     _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, xRight, 0, xRight, 31);
   296 
   297     x1 = 0;
   298     y1 = yTop;
   299     x2 = 31;
   300     y2 = yTop;
   301     rect = refRect;
   302     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   303     _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, 0, yTop, 31, yTop);
   304 
   305     x1 = 0;
   306     y1 = yBottom;
   307     x2 = 31;
   308     y2 = yBottom;
   309     rect = refRect;
   310     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   311     _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, 0, yBottom, 31, yBottom);
   312 }
   313 
   314 /*!
   315  * \brief Negative tests against SDL_IntersectRectAndLine() with invalid parameters
   316  *
   317  * \sa
   318  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
   319  */
   320 int rect_testIntersectRectAndLineParam (void *arg)
   321 {
   322     SDL_Rect rect = { 0, 0, 32, 32 };
   323     int x1 = rect.w / 2;
   324     int y1 = rect.h / 2;
   325     int x2 = x1;
   326     int y2 = 2 * rect.h;
   327     SDL_bool intersected;
   328     
   329     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
   330     AssertTrue(intersected == SDL_TRUE, "Test variables not intersected as expected");
   331     
   332     intersected = SDL_IntersectRectAndLine((SDL_Rect *)NULL, &x1, &y1, &x2, &y2);
   333     AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 1st parameter is NULL");
   334     intersected = SDL_IntersectRectAndLine(&rect, (int *)NULL, &y1, &x2, &y2);
   335     AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 2nd parameter is NULL");
   336     intersected = SDL_IntersectRectAndLine(&rect, &x1, (int *)NULL, &x2, &y2);
   337     AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 3rd parameter is NULL");
   338     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, (int *)NULL, &y2);
   339     AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 4th parameter is NULL");
   340     intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, (int *)NULL);
   341     AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 5th parameter is NULL");
   342     intersected = SDL_IntersectRectAndLine((SDL_Rect *)NULL, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
   343     AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when all parameters are NULL");
   344 }
   345 
   346 /*!
   347  * \brief Private helper to check SDL_HasIntersection results
   348  */
   349 void _validateHasIntersectionResults(
   350     SDL_bool intersection, SDL_bool expectedIntersection, 
   351     SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB)
   352 {
   353     AssertTrue(intersection == expectedIntersection, 
   354         "Incorrect intersection result: expected %s, got %s intersecting A (%d,%d,%d,%d) with B (%d,%d,%d,%d)\n",
   355         (expectedIntersection == SDL_TRUE) ? "true" : "false",
   356         (intersection == SDL_TRUE) ? "true" : "false",
   357         rectA->x, rectA->y, rectA->w, rectA->h, 
   358         rectB->x, rectB->y, rectB->w, rectB->h);
   359     AssertTrue(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
   360         "Source rectangle A was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   361         rectA->x, rectA->y, rectA->w, rectA->h,
   362         refRectA->x, refRectA->y, refRectA->w, refRectA->h);
   363     AssertTrue(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
   364         "Source rectangle B was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   365         rectB->x, rectB->y, rectB->w, rectB->h,
   366         refRectB->x, refRectB->y, refRectB->w, refRectB->h);
   367 }
   368 
   369 /*!
   370  * \brief Private helper to check SDL_IntersectRect results
   371  */
   372 void _validateIntersectRectResults(
   373     SDL_bool intersection, SDL_bool expectedIntersection, 
   374     SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB, 
   375     SDL_Rect *result, SDL_Rect *expectedResult)
   376 {
   377     _validateHasIntersectionResults(intersection, expectedIntersection, rectA, rectB, refRectA, refRectB);
   378     if (result && expectedResult) {
   379         AssertTrue(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
   380             "Intersection of rectangles A (%d,%d,%d,%d) and B (%d,%d,%d,%d) was incorrectly calculated, got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   381             rectA->x, rectA->y, rectA->w, rectA->h, 
   382             rectB->x, rectB->y, rectB->w, rectB->h,
   383             result->x, result->y, result->w, result->h,
   384             expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
   385     }
   386 }
   387 
   388 /*!
   389  * \brief Private helper to check SDL_UnionRect results
   390  */
   391 void _validateUnionRectResults(
   392     SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB, 
   393     SDL_Rect *result, SDL_Rect *expectedResult)
   394 {
   395     AssertTrue(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
   396         "Source rectangle A was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   397         rectA->x, rectA->y, rectA->w, rectA->h,
   398         refRectA->x, refRectA->y, refRectA->w, refRectA->h);
   399     AssertTrue(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
   400         "Source rectangle B was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   401         rectB->x, rectB->y, rectB->w, rectB->h,
   402         refRectB->x, refRectB->y, refRectB->w, refRectB->h);
   403     AssertTrue(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
   404         "Union of rectangles A (%d,%d,%d,%d) and B (%d,%d,%d,%d) was incorrectly calculated, got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   405         rectA->x, rectA->y, rectA->w, rectA->h, 
   406         rectB->x, rectB->y, rectB->w, rectB->h,
   407         result->x, result->y, result->w, result->h,
   408         expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
   409 }
   410 
   411 /*!
   412  * \brief Private helper to check SDL_RectEmpty results
   413  */
   414 void _validateRectEmptyResults(
   415     SDL_bool empty, SDL_bool expectedEmpty, 
   416     SDL_Rect *rect, SDL_Rect *refRect)
   417 {
   418     AssertTrue(empty == expectedEmpty, 
   419         "Incorrect empty result: expected %s, got %s testing (%d,%d,%d,%d)\n",
   420         (expectedEmpty == SDL_TRUE) ? "true" : "false",
   421         (empty == SDL_TRUE) ? "true" : "false",
   422         rect->x, rect->y, rect->w, rect->h);
   423     AssertTrue(rect->x == refRect->x && rect->y == refRect->y && rect->w == refRect->w && rect->h == refRect->h,
   424         "Source rectangle was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
   425         rect->x, rect->y, rect->w, rect->h,
   426         refRect->x, refRect->y, refRect->w, refRect->h);
   427 }
   428 
   429 /*!
   430  * \brief Tests SDL_IntersectRect() with B fully inside A
   431  *
   432  * \sa
   433  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
   434  */
   435 int rect_testIntersectRectInside (void *arg)
   436 {
   437     SDL_Rect refRectA = { 0, 0, 32, 32 };
   438     SDL_Rect refRectB;
   439     SDL_Rect rectA;
   440     SDL_Rect rectB;
   441     SDL_Rect result;
   442     SDL_bool intersection;
   443 
   444     // rectB fully contained in rectA
   445     refRectB.x = 0;
   446     refRectB.y = 0;
   447     refRectB.w = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
   448     refRectB.h = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
   449     rectA = refRectA;
   450     rectB = refRectB;
   451     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   452     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectB);
   453 }
   454 
   455 /*!
   456  * \brief Tests SDL_IntersectRect() with B fully outside A
   457  *
   458  * \sa
   459  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
   460  */
   461 int rect_testIntersectRectOutside (void *arg)
   462 {
   463     SDL_Rect refRectA = { 0, 0, 32, 32 };
   464     SDL_Rect refRectB;
   465     SDL_Rect rectA;
   466     SDL_Rect rectB;
   467     SDL_Rect result;
   468     SDL_bool intersection;
   469 
   470     // rectB fully outside of rectA
   471     refRectB.x = refRectA.x + refRectA.w + RandomIntegerInRange(1, 10);
   472     refRectB.y = refRectA.y + refRectA.h + RandomIntegerInRange(1, 10);
   473     refRectB.w = refRectA.w;
   474     refRectB.h = refRectA.h;
   475     rectA = refRectA;
   476     rectB = refRectB;
   477     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   478     _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);    
   479 }
   480 
   481 /*!
   482  * \brief Tests SDL_IntersectRect() with B partially intersecting A
   483  *
   484  * \sa
   485  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
   486  */
   487 int rect_testIntersectRectPartial (void *arg)
   488 {
   489     SDL_Rect refRectA = { 0, 0, 32, 32 };
   490     SDL_Rect refRectB;
   491     SDL_Rect rectA;
   492     SDL_Rect rectB;
   493     SDL_Rect result;
   494     SDL_Rect expectedResult;
   495     SDL_bool intersection;
   496 
   497     // rectB partially contained in rectA
   498     refRectB.x = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
   499     refRectB.y = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
   500     refRectB.w = refRectA.w;
   501     refRectB.h = refRectA.h;
   502     rectA = refRectA;
   503     rectB = refRectB;
   504     expectedResult.x = refRectB.x;
   505     expectedResult.y = refRectB.y;
   506     expectedResult.w = refRectA.w - refRectB.x;
   507     expectedResult.h = refRectA.h - refRectB.y;    
   508     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   509     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
   510 
   511     // rectB right edge
   512     refRectB.x = rectA.w - 1;
   513     refRectB.y = rectA.y;
   514     refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
   515     refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
   516     rectA = refRectA;
   517     rectB = refRectB;
   518     expectedResult.x = refRectB.x;
   519     expectedResult.y = refRectB.y;
   520     expectedResult.w = 1;
   521     expectedResult.h = refRectB.h;    
   522     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   523     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
   524 
   525     // rectB left edge
   526     refRectB.x = 1 - rectA.w;
   527     refRectB.y = rectA.y;
   528     refRectB.w = refRectA.w;
   529     refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
   530     rectA = refRectA;
   531     rectB = refRectB;
   532     expectedResult.x = 0;
   533     expectedResult.y = refRectB.y;
   534     expectedResult.w = 1;
   535     expectedResult.h = refRectB.h;    
   536     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   537     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
   538 
   539     // rectB bottom edge
   540     refRectB.x = rectA.x;
   541     refRectB.y = rectA.h - 1;
   542     refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
   543     refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
   544     rectA = refRectA;
   545     rectB = refRectB;
   546     expectedResult.x = refRectB.x;
   547     expectedResult.y = refRectB.y;
   548     expectedResult.w = refRectB.w;
   549     expectedResult.h = 1;    
   550     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   551     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
   552 
   553     // rectB top edge
   554     refRectB.x = rectA.x;
   555     refRectB.y = 1 - rectA.h;
   556     refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
   557     refRectB.h = rectA.h;
   558     rectA = refRectA;
   559     rectB = refRectB;
   560     expectedResult.x = refRectB.x;
   561     expectedResult.y = 0;
   562     expectedResult.w = refRectB.w;
   563     expectedResult.h = 1;    
   564     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   565     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
   566 }
   567 
   568 /*!
   569  * \brief Tests SDL_IntersectRect() with 1x1 pixel sized rectangles
   570  *
   571  * \sa
   572  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
   573  */
   574 int rect_testIntersectRectPoint (void *arg)
   575 {
   576     SDL_Rect refRectA = { 0, 0, 1, 1 };
   577     SDL_Rect refRectB = { 0, 0, 1, 1 };
   578     SDL_Rect rectA;
   579     SDL_Rect rectB;
   580     SDL_Rect result;
   581     SDL_bool intersection;
   582     int offsetX, offsetY;
   583 
   584     // intersecting pixels
   585     refRectA.x = RandomIntegerInRange(1, 100);
   586     refRectA.y = RandomIntegerInRange(1, 100);
   587     refRectB.x = refRectA.x;
   588     refRectB.y = refRectA.y;
   589     rectA = refRectA;
   590     rectB = refRectB;
   591     intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   592     _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectA);
   593 
   594     // non-intersecting pixels cases
   595     for (offsetX = -1; offsetX <= 1; offsetX++) {
   596         for (offsetY = -1; offsetY <= 1; offsetY++) {
   597             if (offsetX != 0 || offsetY != 0) {
   598                 refRectA.x = RandomIntegerInRange(1, 100);
   599                 refRectA.y = RandomIntegerInRange(1, 100);
   600                 refRectB.x = refRectA.x;
   601                 refRectB.y = refRectA.y;    
   602                 refRectB.x += offsetX;
   603                 refRectB.y += offsetY;
   604                 rectA = refRectA;
   605                 rectB = refRectB;
   606                 intersection = SDL_IntersectRect(&rectA, &rectB, &result);
   607                 _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
   608             }
   609         }
   610     }
   611 }
   612 
   613 /*!
   614  * \brief Negative tests against SDL_IntersectRect() with invalid parameters
   615  *
   616  * \sa
   617  * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
   618  */
   619 int rect_testIntersectRectParam(void *arg)
   620 {
   621     SDL_Rect rectA;
   622     SDL_Rect rectB;
   623     SDL_Rect result;
   624     SDL_bool intersection;
   625 
   626     // invalid parameter combinations
   627     intersection = SDL_IntersectRect((SDL_Rect *)NULL, &rectB, &result);
   628     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st parameter was NULL"); 
   629     intersection = SDL_IntersectRect(&rectA, (SDL_Rect *)NULL, &result);
   630     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 2st parameter was NULL"); 
   631     intersection = SDL_IntersectRect(&rectA, &rectB, (SDL_Rect *)NULL);
   632     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 3st parameter was NULL"); 
   633     intersection = SDL_IntersectRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, &result);
   634     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st and 2nd parameters were NULL"); 
   635     intersection = SDL_IntersectRect((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
   636     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st and 3rd parameters were NULL "); 
   637     intersection = SDL_IntersectRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
   638     AssertTrue(intersection == SDL_FALSE, "Function did not return false when all parameters were NULL");     
   639 }
   640 
   641 /*!
   642  * \brief Tests SDL_HasIntersection() with B fully inside A
   643  *
   644  * \sa
   645  * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
   646  */
   647 int rect_testHasIntersectionInside (void *arg)
   648 {
   649     SDL_Rect refRectA = { 0, 0, 32, 32 };
   650     SDL_Rect refRectB;
   651     SDL_Rect rectA;
   652     SDL_Rect rectB;
   653     SDL_bool intersection;
   654 
   655     // rectB fully contained in rectA
   656     refRectB.x = 0;
   657     refRectB.y = 0;
   658     refRectB.w = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
   659     refRectB.h = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
   660     rectA = refRectA;
   661     rectB = refRectB;
   662     intersection = SDL_HasIntersection(&rectA, &rectB);
   663     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   664 }
   665 
   666 /*!
   667  * \brief Tests SDL_HasIntersection() with B fully outside A
   668  *
   669  * \sa
   670  * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
   671  */
   672 int rect_testHasIntersectionOutside (void *arg)
   673 {
   674     SDL_Rect refRectA = { 0, 0, 32, 32 };
   675     SDL_Rect refRectB;
   676     SDL_Rect rectA;
   677     SDL_Rect rectB;
   678     SDL_bool intersection;
   679 
   680     // rectB fully outside of rectA
   681     refRectB.x = refRectA.x + refRectA.w + RandomIntegerInRange(1, 10);
   682     refRectB.y = refRectA.y + refRectA.h + RandomIntegerInRange(1, 10);
   683     refRectB.w = refRectA.w;
   684     refRectB.h = refRectA.h;
   685     rectA = refRectA;
   686     rectB = refRectB;
   687     intersection = SDL_HasIntersection(&rectA, &rectB);
   688     _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
   689 }
   690 
   691 /*!
   692  * \brief Tests SDL_HasIntersection() with B partially intersecting A
   693  *
   694  * \sa
   695  * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
   696  */
   697 int rect_testHasIntersectionPartial (void *arg)
   698 {
   699     SDL_Rect refRectA = { 0, 0, 32, 32 };
   700     SDL_Rect refRectB;
   701     SDL_Rect rectA;
   702     SDL_Rect rectB;
   703     SDL_bool intersection;
   704 
   705     // rectB partially contained in rectA
   706     refRectB.x = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
   707     refRectB.y = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
   708     refRectB.w = refRectA.w;
   709     refRectB.h = refRectA.h;
   710     rectA = refRectA;
   711     rectB = refRectB;
   712     intersection = SDL_HasIntersection(&rectA, &rectB);
   713     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   714 
   715     // rectB right edge
   716     refRectB.x = rectA.w - 1;
   717     refRectB.y = rectA.y;
   718     refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
   719     refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
   720     rectA = refRectA;
   721     rectB = refRectB;
   722     intersection = SDL_HasIntersection(&rectA, &rectB);
   723     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   724 
   725     // rectB left edge
   726     refRectB.x = 1 - rectA.w;
   727     refRectB.y = rectA.y;
   728     refRectB.w = refRectA.w;
   729     refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
   730     rectA = refRectA;
   731     rectB = refRectB;
   732     intersection = SDL_HasIntersection(&rectA, &rectB);
   733     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   734 
   735     // rectB bottom edge
   736     refRectB.x = rectA.x;
   737     refRectB.y = rectA.h - 1;
   738     refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
   739     refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
   740     rectA = refRectA;
   741     rectB = refRectB;
   742     intersection = SDL_HasIntersection(&rectA, &rectB);
   743     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   744 
   745     // rectB top edge
   746     refRectB.x = rectA.x;
   747     refRectB.y = 1 - rectA.h;
   748     refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
   749     refRectB.h = rectA.h;
   750     rectA = refRectA;
   751     rectB = refRectB;
   752     intersection = SDL_HasIntersection(&rectA, &rectB);
   753     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   754 }
   755 
   756 /*!
   757  * \brief Tests SDL_HasIntersection() with 1x1 pixel sized rectangles
   758  *
   759  * \sa
   760  * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
   761  */
   762 int rect_testHasIntersectionPoint (void *arg)
   763 {
   764     SDL_Rect refRectA = { 0, 0, 1, 1 };
   765     SDL_Rect refRectB = { 0, 0, 1, 1 };
   766     SDL_Rect rectA;
   767     SDL_Rect rectB;
   768     SDL_Rect result;
   769     SDL_bool intersection;
   770     int offsetX, offsetY;
   771 
   772     // intersecting pixels
   773     refRectA.x = RandomIntegerInRange(1, 100);
   774     refRectA.y = RandomIntegerInRange(1, 100);
   775     refRectB.x = refRectA.x;
   776     refRectB.y = refRectA.y;
   777     rectA = refRectA;
   778     rectB = refRectB;
   779     intersection = SDL_HasIntersection(&rectA, &rectB);
   780     _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
   781 
   782     // non-intersecting pixels cases
   783     for (offsetX = -1; offsetX <= 1; offsetX++) {
   784         for (offsetY = -1; offsetY <= 1; offsetY++) {
   785             if (offsetX != 0 || offsetY != 0) {
   786                 refRectA.x = RandomIntegerInRange(1, 100);
   787                 refRectA.y = RandomIntegerInRange(1, 100);
   788                 refRectB.x = refRectA.x;
   789                 refRectB.y = refRectA.y;    
   790                 refRectB.x += offsetX;
   791                 refRectB.y += offsetY;
   792                 rectA = refRectA;
   793                 rectB = refRectB;
   794                 intersection = SDL_HasIntersection(&rectA, &rectB);
   795                 _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
   796             }
   797         }
   798     }
   799 }
   800 
   801 /*!
   802  * \brief Negative tests against SDL_HasIntersection() with invalid parameters
   803  *
   804  * \sa
   805  * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
   806  */
   807 int rect_testHasIntersectionParam(void *arg)
   808 {
   809     SDL_Rect rectA;
   810     SDL_Rect rectB;
   811     SDL_bool intersection;
   812 
   813     // invalid parameter combinations
   814     intersection = SDL_HasIntersection((SDL_Rect *)NULL, &rectB);
   815     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st parameter was NULL"); 
   816     intersection = SDL_HasIntersection(&rectA, (SDL_Rect *)NULL);
   817     AssertTrue(intersection == SDL_FALSE, "Function did not return false when 2st parameter was NULL"); 
   818     intersection = SDL_HasIntersection((SDL_Rect *)NULL, (SDL_Rect *)NULL);
   819     AssertTrue(intersection == SDL_FALSE, "Function did not return false when all parameters were NULL");     
   820 }
   821 
   822 /*!
   823  * \brief Test SDL_EnclosePoints() without clipping
   824  *
   825  * \sa
   826  * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
   827  */
   828 int rect_testEnclosePoints(void *arg)
   829 {
   830     const int numPoints = 16;
   831     SDL_Point refPoints[numPoints];
   832     SDL_Point points[numPoints];
   833     int count;
   834     SDL_Rect result;
   835     SDL_bool anyEnclosed;
   836     SDL_bool anyEnclosedNoResult;
   837 
   838     // Create input data, tracking result
   839     SDL_bool expectedEnclosed = SDL_TRUE;
   840     int newx, newy;
   841     int minx, maxx, miny, maxy;
   842     int i;
   843     for (i=0; i<numPoints; i++) {
   844         newx = RandomIntegerInRange(-1024, 1024);
   845         newy = RandomIntegerInRange(-1024, 1024);
   846         refPoints[i].x = newx;
   847         refPoints[i].y = newy;
   848         points[i].x = newx;
   849         points[i].y = newy;
   850         if (i==0) {
   851             minx=maxx=newx;
   852             miny=maxy=newy;
   853         } else {
   854             if (newx<minx) minx=newx;
   855             if (newx>maxx) maxx=newx;
   856             if (newy<miny) miny=newy;
   857             if (newy>maxy) maxy=newy;
   858         }
   859     }
   860     
   861     // Call function and validate - special case: no result requested
   862     anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, (SDL_Rect *)NULL);
   863     AssertTrue(expectedEnclosed==anyEnclosedNoResult, 
   864         "Expected return value %s, got %s", 
   865         (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosedNoResult==SDL_TRUE) ? "true" : "false");
   866     for (i=0; i<numPoints; i++) {
   867         AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
   868             "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
   869             i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
   870     }
   871 
   872     // Call function and validate
   873     anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, &result);
   874     AssertTrue(expectedEnclosed==anyEnclosed, 
   875         "Expected return value %s, got %s", 
   876         (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosed==SDL_TRUE) ? "true" : "false");
   877     for (i=0; i<numPoints; i++) {
   878         AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
   879             "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
   880             i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
   881     }
   882     AssertTrue(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
   883         "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
   884         minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
   885 }
   886 
   887 /*!
   888  * \brief Test SDL_EnclosePoints() with repeated input points
   889  *
   890  * \sa
   891  * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
   892  */
   893 int rect_testEnclosePointsRepeatedInput(void *arg)
   894 {
   895     const int numPoints = 8;
   896     const int halfPoints = numPoints/2;
   897     SDL_Point refPoints[numPoints];
   898     SDL_Point points[numPoints];
   899     int count;
   900     SDL_Rect result;
   901     SDL_bool anyEnclosed;
   902     SDL_bool anyEnclosedNoResult;
   903 
   904     // Create input data, tracking result
   905     SDL_bool expectedEnclosed = SDL_TRUE;
   906     int newx, newy;
   907     int minx, maxx, miny, maxy;
   908     int i;
   909     for (i=0; i<numPoints; i++) {
   910         if (i < halfPoints) {
   911             newx = RandomIntegerInRange(-1024, 1024);
   912             newy = RandomIntegerInRange(-1024, 1024);
   913         } else {
   914             newx = refPoints[i-halfPoints].x;
   915             newy = refPoints[i-halfPoints].y;
   916         }
   917         refPoints[i].x = newx;
   918         refPoints[i].y = newy;
   919         points[i].x = newx;
   920         points[i].y = newy;
   921         if (i==0) {
   922             minx=maxx=newx;
   923             miny=maxy=newy;
   924         } else {
   925             if (newx<minx) minx=newx;
   926             if (newx>maxx) maxx=newx;
   927             if (newy<miny) miny=newy;
   928             if (newy>maxy) maxy=newy;
   929         }
   930     }
   931 
   932     // Call function and validate - special case: no result requested
   933     anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, (SDL_Rect *)NULL);
   934     AssertTrue(expectedEnclosed==anyEnclosedNoResult, 
   935         "Expected return value %s, got %s", 
   936         (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosedNoResult==SDL_TRUE) ? "true" : "false");
   937     for (i=0; i<numPoints; i++) {
   938         AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
   939             "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
   940             i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
   941     }
   942     
   943     // Call function and validate
   944     anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, &result);
   945     AssertTrue(expectedEnclosed==anyEnclosed, 
   946         "Expected return value %s, got %s", 
   947         (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosed==SDL_TRUE) ? "true" : "false");
   948     for (i=0; i<numPoints; i++) {
   949         AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
   950             "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
   951             i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
   952     }
   953     AssertTrue(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
   954         "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
   955         minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
   956 }
   957 
   958 /*!
   959  * \brief Test SDL_EnclosePoints() with clipping
   960  *
   961  * \sa
   962  * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
   963  */
   964 int rect_testEnclosePointsWithClipping(void *arg)
   965 {
   966     const int numPoints = 16;
   967     SDL_Point refPoints[numPoints];
   968     SDL_Point points[numPoints];
   969     int count;
   970     SDL_Rect refClip;
   971     SDL_Rect clip;
   972     SDL_Rect result;
   973     SDL_bool anyEnclosed;
   974     SDL_bool anyEnclosedNoResult;
   975 
   976     // Setup clipping rectangle
   977     refClip.x = RandomIntegerInRange(-1024, 1024);
   978     refClip.y = RandomIntegerInRange(-1024, 1024);
   979     refClip.w = RandomIntegerInRange(1, 1024);
   980     refClip.h = RandomIntegerInRange(1, 1024);
   981 
   982     // Create input data, tracking result
   983     SDL_bool expectedEnclosed = SDL_FALSE;
   984     int newx, newy;
   985     int minx, maxx, miny, maxy;
   986     int i;
   987     for (i=0; i<numPoints; i++) {
   988         newx = RandomIntegerInRange(-1024, 1024);
   989         newy = RandomIntegerInRange(-1024, 1024);
   990         refPoints[i].x = newx;
   991         refPoints[i].y = newy;
   992         points[i].x = newx;
   993         points[i].y = newy;
   994         if ((newx>=refClip.x) && (newx<(refClip.x + refClip.w)) &&
   995             (newy>=refClip.y) && (newy<(refClip.y + refClip.h))) {
   996             if (expectedEnclosed==SDL_FALSE) {
   997                 minx=maxx=newx;
   998                 miny=maxy=newy;
   999             } else {
  1000                 if (newx<minx) minx=newx;
  1001                 if (newx>maxx) maxx=newx;
  1002                 if (newy<miny) miny=newy;
  1003                 if (newy>maxy) maxy=newy;
  1004             }
  1005             expectedEnclosed = SDL_TRUE;
  1006         }
  1007     }
  1008 
  1009     // Call function and validate - special case: no result requested
  1010     clip = refClip;
  1011     anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, (SDL_Rect *)NULL);
  1012     AssertTrue(expectedEnclosed==anyEnclosedNoResult, 
  1013         "Expected return value %s, got %s", 
  1014         (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosedNoResult==SDL_TRUE) ? "true" : "false");
  1015     for (i=0; i<numPoints; i++) {
  1016         AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  1017             "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
  1018             i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1019     }
  1020     AssertTrue(refClip.x==clip.x && refClip.y==clip.y && refClip.w==clip.w && refClip.h==clip.h,
  1021         "Source clipping rectangle was incorrectly modified");
  1022     
  1023     // Call function and validate
  1024     anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, &result);
  1025     AssertTrue(expectedEnclosed==anyEnclosed, 
  1026         "Expected return value %s, got %s", 
  1027         (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosed==SDL_TRUE) ? "true" : "false");
  1028     for (i=0; i<numPoints; i++) {
  1029         AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  1030             "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
  1031             i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1032     }
  1033     AssertTrue(refClip.x==clip.x && refClip.y==clip.y && refClip.w==clip.w && refClip.h==clip.h,
  1034         "Source clipping rectangle was incorrectly modified");
  1035     if (expectedEnclosed==SDL_TRUE) {
  1036         AssertTrue(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
  1037             "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  1038             minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  1039     }    
  1040 }
  1041 
  1042 /*!
  1043  * \brief Negative tests against SDL_EnclosePoints() with invalid parameters
  1044  *
  1045  * \sa
  1046  * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
  1047  */
  1048 int rect_testEnclosePointsParam(void *arg)
  1049 {
  1050     SDL_Point points[1];
  1051     int count;
  1052     SDL_Rect clip;
  1053     SDL_Rect result;
  1054     SDL_bool anyEnclosed;
  1055 
  1056     // invalid parameter combinations
  1057     anyEnclosed = SDL_EnclosePoints((SDL_Point *)NULL, 1, (const SDL_Rect *)&clip, &result);
  1058     AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 1st parameter was NULL"); 
  1059     anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, 0, (const SDL_Rect *)&clip, &result);
  1060     AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 2nd parameter was 0");
  1061     count = RandomIntegerInRange(-100, -1);
  1062     anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, count, (const SDL_Rect *)&clip, &result);
  1063     AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 2nd parameter was %i", count); 
  1064     anyEnclosed = SDL_EnclosePoints((SDL_Point *)NULL, 0, (const SDL_Rect *)&clip, &result);
  1065     AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 1st parameter was NULL and 2nd parameter was 0"); 
  1066 }
  1067 
  1068 
  1069 /*!
  1070  * \brief Tests SDL_UnionRect() where rect B is outside rect A
  1071  *
  1072  * \sa
  1073  * http://wiki.libsdl.org/moin.cgi/SDL_UnionRect
  1074  */
  1075 int rect_testUnionRectOutside(void *arg)
  1076 {
  1077     SDL_Rect refRectA, refRectB;
  1078     SDL_Rect rectA, rectB;
  1079     SDL_Rect expectedResult;
  1080     SDL_Rect result;
  1081     int minx, maxx, miny, maxy;
  1082     int dx, dy;
  1083     
  1084     /* Union 1x1 outside */
  1085     for (dx = -1; dx < 2; dx++) {     
  1086         for (dy = -1; dy < 2; dy++) {
  1087             if ((dx != 0) || (dy != 0)) {
  1088                 refRectA.x=RandomIntegerInRange(-1024, 1024);
  1089                 refRectA.y=RandomIntegerInRange(-1024, 1024);
  1090                 refRectA.w=1;
  1091                 refRectA.h=1;
  1092                 refRectB.x=RandomIntegerInRange(-1024, 1024) + dx*2048;
  1093                 refRectB.y=RandomIntegerInRange(-1024, 1024) + dx*2048;
  1094                 refRectB.w=1;
  1095                 refRectB.h=1;
  1096                 minx = (refRectA.x<refRectB.x) ? refRectA.x : refRectB.x;
  1097                 maxx = (refRectA.x>refRectB.x) ? refRectA.x : refRectB.x;
  1098                 miny = (refRectA.y<refRectB.y) ? refRectA.y : refRectB.y;
  1099                 maxy = (refRectA.y>refRectB.y) ? refRectA.y : refRectB.y;                
  1100                 expectedResult.x = minx;
  1101                 expectedResult.y = miny;
  1102                 expectedResult.w = maxx - minx + 1;
  1103                 expectedResult.h = maxy - miny + 1;
  1104                 rectA = refRectA;
  1105                 rectB = refRectB;
  1106                 SDL_UnionRect(&rectA, &rectB, &result);
  1107                 _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1108             }
  1109         }
  1110     }
  1111 
  1112     /* Union outside overlap */
  1113     for (dx = -1; dx < 2; dx++) {     
  1114         for (dy = -1; dy < 2; dy++) {
  1115             if ((dx != 0) || (dy != 0)) {
  1116                 refRectA.x=RandomIntegerInRange(-1024, 1024);
  1117                 refRectA.y=RandomIntegerInRange(-1024, 1024);
  1118                 refRectA.w=RandomIntegerInRange(256, 512);
  1119                 refRectA.h=RandomIntegerInRange(256, 512);
  1120                 refRectB.x=refRectA.x + 1 + dx*2;
  1121                 refRectB.y=refRectA.y + 1 + dy*2;
  1122                 refRectB.w=refRectA.w - 2;
  1123                 refRectB.h=refRectA.h - 2;
  1124                 expectedResult = refRectA;
  1125                 if (dx == -1) expectedResult.x--;
  1126                 if (dy == -1) expectedResult.y--;
  1127                 if ((dx == 1) || (dx == -1)) expectedResult.w++;
  1128                 if ((dy == 1) || (dy == -1)) expectedResult.h++;
  1129                 rectA = refRectA;
  1130                 rectB = refRectB;
  1131                 SDL_UnionRect(&rectA, &rectB, &result);
  1132                 _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1133             }
  1134         }
  1135     }
  1136 }
  1137 
  1138 /*!
  1139  * \brief Tests SDL_UnionRect() where rect B is inside rect A
  1140  *
  1141  * \sa
  1142  * http://wiki.libsdl.org/moin.cgi/SDL_UnionRect
  1143  */
  1144 int rect_testUnionRectInside(void *arg)
  1145 {
  1146     SDL_Rect refRectA, refRectB;
  1147     SDL_Rect rectA, rectB;
  1148     SDL_Rect expectedResult;
  1149     SDL_Rect result;
  1150     int minx, maxx, miny, maxy;
  1151     int dx, dy;
  1152     
  1153     /* Union 1x1 with itself */
  1154     refRectA.x=RandomIntegerInRange(-1024, 1024);
  1155     refRectA.y=RandomIntegerInRange(-1024, 1024);
  1156     refRectA.w=1;
  1157     refRectA.h=1;
  1158     expectedResult = refRectA;
  1159     rectA = refRectA;
  1160     SDL_UnionRect(&rectA, &rectA, &result);
  1161     _validateUnionRectResults(&rectA, &rectA, &refRectA, &refRectA, &result, &expectedResult);
  1162 
  1163     /* Union 1x1 somewhere inside */
  1164     refRectA.x=RandomIntegerInRange(-1024, 1024);
  1165     refRectA.y=RandomIntegerInRange(-1024, 1024);
  1166     refRectA.w=RandomIntegerInRange(256, 1024);
  1167     refRectA.h=RandomIntegerInRange(256, 1024);
  1168     refRectB.x=refRectA.x + 1 + RandomIntegerInRange(1, refRectA.w - 2);
  1169     refRectB.y=refRectA.y + 1 + RandomIntegerInRange(1, refRectA.h - 2);
  1170     refRectB.w=1;
  1171     refRectB.h=1;
  1172     expectedResult = refRectA;
  1173     rectA = refRectA;
  1174     rectB = refRectB;
  1175     SDL_UnionRect(&rectA, &rectB, &result);
  1176     _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1177 
  1178     /* Union inside with edges modified */
  1179     for (dx = -1; dx < 2; dx++) {     
  1180         for (dy = -1; dy < 2; dy++) {
  1181             if ((dx != 0) || (dy != 0)) {
  1182                 refRectA.x=RandomIntegerInRange(-1024, 1024);
  1183                 refRectA.y=RandomIntegerInRange(-1024, 1024);
  1184                 refRectA.w=RandomIntegerInRange(256, 1024);
  1185                 refRectA.h=RandomIntegerInRange(256, 1024);
  1186                 refRectB = refRectA;
  1187                 if (dx == -1) refRectB.x++;
  1188                 if ((dx == 1) || (dx == -1)) refRectB.w--;
  1189                 if (dy == -1) refRectB.y++;
  1190                 if ((dy == 1) || (dy == -1)) refRectB.h--;                
  1191                 expectedResult = refRectA;
  1192                 rectA = refRectA;
  1193                 rectB = refRectB;
  1194                 SDL_UnionRect(&rectA, &rectB, &result);
  1195                 _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1196             }
  1197         }
  1198     }
  1199 }
  1200 
  1201 /*!
  1202  * \brief Negative tests against SDL_UnionRect() with invalid parameters
  1203  *
  1204  * \sa
  1205  * http://wiki.libsdl.org/moin.cgi/SDL_UnionRect
  1206  */
  1207 int rect_testUnionRectParam(void *arg)
  1208 {
  1209     SDL_Rect rectA, rectB;
  1210     SDL_Rect result;
  1211 
  1212     // invalid parameter combinations
  1213     SDL_UnionRect((SDL_Rect *)NULL, &rectB, &result);
  1214     AssertPass("Function did return when 1st parameter was NULL"); 
  1215     SDL_UnionRect(&rectA, (SDL_Rect *)NULL, &result);
  1216     AssertPass("Function did return when 2nd parameter was NULL"); 
  1217     SDL_UnionRect(&rectA, &rectB, (SDL_Rect *)NULL);
  1218     AssertPass("Function did return when 3rd parameter was NULL"); 
  1219     SDL_UnionRect((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
  1220     AssertPass("Function did return when 1st and 3rd parameter were NULL"); 
  1221     SDL_UnionRect(&rectA, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1222     AssertPass("Function did return when 2nd and 3rd parameter were NULL"); 
  1223     SDL_UnionRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1224     AssertPass("Function did return when all parameters were NULL"); 
  1225 }
  1226 
  1227 /*!
  1228  * \brief Tests SDL_RectEmpty() with various inputs
  1229  *
  1230  * \sa
  1231  * http://wiki.libsdl.org/moin.cgi/SDL_RectEmpty
  1232  */
  1233 int rect_testRectEmpty(void *arg)
  1234 {
  1235     SDL_Rect refRect;
  1236     SDL_Rect rect;
  1237     SDL_bool expectedResult;
  1238     SDL_bool result;
  1239     int w, h;
  1240 
  1241     // Non-empty case
  1242     refRect.x=RandomIntegerInRange(-1024, 1024);
  1243     refRect.y=RandomIntegerInRange(-1024, 1024);
  1244     refRect.w=RandomIntegerInRange(256, 1024);
  1245     refRect.h=RandomIntegerInRange(256, 1024);
  1246     expectedResult = SDL_FALSE;
  1247     rect = refRect;
  1248     result = SDL_RectEmpty((const SDL_Rect *)&rect);
  1249     _validateRectEmptyResults(result, expectedResult, &rect, &refRect);
  1250     
  1251     // Empty case
  1252     for (w=-1; w<2; w++) {
  1253         for (h=-1; h<2; h++) {
  1254             if ((w != 1) || (h != 1)) {
  1255                 refRect.x=RandomIntegerInRange(-1024, 1024);
  1256                 refRect.y=RandomIntegerInRange(-1024, 1024);
  1257                 refRect.w=w;
  1258                 refRect.h=h;
  1259                 expectedResult = SDL_TRUE;
  1260                 rect = refRect;
  1261                 result = SDL_RectEmpty((const SDL_Rect *)&rect);
  1262                 _validateRectEmptyResults(result, expectedResult, &rect, &refRect);
  1263             }
  1264         }
  1265     }
  1266 }
  1267 
  1268 /*!
  1269  * \brief Negative tests against SDL_RectEmpty() with invalid parameters
  1270  *
  1271  * \sa
  1272  * http://wiki.libsdl.org/moin.cgi/SDL_RectEmpty
  1273  */
  1274 int rect_testRectEmptyParam(void *arg)
  1275 {
  1276     SDL_Rect rect;
  1277     SDL_bool result;
  1278 
  1279     // invalid parameter combinations
  1280     result = SDL_RectEmpty((const SDL_Rect *)NULL);
  1281     AssertTrue(result = SDL_TRUE, "Function did not return TRUE when 1st parameter was NULL"); 
  1282 }