testautomation_rect.c 66 KB

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