testautomation_rect.c 66 KB

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