testautomation_rect.c 81 KB

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