testautomation_rect.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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 <limits.h>
  6. #include <SDL3/SDL.h>
  7. #include <SDL3/SDL_test.h>
  8. #include "testautomation_suites.h"
  9. /* ================= Test Case Implementation ================== */
  10. /* Helper functions */
  11. /**
  12. * Private helper to check SDL_GetRectAndLineIntersectionFloat results
  13. */
  14. static void validateIntersectRectAndLineFloatResults(
  15. bool intersection, bool expectedIntersection,
  16. SDL_FRect *rect,
  17. float x1, float y1, float x2, float y2,
  18. float x1Ref, float y1Ref, float x2Ref, float y2Ref)
  19. {
  20. SDLTest_AssertCheck(intersection == expectedIntersection,
  21. "Check for correct intersection result: expected %s, got %s intersecting rect (%.2f,%.2f,%.2f,%.2f) with line (%.2f,%.2f - %.2f,%.2f)",
  22. (expectedIntersection == true) ? "true" : "false",
  23. (intersection == true) ? "true" : "false",
  24. rect->x, rect->y, rect->w, rect->h,
  25. x1Ref, y1Ref, x2Ref, y2Ref);
  26. SDLTest_AssertCheck(x1 == x1Ref && y1 == y1Ref && x2 == x2Ref && y2 == y2Ref,
  27. "Check if line was incorrectly clipped or modified: got (%.2f,%.2f - %.2f,%.2f) expected (%.2f,%.2f - %.2f,%.2f)",
  28. x1, y1, x2, y2,
  29. x1Ref, y1Ref, x2Ref, y2Ref);
  30. }
  31. /**
  32. * Private helper to check SDL_GetRectAndLineIntersection results
  33. */
  34. static void validateIntersectRectAndLineResults(
  35. bool intersection, bool expectedIntersection,
  36. SDL_Rect *rect, SDL_Rect *refRect,
  37. int x1, int y1, int x2, int y2,
  38. int x1Ref, int y1Ref, int x2Ref, int y2Ref)
  39. {
  40. SDLTest_AssertCheck(intersection == expectedIntersection,
  41. "Check for correct intersection result: expected %s, got %s intersecting rect (%d,%d,%d,%d) with line (%d,%d - %d,%d)",
  42. (expectedIntersection == true) ? "true" : "false",
  43. (intersection == true) ? "true" : "false",
  44. refRect->x, refRect->y, refRect->w, refRect->h,
  45. x1Ref, y1Ref, x2Ref, y2Ref);
  46. SDLTest_AssertCheck(rect->x == refRect->x && rect->y == refRect->y && rect->w == refRect->w && rect->h == refRect->h,
  47. "Check that source rectangle was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  48. rect->x, rect->y, rect->w, rect->h,
  49. refRect->x, refRect->y, refRect->w, refRect->h);
  50. SDLTest_AssertCheck(x1 == x1Ref && y1 == y1Ref && x2 == x2Ref && y2 == y2Ref,
  51. "Check if line was incorrectly clipped or modified: got (%d,%d - %d,%d) expected (%d,%d - %d,%d)",
  52. x1, y1, x2, y2,
  53. x1Ref, y1Ref, x2Ref, y2Ref);
  54. }
  55. /* Test case functions */
  56. /**
  57. * Tests SDL_GetRectAndLineIntersectionFloat() clipping cases
  58. *
  59. * \sa SDL_GetRectAndLineIntersectionFloat
  60. */
  61. static int SDLCALL rect_testIntersectRectAndLineFloat(void *arg)
  62. {
  63. SDL_FRect rect;
  64. float x1, y1;
  65. float x2, y2;
  66. bool intersected;
  67. x1 = 5.0f;
  68. y1 = 6.0f;
  69. x2 = 23.0f;
  70. y2 = 6.0f;
  71. rect.x = 2.5f;
  72. rect.y = 1.5f;
  73. rect.w = 15.25f;
  74. rect.h = 12.0f;
  75. intersected = SDL_GetRectAndLineIntersectionFloat(&rect, &x1, &y1, &x2, &y2);
  76. validateIntersectRectAndLineFloatResults(intersected, true, &rect, x1, y1, x2, y2, 5.0f, 6.0f, 17.75f, 6.0f);
  77. x1 = 0.0f;
  78. y1 = 6.0f;
  79. x2 = 23.0f;
  80. y2 = 6.0f;
  81. rect.x = 2.5f;
  82. rect.y = 1.5f;
  83. rect.w = 0.25f;
  84. rect.h = 12.0f;
  85. intersected = SDL_GetRectAndLineIntersectionFloat(&rect, &x1, &y1, &x2, &y2);
  86. validateIntersectRectAndLineFloatResults(intersected, true, &rect, x1, y1, x2, y2, 2.5f, 6.0f, 2.75f, 6.0f);
  87. return TEST_COMPLETED;
  88. }
  89. /**
  90. * Tests SDL_GetRectAndLineIntersection() clipping cases
  91. *
  92. * \sa SDL_GetRectAndLineIntersection
  93. */
  94. static int SDLCALL rect_testIntersectRectAndLine(void *arg)
  95. {
  96. SDL_Rect refRect = { 0, 0, 32, 32 };
  97. SDL_Rect rect;
  98. int x1, y1;
  99. int x2, y2;
  100. bool intersected;
  101. int xLeft = -SDLTest_RandomIntegerInRange(1, refRect.w);
  102. int xRight = refRect.w + SDLTest_RandomIntegerInRange(1, refRect.w);
  103. int yTop = -SDLTest_RandomIntegerInRange(1, refRect.h);
  104. int yBottom = refRect.h + SDLTest_RandomIntegerInRange(1, refRect.h);
  105. x1 = xLeft;
  106. y1 = 15;
  107. x2 = xRight;
  108. y2 = 15;
  109. rect = refRect;
  110. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  111. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, 0, 15, 31, 15);
  112. x1 = 15;
  113. y1 = yTop;
  114. x2 = 15;
  115. y2 = yBottom;
  116. rect = refRect;
  117. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  118. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, 15, 0, 15, 31);
  119. x1 = -refRect.w;
  120. y1 = -refRect.h;
  121. x2 = 2 * refRect.w;
  122. y2 = 2 * refRect.h;
  123. rect = refRect;
  124. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  125. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, 0, 0, 31, 31);
  126. x1 = 2 * refRect.w;
  127. y1 = 2 * refRect.h;
  128. x2 = -refRect.w;
  129. y2 = -refRect.h;
  130. rect = refRect;
  131. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  132. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, 31, 31, 0, 0);
  133. x1 = -1;
  134. y1 = 32;
  135. x2 = 32;
  136. y2 = -1;
  137. rect = refRect;
  138. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  139. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, 0, 31, 31, 0);
  140. x1 = 32;
  141. y1 = -1;
  142. x2 = -1;
  143. y2 = 32;
  144. rect = refRect;
  145. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  146. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, 31, 0, 0, 31);
  147. /* Test some overflow cases */
  148. refRect.x = INT_MAX - 4;
  149. refRect.y = INT_MAX - 4;
  150. x1 = INT_MAX;
  151. y1 = INT_MIN;
  152. x2 = INT_MIN;
  153. y2 = INT_MAX;
  154. rect = refRect;
  155. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  156. validateIntersectRectAndLineResults(intersected, false, &rect, &refRect, x1, y1, x2, y2, x1, y1, x2, y2);
  157. return TEST_COMPLETED;
  158. }
  159. /**
  160. * Tests SDL_GetRectAndLineIntersection() non-clipping case line inside
  161. *
  162. * \sa SDL_GetRectAndLineIntersection
  163. */
  164. static int SDLCALL rect_testIntersectRectAndLineInside(void *arg)
  165. {
  166. SDL_Rect refRect = { 0, 0, 32, 32 };
  167. SDL_Rect rect;
  168. int x1, y1;
  169. int x2, y2;
  170. bool intersected;
  171. int xmin = refRect.x;
  172. int xmax = refRect.x + refRect.w - 1;
  173. int ymin = refRect.y;
  174. int ymax = refRect.y + refRect.h - 1;
  175. int x1Ref = SDLTest_RandomIntegerInRange(xmin + 1, xmax - 1);
  176. int y1Ref = SDLTest_RandomIntegerInRange(ymin + 1, ymax - 1);
  177. int x2Ref = SDLTest_RandomIntegerInRange(xmin + 1, xmax - 1);
  178. int y2Ref = SDLTest_RandomIntegerInRange(ymin + 1, ymax - 1);
  179. x1 = x1Ref;
  180. y1 = y1Ref;
  181. x2 = x2Ref;
  182. y2 = y2Ref;
  183. rect = refRect;
  184. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  185. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, x2Ref, y2Ref);
  186. x1 = x1Ref;
  187. y1 = y1Ref;
  188. x2 = xmax;
  189. y2 = ymax;
  190. rect = refRect;
  191. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  192. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, xmax, ymax);
  193. x1 = xmin;
  194. y1 = ymin;
  195. x2 = x2Ref;
  196. y2 = y2Ref;
  197. rect = refRect;
  198. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  199. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, xmin, ymin, x2Ref, y2Ref);
  200. x1 = xmin;
  201. y1 = ymin;
  202. x2 = xmax;
  203. y2 = ymax;
  204. rect = refRect;
  205. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  206. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, xmin, ymin, xmax, ymax);
  207. x1 = xmin;
  208. y1 = ymax;
  209. x2 = xmax;
  210. y2 = ymin;
  211. rect = refRect;
  212. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  213. validateIntersectRectAndLineResults(intersected, true, &rect, &refRect, x1, y1, x2, y2, xmin, ymax, xmax, ymin);
  214. return TEST_COMPLETED;
  215. }
  216. /**
  217. * Tests SDL_GetRectAndLineIntersection() non-clipping cases outside
  218. *
  219. * \sa SDL_GetRectAndLineIntersection
  220. */
  221. static int SDLCALL rect_testIntersectRectAndLineOutside(void *arg)
  222. {
  223. SDL_Rect refRect = { 0, 0, 32, 32 };
  224. SDL_Rect rect;
  225. int x1, y1;
  226. int x2, y2;
  227. bool intersected;
  228. int xLeft = -SDLTest_RandomIntegerInRange(1, refRect.w);
  229. int xRight = refRect.w + SDLTest_RandomIntegerInRange(1, refRect.w);
  230. int yTop = -SDLTest_RandomIntegerInRange(1, refRect.h);
  231. int yBottom = refRect.h + SDLTest_RandomIntegerInRange(1, refRect.h);
  232. x1 = xLeft;
  233. y1 = 0;
  234. x2 = xLeft;
  235. y2 = 31;
  236. rect = refRect;
  237. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  238. validateIntersectRectAndLineResults(intersected, false, &rect, &refRect, x1, y1, x2, y2, xLeft, 0, xLeft, 31);
  239. x1 = xRight;
  240. y1 = 0;
  241. x2 = xRight;
  242. y2 = 31;
  243. rect = refRect;
  244. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  245. validateIntersectRectAndLineResults(intersected, false, &rect, &refRect, x1, y1, x2, y2, xRight, 0, xRight, 31);
  246. x1 = 0;
  247. y1 = yTop;
  248. x2 = 31;
  249. y2 = yTop;
  250. rect = refRect;
  251. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  252. validateIntersectRectAndLineResults(intersected, false, &rect, &refRect, x1, y1, x2, y2, 0, yTop, 31, yTop);
  253. x1 = 0;
  254. y1 = yBottom;
  255. x2 = 31;
  256. y2 = yBottom;
  257. rect = refRect;
  258. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  259. validateIntersectRectAndLineResults(intersected, false, &rect, &refRect, x1, y1, x2, y2, 0, yBottom, 31, yBottom);
  260. return TEST_COMPLETED;
  261. }
  262. /**
  263. * Tests SDL_GetRectAndLineIntersection() with empty rectangle
  264. *
  265. * \sa SDL_GetRectAndLineIntersection
  266. */
  267. static int SDLCALL rect_testIntersectRectAndLineEmpty(void *arg)
  268. {
  269. SDL_Rect refRect;
  270. SDL_Rect rect;
  271. int x1, y1, x1Ref, y1Ref;
  272. int x2, y2, x2Ref, y2Ref;
  273. bool intersected;
  274. refRect.x = SDLTest_RandomIntegerInRange(1, 1024);
  275. refRect.y = SDLTest_RandomIntegerInRange(1, 1024);
  276. refRect.w = 0;
  277. refRect.h = 0;
  278. x1Ref = refRect.x;
  279. y1Ref = refRect.y;
  280. x2Ref = SDLTest_RandomIntegerInRange(1, 1024);
  281. y2Ref = SDLTest_RandomIntegerInRange(1, 1024);
  282. x1 = x1Ref;
  283. y1 = y1Ref;
  284. x2 = x2Ref;
  285. y2 = y2Ref;
  286. rect = refRect;
  287. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  288. validateIntersectRectAndLineResults(intersected, false, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, x2Ref, y2Ref);
  289. return TEST_COMPLETED;
  290. }
  291. /**
  292. * Negative tests against SDL_GetRectAndLineIntersection() with invalid parameters
  293. *
  294. * \sa SDL_GetRectAndLineIntersection
  295. */
  296. static int SDLCALL rect_testIntersectRectAndLineParam(void *arg)
  297. {
  298. SDL_Rect rect = { 0, 0, 32, 32 };
  299. int x1 = rect.w / 2;
  300. int y1 = rect.h / 2;
  301. int x2 = x1;
  302. int y2 = 2 * rect.h;
  303. bool intersected;
  304. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, &y2);
  305. SDLTest_AssertCheck(intersected == true, "Check that intersection result was true");
  306. intersected = SDL_GetRectAndLineIntersection((SDL_Rect *)NULL, &x1, &y1, &x2, &y2);
  307. SDLTest_AssertCheck(intersected == false, "Check that function returns false when 1st parameter is NULL");
  308. intersected = SDL_GetRectAndLineIntersection(&rect, (int *)NULL, &y1, &x2, &y2);
  309. SDLTest_AssertCheck(intersected == false, "Check that function returns false when 2nd parameter is NULL");
  310. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, (int *)NULL, &x2, &y2);
  311. SDLTest_AssertCheck(intersected == false, "Check that function returns false when 3rd parameter is NULL");
  312. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, (int *)NULL, &y2);
  313. SDLTest_AssertCheck(intersected == false, "Check that function returns false when 4th parameter is NULL");
  314. intersected = SDL_GetRectAndLineIntersection(&rect, &x1, &y1, &x2, (int *)NULL);
  315. SDLTest_AssertCheck(intersected == false, "Check that function returns false when 5th parameter is NULL");
  316. intersected = SDL_GetRectAndLineIntersection((SDL_Rect *)NULL, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
  317. SDLTest_AssertCheck(intersected == false, "Check that function returns false when all parameters are NULL");
  318. return TEST_COMPLETED;
  319. }
  320. /**
  321. * Private helper to check SDL_HasRectIntersectionFloat results
  322. */
  323. static void validateHasIntersectionFloatResults(
  324. bool intersection, bool expectedIntersection,
  325. SDL_FRect *rectA, SDL_FRect *rectB)
  326. {
  327. SDLTest_AssertCheck(intersection == expectedIntersection,
  328. "Check intersection result: expected %s, got %s intersecting A (%.2f,%.2f,%.2f,%.2f) with B (%.2f,%.2f,%.2f,%.2f)",
  329. (expectedIntersection == true) ? "true" : "false",
  330. (intersection == true) ? "true" : "false",
  331. rectA->x, rectA->y, rectA->w, rectA->h,
  332. rectB->x, rectB->y, rectB->w, rectB->h);
  333. }
  334. /**
  335. * Private helper to check SDL_HasRectIntersection results
  336. */
  337. static void validateHasIntersectionResults(
  338. bool intersection, bool expectedIntersection,
  339. SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB)
  340. {
  341. SDLTest_AssertCheck(intersection == expectedIntersection,
  342. "Check intersection result: expected %s, got %s intersecting A (%d,%d,%d,%d) with B (%d,%d,%d,%d)",
  343. (expectedIntersection == true) ? "true" : "false",
  344. (intersection == true) ? "true" : "false",
  345. rectA->x, rectA->y, rectA->w, rectA->h,
  346. rectB->x, rectB->y, rectB->w, rectB->h);
  347. SDLTest_AssertCheck(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
  348. "Check that source rectangle A was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  349. rectA->x, rectA->y, rectA->w, rectA->h,
  350. refRectA->x, refRectA->y, refRectA->w, refRectA->h);
  351. SDLTest_AssertCheck(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
  352. "Check that source rectangle B was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  353. rectB->x, rectB->y, rectB->w, rectB->h,
  354. refRectB->x, refRectB->y, refRectB->w, refRectB->h);
  355. }
  356. /**
  357. * Private helper to check SDL_GetRectIntersection results
  358. */
  359. static void validateIntersectRectFloatResults(
  360. bool intersection, bool expectedIntersection,
  361. SDL_FRect *rectA, SDL_FRect *rectB,
  362. SDL_FRect *result, SDL_FRect *expectedResult)
  363. {
  364. validateHasIntersectionFloatResults(intersection, expectedIntersection, rectA, rectB);
  365. if (result && expectedResult) {
  366. SDLTest_AssertCheck(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
  367. "Check that intersection of rectangles A (%.2f,%.2f, %.2fx%.2f) and B (%.2f,%.2f %.2fx%.2f) was correctly calculated, got (%.2f,%.2f %.2fx%.2f) expected (%.2f,%.2f,%.2f,%.2f)",
  368. rectA->x, rectA->y, rectA->w, rectA->h,
  369. rectB->x, rectB->y, rectB->w, rectB->h,
  370. result->x, result->y, result->w, result->h,
  371. expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
  372. }
  373. SDLTest_AssertCheck(intersection == SDL_HasRectIntersectionFloat(rectA, rectB),
  374. "Check that intersection (%s) matches SDL_HasRectIntersectionFloat() result (%s)",
  375. intersection ? "true" : "false",
  376. SDL_HasRectIntersectionFloat(rectA, rectB) ? "true" : "false");
  377. }
  378. /**
  379. * Private helper to check SDL_GetRectIntersection results
  380. */
  381. static void validateIntersectRectResults(
  382. bool intersection, bool expectedIntersection,
  383. SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB,
  384. SDL_Rect *result, SDL_Rect *expectedResult)
  385. {
  386. validateHasIntersectionResults(intersection, expectedIntersection, rectA, rectB, refRectA, refRectB);
  387. if (result && expectedResult) {
  388. SDLTest_AssertCheck(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
  389. "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)",
  390. rectA->x, rectA->y, rectA->w, rectA->h,
  391. rectB->x, rectB->y, rectB->w, rectB->h,
  392. result->x, result->y, result->w, result->h,
  393. expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
  394. }
  395. }
  396. /**
  397. * Private helper to check SDL_GetRectUnion results
  398. */
  399. static void validateUnionRectResults(
  400. SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB,
  401. SDL_Rect *result, SDL_Rect *expectedResult)
  402. {
  403. SDLTest_AssertCheck(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
  404. "Check that source rectangle A was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  405. rectA->x, rectA->y, rectA->w, rectA->h,
  406. refRectA->x, refRectA->y, refRectA->w, refRectA->h);
  407. SDLTest_AssertCheck(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
  408. "Check that source rectangle B was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  409. rectB->x, rectB->y, rectB->w, rectB->h,
  410. refRectB->x, refRectB->y, refRectB->w, refRectB->h);
  411. SDLTest_AssertCheck(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
  412. "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)",
  413. rectA->x, rectA->y, rectA->w, rectA->h,
  414. rectB->x, rectB->y, rectB->w, rectB->h,
  415. result->x, result->y, result->w, result->h,
  416. expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
  417. }
  418. /**
  419. * Private helper to check SDL_RectEmptyFloat results
  420. */
  421. static void validateRectEmptyFloatResults(
  422. bool empty, bool expectedEmpty,
  423. SDL_FRect *rect)
  424. {
  425. SDLTest_AssertCheck(empty == expectedEmpty,
  426. "Check for correct empty result: expected %s, got %s testing (%.2f,%.2f,%.2f,%.2f)",
  427. (expectedEmpty == true) ? "true" : "false",
  428. (empty == true) ? "true" : "false",
  429. rect->x, rect->y, rect->w, rect->h);
  430. }
  431. /**
  432. * Private helper to check SDL_RectEmpty results
  433. */
  434. static void validateRectEmptyResults(
  435. bool empty, bool expectedEmpty,
  436. SDL_Rect *rect, SDL_Rect *refRect)
  437. {
  438. SDLTest_AssertCheck(empty == expectedEmpty,
  439. "Check for correct empty result: expected %s, got %s testing (%d,%d,%d,%d)",
  440. (expectedEmpty == true) ? "true" : "false",
  441. (empty == true) ? "true" : "false",
  442. rect->x, rect->y, rect->w, rect->h);
  443. SDLTest_AssertCheck(rect->x == refRect->x && rect->y == refRect->y && rect->w == refRect->w && rect->h == refRect->h,
  444. "Check that source rectangle was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  445. rect->x, rect->y, rect->w, rect->h,
  446. refRect->x, refRect->y, refRect->w, refRect->h);
  447. }
  448. /**
  449. * Private helper to check SDL_RectsEqual results
  450. */
  451. static void validateRectEqualsResults(
  452. bool equals, bool expectedEquals,
  453. SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB)
  454. {
  455. SDLTest_AssertCheck(equals == expectedEquals,
  456. "Check for correct equals result: expected %s, got %s testing (%d,%d,%d,%d) and (%d,%d,%d,%d)",
  457. (expectedEquals == true) ? "true" : "false",
  458. (equals == true) ? "true" : "false",
  459. rectA->x, rectA->y, rectA->w, rectA->h,
  460. rectB->x, rectB->y, rectB->w, rectB->h);
  461. SDLTest_AssertCheck(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
  462. "Check that source rectangle A was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  463. rectA->x, rectA->y, rectA->w, rectA->h,
  464. refRectA->x, refRectA->y, refRectA->w, refRectA->h);
  465. SDLTest_AssertCheck(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
  466. "Check that source rectangle B was not modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
  467. rectB->x, rectB->y, rectB->w, rectB->h,
  468. refRectB->x, refRectB->y, refRectB->w, refRectB->h);
  469. }
  470. /**
  471. * Private helper to check SDL_RectsEqualFloat results
  472. */
  473. static void validateFRectEqualsResults(
  474. bool equals, bool expectedEquals,
  475. SDL_FRect *rectA, SDL_FRect *rectB, SDL_FRect *refRectA, SDL_FRect *refRectB)
  476. {
  477. int cmpRes;
  478. SDLTest_AssertCheck(equals == expectedEquals,
  479. "Check for correct equals result: expected %s, got %s testing (%f,%f,%f,%f) and (%f,%f,%f,%f)",
  480. (expectedEquals == true) ? "true" : "false",
  481. (equals == true) ? "true" : "false",
  482. rectA->x, rectA->y, rectA->w, rectA->h,
  483. rectB->x, rectB->y, rectB->w, rectB->h);
  484. cmpRes = SDL_memcmp(rectA, refRectA, sizeof(*rectA));
  485. SDLTest_AssertCheck(cmpRes == 0,
  486. "Check that source rectangle A was not modified: got (%f,%f,%f,%f) expected (%f,%f,%f,%f)",
  487. rectA->x, rectA->y, rectA->w, rectA->h,
  488. refRectA->x, refRectA->y, refRectA->w, refRectA->h);
  489. cmpRes = SDL_memcmp(rectB, refRectB, sizeof(*rectB));
  490. SDLTest_AssertCheck(cmpRes == 0,
  491. "Check that source rectangle B was not modified: got (%f,%f,%f,%f) expected (%f,%f,%f,%f)",
  492. rectB->x, rectB->y, rectB->w, rectB->h,
  493. refRectB->x, refRectB->y, refRectB->w, refRectB->h);
  494. }
  495. /**
  496. * Tests SDL_GetRectIntersectionFloat()
  497. *
  498. * \sa SDL_GetRectIntersectionFloat
  499. */
  500. static int SDLCALL rect_testIntersectRectFloat(void *arg)
  501. {
  502. SDL_FRect rectA;
  503. SDL_FRect rectB;
  504. SDL_FRect result;
  505. SDL_FRect expectedResult;
  506. bool intersection;
  507. rectA.x = 0.0f;
  508. rectA.y = 0.0f;
  509. rectA.w = 1.0f;
  510. rectA.h = 1.0f;
  511. rectB.x = 0.0f;
  512. rectB.y = 0.0f;
  513. rectB.w = 1.0f;
  514. rectB.h = 1.0f;
  515. expectedResult = rectA;
  516. intersection = SDL_GetRectIntersectionFloat(&rectA, &rectB, &result);
  517. validateIntersectRectFloatResults(intersection, true, &rectA, &rectB, &result, &expectedResult);
  518. rectA.x = 0.0f;
  519. rectA.y = 0.0f;
  520. rectA.w = 1.0f;
  521. rectA.h = 1.0f;
  522. rectB.x = 1.0f;
  523. rectB.y = 0.0f;
  524. rectB.w = 1.0f;
  525. rectB.h = 1.0f;
  526. expectedResult = rectB;
  527. expectedResult.w = 0.0f;
  528. intersection = SDL_GetRectIntersectionFloat(&rectA, &rectB, &result);
  529. validateIntersectRectFloatResults(intersection, true, &rectA, &rectB, &result, &expectedResult);
  530. rectA.x = 0.0f;
  531. rectA.y = 0.0f;
  532. rectA.w = 1.0f;
  533. rectA.h = 1.0f;
  534. rectB.x = 1.0f;
  535. rectB.y = 1.0f;
  536. rectB.w = 1.0f;
  537. rectB.h = 1.0f;
  538. expectedResult = rectB;
  539. expectedResult.w = 0.0f;
  540. expectedResult.h = 0.0f;
  541. intersection = SDL_GetRectIntersectionFloat(&rectA, &rectB, &result);
  542. validateIntersectRectFloatResults(intersection, true, &rectA, &rectB, &result, &expectedResult);
  543. rectA.x = 0.0f;
  544. rectA.y = 0.0f;
  545. rectA.w = 1.0f;
  546. rectA.h = 1.0f;
  547. rectB.x = 2.0f;
  548. rectB.y = 0.0f;
  549. rectB.w = 1.0f;
  550. rectB.h = 1.0f;
  551. expectedResult = rectB;
  552. expectedResult.w = -1.0f;
  553. intersection = SDL_GetRectIntersectionFloat(&rectA, &rectB, &result);
  554. validateIntersectRectFloatResults(intersection, false, &rectA, &rectB, &result, &expectedResult);
  555. return TEST_COMPLETED;
  556. }
  557. /**
  558. * Tests SDL_GetRectIntersection() with B fully inside A
  559. *
  560. * \sa SDL_GetRectIntersection
  561. */
  562. static int SDLCALL rect_testIntersectRectInside(void *arg)
  563. {
  564. SDL_Rect refRectA = { 0, 0, 32, 32 };
  565. SDL_Rect refRectB;
  566. SDL_Rect rectA;
  567. SDL_Rect rectB;
  568. SDL_Rect result;
  569. bool intersection;
  570. /* rectB fully contained in rectA */
  571. refRectB.x = 0;
  572. refRectB.y = 0;
  573. refRectB.w = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  574. refRectB.h = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  575. rectA = refRectA;
  576. rectB = refRectB;
  577. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  578. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectB);
  579. return TEST_COMPLETED;
  580. }
  581. /**
  582. * Tests SDL_GetRectIntersection() with B fully outside A
  583. *
  584. * \sa SDL_GetRectIntersection
  585. */
  586. static int SDLCALL rect_testIntersectRectOutside(void *arg)
  587. {
  588. SDL_Rect refRectA = { 0, 0, 32, 32 };
  589. SDL_Rect refRectB;
  590. SDL_Rect rectA;
  591. SDL_Rect rectB;
  592. SDL_Rect result;
  593. bool intersection;
  594. /* rectB fully outside of rectA */
  595. refRectB.x = refRectA.x + refRectA.w + SDLTest_RandomIntegerInRange(1, 10);
  596. refRectB.y = refRectA.y + refRectA.h + SDLTest_RandomIntegerInRange(1, 10);
  597. refRectB.w = refRectA.w;
  598. refRectB.h = refRectA.h;
  599. rectA = refRectA;
  600. rectB = refRectB;
  601. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  602. validateIntersectRectResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  603. return TEST_COMPLETED;
  604. }
  605. /**
  606. * Tests SDL_GetRectIntersection() with B partially intersecting A
  607. *
  608. * \sa SDL_GetRectIntersection
  609. */
  610. static int SDLCALL rect_testIntersectRectPartial(void *arg)
  611. {
  612. SDL_Rect refRectA = { 0, 0, 32, 32 };
  613. SDL_Rect refRectB;
  614. SDL_Rect rectA;
  615. SDL_Rect rectB;
  616. SDL_Rect result;
  617. SDL_Rect expectedResult;
  618. bool intersection;
  619. /* rectB partially contained in rectA */
  620. refRectB.x = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  621. refRectB.y = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  622. refRectB.w = refRectA.w;
  623. refRectB.h = refRectA.h;
  624. rectA = refRectA;
  625. rectB = refRectB;
  626. expectedResult.x = refRectB.x;
  627. expectedResult.y = refRectB.y;
  628. expectedResult.w = refRectA.w - refRectB.x;
  629. expectedResult.h = refRectA.h - refRectB.y;
  630. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  631. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  632. /* rectB right edge */
  633. refRectB.x = rectA.w - 1;
  634. refRectB.y = rectA.y;
  635. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  636. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  637. rectA = refRectA;
  638. rectB = refRectB;
  639. expectedResult.x = refRectB.x;
  640. expectedResult.y = refRectB.y;
  641. expectedResult.w = 1;
  642. expectedResult.h = refRectB.h;
  643. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  644. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  645. /* rectB left edge */
  646. refRectB.x = 1 - rectA.w;
  647. refRectB.y = rectA.y;
  648. refRectB.w = refRectA.w;
  649. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  650. rectA = refRectA;
  651. rectB = refRectB;
  652. expectedResult.x = 0;
  653. expectedResult.y = refRectB.y;
  654. expectedResult.w = 1;
  655. expectedResult.h = refRectB.h;
  656. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  657. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  658. /* rectB bottom edge */
  659. refRectB.x = rectA.x;
  660. refRectB.y = rectA.h - 1;
  661. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  662. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  663. rectA = refRectA;
  664. rectB = refRectB;
  665. expectedResult.x = refRectB.x;
  666. expectedResult.y = refRectB.y;
  667. expectedResult.w = refRectB.w;
  668. expectedResult.h = 1;
  669. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  670. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  671. /* rectB top edge */
  672. refRectB.x = rectA.x;
  673. refRectB.y = 1 - rectA.h;
  674. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  675. refRectB.h = rectA.h;
  676. rectA = refRectA;
  677. rectB = refRectB;
  678. expectedResult.x = refRectB.x;
  679. expectedResult.y = 0;
  680. expectedResult.w = refRectB.w;
  681. expectedResult.h = 1;
  682. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  683. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  684. return TEST_COMPLETED;
  685. }
  686. /**
  687. * Tests SDL_GetRectIntersection() with 1x1 pixel sized rectangles
  688. *
  689. * \sa SDL_GetRectIntersection
  690. */
  691. static int SDLCALL rect_testIntersectRectPoint(void *arg)
  692. {
  693. SDL_Rect refRectA = { 0, 0, 1, 1 };
  694. SDL_Rect refRectB = { 0, 0, 1, 1 };
  695. SDL_Rect rectA;
  696. SDL_Rect rectB;
  697. SDL_Rect result;
  698. bool intersection;
  699. int offsetX, offsetY;
  700. /* intersecting pixels */
  701. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  702. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  703. refRectB.x = refRectA.x;
  704. refRectB.y = refRectA.y;
  705. rectA = refRectA;
  706. rectB = refRectB;
  707. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  708. validateIntersectRectResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectA);
  709. /* non-intersecting pixels cases */
  710. for (offsetX = -1; offsetX <= 1; offsetX++) {
  711. for (offsetY = -1; offsetY <= 1; offsetY++) {
  712. if (offsetX != 0 || offsetY != 0) {
  713. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  714. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  715. refRectB.x = refRectA.x;
  716. refRectB.y = refRectA.y;
  717. refRectB.x += offsetX;
  718. refRectB.y += offsetY;
  719. rectA = refRectA;
  720. rectB = refRectB;
  721. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  722. validateIntersectRectResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  723. }
  724. }
  725. }
  726. return TEST_COMPLETED;
  727. }
  728. /**
  729. * Tests SDL_GetRectIntersection() with empty rectangles
  730. *
  731. * \sa SDL_GetRectIntersection
  732. */
  733. static int SDLCALL rect_testIntersectRectEmpty(void *arg)
  734. {
  735. SDL_Rect refRectA;
  736. SDL_Rect refRectB;
  737. SDL_Rect rectA;
  738. SDL_Rect rectB;
  739. SDL_Rect result;
  740. bool intersection;
  741. bool empty;
  742. /* Rect A empty */
  743. result.w = SDLTest_RandomIntegerInRange(1, 100);
  744. result.h = SDLTest_RandomIntegerInRange(1, 100);
  745. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  746. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  747. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  748. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  749. refRectB = refRectA;
  750. refRectA.w = 0;
  751. refRectA.h = 0;
  752. rectA = refRectA;
  753. rectB = refRectB;
  754. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  755. validateIntersectRectResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  756. empty = SDL_RectEmpty(&result);
  757. SDLTest_AssertCheck(empty == true, "Validate result is empty Rect; got: %s", (empty == true) ? "true" : "false");
  758. /* Rect B empty */
  759. result.w = SDLTest_RandomIntegerInRange(1, 100);
  760. result.h = SDLTest_RandomIntegerInRange(1, 100);
  761. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  762. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  763. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  764. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  765. refRectB = refRectA;
  766. refRectB.w = 0;
  767. refRectB.h = 0;
  768. rectA = refRectA;
  769. rectB = refRectB;
  770. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  771. validateIntersectRectResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  772. empty = SDL_RectEmpty(&result);
  773. SDLTest_AssertCheck(empty == true, "Validate result is empty Rect; got: %s", (empty == true) ? "true" : "false");
  774. /* Rect A and B empty */
  775. result.w = SDLTest_RandomIntegerInRange(1, 100);
  776. result.h = SDLTest_RandomIntegerInRange(1, 100);
  777. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  778. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  779. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  780. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  781. refRectB = refRectA;
  782. refRectA.w = 0;
  783. refRectA.h = 0;
  784. refRectB.w = 0;
  785. refRectB.h = 0;
  786. rectA = refRectA;
  787. rectB = refRectB;
  788. intersection = SDL_GetRectIntersection(&rectA, &rectB, &result);
  789. validateIntersectRectResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  790. empty = SDL_RectEmpty(&result);
  791. SDLTest_AssertCheck(empty == true, "Validate result is empty Rect; got: %s", (empty == true) ? "true" : "false");
  792. return TEST_COMPLETED;
  793. }
  794. /**
  795. * Negative tests against SDL_GetRectIntersection() with invalid parameters
  796. *
  797. * \sa SDL_GetRectIntersection
  798. */
  799. static int SDLCALL rect_testIntersectRectParam(void *arg)
  800. {
  801. SDL_Rect rectA;
  802. SDL_Rect rectB = { 0 };
  803. SDL_Rect result;
  804. bool intersection;
  805. /* invalid parameter combinations */
  806. intersection = SDL_GetRectIntersection((SDL_Rect *)NULL, &rectB, &result);
  807. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 1st parameter is NULL");
  808. intersection = SDL_GetRectIntersection(&rectA, (SDL_Rect *)NULL, &result);
  809. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 2nd parameter is NULL");
  810. intersection = SDL_GetRectIntersection(&rectA, &rectB, (SDL_Rect *)NULL);
  811. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 3rd parameter is NULL");
  812. intersection = SDL_GetRectIntersection((SDL_Rect *)NULL, (SDL_Rect *)NULL, &result);
  813. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 1st and 2nd parameters are NULL");
  814. intersection = SDL_GetRectIntersection((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
  815. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 1st and 3rd parameters are NULL ");
  816. intersection = SDL_GetRectIntersection((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  817. SDLTest_AssertCheck(intersection == false, "Check that function returns false when all parameters are NULL");
  818. return TEST_COMPLETED;
  819. }
  820. /**
  821. * Tests SDL_HasRectIntersection() with B fully inside A
  822. *
  823. * \sa SDL_HasRectIntersection
  824. */
  825. static int SDLCALL rect_testHasIntersectionInside(void *arg)
  826. {
  827. SDL_Rect refRectA = { 0, 0, 32, 32 };
  828. SDL_Rect refRectB;
  829. SDL_Rect rectA;
  830. SDL_Rect rectB;
  831. bool intersection;
  832. /* rectB fully contained in rectA */
  833. refRectB.x = 0;
  834. refRectB.y = 0;
  835. refRectB.w = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  836. refRectB.h = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  837. rectA = refRectA;
  838. rectB = refRectB;
  839. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  840. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  841. return TEST_COMPLETED;
  842. }
  843. /**
  844. * Tests SDL_HasRectIntersection() with B fully outside A
  845. *
  846. * \sa SDL_HasRectIntersection
  847. */
  848. static int SDLCALL rect_testHasIntersectionOutside(void *arg)
  849. {
  850. SDL_Rect refRectA = { 0, 0, 32, 32 };
  851. SDL_Rect refRectB;
  852. SDL_Rect rectA;
  853. SDL_Rect rectB;
  854. bool intersection;
  855. /* rectB fully outside of rectA */
  856. refRectB.x = refRectA.x + refRectA.w + SDLTest_RandomIntegerInRange(1, 10);
  857. refRectB.y = refRectA.y + refRectA.h + SDLTest_RandomIntegerInRange(1, 10);
  858. refRectB.w = refRectA.w;
  859. refRectB.h = refRectA.h;
  860. rectA = refRectA;
  861. rectB = refRectB;
  862. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  863. validateHasIntersectionResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB);
  864. return TEST_COMPLETED;
  865. }
  866. /**
  867. * Tests SDL_HasRectIntersection() with B partially intersecting A
  868. *
  869. * \sa SDL_HasRectIntersection
  870. */
  871. static int SDLCALL rect_testHasIntersectionPartial(void *arg)
  872. {
  873. SDL_Rect refRectA = { 0, 0, 32, 32 };
  874. SDL_Rect refRectB;
  875. SDL_Rect rectA;
  876. SDL_Rect rectB;
  877. bool intersection;
  878. /* rectB partially contained in rectA */
  879. refRectB.x = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  880. refRectB.y = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  881. refRectB.w = refRectA.w;
  882. refRectB.h = refRectA.h;
  883. rectA = refRectA;
  884. rectB = refRectB;
  885. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  886. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  887. /* rectB right edge */
  888. refRectB.x = rectA.w - 1;
  889. refRectB.y = rectA.y;
  890. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  891. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  892. rectA = refRectA;
  893. rectB = refRectB;
  894. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  895. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  896. /* rectB left edge */
  897. refRectB.x = 1 - rectA.w;
  898. refRectB.y = rectA.y;
  899. refRectB.w = refRectA.w;
  900. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  901. rectA = refRectA;
  902. rectB = refRectB;
  903. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  904. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  905. /* rectB bottom edge */
  906. refRectB.x = rectA.x;
  907. refRectB.y = rectA.h - 1;
  908. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  909. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  910. rectA = refRectA;
  911. rectB = refRectB;
  912. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  913. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  914. /* rectB top edge */
  915. refRectB.x = rectA.x;
  916. refRectB.y = 1 - rectA.h;
  917. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  918. refRectB.h = rectA.h;
  919. rectA = refRectA;
  920. rectB = refRectB;
  921. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  922. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  923. return TEST_COMPLETED;
  924. }
  925. /**
  926. * Tests SDL_HasRectIntersection() with 1x1 pixel sized rectangles
  927. *
  928. * \sa SDL_HasRectIntersection
  929. */
  930. static int SDLCALL rect_testHasIntersectionPoint(void *arg)
  931. {
  932. SDL_Rect refRectA = { 0, 0, 1, 1 };
  933. SDL_Rect refRectB = { 0, 0, 1, 1 };
  934. SDL_Rect rectA;
  935. SDL_Rect rectB;
  936. bool intersection;
  937. int offsetX, offsetY;
  938. /* intersecting pixels */
  939. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  940. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  941. refRectB.x = refRectA.x;
  942. refRectB.y = refRectA.y;
  943. rectA = refRectA;
  944. rectB = refRectB;
  945. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  946. validateHasIntersectionResults(intersection, true, &rectA, &rectB, &refRectA, &refRectB);
  947. /* non-intersecting pixels cases */
  948. for (offsetX = -1; offsetX <= 1; offsetX++) {
  949. for (offsetY = -1; offsetY <= 1; offsetY++) {
  950. if (offsetX != 0 || offsetY != 0) {
  951. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  952. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  953. refRectB.x = refRectA.x;
  954. refRectB.y = refRectA.y;
  955. refRectB.x += offsetX;
  956. refRectB.y += offsetY;
  957. rectA = refRectA;
  958. rectB = refRectB;
  959. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  960. validateHasIntersectionResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB);
  961. }
  962. }
  963. }
  964. return TEST_COMPLETED;
  965. }
  966. /**
  967. * Tests SDL_HasRectIntersection() with empty rectangles
  968. *
  969. * \sa SDL_HasRectIntersection
  970. */
  971. static int SDLCALL rect_testHasIntersectionEmpty(void *arg)
  972. {
  973. SDL_Rect refRectA;
  974. SDL_Rect refRectB;
  975. SDL_Rect rectA;
  976. SDL_Rect rectB;
  977. bool intersection;
  978. /* Rect A empty */
  979. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  980. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  981. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  982. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  983. refRectB = refRectA;
  984. refRectA.w = 0;
  985. refRectA.h = 0;
  986. rectA = refRectA;
  987. rectB = refRectB;
  988. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  989. validateHasIntersectionResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB);
  990. /* Rect B empty */
  991. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  992. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  993. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  994. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  995. refRectB = refRectA;
  996. refRectB.w = 0;
  997. refRectB.h = 0;
  998. rectA = refRectA;
  999. rectB = refRectB;
  1000. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  1001. validateHasIntersectionResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB);
  1002. /* Rect A and B empty */
  1003. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  1004. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  1005. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  1006. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  1007. refRectB = refRectA;
  1008. refRectA.w = 0;
  1009. refRectA.h = 0;
  1010. refRectB.w = 0;
  1011. refRectB.h = 0;
  1012. rectA = refRectA;
  1013. rectB = refRectB;
  1014. intersection = SDL_HasRectIntersection(&rectA, &rectB);
  1015. validateHasIntersectionResults(intersection, false, &rectA, &rectB, &refRectA, &refRectB);
  1016. return TEST_COMPLETED;
  1017. }
  1018. /**
  1019. * Negative tests against SDL_HasRectIntersection() with invalid parameters
  1020. *
  1021. * \sa SDL_HasRectIntersection
  1022. */
  1023. static int SDLCALL rect_testHasIntersectionParam(void *arg)
  1024. {
  1025. SDL_Rect rectA;
  1026. SDL_Rect rectB = { 0 };
  1027. bool intersection;
  1028. /* invalid parameter combinations */
  1029. intersection = SDL_HasRectIntersection((SDL_Rect *)NULL, &rectB);
  1030. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 1st parameter is NULL");
  1031. intersection = SDL_HasRectIntersection(&rectA, (SDL_Rect *)NULL);
  1032. SDLTest_AssertCheck(intersection == false, "Check that function returns false when 2nd parameter is NULL");
  1033. intersection = SDL_HasRectIntersection((SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1034. SDLTest_AssertCheck(intersection == false, "Check that function returns false when all parameters are NULL");
  1035. return TEST_COMPLETED;
  1036. }
  1037. /**
  1038. * Test SDL_GetRectEnclosingPointsFloat()
  1039. *
  1040. * \sa SDL_GetRectEnclosingPointsFloat
  1041. */
  1042. static int SDLCALL rect_testEnclosePointsFloat(void *arg)
  1043. {
  1044. SDL_FPoint fpts[3] = { { 1.25f, 2.5f }, { 1.75f, 3.75f }, { 3.5f, 3.0f } };
  1045. int i, count = 3;
  1046. SDL_FRect clip = { 0.0f, 1.0f, 4.0f, 4.0f };
  1047. SDL_FRect result;
  1048. SDL_GetRectEnclosingPointsFloat(fpts, count, &clip, &result);
  1049. SDLTest_AssertCheck(result.x == 1.25f && result.y == 2.5f && result.w == 2.25f && result.h == 1.25f,
  1050. "Resulting enclosing rectangle incorrect: expected (%.2f,%.2f - %.2fx%.2f), actual (%.2f,%.2f - %.2fx%.2f)",
  1051. 1.25f, 2.5f, 2.25f, 1.25f, result.x, result.y, result.w, result.h);
  1052. for (i = 0; i != count; i++) {
  1053. bool inside;
  1054. inside = SDL_PointInRectFloat(&fpts[i], &clip);
  1055. SDLTest_AssertCheck(inside,
  1056. "Expected point (%.2f,%.2f) to be inside clip rect (%.2f,%.2f - %.2fx%.2f)",
  1057. fpts[i].x, fpts[i].y, clip.x, clip.y, clip.w, clip.h);
  1058. inside = SDL_PointInRectFloat(&fpts[i], &result);
  1059. SDLTest_AssertCheck(inside,
  1060. "Expected point (%.2f,%.2f) to be inside result rect (%.2f,%.2f - %.2fx%.2f)",
  1061. fpts[i].x, fpts[i].y, result.x, result.y, result.w, result.h);
  1062. }
  1063. return TEST_COMPLETED;
  1064. }
  1065. /**
  1066. * Test SDL_GetRectEnclosingPoints() without clipping
  1067. *
  1068. * \sa SDL_GetRectEnclosingPoints
  1069. */
  1070. static int SDLCALL rect_testEnclosePoints(void *arg)
  1071. {
  1072. const int numPoints = 16;
  1073. SDL_Point refPoints[16];
  1074. SDL_Point points[16];
  1075. SDL_Rect result;
  1076. bool anyEnclosed;
  1077. bool anyEnclosedNoResult;
  1078. bool expectedEnclosed = true;
  1079. int newx, newy;
  1080. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  1081. int i;
  1082. /* Create input data, tracking result */
  1083. for (i = 0; i < numPoints; i++) {
  1084. newx = SDLTest_RandomIntegerInRange(-1024, 1024);
  1085. newy = SDLTest_RandomIntegerInRange(-1024, 1024);
  1086. refPoints[i].x = newx;
  1087. refPoints[i].y = newy;
  1088. points[i].x = newx;
  1089. points[i].y = newy;
  1090. if (i == 0) {
  1091. minx = newx;
  1092. maxx = newx;
  1093. miny = newy;
  1094. maxy = newy;
  1095. } else {
  1096. if (newx < minx) {
  1097. minx = newx;
  1098. }
  1099. if (newx > maxx) {
  1100. maxx = newx;
  1101. }
  1102. if (newy < miny) {
  1103. miny = newy;
  1104. }
  1105. if (newy > maxy) {
  1106. maxy = newy;
  1107. }
  1108. }
  1109. }
  1110. /* Call function and validate - special case: no result requested */
  1111. anyEnclosedNoResult = SDL_GetRectEnclosingPoints(points, numPoints, NULL, (SDL_Rect *)NULL);
  1112. SDLTest_AssertCheck(expectedEnclosed == anyEnclosedNoResult,
  1113. "Check expected return value %s, got %s",
  1114. (expectedEnclosed == true) ? "true" : "false",
  1115. (anyEnclosedNoResult == true) ? "true" : "false");
  1116. for (i = 0; i < numPoints; i++) {
  1117. SDLTest_AssertCheck(refPoints[i].x == points[i].x && refPoints[i].y == points[i].y,
  1118. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1119. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1120. }
  1121. /* Call function and validate */
  1122. anyEnclosed = SDL_GetRectEnclosingPoints(points, numPoints, NULL, &result);
  1123. SDLTest_AssertCheck(expectedEnclosed == anyEnclosed,
  1124. "Check return value %s, got %s",
  1125. (expectedEnclosed == true) ? "true" : "false",
  1126. (anyEnclosed == true) ? "true" : "false");
  1127. for (i = 0; i < numPoints; i++) {
  1128. SDLTest_AssertCheck(refPoints[i].x == points[i].x && refPoints[i].y == points[i].y,
  1129. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1130. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1131. }
  1132. SDLTest_AssertCheck(result.x == minx && result.y == miny && result.w == (maxx - minx + 1) && result.h == (maxy - miny + 1),
  1133. "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  1134. minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  1135. return TEST_COMPLETED;
  1136. }
  1137. /**
  1138. * Test SDL_GetRectEnclosingPoints() with repeated input points
  1139. *
  1140. * \sa SDL_GetRectEnclosingPoints
  1141. */
  1142. static int SDLCALL rect_testEnclosePointsRepeatedInput(void *arg)
  1143. {
  1144. const int numPoints = 8;
  1145. const int halfPoints = 4;
  1146. SDL_Point refPoints[8];
  1147. SDL_Point points[8];
  1148. SDL_Rect result;
  1149. bool anyEnclosed;
  1150. bool anyEnclosedNoResult;
  1151. bool expectedEnclosed = true;
  1152. int newx, newy;
  1153. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  1154. int i;
  1155. /* Create input data, tracking result */
  1156. for (i = 0; i < numPoints; i++) {
  1157. if (i < halfPoints) {
  1158. newx = SDLTest_RandomIntegerInRange(-1024, 1024);
  1159. newy = SDLTest_RandomIntegerInRange(-1024, 1024);
  1160. } else {
  1161. newx = refPoints[i - halfPoints].x;
  1162. newy = refPoints[i - halfPoints].y;
  1163. }
  1164. refPoints[i].x = newx;
  1165. refPoints[i].y = newy;
  1166. points[i].x = newx;
  1167. points[i].y = newy;
  1168. if (i == 0) {
  1169. minx = newx;
  1170. maxx = newx;
  1171. miny = newy;
  1172. maxy = newy;
  1173. } else {
  1174. if (newx < minx) {
  1175. minx = newx;
  1176. }
  1177. if (newx > maxx) {
  1178. maxx = newx;
  1179. }
  1180. if (newy < miny) {
  1181. miny = newy;
  1182. }
  1183. if (newy > maxy) {
  1184. maxy = newy;
  1185. }
  1186. }
  1187. }
  1188. /* Call function and validate - special case: no result requested */
  1189. anyEnclosedNoResult = SDL_GetRectEnclosingPoints(points, numPoints, NULL, (SDL_Rect *)NULL);
  1190. SDLTest_AssertCheck(expectedEnclosed == anyEnclosedNoResult,
  1191. "Check return value %s, got %s",
  1192. (expectedEnclosed == true) ? "true" : "false",
  1193. (anyEnclosedNoResult == true) ? "true" : "false");
  1194. for (i = 0; i < numPoints; i++) {
  1195. SDLTest_AssertCheck(refPoints[i].x == points[i].x && refPoints[i].y == points[i].y,
  1196. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1197. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1198. }
  1199. /* Call function and validate */
  1200. anyEnclosed = SDL_GetRectEnclosingPoints(points, numPoints, NULL, &result);
  1201. SDLTest_AssertCheck(expectedEnclosed == anyEnclosed,
  1202. "Check return value %s, got %s",
  1203. (expectedEnclosed == true) ? "true" : "false",
  1204. (anyEnclosed == true) ? "true" : "false");
  1205. for (i = 0; i < numPoints; i++) {
  1206. SDLTest_AssertCheck(refPoints[i].x == points[i].x && refPoints[i].y == points[i].y,
  1207. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1208. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1209. }
  1210. SDLTest_AssertCheck(result.x == minx && result.y == miny && result.w == (maxx - minx + 1) && result.h == (maxy - miny + 1),
  1211. "Check resulting enclosing rectangle: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  1212. minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  1213. return TEST_COMPLETED;
  1214. }
  1215. /**
  1216. * Test SDL_GetRectEnclosingPoints() with clipping
  1217. *
  1218. * \sa SDL_GetRectEnclosingPoints
  1219. */
  1220. static int SDLCALL rect_testEnclosePointsWithClipping(void *arg)
  1221. {
  1222. const int numPoints = 16;
  1223. SDL_Point refPoints[16];
  1224. SDL_Point points[16];
  1225. SDL_Rect refClip;
  1226. SDL_Rect clip;
  1227. SDL_Rect result;
  1228. bool anyEnclosed;
  1229. bool anyEnclosedNoResult;
  1230. bool expectedEnclosed = false;
  1231. int newx, newy;
  1232. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  1233. int i;
  1234. /* Setup clipping rectangle */
  1235. refClip.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1236. refClip.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1237. refClip.w = SDLTest_RandomIntegerInRange(1, 1024);
  1238. refClip.h = SDLTest_RandomIntegerInRange(1, 1024);
  1239. /* Create input data, tracking result */
  1240. for (i = 0; i < numPoints; i++) {
  1241. newx = SDLTest_RandomIntegerInRange(-1024, 1024);
  1242. newy = SDLTest_RandomIntegerInRange(-1024, 1024);
  1243. refPoints[i].x = newx;
  1244. refPoints[i].y = newy;
  1245. points[i].x = newx;
  1246. points[i].y = newy;
  1247. if ((newx >= refClip.x) && (newx < (refClip.x + refClip.w)) &&
  1248. (newy >= refClip.y) && (newy < (refClip.y + refClip.h))) {
  1249. if (expectedEnclosed == false) {
  1250. minx = newx;
  1251. maxx = newx;
  1252. miny = newy;
  1253. maxy = newy;
  1254. } else {
  1255. if (newx < minx) {
  1256. minx = newx;
  1257. }
  1258. if (newx > maxx) {
  1259. maxx = newx;
  1260. }
  1261. if (newy < miny) {
  1262. miny = newy;
  1263. }
  1264. if (newy > maxy) {
  1265. maxy = newy;
  1266. }
  1267. }
  1268. expectedEnclosed = true;
  1269. }
  1270. }
  1271. /* Call function and validate - special case: no result requested */
  1272. clip = refClip;
  1273. anyEnclosedNoResult = SDL_GetRectEnclosingPoints(points, numPoints, &clip, NULL);
  1274. SDLTest_AssertCheck(expectedEnclosed == anyEnclosedNoResult,
  1275. "Expected return value %s, got %s",
  1276. (expectedEnclosed == true) ? "true" : "false",
  1277. (anyEnclosedNoResult == true) ? "true" : "false");
  1278. for (i = 0; i < numPoints; i++) {
  1279. SDLTest_AssertCheck(refPoints[i].x == points[i].x && refPoints[i].y == points[i].y,
  1280. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1281. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1282. }
  1283. SDLTest_AssertCheck(refClip.x == clip.x && refClip.y == clip.y && refClip.w == clip.w && refClip.h == clip.h,
  1284. "Check that source clipping rectangle was not modified");
  1285. /* Call function and validate */
  1286. anyEnclosed = SDL_GetRectEnclosingPoints(points, numPoints, &clip, &result);
  1287. SDLTest_AssertCheck(expectedEnclosed == anyEnclosed,
  1288. "Check return value %s, got %s",
  1289. (expectedEnclosed == true) ? "true" : "false",
  1290. (anyEnclosed == true) ? "true" : "false");
  1291. for (i = 0; i < numPoints; i++) {
  1292. SDLTest_AssertCheck(refPoints[i].x == points[i].x && refPoints[i].y == points[i].y,
  1293. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1294. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1295. }
  1296. SDLTest_AssertCheck(refClip.x == clip.x && refClip.y == clip.y && refClip.w == clip.w && refClip.h == clip.h,
  1297. "Check that source clipping rectangle was not modified");
  1298. if (expectedEnclosed == true) {
  1299. SDLTest_AssertCheck(result.x == minx && result.y == miny && result.w == (maxx - minx + 1) && result.h == (maxy - miny + 1),
  1300. "Check resulting enclosing rectangle: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  1301. minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  1302. }
  1303. /* Empty clipping rectangle */
  1304. clip.w = 0;
  1305. clip.h = 0;
  1306. expectedEnclosed = false;
  1307. anyEnclosed = SDL_GetRectEnclosingPoints(points, numPoints, &clip, &result);
  1308. SDLTest_AssertCheck(expectedEnclosed == anyEnclosed,
  1309. "Check return value %s, got %s",
  1310. (expectedEnclosed == true) ? "true" : "false",
  1311. (anyEnclosed == true) ? "true" : "false");
  1312. return TEST_COMPLETED;
  1313. }
  1314. /**
  1315. * Negative tests against SDL_GetRectEnclosingPoints() with invalid parameters
  1316. *
  1317. * \sa SDL_GetRectEnclosingPoints
  1318. */
  1319. static int SDLCALL rect_testEnclosePointsParam(void *arg)
  1320. {
  1321. SDL_Point points[1];
  1322. int count;
  1323. SDL_Rect clip = { 0 };
  1324. SDL_Rect result;
  1325. bool anyEnclosed;
  1326. /* invalid parameter combinations */
  1327. anyEnclosed = SDL_GetRectEnclosingPoints((SDL_Point *)NULL, 1, &clip, &result);
  1328. SDLTest_AssertCheck(anyEnclosed == false, "Check that functions returns false when 1st parameter is NULL");
  1329. anyEnclosed = SDL_GetRectEnclosingPoints(points, 0, &clip, &result);
  1330. SDLTest_AssertCheck(anyEnclosed == false, "Check that functions returns false when 2nd parameter is 0");
  1331. count = SDLTest_RandomIntegerInRange(-100, -1);
  1332. anyEnclosed = SDL_GetRectEnclosingPoints(points, count, &clip, &result);
  1333. SDLTest_AssertCheck(anyEnclosed == false, "Check that functions returns false when 2nd parameter is %i (negative)", count);
  1334. anyEnclosed = SDL_GetRectEnclosingPoints((SDL_Point *)NULL, 0, &clip, &result);
  1335. SDLTest_AssertCheck(anyEnclosed == false, "Check that functions returns false when 1st parameter is NULL and 2nd parameter was 0");
  1336. return TEST_COMPLETED;
  1337. }
  1338. /**
  1339. * Tests SDL_GetRectUnion() where rect B is outside rect A
  1340. *
  1341. * \sa SDL_GetRectUnion
  1342. */
  1343. static int SDLCALL rect_testUnionRectOutside(void *arg)
  1344. {
  1345. SDL_Rect refRectA, refRectB;
  1346. SDL_Rect rectA, rectB;
  1347. SDL_Rect expectedResult;
  1348. SDL_Rect result;
  1349. int minx, maxx, miny, maxy;
  1350. int dx, dy;
  1351. /* Union 1x1 outside */
  1352. for (dx = -1; dx < 2; dx++) {
  1353. for (dy = -1; dy < 2; dy++) {
  1354. if ((dx != 0) || (dy != 0)) {
  1355. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1356. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1357. refRectA.w = 1;
  1358. refRectA.h = 1;
  1359. refRectB.x = SDLTest_RandomIntegerInRange(-1024, 1024) + dx * 2048;
  1360. refRectB.y = SDLTest_RandomIntegerInRange(-1024, 1024) + dx * 2048;
  1361. refRectB.w = 1;
  1362. refRectB.h = 1;
  1363. minx = (refRectA.x < refRectB.x) ? refRectA.x : refRectB.x;
  1364. maxx = (refRectA.x > refRectB.x) ? refRectA.x : refRectB.x;
  1365. miny = (refRectA.y < refRectB.y) ? refRectA.y : refRectB.y;
  1366. maxy = (refRectA.y > refRectB.y) ? refRectA.y : refRectB.y;
  1367. expectedResult.x = minx;
  1368. expectedResult.y = miny;
  1369. expectedResult.w = maxx - minx + 1;
  1370. expectedResult.h = maxy - miny + 1;
  1371. rectA = refRectA;
  1372. rectB = refRectB;
  1373. SDL_GetRectUnion(&rectA, &rectB, &result);
  1374. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1375. }
  1376. }
  1377. }
  1378. /* Union outside overlap */
  1379. for (dx = -1; dx < 2; dx++) {
  1380. for (dy = -1; dy < 2; dy++) {
  1381. if ((dx != 0) || (dy != 0)) {
  1382. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1383. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1384. refRectA.w = SDLTest_RandomIntegerInRange(256, 512);
  1385. refRectA.h = SDLTest_RandomIntegerInRange(256, 512);
  1386. refRectB.x = refRectA.x + 1 + dx * 2;
  1387. refRectB.y = refRectA.y + 1 + dy * 2;
  1388. refRectB.w = refRectA.w - 2;
  1389. refRectB.h = refRectA.h - 2;
  1390. expectedResult = refRectA;
  1391. if (dx == -1) {
  1392. expectedResult.x--;
  1393. }
  1394. if (dy == -1) {
  1395. expectedResult.y--;
  1396. }
  1397. if ((dx == 1) || (dx == -1)) {
  1398. expectedResult.w++;
  1399. }
  1400. if ((dy == 1) || (dy == -1)) {
  1401. expectedResult.h++;
  1402. }
  1403. rectA = refRectA;
  1404. rectB = refRectB;
  1405. SDL_GetRectUnion(&rectA, &rectB, &result);
  1406. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1407. }
  1408. }
  1409. }
  1410. return TEST_COMPLETED;
  1411. }
  1412. /**
  1413. * Tests SDL_GetRectUnion() where rect A or rect B are empty
  1414. *
  1415. * \sa SDL_GetRectUnion
  1416. */
  1417. static int SDLCALL rect_testUnionRectEmpty(void *arg)
  1418. {
  1419. SDL_Rect refRectA, refRectB;
  1420. SDL_Rect rectA, rectB;
  1421. SDL_Rect expectedResult;
  1422. SDL_Rect result;
  1423. /* A empty */
  1424. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1425. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1426. refRectA.w = 0;
  1427. refRectA.h = 0;
  1428. refRectB.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1429. refRectB.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1430. refRectB.w = SDLTest_RandomIntegerInRange(1, 1024);
  1431. refRectB.h = SDLTest_RandomIntegerInRange(1, 1024);
  1432. expectedResult = refRectB;
  1433. rectA = refRectA;
  1434. rectB = refRectB;
  1435. SDL_GetRectUnion(&rectA, &rectB, &result);
  1436. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1437. /* B empty */
  1438. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1439. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1440. refRectA.w = SDLTest_RandomIntegerInRange(1, 1024);
  1441. refRectA.h = SDLTest_RandomIntegerInRange(1, 1024);
  1442. refRectB.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1443. refRectB.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1444. refRectB.w = 0;
  1445. refRectB.h = 0;
  1446. expectedResult = refRectA;
  1447. rectA = refRectA;
  1448. rectB = refRectB;
  1449. SDL_GetRectUnion(&rectA, &rectB, &result);
  1450. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1451. /* A and B empty */
  1452. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1453. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1454. refRectA.w = 0;
  1455. refRectA.h = 0;
  1456. refRectB.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1457. refRectB.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1458. refRectB.w = 0;
  1459. refRectB.h = 0;
  1460. result.x = 0;
  1461. result.y = 0;
  1462. result.w = 0;
  1463. result.h = 0;
  1464. expectedResult = result;
  1465. rectA = refRectA;
  1466. rectB = refRectB;
  1467. SDL_GetRectUnion(&rectA, &rectB, &result);
  1468. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1469. return TEST_COMPLETED;
  1470. }
  1471. /**
  1472. * Tests SDL_GetRectUnion() where rect B is inside rect A
  1473. *
  1474. * \sa SDL_GetRectUnion
  1475. */
  1476. static int SDLCALL rect_testUnionRectInside(void *arg)
  1477. {
  1478. SDL_Rect refRectA, refRectB;
  1479. SDL_Rect rectA, rectB;
  1480. SDL_Rect expectedResult;
  1481. SDL_Rect result;
  1482. int dx, dy;
  1483. /* Union 1x1 with itself */
  1484. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1485. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1486. refRectA.w = 1;
  1487. refRectA.h = 1;
  1488. expectedResult = refRectA;
  1489. rectA = refRectA;
  1490. SDL_GetRectUnion(&rectA, &rectA, &result);
  1491. validateUnionRectResults(&rectA, &rectA, &refRectA, &refRectA, &result, &expectedResult);
  1492. /* Union 1x1 somewhere inside */
  1493. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1494. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1495. refRectA.w = SDLTest_RandomIntegerInRange(256, 1024);
  1496. refRectA.h = SDLTest_RandomIntegerInRange(256, 1024);
  1497. refRectB.x = refRectA.x + 1 + SDLTest_RandomIntegerInRange(1, refRectA.w - 2);
  1498. refRectB.y = refRectA.y + 1 + SDLTest_RandomIntegerInRange(1, refRectA.h - 2);
  1499. refRectB.w = 1;
  1500. refRectB.h = 1;
  1501. expectedResult = refRectA;
  1502. rectA = refRectA;
  1503. rectB = refRectB;
  1504. SDL_GetRectUnion(&rectA, &rectB, &result);
  1505. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1506. /* Union inside with edges modified */
  1507. for (dx = -1; dx < 2; dx++) {
  1508. for (dy = -1; dy < 2; dy++) {
  1509. if ((dx != 0) || (dy != 0)) {
  1510. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1511. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1512. refRectA.w = SDLTest_RandomIntegerInRange(256, 1024);
  1513. refRectA.h = SDLTest_RandomIntegerInRange(256, 1024);
  1514. refRectB = refRectA;
  1515. if (dx == -1) {
  1516. refRectB.x++;
  1517. }
  1518. if ((dx == 1) || (dx == -1)) {
  1519. refRectB.w--;
  1520. }
  1521. if (dy == -1) {
  1522. refRectB.y++;
  1523. }
  1524. if ((dy == 1) || (dy == -1)) {
  1525. refRectB.h--;
  1526. }
  1527. expectedResult = refRectA;
  1528. rectA = refRectA;
  1529. rectB = refRectB;
  1530. SDL_GetRectUnion(&rectA, &rectB, &result);
  1531. validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1532. }
  1533. }
  1534. }
  1535. return TEST_COMPLETED;
  1536. }
  1537. /**
  1538. * Negative tests against SDL_GetRectUnion() with invalid parameters
  1539. *
  1540. * \sa SDL_GetRectUnion
  1541. */
  1542. static int SDLCALL rect_testUnionRectParam(void *arg)
  1543. {
  1544. SDL_Rect rectA, rectB = { 0 };
  1545. SDL_Rect result;
  1546. /* invalid parameter combinations */
  1547. SDL_GetRectUnion((SDL_Rect *)NULL, &rectB, &result);
  1548. SDLTest_AssertPass("Check that function returns when 1st parameter is NULL");
  1549. SDL_GetRectUnion(&rectA, (SDL_Rect *)NULL, &result);
  1550. SDLTest_AssertPass("Check that function returns when 2nd parameter is NULL");
  1551. SDL_GetRectUnion(&rectA, &rectB, (SDL_Rect *)NULL);
  1552. SDLTest_AssertPass("Check that function returns when 3rd parameter is NULL");
  1553. SDL_GetRectUnion((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
  1554. SDLTest_AssertPass("Check that function returns when 1st and 3rd parameter are NULL");
  1555. SDL_GetRectUnion(&rectA, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1556. SDLTest_AssertPass("Check that function returns when 2nd and 3rd parameter are NULL");
  1557. SDL_GetRectUnion((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1558. SDLTest_AssertPass("Check that function returns when all parameters are NULL");
  1559. return TEST_COMPLETED;
  1560. }
  1561. /**
  1562. * Tests SDL_RectEmptyFloat() with various inputs
  1563. *
  1564. * \sa SDL_RectEmptyFloat
  1565. */
  1566. static int SDLCALL rect_testRectEmptyFloat(void *arg)
  1567. {
  1568. SDL_FRect rect;
  1569. bool result;
  1570. rect.x = 0.0f;
  1571. rect.y = 0.0f;
  1572. rect.w = 1.0f;
  1573. rect.h = 1.0f;
  1574. result = SDL_RectEmptyFloat(&rect);
  1575. validateRectEmptyFloatResults(result, false, &rect);
  1576. rect.x = 0.0f;
  1577. rect.y = 0.0f;
  1578. rect.w = 0.0f;
  1579. rect.h = 0.0f;
  1580. result = SDL_RectEmptyFloat(&rect);
  1581. validateRectEmptyFloatResults(result, false, &rect);
  1582. rect.x = 0.0f;
  1583. rect.y = 0.0f;
  1584. rect.w = -1.0f;
  1585. rect.h = 1.0f;
  1586. result = SDL_RectEmptyFloat(&rect);
  1587. validateRectEmptyFloatResults(result, true, &rect);
  1588. rect.x = 0.0f;
  1589. rect.y = 0.0f;
  1590. rect.w = 1.0f;
  1591. rect.h = -1.0f;
  1592. result = SDL_RectEmptyFloat(&rect);
  1593. validateRectEmptyFloatResults(result, true, &rect);
  1594. return TEST_COMPLETED;
  1595. }
  1596. /**
  1597. * Tests SDL_RectEmpty() with various inputs
  1598. *
  1599. * \sa SDL_RectEmpty
  1600. */
  1601. static int SDLCALL rect_testRectEmpty(void *arg)
  1602. {
  1603. SDL_Rect refRect;
  1604. SDL_Rect rect;
  1605. bool expectedResult;
  1606. bool result;
  1607. int w, h;
  1608. /* Non-empty case */
  1609. refRect.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1610. refRect.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1611. refRect.w = SDLTest_RandomIntegerInRange(256, 1024);
  1612. refRect.h = SDLTest_RandomIntegerInRange(256, 1024);
  1613. expectedResult = false;
  1614. rect = refRect;
  1615. result = SDL_RectEmpty(&rect);
  1616. validateRectEmptyResults(result, expectedResult, &rect, &refRect);
  1617. /* Empty case */
  1618. for (w = -1; w < 2; w++) {
  1619. for (h = -1; h < 2; h++) {
  1620. if ((w != 1) || (h != 1)) {
  1621. refRect.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1622. refRect.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1623. refRect.w = w;
  1624. refRect.h = h;
  1625. expectedResult = true;
  1626. rect = refRect;
  1627. result = SDL_RectEmpty(&rect);
  1628. validateRectEmptyResults(result, expectedResult, &rect, &refRect);
  1629. }
  1630. }
  1631. }
  1632. return TEST_COMPLETED;
  1633. }
  1634. /**
  1635. * Negative tests against SDL_RectEmpty() with invalid parameters
  1636. *
  1637. * \sa SDL_RectEmpty
  1638. */
  1639. static int SDLCALL rect_testRectEmptyParam(void *arg)
  1640. {
  1641. bool result;
  1642. /* invalid parameter combinations */
  1643. result = SDL_RectEmpty(NULL);
  1644. SDLTest_AssertCheck(result == true, "Check that function returns TRUE when 1st parameter is NULL");
  1645. return TEST_COMPLETED;
  1646. }
  1647. /**
  1648. * Tests SDL_RectsEqual() with various inputs
  1649. *
  1650. * \sa SDL_RectsEqual
  1651. */
  1652. static int SDLCALL rect_testRectEquals(void *arg)
  1653. {
  1654. SDL_Rect refRectA;
  1655. SDL_Rect refRectB;
  1656. SDL_Rect rectA;
  1657. SDL_Rect rectB;
  1658. bool expectedResult;
  1659. bool result;
  1660. /* Equals */
  1661. refRectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1662. refRectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1663. refRectA.w = SDLTest_RandomIntegerInRange(1, 1024);
  1664. refRectA.h = SDLTest_RandomIntegerInRange(1, 1024);
  1665. refRectB = refRectA;
  1666. expectedResult = true;
  1667. rectA = refRectA;
  1668. rectB = refRectB;
  1669. result = SDL_RectsEqual(&rectA, &rectB);
  1670. validateRectEqualsResults(result, expectedResult, &rectA, &rectB, &refRectA, &refRectB);
  1671. return TEST_COMPLETED;
  1672. }
  1673. /**
  1674. * Negative tests against SDL_RectsEqual() with invalid parameters
  1675. *
  1676. * \sa SDL_RectsEqual
  1677. */
  1678. static int SDLCALL rect_testRectEqualsParam(void *arg)
  1679. {
  1680. SDL_Rect rectA;
  1681. SDL_Rect rectB;
  1682. bool result;
  1683. /* data setup */
  1684. rectA.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1685. rectA.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1686. rectA.w = SDLTest_RandomIntegerInRange(1, 1024);
  1687. rectA.h = SDLTest_RandomIntegerInRange(1, 1024);
  1688. rectB.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1689. rectB.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1690. rectB.w = SDLTest_RandomIntegerInRange(1, 1024);
  1691. rectB.h = SDLTest_RandomIntegerInRange(1, 1024);
  1692. /* invalid parameter combinations */
  1693. result = SDL_RectsEqual(NULL, &rectB);
  1694. SDLTest_AssertCheck(result == false, "Check that function returns false when 1st parameter is NULL");
  1695. result = SDL_RectsEqual(&rectA, NULL);
  1696. SDLTest_AssertCheck(result == false, "Check that function returns false when 2nd parameter is NULL");
  1697. result = SDL_RectsEqual(NULL, NULL);
  1698. SDLTest_AssertCheck(result == false, "Check that function returns false when 1st and 2nd parameter are NULL");
  1699. return TEST_COMPLETED;
  1700. }
  1701. /**
  1702. * Tests SDL_RectsEqualFloat() with various inputs
  1703. *
  1704. * \sa SDL_RectsEqualFloat
  1705. */
  1706. static int SDLCALL rect_testFRectEquals(void *arg)
  1707. {
  1708. SDL_FRect refRectA;
  1709. SDL_FRect refRectB;
  1710. SDL_FRect rectA;
  1711. SDL_FRect rectB;
  1712. bool expectedResult;
  1713. bool result;
  1714. /* Equals */
  1715. refRectA.x = (float)SDLTest_RandomIntegerInRange(-1024, 1024);
  1716. refRectA.y = (float)SDLTest_RandomIntegerInRange(-1024, 1024);
  1717. refRectA.w = (float)SDLTest_RandomIntegerInRange(1, 1024);
  1718. refRectA.h = (float)SDLTest_RandomIntegerInRange(1, 1024);
  1719. refRectB = refRectA;
  1720. expectedResult = true;
  1721. rectA = refRectA;
  1722. rectB = refRectB;
  1723. result = SDL_RectsEqualFloat(&rectA, &rectB);
  1724. validateFRectEqualsResults(result, expectedResult, &rectA, &rectB, &refRectA, &refRectB);
  1725. return TEST_COMPLETED;
  1726. }
  1727. /**
  1728. * Negative tests against SDL_RectsEqualFloat() with invalid parameters
  1729. *
  1730. * \sa SDL_RectsEqualFloat
  1731. */
  1732. static int SDLCALL rect_testFRectEqualsParam(void *arg)
  1733. {
  1734. SDL_FRect rectA;
  1735. SDL_FRect rectB;
  1736. bool result;
  1737. /* data setup -- For the purpose of this test, the values don't matter. */
  1738. rectA.x = SDLTest_RandomFloat();
  1739. rectA.y = SDLTest_RandomFloat();
  1740. rectA.w = SDLTest_RandomFloat();
  1741. rectA.h = SDLTest_RandomFloat();
  1742. rectB.x = SDLTest_RandomFloat();
  1743. rectB.y = SDLTest_RandomFloat();
  1744. rectB.w = SDLTest_RandomFloat();
  1745. rectB.h = SDLTest_RandomFloat();
  1746. /* invalid parameter combinations */
  1747. result = SDL_RectsEqualFloat(NULL, &rectB);
  1748. SDLTest_AssertCheck(result == false, "Check that function returns false when 1st parameter is NULL");
  1749. result = SDL_RectsEqualFloat(&rectA, NULL);
  1750. SDLTest_AssertCheck(result == false, "Check that function returns false when 2nd parameter is NULL");
  1751. result = SDL_RectsEqualFloat(NULL, NULL);
  1752. SDLTest_AssertCheck(result == false, "Check that function returns false when 1st and 2nd parameter are NULL");
  1753. return TEST_COMPLETED;
  1754. }
  1755. /* ================= Test References ================== */
  1756. /* Rect test cases */
  1757. /* SDL_GetRectAndLineIntersectionFloat */
  1758. static const SDLTest_TestCaseReference rectTestIntersectRectAndLineFloat = {
  1759. rect_testIntersectRectAndLineFloat, "rect_testIntersectRectAndLineFloat", "Tests SDL_GetRectAndLineIntersectionFloat", TEST_ENABLED
  1760. };
  1761. /* SDL_GetRectAndLineIntersection */
  1762. static const SDLTest_TestCaseReference rectTestIntersectRectAndLine = {
  1763. rect_testIntersectRectAndLine, "rect_testIntersectRectAndLine", "Tests SDL_GetRectAndLineIntersection clipping cases", TEST_ENABLED
  1764. };
  1765. static const SDLTest_TestCaseReference rectTestIntersectRectAndLineInside = {
  1766. rect_testIntersectRectAndLineInside, "rect_testIntersectRectAndLineInside", "Tests SDL_GetRectAndLineIntersection with line fully contained in rect", TEST_ENABLED
  1767. };
  1768. static const SDLTest_TestCaseReference rectTestIntersectRectAndLineOutside = {
  1769. rect_testIntersectRectAndLineOutside, "rect_testIntersectRectAndLineOutside", "Tests SDL_GetRectAndLineIntersection with line fully outside of rect", TEST_ENABLED
  1770. };
  1771. static const SDLTest_TestCaseReference rectTestIntersectRectAndLineEmpty = {
  1772. rect_testIntersectRectAndLineEmpty, "rect_testIntersectRectAndLineEmpty", "Tests SDL_GetRectAndLineIntersection with empty rectangle ", TEST_ENABLED
  1773. };
  1774. static const SDLTest_TestCaseReference rectTestIntersectRectAndLineParam = {
  1775. rect_testIntersectRectAndLineParam, "rect_testIntersectRectAndLineParam", "Negative tests against SDL_GetRectAndLineIntersection with invalid parameters", TEST_ENABLED
  1776. };
  1777. /* SDL_GetRectIntersectionFloat */
  1778. static const SDLTest_TestCaseReference rectTestIntersectRectFloat = {
  1779. rect_testIntersectRectFloat, "rect_testIntersectRectFloat", "Tests SDL_GetRectIntersectionFloat", TEST_ENABLED
  1780. };
  1781. /* SDL_GetRectIntersection */
  1782. static const SDLTest_TestCaseReference rectTestIntersectRectInside = {
  1783. rect_testIntersectRectInside, "rect_testIntersectRectInside", "Tests SDL_GetRectIntersection with B fully contained in A", TEST_ENABLED
  1784. };
  1785. static const SDLTest_TestCaseReference rectTestIntersectRectOutside = {
  1786. rect_testIntersectRectOutside, "rect_testIntersectRectOutside", "Tests SDL_GetRectIntersection with B fully outside of A", TEST_ENABLED
  1787. };
  1788. static const SDLTest_TestCaseReference rectTestIntersectRectPartial = {
  1789. rect_testIntersectRectPartial, "rect_testIntersectRectPartial", "Tests SDL_GetRectIntersection with B partially intersecting A", TEST_ENABLED
  1790. };
  1791. static const SDLTest_TestCaseReference rectTestIntersectRectPoint = {
  1792. rect_testIntersectRectPoint, "rect_testIntersectRectPoint", "Tests SDL_GetRectIntersection with 1x1 sized rectangles", TEST_ENABLED
  1793. };
  1794. static const SDLTest_TestCaseReference rectTestIntersectRectEmpty = {
  1795. rect_testIntersectRectEmpty, "rect_testIntersectRectEmpty", "Tests SDL_GetRectIntersection with empty rectangles", TEST_ENABLED
  1796. };
  1797. static const SDLTest_TestCaseReference rectTestIntersectRectParam = {
  1798. rect_testIntersectRectParam, "rect_testIntersectRectParam", "Negative tests against SDL_GetRectIntersection with invalid parameters", TEST_ENABLED
  1799. };
  1800. /* SDL_HasRectIntersection */
  1801. static const SDLTest_TestCaseReference rectTestHasIntersectionInside = {
  1802. rect_testHasIntersectionInside, "rect_testHasIntersectionInside", "Tests SDL_HasRectIntersection with B fully contained in A", TEST_ENABLED
  1803. };
  1804. static const SDLTest_TestCaseReference rectTestHasIntersectionOutside = {
  1805. rect_testHasIntersectionOutside, "rect_testHasIntersectionOutside", "Tests SDL_HasRectIntersection with B fully outside of A", TEST_ENABLED
  1806. };
  1807. static const SDLTest_TestCaseReference rectTestHasIntersectionPartial = {
  1808. rect_testHasIntersectionPartial, "rect_testHasIntersectionPartial", "Tests SDL_HasRectIntersection with B partially intersecting A", TEST_ENABLED
  1809. };
  1810. static const SDLTest_TestCaseReference rectTestHasIntersectionPoint = {
  1811. rect_testHasIntersectionPoint, "rect_testHasIntersectionPoint", "Tests SDL_HasRectIntersection with 1x1 sized rectangles", TEST_ENABLED
  1812. };
  1813. static const SDLTest_TestCaseReference rectTestHasIntersectionEmpty = {
  1814. rect_testHasIntersectionEmpty, "rect_testHasIntersectionEmpty", "Tests SDL_HasRectIntersection with empty rectangles", TEST_ENABLED
  1815. };
  1816. static const SDLTest_TestCaseReference rectTestHasIntersectionParam = {
  1817. rect_testHasIntersectionParam, "rect_testHasIntersectionParam", "Negative tests against SDL_HasRectIntersection with invalid parameters", TEST_ENABLED
  1818. };
  1819. /* SDL_GetRectEnclosingPointsFloat */
  1820. static const SDLTest_TestCaseReference rectTestEnclosePointsFloat = {
  1821. rect_testEnclosePointsFloat, "rect_testEnclosePointsFloat", "Tests SDL_GetRectEnclosingPointsFloat", TEST_ENABLED
  1822. };
  1823. /* SDL_GetRectEnclosingPoints */
  1824. static const SDLTest_TestCaseReference rectTestEnclosePoints = {
  1825. rect_testEnclosePoints, "rect_testEnclosePoints", "Tests SDL_GetRectEnclosingPoints without clipping", TEST_ENABLED
  1826. };
  1827. static const SDLTest_TestCaseReference rectTestEnclosePointsWithClipping = {
  1828. rect_testEnclosePointsWithClipping, "rect_testEnclosePointsWithClipping", "Tests SDL_GetRectEnclosingPoints with clipping", TEST_ENABLED
  1829. };
  1830. static const SDLTest_TestCaseReference rectTestEnclosePointsRepeatedInput = {
  1831. rect_testEnclosePointsRepeatedInput, "rect_testEnclosePointsRepeatedInput", "Tests SDL_GetRectEnclosingPoints with repeated input", TEST_ENABLED
  1832. };
  1833. static const SDLTest_TestCaseReference rectTestEnclosePointsParam = {
  1834. rect_testEnclosePointsParam, "rect_testEnclosePointsParam", "Negative tests against SDL_GetRectEnclosingPoints with invalid parameters", TEST_ENABLED
  1835. };
  1836. /* SDL_GetRectUnion */
  1837. static const SDLTest_TestCaseReference rectTestUnionRectInside = {
  1838. rect_testUnionRectInside, "rect_testUnionRectInside", "Tests SDL_GetRectUnion where rect B is inside rect A", TEST_ENABLED
  1839. };
  1840. static const SDLTest_TestCaseReference rectTestUnionRectOutside = {
  1841. rect_testUnionRectOutside, "rect_testUnionRectOutside", "Tests SDL_GetRectUnion where rect B is outside rect A", TEST_ENABLED
  1842. };
  1843. static const SDLTest_TestCaseReference rectTestUnionRectEmpty = {
  1844. rect_testUnionRectEmpty, "rect_testUnionRectEmpty", "Tests SDL_GetRectUnion where rect A or rect B are empty", TEST_ENABLED
  1845. };
  1846. static const SDLTest_TestCaseReference rectTestUnionRectParam = {
  1847. rect_testUnionRectParam, "rect_testUnionRectParam", "Negative tests against SDL_GetRectUnion with invalid parameters", TEST_ENABLED
  1848. };
  1849. /* SDL_RectEmptyFloat */
  1850. static const SDLTest_TestCaseReference rectTestRectEmptyFloat = {
  1851. rect_testRectEmptyFloat, "rect_testRectEmptyFloat", "Tests SDL_RectEmptyFloat with various inputs", TEST_ENABLED
  1852. };
  1853. /* SDL_RectEmpty */
  1854. static const SDLTest_TestCaseReference rectTestRectEmpty = {
  1855. rect_testRectEmpty, "rect_testRectEmpty", "Tests SDL_RectEmpty with various inputs", TEST_ENABLED
  1856. };
  1857. static const SDLTest_TestCaseReference rectTestRectEmptyParam = {
  1858. rect_testRectEmptyParam, "rect_testRectEmptyParam", "Negative tests against SDL_RectEmpty with invalid parameters", TEST_ENABLED
  1859. };
  1860. /* SDL_RectsEqual */
  1861. static const SDLTest_TestCaseReference rectTestRectEquals = {
  1862. rect_testRectEquals, "rect_testRectEquals", "Tests SDL_RectsEqual with various inputs", TEST_ENABLED
  1863. };
  1864. static const SDLTest_TestCaseReference rectTestRectEqualsParam = {
  1865. rect_testRectEqualsParam, "rect_testRectEqualsParam", "Negative tests against SDL_RectsEqual with invalid parameters", TEST_ENABLED
  1866. };
  1867. /* SDL_RectsEqualFloat */
  1868. static const SDLTest_TestCaseReference rectTestFRectEquals = {
  1869. rect_testFRectEquals, "rect_testFRectEquals", "Tests SDL_RectsEqualFloat with various inputs", TEST_ENABLED
  1870. };
  1871. static const SDLTest_TestCaseReference rectTestFRectEqualsParam = {
  1872. rect_testFRectEqualsParam, "rect_testFRectEqualsParam", "Negative tests against SDL_RectsEqualFloat with invalid parameters", TEST_ENABLED
  1873. };
  1874. /**
  1875. * Sequence of Rect test cases; functions that handle simple rectangles including overlaps and merges.
  1876. */
  1877. static const SDLTest_TestCaseReference *rectTests[] = {
  1878. &rectTestIntersectRectAndLineFloat,
  1879. &rectTestIntersectRectAndLine,
  1880. &rectTestIntersectRectAndLineInside,
  1881. &rectTestIntersectRectAndLineOutside,
  1882. &rectTestIntersectRectAndLineEmpty,
  1883. &rectTestIntersectRectAndLineParam,
  1884. &rectTestIntersectRectFloat,
  1885. &rectTestIntersectRectInside,
  1886. &rectTestIntersectRectOutside,
  1887. &rectTestIntersectRectPartial,
  1888. &rectTestIntersectRectPoint,
  1889. &rectTestIntersectRectEmpty,
  1890. &rectTestIntersectRectParam,
  1891. &rectTestHasIntersectionInside,
  1892. &rectTestHasIntersectionOutside,
  1893. &rectTestHasIntersectionPartial,
  1894. &rectTestHasIntersectionPoint,
  1895. &rectTestHasIntersectionEmpty,
  1896. &rectTestHasIntersectionParam,
  1897. &rectTestEnclosePointsFloat,
  1898. &rectTestEnclosePoints,
  1899. &rectTestEnclosePointsWithClipping,
  1900. &rectTestEnclosePointsRepeatedInput,
  1901. &rectTestEnclosePointsParam,
  1902. &rectTestUnionRectInside,
  1903. &rectTestUnionRectOutside,
  1904. &rectTestUnionRectEmpty,
  1905. &rectTestUnionRectParam,
  1906. &rectTestRectEmptyFloat,
  1907. &rectTestRectEmpty,
  1908. &rectTestRectEmptyParam,
  1909. &rectTestRectEquals,
  1910. &rectTestRectEqualsParam,
  1911. &rectTestFRectEquals,
  1912. &rectTestFRectEqualsParam,
  1913. NULL
  1914. };
  1915. /* Rect test suite (global) */
  1916. SDLTest_TestSuiteReference rectTestSuite = {
  1917. "Rect",
  1918. NULL,
  1919. rectTests,
  1920. NULL
  1921. };