🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1792 lines
69 KiB

  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/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/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/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/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/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 Private helper to check SDL_FRectEquals results
  369. */
  370. void _validateFRectEqualsResults(
  371. SDL_bool equals, SDL_bool expectedEquals,
  372. SDL_FRect *rectA, SDL_FRect *rectB, SDL_FRect *refRectA, SDL_FRect *refRectB)
  373. {
  374. int cmpRes;
  375. SDLTest_AssertCheck(equals == expectedEquals,
  376. "Check for correct equals result: expected %s, got %s testing (%f,%f,%f,%f) and (%f,%f,%f,%f)",
  377. (expectedEquals == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  378. (equals == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  379. rectA->x, rectA->y, rectA->w, rectA->h,
  380. rectB->x, rectB->y, rectB->w, rectB->h);
  381. cmpRes = SDL_memcmp(rectA, refRectA, sizeof(*rectA));
  382. SDLTest_AssertCheck(cmpRes == 0,
  383. "Check that source rectangle A was not modified: got (%f,%f,%f,%f) expected (%f,%f,%f,%f)",
  384. rectA->x, rectA->y, rectA->w, rectA->h,
  385. refRectA->x, refRectA->y, refRectA->w, refRectA->h);
  386. cmpRes = SDL_memcmp(rectB, refRectB, sizeof(*rectB));
  387. SDLTest_AssertCheck(cmpRes == 0,
  388. "Check that source rectangle B was not modified: got (%f,%f,%f,%f) expected (%f,%f,%f,%f)",
  389. rectB->x, rectB->y, rectB->w, rectB->h,
  390. refRectB->x, refRectB->y, refRectB->w, refRectB->h);
  391. }
  392. /* !
  393. * \brief Tests SDL_IntersectRect() with B fully inside A
  394. *
  395. * \sa
  396. * http://wiki.libsdl.org/SDL_IntersectRect
  397. */
  398. int rect_testIntersectRectInside (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 contained in rectA */
  407. refRectB.x = 0;
  408. refRectB.y = 0;
  409. refRectB.w = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  410. refRectB.h = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  411. rectA = refRectA;
  412. rectB = refRectB;
  413. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  414. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectB);
  415. return TEST_COMPLETED;
  416. }
  417. /* !
  418. * \brief Tests SDL_IntersectRect() with B fully outside A
  419. *
  420. * \sa
  421. * http://wiki.libsdl.org/SDL_IntersectRect
  422. */
  423. int rect_testIntersectRectOutside (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_bool intersection;
  431. /* rectB fully outside of rectA */
  432. refRectB.x = refRectA.x + refRectA.w + SDLTest_RandomIntegerInRange(1, 10);
  433. refRectB.y = refRectA.y + refRectA.h + SDLTest_RandomIntegerInRange(1, 10);
  434. refRectB.w = refRectA.w;
  435. refRectB.h = refRectA.h;
  436. rectA = refRectA;
  437. rectB = refRectB;
  438. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  439. _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  440. return TEST_COMPLETED;
  441. }
  442. /* !
  443. * \brief Tests SDL_IntersectRect() with B partially intersecting A
  444. *
  445. * \sa
  446. * http://wiki.libsdl.org/SDL_IntersectRect
  447. */
  448. int rect_testIntersectRectPartial (void *arg)
  449. {
  450. SDL_Rect refRectA = { 0, 0, 32, 32 };
  451. SDL_Rect refRectB;
  452. SDL_Rect rectA;
  453. SDL_Rect rectB;
  454. SDL_Rect result;
  455. SDL_Rect expectedResult;
  456. SDL_bool intersection;
  457. /* rectB partially contained in rectA */
  458. refRectB.x = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  459. refRectB.y = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  460. refRectB.w = refRectA.w;
  461. refRectB.h = refRectA.h;
  462. rectA = refRectA;
  463. rectB = refRectB;
  464. expectedResult.x = refRectB.x;
  465. expectedResult.y = refRectB.y;
  466. expectedResult.w = refRectA.w - refRectB.x;
  467. expectedResult.h = refRectA.h - refRectB.y;
  468. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  469. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  470. /* rectB right edge */
  471. refRectB.x = rectA.w - 1;
  472. refRectB.y = rectA.y;
  473. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  474. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  475. rectA = refRectA;
  476. rectB = refRectB;
  477. expectedResult.x = refRectB.x;
  478. expectedResult.y = refRectB.y;
  479. expectedResult.w = 1;
  480. expectedResult.h = refRectB.h;
  481. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  482. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  483. /* rectB left edge */
  484. refRectB.x = 1 - rectA.w;
  485. refRectB.y = rectA.y;
  486. refRectB.w = refRectA.w;
  487. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  488. rectA = refRectA;
  489. rectB = refRectB;
  490. expectedResult.x = 0;
  491. expectedResult.y = refRectB.y;
  492. expectedResult.w = 1;
  493. expectedResult.h = refRectB.h;
  494. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  495. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  496. /* rectB bottom edge */
  497. refRectB.x = rectA.x;
  498. refRectB.y = rectA.h - 1;
  499. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  500. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  501. rectA = refRectA;
  502. rectB = refRectB;
  503. expectedResult.x = refRectB.x;
  504. expectedResult.y = refRectB.y;
  505. expectedResult.w = refRectB.w;
  506. expectedResult.h = 1;
  507. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  508. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  509. /* rectB top edge */
  510. refRectB.x = rectA.x;
  511. refRectB.y = 1 - rectA.h;
  512. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  513. refRectB.h = rectA.h;
  514. rectA = refRectA;
  515. rectB = refRectB;
  516. expectedResult.x = refRectB.x;
  517. expectedResult.y = 0;
  518. expectedResult.w = refRectB.w;
  519. expectedResult.h = 1;
  520. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  521. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  522. return TEST_COMPLETED;
  523. }
  524. /* !
  525. * \brief Tests SDL_IntersectRect() with 1x1 pixel sized rectangles
  526. *
  527. * \sa
  528. * http://wiki.libsdl.org/SDL_IntersectRect
  529. */
  530. int rect_testIntersectRectPoint (void *arg)
  531. {
  532. SDL_Rect refRectA = { 0, 0, 1, 1 };
  533. SDL_Rect refRectB = { 0, 0, 1, 1 };
  534. SDL_Rect rectA;
  535. SDL_Rect rectB;
  536. SDL_Rect result;
  537. SDL_bool intersection;
  538. int offsetX, offsetY;
  539. /* intersecting pixels */
  540. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  541. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  542. refRectB.x = refRectA.x;
  543. refRectB.y = refRectA.y;
  544. rectA = refRectA;
  545. rectB = refRectB;
  546. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  547. _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectA);
  548. /* non-intersecting pixels cases */
  549. for (offsetX = -1; offsetX <= 1; offsetX++) {
  550. for (offsetY = -1; offsetY <= 1; offsetY++) {
  551. if (offsetX != 0 || offsetY != 0) {
  552. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  553. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  554. refRectB.x = refRectA.x;
  555. refRectB.y = refRectA.y;
  556. refRectB.x += offsetX;
  557. refRectB.y += offsetY;
  558. rectA = refRectA;
  559. rectB = refRectB;
  560. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  561. _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  562. }
  563. }
  564. }
  565. return TEST_COMPLETED;
  566. }
  567. /* !
  568. * \brief Tests SDL_IntersectRect() with empty rectangles
  569. *
  570. * \sa
  571. * http://wiki.libsdl.org/SDL_IntersectRect
  572. */
  573. int rect_testIntersectRectEmpty (void *arg)
  574. {
  575. SDL_Rect refRectA;
  576. SDL_Rect refRectB;
  577. SDL_Rect rectA;
  578. SDL_Rect rectB;
  579. SDL_Rect result;
  580. SDL_bool intersection;
  581. SDL_bool empty;
  582. /* Rect A empty */
  583. result.w = SDLTest_RandomIntegerInRange(1, 100);
  584. result.h = SDLTest_RandomIntegerInRange(1, 100);
  585. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  586. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  587. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  588. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  589. refRectB = refRectA;
  590. refRectA.w = 0;
  591. refRectA.h = 0;
  592. rectA = refRectA;
  593. rectB = refRectB;
  594. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  595. _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  596. empty = (SDL_bool)SDL_RectEmpty(&result);
  597. SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  598. /* Rect B empty */
  599. result.w = SDLTest_RandomIntegerInRange(1, 100);
  600. result.h = SDLTest_RandomIntegerInRange(1, 100);
  601. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  602. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  603. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  604. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  605. refRectB = refRectA;
  606. refRectB.w = 0;
  607. refRectB.h = 0;
  608. rectA = refRectA;
  609. rectB = refRectB;
  610. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  611. _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  612. empty = (SDL_bool)SDL_RectEmpty(&result);
  613. SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  614. /* Rect A and B empty */
  615. result.w = SDLTest_RandomIntegerInRange(1, 100);
  616. result.h = SDLTest_RandomIntegerInRange(1, 100);
  617. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  618. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  619. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  620. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  621. refRectB = refRectA;
  622. refRectA.w = 0;
  623. refRectA.h = 0;
  624. refRectB.w = 0;
  625. refRectB.h = 0;
  626. rectA = refRectA;
  627. rectB = refRectB;
  628. intersection = SDL_IntersectRect(&rectA, &rectB, &result);
  629. _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  630. empty = (SDL_bool)SDL_RectEmpty(&result);
  631. SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  632. return TEST_COMPLETED;
  633. }
  634. /* !
  635. * \brief Negative tests against SDL_IntersectRect() with invalid parameters
  636. *
  637. * \sa
  638. * http://wiki.libsdl.org/SDL_IntersectRect
  639. */
  640. int rect_testIntersectRectParam(void *arg)
  641. {
  642. SDL_Rect rectA;
  643. SDL_Rect rectB = {0};
  644. SDL_Rect result;
  645. SDL_bool intersection;
  646. /* invalid parameter combinations */
  647. intersection = SDL_IntersectRect((SDL_Rect *)NULL, &rectB, &result);
  648. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 1st parameter is NULL");
  649. intersection = SDL_IntersectRect(&rectA, (SDL_Rect *)NULL, &result);
  650. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 2st parameter is NULL");
  651. intersection = SDL_IntersectRect(&rectA, &rectB, (SDL_Rect *)NULL);
  652. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 3st parameter is NULL");
  653. intersection = SDL_IntersectRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, &result);
  654. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 1st and 2nd parameters are NULL");
  655. intersection = SDL_IntersectRect((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
  656. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 1st and 3rd parameters are NULL ");
  657. intersection = SDL_IntersectRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  658. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when all parameters are NULL");
  659. return TEST_COMPLETED;
  660. }
  661. /* !
  662. * \brief Tests SDL_HasIntersection() with B fully inside A
  663. *
  664. * \sa
  665. * http://wiki.libsdl.org/SDL_HasIntersection
  666. */
  667. int rect_testHasIntersectionInside (void *arg)
  668. {
  669. SDL_Rect refRectA = { 0, 0, 32, 32 };
  670. SDL_Rect refRectB;
  671. SDL_Rect rectA;
  672. SDL_Rect rectB;
  673. SDL_bool intersection;
  674. /* rectB fully contained in rectA */
  675. refRectB.x = 0;
  676. refRectB.y = 0;
  677. refRectB.w = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  678. refRectB.h = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  679. rectA = refRectA;
  680. rectB = refRectB;
  681. intersection = SDL_HasIntersection(&rectA, &rectB);
  682. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  683. return TEST_COMPLETED;
  684. }
  685. /* !
  686. * \brief Tests SDL_HasIntersection() with B fully outside A
  687. *
  688. * \sa
  689. * http://wiki.libsdl.org/SDL_HasIntersection
  690. */
  691. int rect_testHasIntersectionOutside (void *arg)
  692. {
  693. SDL_Rect refRectA = { 0, 0, 32, 32 };
  694. SDL_Rect refRectB;
  695. SDL_Rect rectA;
  696. SDL_Rect rectB;
  697. SDL_bool intersection;
  698. /* rectB fully outside of rectA */
  699. refRectB.x = refRectA.x + refRectA.w + SDLTest_RandomIntegerInRange(1, 10);
  700. refRectB.y = refRectA.y + refRectA.h + SDLTest_RandomIntegerInRange(1, 10);
  701. refRectB.w = refRectA.w;
  702. refRectB.h = refRectA.h;
  703. rectA = refRectA;
  704. rectB = refRectB;
  705. intersection = SDL_HasIntersection(&rectA, &rectB);
  706. _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
  707. return TEST_COMPLETED;
  708. }
  709. /* !
  710. * \brief Tests SDL_HasIntersection() with B partially intersecting A
  711. *
  712. * \sa
  713. * http://wiki.libsdl.org/SDL_HasIntersection
  714. */
  715. int rect_testHasIntersectionPartial (void *arg)
  716. {
  717. SDL_Rect refRectA = { 0, 0, 32, 32 };
  718. SDL_Rect refRectB;
  719. SDL_Rect rectA;
  720. SDL_Rect rectB;
  721. SDL_bool intersection;
  722. /* rectB partially contained in rectA */
  723. refRectB.x = SDLTest_RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
  724. refRectB.y = SDLTest_RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
  725. refRectB.w = refRectA.w;
  726. refRectB.h = refRectA.h;
  727. rectA = refRectA;
  728. rectB = refRectB;
  729. intersection = SDL_HasIntersection(&rectA, &rectB);
  730. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  731. /* rectB right edge */
  732. refRectB.x = rectA.w - 1;
  733. refRectB.y = rectA.y;
  734. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  735. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  736. rectA = refRectA;
  737. rectB = refRectB;
  738. intersection = SDL_HasIntersection(&rectA, &rectB);
  739. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  740. /* rectB left edge */
  741. refRectB.x = 1 - rectA.w;
  742. refRectB.y = rectA.y;
  743. refRectB.w = refRectA.w;
  744. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  745. rectA = refRectA;
  746. rectB = refRectB;
  747. intersection = SDL_HasIntersection(&rectA, &rectB);
  748. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  749. /* rectB bottom edge */
  750. refRectB.x = rectA.x;
  751. refRectB.y = rectA.h - 1;
  752. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  753. refRectB.h = SDLTest_RandomIntegerInRange(1, refRectA.h - 1);
  754. rectA = refRectA;
  755. rectB = refRectB;
  756. intersection = SDL_HasIntersection(&rectA, &rectB);
  757. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  758. /* rectB top edge */
  759. refRectB.x = rectA.x;
  760. refRectB.y = 1 - rectA.h;
  761. refRectB.w = SDLTest_RandomIntegerInRange(1, refRectA.w - 1);
  762. refRectB.h = rectA.h;
  763. rectA = refRectA;
  764. rectB = refRectB;
  765. intersection = SDL_HasIntersection(&rectA, &rectB);
  766. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  767. return TEST_COMPLETED;
  768. }
  769. /* !
  770. * \brief Tests SDL_HasIntersection() with 1x1 pixel sized rectangles
  771. *
  772. * \sa
  773. * http://wiki.libsdl.org/SDL_HasIntersection
  774. */
  775. int rect_testHasIntersectionPoint (void *arg)
  776. {
  777. SDL_Rect refRectA = { 0, 0, 1, 1 };
  778. SDL_Rect refRectB = { 0, 0, 1, 1 };
  779. SDL_Rect rectA;
  780. SDL_Rect rectB;
  781. SDL_bool intersection;
  782. int offsetX, offsetY;
  783. /* intersecting pixels */
  784. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  785. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  786. refRectB.x = refRectA.x;
  787. refRectB.y = refRectA.y;
  788. rectA = refRectA;
  789. rectB = refRectB;
  790. intersection = SDL_HasIntersection(&rectA, &rectB);
  791. _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
  792. /* non-intersecting pixels cases */
  793. for (offsetX = -1; offsetX <= 1; offsetX++) {
  794. for (offsetY = -1; offsetY <= 1; offsetY++) {
  795. if (offsetX != 0 || offsetY != 0) {
  796. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  797. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  798. refRectB.x = refRectA.x;
  799. refRectB.y = refRectA.y;
  800. refRectB.x += offsetX;
  801. refRectB.y += offsetY;
  802. rectA = refRectA;
  803. rectB = refRectB;
  804. intersection = SDL_HasIntersection(&rectA, &rectB);
  805. _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
  806. }
  807. }
  808. }
  809. return TEST_COMPLETED;
  810. }
  811. /* !
  812. * \brief Tests SDL_HasIntersection() with empty rectangles
  813. *
  814. * \sa
  815. * http://wiki.libsdl.org/SDL_HasIntersection
  816. */
  817. int rect_testHasIntersectionEmpty (void *arg)
  818. {
  819. SDL_Rect refRectA;
  820. SDL_Rect refRectB;
  821. SDL_Rect rectA;
  822. SDL_Rect rectB;
  823. SDL_bool intersection;
  824. /* Rect A empty */
  825. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  826. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  827. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  828. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  829. refRectB = refRectA;
  830. refRectA.w = 0;
  831. refRectA.h = 0;
  832. rectA = refRectA;
  833. rectB = refRectB;
  834. intersection = SDL_HasIntersection(&rectA, &rectB);
  835. _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
  836. /* Rect B empty */
  837. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  838. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  839. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  840. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  841. refRectB = refRectA;
  842. refRectB.w = 0;
  843. refRectB.h = 0;
  844. rectA = refRectA;
  845. rectB = refRectB;
  846. intersection = SDL_HasIntersection(&rectA, &rectB);
  847. _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
  848. /* Rect A and B empty */
  849. refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
  850. refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
  851. refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
  852. refRectA.h = SDLTest_RandomIntegerInRange(1, 100);
  853. refRectB = refRectA;
  854. refRectA.w = 0;
  855. refRectA.h = 0;
  856. refRectB.w = 0;
  857. refRectB.h = 0;
  858. rectA = refRectA;
  859. rectB = refRectB;
  860. intersection = SDL_HasIntersection(&rectA, &rectB);
  861. _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
  862. return TEST_COMPLETED;
  863. }
  864. /* !
  865. * \brief Negative tests against SDL_HasIntersection() with invalid parameters
  866. *
  867. * \sa
  868. * http://wiki.libsdl.org/SDL_HasIntersection
  869. */
  870. int rect_testHasIntersectionParam(void *arg)
  871. {
  872. SDL_Rect rectA;
  873. SDL_Rect rectB = {0};
  874. SDL_bool intersection;
  875. /* invalid parameter combinations */
  876. intersection = SDL_HasIntersection((SDL_Rect *)NULL, &rectB);
  877. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 1st parameter is NULL");
  878. intersection = SDL_HasIntersection(&rectA, (SDL_Rect *)NULL);
  879. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when 2st parameter is NULL");
  880. intersection = SDL_HasIntersection((SDL_Rect *)NULL, (SDL_Rect *)NULL);
  881. SDLTest_AssertCheck(intersection == SDL_FALSE, "Check that function returns SDL_FALSE when all parameters are NULL");
  882. return TEST_COMPLETED;
  883. }
  884. /* !
  885. * \brief Test SDL_EnclosePoints() without clipping
  886. *
  887. * \sa
  888. * http://wiki.libsdl.org/SDL_EnclosePoints
  889. */
  890. int rect_testEnclosePoints(void *arg)
  891. {
  892. const int numPoints = 16;
  893. SDL_Point refPoints[16];
  894. SDL_Point points[16];
  895. SDL_Rect result;
  896. SDL_bool anyEnclosed;
  897. SDL_bool anyEnclosedNoResult;
  898. SDL_bool expectedEnclosed = SDL_TRUE;
  899. int newx, newy;
  900. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  901. int i;
  902. /* Create input data, tracking result */
  903. for (i=0; i<numPoints; i++) {
  904. newx = SDLTest_RandomIntegerInRange(-1024, 1024);
  905. newy = SDLTest_RandomIntegerInRange(-1024, 1024);
  906. refPoints[i].x = newx;
  907. refPoints[i].y = newy;
  908. points[i].x = newx;
  909. points[i].y = newy;
  910. if (i==0) {
  911. minx = newx;
  912. maxx = newx;
  913. miny = newy;
  914. maxy = newy;
  915. } else {
  916. if (newx < minx) minx = newx;
  917. if (newx > maxx) maxx = newx;
  918. if (newy < miny) miny = newy;
  919. if (newy > maxy) maxy = newy;
  920. }
  921. }
  922. /* Call function and validate - special case: no result requested */
  923. anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, (SDL_Rect *)NULL);
  924. SDLTest_AssertCheck(expectedEnclosed==anyEnclosedNoResult,
  925. "Check expected return value %s, got %s",
  926. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  927. (anyEnclosedNoResult==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  928. for (i=0; i<numPoints; i++) {
  929. SDLTest_AssertCheck(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  930. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  931. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  932. }
  933. /* Call function and validate */
  934. anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, &result);
  935. SDLTest_AssertCheck(expectedEnclosed==anyEnclosed,
  936. "Check return value %s, got %s",
  937. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  938. (anyEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  939. for (i=0; i<numPoints; i++) {
  940. SDLTest_AssertCheck(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  941. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  942. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  943. }
  944. SDLTest_AssertCheck(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
  945. "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  946. minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  947. return TEST_COMPLETED;
  948. }
  949. /* !
  950. * \brief Test SDL_EnclosePoints() with repeated input points
  951. *
  952. * \sa
  953. * http://wiki.libsdl.org/SDL_EnclosePoints
  954. */
  955. int rect_testEnclosePointsRepeatedInput(void *arg)
  956. {
  957. const int numPoints = 8;
  958. const int halfPoints = 4;
  959. SDL_Point refPoints[8];
  960. SDL_Point points[8];
  961. SDL_Rect result;
  962. SDL_bool anyEnclosed;
  963. SDL_bool anyEnclosedNoResult;
  964. SDL_bool expectedEnclosed = SDL_TRUE;
  965. int newx, newy;
  966. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  967. int i;
  968. /* Create input data, tracking result */
  969. for (i=0; i<numPoints; i++) {
  970. if (i < halfPoints) {
  971. newx = SDLTest_RandomIntegerInRange(-1024, 1024);
  972. newy = SDLTest_RandomIntegerInRange(-1024, 1024);
  973. } else {
  974. newx = refPoints[i-halfPoints].x;
  975. newy = refPoints[i-halfPoints].y;
  976. }
  977. refPoints[i].x = newx;
  978. refPoints[i].y = newy;
  979. points[i].x = newx;
  980. points[i].y = newy;
  981. if (i==0) {
  982. minx = newx;
  983. maxx = newx;
  984. miny = newy;
  985. maxy = newy;
  986. } else {
  987. if (newx < minx) minx = newx;
  988. if (newx > maxx) maxx = newx;
  989. if (newy < miny) miny = newy;
  990. if (newy > maxy) maxy = newy;
  991. }
  992. }
  993. /* Call function and validate - special case: no result requested */
  994. anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, (SDL_Rect *)NULL);
  995. SDLTest_AssertCheck(expectedEnclosed==anyEnclosedNoResult,
  996. "Check return value %s, got %s",
  997. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  998. (anyEnclosedNoResult==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  999. for (i=0; i<numPoints; i++) {
  1000. SDLTest_AssertCheck(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  1001. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1002. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1003. }
  1004. /* Call function and validate */
  1005. anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, &result);
  1006. SDLTest_AssertCheck(expectedEnclosed==anyEnclosed,
  1007. "Check return value %s, got %s",
  1008. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  1009. (anyEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  1010. for (i=0; i<numPoints; i++) {
  1011. SDLTest_AssertCheck(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  1012. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1013. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1014. }
  1015. SDLTest_AssertCheck(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
  1016. "Check resulting enclosing rectangle: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  1017. minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  1018. return TEST_COMPLETED;
  1019. }
  1020. /* !
  1021. * \brief Test SDL_EnclosePoints() with clipping
  1022. *
  1023. * \sa
  1024. * http://wiki.libsdl.org/SDL_EnclosePoints
  1025. */
  1026. int rect_testEnclosePointsWithClipping(void *arg)
  1027. {
  1028. const int numPoints = 16;
  1029. SDL_Point refPoints[16];
  1030. SDL_Point points[16];
  1031. SDL_Rect refClip;
  1032. SDL_Rect clip;
  1033. SDL_Rect result;
  1034. SDL_bool anyEnclosed;
  1035. SDL_bool anyEnclosedNoResult;
  1036. SDL_bool expectedEnclosed = SDL_FALSE;
  1037. int newx, newy;
  1038. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  1039. int i;
  1040. /* Setup clipping rectangle */
  1041. refClip.x = SDLTest_RandomIntegerInRange(-1024, 1024);
  1042. refClip.y = SDLTest_RandomIntegerInRange(-1024, 1024);
  1043. refClip.w = SDLTest_RandomIntegerInRange(1, 1024);
  1044. refClip.h = SDLTest_RandomIntegerInRange(1, 1024);
  1045. /* Create input data, tracking result */
  1046. for (i=0; i<numPoints; i++) {
  1047. newx = SDLTest_RandomIntegerInRange(-1024, 1024);
  1048. newy = SDLTest_RandomIntegerInRange(-1024, 1024);
  1049. refPoints[i].x = newx;
  1050. refPoints[i].y = newy;
  1051. points[i].x = newx;
  1052. points[i].y = newy;
  1053. if ((newx>=refClip.x) && (newx<(refClip.x + refClip.w)) &&
  1054. (newy>=refClip.y) && (newy<(refClip.y + refClip.h))) {
  1055. if (expectedEnclosed==SDL_FALSE) {
  1056. minx = newx;
  1057. maxx = newx;
  1058. miny = newy;
  1059. maxy = newy;
  1060. } else {
  1061. if (newx < minx) minx = newx;
  1062. if (newx > maxx) maxx = newx;
  1063. if (newy < miny) miny = newy;
  1064. if (newy > maxy) maxy = newy;
  1065. }
  1066. expectedEnclosed = SDL_TRUE;
  1067. }
  1068. }
  1069. /* Call function and validate - special case: no result requested */
  1070. clip = refClip;
  1071. anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, (SDL_Rect *)NULL);
  1072. SDLTest_AssertCheck(expectedEnclosed==anyEnclosedNoResult,
  1073. "Expected return value %s, got %s",
  1074. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  1075. (anyEnclosedNoResult==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  1076. for (i=0; i<numPoints; i++) {
  1077. SDLTest_AssertCheck(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  1078. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1079. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1080. }
  1081. SDLTest_AssertCheck(refClip.x==clip.x && refClip.y==clip.y && refClip.w==clip.w && refClip.h==clip.h,
  1082. "Check that source clipping rectangle was not modified");
  1083. /* Call function and validate */
  1084. anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, &result);
  1085. SDLTest_AssertCheck(expectedEnclosed==anyEnclosed,
  1086. "Check return value %s, got %s",
  1087. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  1088. (anyEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  1089. for (i=0; i<numPoints; i++) {
  1090. SDLTest_AssertCheck(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
  1091. "Check that source point %i was not modified: expected (%i,%i) actual (%i,%i)",
  1092. i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
  1093. }
  1094. SDLTest_AssertCheck(refClip.x==clip.x && refClip.y==clip.y && refClip.w==clip.w && refClip.h==clip.h,
  1095. "Check that source clipping rectangle was not modified");
  1096. if (expectedEnclosed==SDL_TRUE) {
  1097. SDLTest_AssertCheck(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
  1098. "Check resulting enclosing rectangle: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
  1099. minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
  1100. }
  1101. /* Empty clipping rectangle */
  1102. clip.w = 0;
  1103. clip.h = 0;
  1104. expectedEnclosed = SDL_FALSE;
  1105. anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, &result);
  1106. SDLTest_AssertCheck(expectedEnclosed==anyEnclosed,
  1107. "Check return value %s, got %s",
  1108. (expectedEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE",
  1109. (anyEnclosed==SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");
  1110. return TEST_COMPLETED;
  1111. }
  1112. /* !
  1113. * \brief Negative tests against SDL_EnclosePoints() with invalid parameters
  1114. *
  1115. * \sa
  1116. * http://wiki.libsdl.org/SDL_EnclosePoints
  1117. */
  1118. int rect_testEnclosePointsParam(void *arg)
  1119. {
  1120. SDL_Point points[1];
  1121. int count;
  1122. SDL_Rect clip = { 0 };
  1123. SDL_Rect result;
  1124. SDL_bool anyEnclosed;
  1125. /* invalid parameter combinations */
  1126. anyEnclosed = SDL_EnclosePoints((SDL_Point *)NULL, 1, (const SDL_Rect *)&clip, &result);
  1127. SDLTest_AssertCheck(anyEnclosed == SDL_FALSE, "Check that functions returns SDL_FALSE when 1st parameter is NULL");
  1128. anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, 0, (const SDL_Rect *)&clip, &result);
  1129. SDLTest_AssertCheck(anyEnclosed == SDL_FALSE, "Check that functions returns SDL_FALSE when 2nd parameter is 0");
  1130. count = SDLTest_RandomIntegerInRange(-100, -1);
  1131. anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, count, (const SDL_Rect *)&clip, &result);
  1132. SDLTest_AssertCheck(anyEnclosed == SDL_FALSE, "Check that functions returns SDL_FALSE when 2nd parameter is %i (negative)", count);
  1133. anyEnclosed = SDL_EnclosePoints((SDL_Point *)NULL, 0, (const SDL_Rect *)&clip, &result);
  1134. SDLTest_AssertCheck(anyEnclosed == SDL_FALSE, "Check that functions returns SDL_FALSE when 1st parameter is NULL and 2nd parameter was 0");
  1135. return TEST_COMPLETED;
  1136. }
  1137. /* !
  1138. * \brief Tests SDL_UnionRect() where rect B is outside rect A
  1139. *
  1140. * \sa
  1141. * http://wiki.libsdl.org/SDL_UnionRect
  1142. */
  1143. int rect_testUnionRectOutside(void *arg)
  1144. {
  1145. SDL_Rect refRectA, refRectB;
  1146. SDL_Rect rectA, rectB;
  1147. SDL_Rect expectedResult;
  1148. SDL_Rect result;
  1149. int minx, maxx, miny, maxy;
  1150. int dx, dy;
  1151. /* Union 1x1 outside */
  1152. for (dx = -1; dx < 2; dx++) {
  1153. for (dy = -1; dy < 2; dy++) {
  1154. if ((dx != 0) || (dy != 0)) {
  1155. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1156. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1157. refRectA.w=1;
  1158. refRectA.h=1;
  1159. refRectB.x=SDLTest_RandomIntegerInRange(-1024, 1024) + dx*2048;
  1160. refRectB.y=SDLTest_RandomIntegerInRange(-1024, 1024) + dx*2048;
  1161. refRectB.w=1;
  1162. refRectB.h=1;
  1163. minx = (refRectA.x<refRectB.x) ? refRectA.x : refRectB.x;
  1164. maxx = (refRectA.x>refRectB.x) ? refRectA.x : refRectB.x;
  1165. miny = (refRectA.y<refRectB.y) ? refRectA.y : refRectB.y;
  1166. maxy = (refRectA.y>refRectB.y) ? refRectA.y : refRectB.y;
  1167. expectedResult.x = minx;
  1168. expectedResult.y = miny;
  1169. expectedResult.w = maxx - minx + 1;
  1170. expectedResult.h = maxy - miny + 1;
  1171. rectA = refRectA;
  1172. rectB = refRectB;
  1173. SDL_UnionRect(&rectA, &rectB, &result);
  1174. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1175. }
  1176. }
  1177. }
  1178. /* Union outside overlap */
  1179. for (dx = -1; dx < 2; dx++) {
  1180. for (dy = -1; dy < 2; dy++) {
  1181. if ((dx != 0) || (dy != 0)) {
  1182. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1183. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1184. refRectA.w=SDLTest_RandomIntegerInRange(256, 512);
  1185. refRectA.h=SDLTest_RandomIntegerInRange(256, 512);
  1186. refRectB.x=refRectA.x + 1 + dx*2;
  1187. refRectB.y=refRectA.y + 1 + dy*2;
  1188. refRectB.w=refRectA.w - 2;
  1189. refRectB.h=refRectA.h - 2;
  1190. expectedResult = refRectA;
  1191. if (dx == -1) expectedResult.x--;
  1192. if (dy == -1) expectedResult.y--;
  1193. if ((dx == 1) || (dx == -1)) expectedResult.w++;
  1194. if ((dy == 1) || (dy == -1)) expectedResult.h++;
  1195. rectA = refRectA;
  1196. rectB = refRectB;
  1197. SDL_UnionRect(&rectA, &rectB, &result);
  1198. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1199. }
  1200. }
  1201. }
  1202. return TEST_COMPLETED;
  1203. }
  1204. /* !
  1205. * \brief Tests SDL_UnionRect() where rect A or rect B are empty
  1206. *
  1207. * \sa
  1208. * http://wiki.libsdl.org/SDL_UnionRect
  1209. */
  1210. int rect_testUnionRectEmpty(void *arg)
  1211. {
  1212. SDL_Rect refRectA, refRectB;
  1213. SDL_Rect rectA, rectB;
  1214. SDL_Rect expectedResult;
  1215. SDL_Rect result;
  1216. /* A empty */
  1217. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1218. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1219. refRectA.w=0;
  1220. refRectA.h=0;
  1221. refRectB.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1222. refRectB.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1223. refRectB.w=SDLTest_RandomIntegerInRange(1, 1024);
  1224. refRectB.h=SDLTest_RandomIntegerInRange(1, 1024);
  1225. expectedResult = refRectB;
  1226. rectA = refRectA;
  1227. rectB = refRectB;
  1228. SDL_UnionRect(&rectA, &rectB, &result);
  1229. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1230. /* B empty */
  1231. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1232. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1233. refRectA.w=SDLTest_RandomIntegerInRange(1, 1024);
  1234. refRectA.h=SDLTest_RandomIntegerInRange(1, 1024);
  1235. refRectB.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1236. refRectB.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1237. refRectB.w=0;
  1238. refRectB.h=0;
  1239. expectedResult = refRectA;
  1240. rectA = refRectA;
  1241. rectB = refRectB;
  1242. SDL_UnionRect(&rectA, &rectB, &result);
  1243. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1244. /* A and B empty */
  1245. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1246. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1247. refRectA.w=0;
  1248. refRectA.h=0;
  1249. refRectB.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1250. refRectB.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1251. refRectB.w=0;
  1252. refRectB.h=0;
  1253. result.x=0;
  1254. result.y=0;
  1255. result.w=0;
  1256. result.h=0;
  1257. expectedResult = result;
  1258. rectA = refRectA;
  1259. rectB = refRectB;
  1260. SDL_UnionRect(&rectA, &rectB, &result);
  1261. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1262. return TEST_COMPLETED;
  1263. }
  1264. /* !
  1265. * \brief Tests SDL_UnionRect() where rect B is inside rect A
  1266. *
  1267. * \sa
  1268. * http://wiki.libsdl.org/SDL_UnionRect
  1269. */
  1270. int rect_testUnionRectInside(void *arg)
  1271. {
  1272. SDL_Rect refRectA, refRectB;
  1273. SDL_Rect rectA, rectB;
  1274. SDL_Rect expectedResult;
  1275. SDL_Rect result;
  1276. int dx, dy;
  1277. /* Union 1x1 with itself */
  1278. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1279. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1280. refRectA.w=1;
  1281. refRectA.h=1;
  1282. expectedResult = refRectA;
  1283. rectA = refRectA;
  1284. SDL_UnionRect(&rectA, &rectA, &result);
  1285. _validateUnionRectResults(&rectA, &rectA, &refRectA, &refRectA, &result, &expectedResult);
  1286. /* Union 1x1 somewhere inside */
  1287. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1288. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1289. refRectA.w=SDLTest_RandomIntegerInRange(256, 1024);
  1290. refRectA.h=SDLTest_RandomIntegerInRange(256, 1024);
  1291. refRectB.x=refRectA.x + 1 + SDLTest_RandomIntegerInRange(1, refRectA.w - 2);
  1292. refRectB.y=refRectA.y + 1 + SDLTest_RandomIntegerInRange(1, refRectA.h - 2);
  1293. refRectB.w=1;
  1294. refRectB.h=1;
  1295. expectedResult = refRectA;
  1296. rectA = refRectA;
  1297. rectB = refRectB;
  1298. SDL_UnionRect(&rectA, &rectB, &result);
  1299. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1300. /* Union inside with edges modified */
  1301. for (dx = -1; dx < 2; dx++) {
  1302. for (dy = -1; dy < 2; dy++) {
  1303. if ((dx != 0) || (dy != 0)) {
  1304. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1305. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1306. refRectA.w=SDLTest_RandomIntegerInRange(256, 1024);
  1307. refRectA.h=SDLTest_RandomIntegerInRange(256, 1024);
  1308. refRectB = refRectA;
  1309. if (dx == -1) refRectB.x++;
  1310. if ((dx == 1) || (dx == -1)) refRectB.w--;
  1311. if (dy == -1) refRectB.y++;
  1312. if ((dy == 1) || (dy == -1)) refRectB.h--;
  1313. expectedResult = refRectA;
  1314. rectA = refRectA;
  1315. rectB = refRectB;
  1316. SDL_UnionRect(&rectA, &rectB, &result);
  1317. _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
  1318. }
  1319. }
  1320. }
  1321. return TEST_COMPLETED;
  1322. }
  1323. /* !
  1324. * \brief Negative tests against SDL_UnionRect() with invalid parameters
  1325. *
  1326. * \sa
  1327. * http://wiki.libsdl.org/SDL_UnionRect
  1328. */
  1329. int rect_testUnionRectParam(void *arg)
  1330. {
  1331. SDL_Rect rectA, rectB = { 0 };
  1332. SDL_Rect result;
  1333. /* invalid parameter combinations */
  1334. SDL_UnionRect((SDL_Rect *)NULL, &rectB, &result);
  1335. SDLTest_AssertPass("Check that function returns when 1st parameter is NULL");
  1336. SDL_UnionRect(&rectA, (SDL_Rect *)NULL, &result);
  1337. SDLTest_AssertPass("Check that function returns when 2nd parameter is NULL");
  1338. SDL_UnionRect(&rectA, &rectB, (SDL_Rect *)NULL);
  1339. SDLTest_AssertPass("Check that function returns when 3rd parameter is NULL");
  1340. SDL_UnionRect((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
  1341. SDLTest_AssertPass("Check that function returns when 1st and 3rd parameter are NULL");
  1342. SDL_UnionRect(&rectA, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1343. SDLTest_AssertPass("Check that function returns when 2nd and 3rd parameter are NULL");
  1344. SDL_UnionRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
  1345. SDLTest_AssertPass("Check that function returns when all parameters are NULL");
  1346. return TEST_COMPLETED;
  1347. }
  1348. /* !
  1349. * \brief Tests SDL_RectEmpty() with various inputs
  1350. *
  1351. * \sa
  1352. * http://wiki.libsdl.org/SDL_RectEmpty
  1353. */
  1354. int rect_testRectEmpty(void *arg)
  1355. {
  1356. SDL_Rect refRect;
  1357. SDL_Rect rect;
  1358. SDL_bool expectedResult;
  1359. SDL_bool result;
  1360. int w, h;
  1361. /* Non-empty case */
  1362. refRect.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1363. refRect.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1364. refRect.w=SDLTest_RandomIntegerInRange(256, 1024);
  1365. refRect.h=SDLTest_RandomIntegerInRange(256, 1024);
  1366. expectedResult = SDL_FALSE;
  1367. rect = refRect;
  1368. result = (SDL_bool)SDL_RectEmpty((const SDL_Rect *)&rect);
  1369. _validateRectEmptyResults(result, expectedResult, &rect, &refRect);
  1370. /* Empty case */
  1371. for (w=-1; w<2; w++) {
  1372. for (h=-1; h<2; h++) {
  1373. if ((w != 1) || (h != 1)) {
  1374. refRect.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1375. refRect.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1376. refRect.w=w;
  1377. refRect.h=h;
  1378. expectedResult = SDL_TRUE;
  1379. rect = refRect;
  1380. result = (SDL_bool)SDL_RectEmpty((const SDL_Rect *)&rect);
  1381. _validateRectEmptyResults(result, expectedResult, &rect, &refRect);
  1382. }
  1383. }
  1384. }
  1385. return TEST_COMPLETED;
  1386. }
  1387. /* !
  1388. * \brief Negative tests against SDL_RectEmpty() with invalid parameters
  1389. *
  1390. * \sa
  1391. * http://wiki.libsdl.org/SDL_RectEmpty
  1392. */
  1393. int rect_testRectEmptyParam(void *arg)
  1394. {
  1395. SDL_bool result;
  1396. /* invalid parameter combinations */
  1397. result = (SDL_bool)SDL_RectEmpty((const SDL_Rect *)NULL);
  1398. SDLTest_AssertCheck(result == SDL_TRUE, "Check that function returns TRUE when 1st parameter is NULL");
  1399. return TEST_COMPLETED;
  1400. }
  1401. /* !
  1402. * \brief Tests SDL_RectEquals() with various inputs
  1403. *
  1404. * \sa
  1405. * http://wiki.libsdl.org/SDL_RectEquals
  1406. */
  1407. int rect_testRectEquals(void *arg)
  1408. {
  1409. SDL_Rect refRectA;
  1410. SDL_Rect refRectB;
  1411. SDL_Rect rectA;
  1412. SDL_Rect rectB;
  1413. SDL_bool expectedResult;
  1414. SDL_bool result;
  1415. /* Equals */
  1416. refRectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1417. refRectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1418. refRectA.w=SDLTest_RandomIntegerInRange(1, 1024);
  1419. refRectA.h=SDLTest_RandomIntegerInRange(1, 1024);
  1420. refRectB = refRectA;
  1421. expectedResult = SDL_TRUE;
  1422. rectA = refRectA;
  1423. rectB = refRectB;
  1424. result = (SDL_bool)SDL_RectEquals((const SDL_Rect *)&rectA, (const SDL_Rect *)&rectB);
  1425. _validateRectEqualsResults(result, expectedResult, &rectA, &rectB, &refRectA, &refRectB);
  1426. return TEST_COMPLETED;
  1427. }
  1428. /* !
  1429. * \brief Negative tests against SDL_RectEquals() with invalid parameters
  1430. *
  1431. * \sa
  1432. * http://wiki.libsdl.org/SDL_RectEquals
  1433. */
  1434. int rect_testRectEqualsParam(void *arg)
  1435. {
  1436. SDL_Rect rectA;
  1437. SDL_Rect rectB;
  1438. SDL_bool result;
  1439. /* data setup */
  1440. rectA.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1441. rectA.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1442. rectA.w=SDLTest_RandomIntegerInRange(1, 1024);
  1443. rectA.h=SDLTest_RandomIntegerInRange(1, 1024);
  1444. rectB.x=SDLTest_RandomIntegerInRange(-1024, 1024);
  1445. rectB.y=SDLTest_RandomIntegerInRange(-1024, 1024);
  1446. rectB.w=SDLTest_RandomIntegerInRange(1, 1024);
  1447. rectB.h=SDLTest_RandomIntegerInRange(1, 1024);
  1448. /* invalid parameter combinations */
  1449. result = (SDL_bool)SDL_RectEquals((const SDL_Rect *)NULL, (const SDL_Rect *)&rectB);
  1450. SDLTest_AssertCheck(result == SDL_FALSE, "Check that function returns SDL_FALSE when 1st parameter is NULL");
  1451. result = (SDL_bool)SDL_RectEquals((const SDL_Rect *)&rectA, (const SDL_Rect *)NULL);
  1452. SDLTest_AssertCheck(result == SDL_FALSE, "Check that function returns SDL_FALSE when 2nd parameter is NULL");
  1453. result = (SDL_bool)SDL_RectEquals((const SDL_Rect *)NULL, (const SDL_Rect *)NULL);
  1454. SDLTest_AssertCheck(result == SDL_FALSE, "Check that function returns SDL_FALSE when 1st and 2nd parameter are NULL");
  1455. return TEST_COMPLETED;
  1456. }
  1457. /* !
  1458. * \brief Tests SDL_FRectEquals() with various inputs
  1459. *
  1460. * \sa
  1461. * http://wiki.libsdl.org/SDL_FRectEquals
  1462. */
  1463. int rect_testFRectEquals(void *arg)
  1464. {
  1465. SDL_FRect refRectA;
  1466. SDL_FRect refRectB;
  1467. SDL_FRect rectA;
  1468. SDL_FRect rectB;
  1469. SDL_bool expectedResult;
  1470. SDL_bool result;
  1471. /* Equals */
  1472. refRectA.x=(float)SDLTest_RandomIntegerInRange(-1024, 1024);
  1473. refRectA.y=(float)SDLTest_RandomIntegerInRange(-1024, 1024);
  1474. refRectA.w=(float)SDLTest_RandomIntegerInRange(1, 1024);
  1475. refRectA.h=(float)SDLTest_RandomIntegerInRange(1, 1024);
  1476. refRectB = refRectA;
  1477. expectedResult = SDL_TRUE;
  1478. rectA = refRectA;
  1479. rectB = refRectB;
  1480. result = (SDL_bool)SDL_FRectEquals((const SDL_FRect *)&rectA, (const SDL_FRect *)&rectB);
  1481. _validateFRectEqualsResults(result, expectedResult, &rectA, &rectB, &refRectA, &refRectB);
  1482. return TEST_COMPLETED;
  1483. }
  1484. /* !
  1485. * \brief Negative tests against SDL_FRectEquals() with invalid parameters
  1486. *
  1487. * \sa
  1488. * http://wiki.libsdl.org/SDL_FRectEquals
  1489. */
  1490. int rect_testFRectEqualsParam(void *arg)
  1491. {
  1492. SDL_FRect rectA;
  1493. SDL_FRect rectB;
  1494. SDL_bool result;
  1495. /* data setup -- For the purpose of this test, the values don't matter. */
  1496. rectA.x=SDLTest_RandomFloat();
  1497. rectA.y=SDLTest_RandomFloat();
  1498. rectA.w=SDLTest_RandomFloat();
  1499. rectA.h=SDLTest_RandomFloat();
  1500. rectB.x=SDLTest_RandomFloat();
  1501. rectB.y=SDLTest_RandomFloat();
  1502. rectB.w=SDLTest_RandomFloat();
  1503. rectB.h=SDLTest_RandomFloat();
  1504. /* invalid parameter combinations */
  1505. result = (SDL_bool)SDL_FRectEquals((const SDL_FRect *)NULL, (const SDL_FRect *)&rectB);
  1506. SDLTest_AssertCheck(result == SDL_FALSE, "Check that function returns SDL_FALSE when 1st parameter is NULL");
  1507. result = (SDL_bool)SDL_FRectEquals((const SDL_FRect *)&rectA, (const SDL_FRect *)NULL);
  1508. SDLTest_AssertCheck(result == SDL_FALSE, "Check that function returns SDL_FALSE when 2nd parameter is NULL");
  1509. result = (SDL_bool)SDL_FRectEquals((const SDL_FRect *)NULL, (const SDL_FRect *)NULL);
  1510. SDLTest_AssertCheck(result == SDL_FALSE, "Check that function returns SDL_FALSE when 1st and 2nd parameter are NULL");
  1511. return TEST_COMPLETED;
  1512. }
  1513. /* ================= Test References ================== */
  1514. /* Rect test cases */
  1515. /* SDL_IntersectRectAndLine */
  1516. static const SDLTest_TestCaseReference rectTest1 =
  1517. { (SDLTest_TestCaseFp)rect_testIntersectRectAndLine,"rect_testIntersectRectAndLine", "Tests SDL_IntersectRectAndLine clipping cases", TEST_ENABLED };
  1518. static const SDLTest_TestCaseReference rectTest2 =
  1519. { (SDLTest_TestCaseFp)rect_testIntersectRectAndLineInside, "rect_testIntersectRectAndLineInside", "Tests SDL_IntersectRectAndLine with line fully contained in rect", TEST_ENABLED };
  1520. static const SDLTest_TestCaseReference rectTest3 =
  1521. { (SDLTest_TestCaseFp)rect_testIntersectRectAndLineOutside, "rect_testIntersectRectAndLineOutside", "Tests SDL_IntersectRectAndLine with line fully outside of rect", TEST_ENABLED };
  1522. static const SDLTest_TestCaseReference rectTest4 =
  1523. { (SDLTest_TestCaseFp)rect_testIntersectRectAndLineEmpty, "rect_testIntersectRectAndLineEmpty", "Tests SDL_IntersectRectAndLine with empty rectangle ", TEST_ENABLED };
  1524. static const SDLTest_TestCaseReference rectTest5 =
  1525. { (SDLTest_TestCaseFp)rect_testIntersectRectAndLineParam, "rect_testIntersectRectAndLineParam", "Negative tests against SDL_IntersectRectAndLine with invalid parameters", TEST_ENABLED };
  1526. /* SDL_IntersectRect */
  1527. static const SDLTest_TestCaseReference rectTest6 =
  1528. { (SDLTest_TestCaseFp)rect_testIntersectRectInside, "rect_testIntersectRectInside", "Tests SDL_IntersectRect with B fully contained in A", TEST_ENABLED };
  1529. static const SDLTest_TestCaseReference rectTest7 =
  1530. { (SDLTest_TestCaseFp)rect_testIntersectRectOutside, "rect_testIntersectRectOutside", "Tests SDL_IntersectRect with B fully outside of A", TEST_ENABLED };
  1531. static const SDLTest_TestCaseReference rectTest8 =
  1532. { (SDLTest_TestCaseFp)rect_testIntersectRectPartial, "rect_testIntersectRectPartial", "Tests SDL_IntersectRect with B partially intersecting A", TEST_ENABLED };
  1533. static const SDLTest_TestCaseReference rectTest9 =
  1534. { (SDLTest_TestCaseFp)rect_testIntersectRectPoint, "rect_testIntersectRectPoint", "Tests SDL_IntersectRect with 1x1 sized rectangles", TEST_ENABLED };
  1535. static const SDLTest_TestCaseReference rectTest10 =
  1536. { (SDLTest_TestCaseFp)rect_testIntersectRectEmpty, "rect_testIntersectRectEmpty", "Tests SDL_IntersectRect with empty rectangles", TEST_ENABLED };
  1537. static const SDLTest_TestCaseReference rectTest11 =
  1538. { (SDLTest_TestCaseFp)rect_testIntersectRectParam, "rect_testIntersectRectParam", "Negative tests against SDL_IntersectRect with invalid parameters", TEST_ENABLED };
  1539. /* SDL_HasIntersection */
  1540. static const SDLTest_TestCaseReference rectTest12 =
  1541. { (SDLTest_TestCaseFp)rect_testHasIntersectionInside, "rect_testHasIntersectionInside", "Tests SDL_HasIntersection with B fully contained in A", TEST_ENABLED };
  1542. static const SDLTest_TestCaseReference rectTest13 =
  1543. { (SDLTest_TestCaseFp)rect_testHasIntersectionOutside, "rect_testHasIntersectionOutside", "Tests SDL_HasIntersection with B fully outside of A", TEST_ENABLED };
  1544. static const SDLTest_TestCaseReference rectTest14 =
  1545. { (SDLTest_TestCaseFp)rect_testHasIntersectionPartial,"rect_testHasIntersectionPartial", "Tests SDL_HasIntersection with B partially intersecting A", TEST_ENABLED };
  1546. static const SDLTest_TestCaseReference rectTest15 =
  1547. { (SDLTest_TestCaseFp)rect_testHasIntersectionPoint, "rect_testHasIntersectionPoint", "Tests SDL_HasIntersection with 1x1 sized rectangles", TEST_ENABLED };
  1548. static const SDLTest_TestCaseReference rectTest16 =
  1549. { (SDLTest_TestCaseFp)rect_testHasIntersectionEmpty, "rect_testHasIntersectionEmpty", "Tests SDL_HasIntersection with empty rectangles", TEST_ENABLED };
  1550. static const SDLTest_TestCaseReference rectTest17 =
  1551. { (SDLTest_TestCaseFp)rect_testHasIntersectionParam, "rect_testHasIntersectionParam", "Negative tests against SDL_HasIntersection with invalid parameters", TEST_ENABLED };
  1552. /* SDL_EnclosePoints */
  1553. static const SDLTest_TestCaseReference rectTest18 =
  1554. { (SDLTest_TestCaseFp)rect_testEnclosePoints, "rect_testEnclosePoints", "Tests SDL_EnclosePoints without clipping", TEST_ENABLED };
  1555. static const SDLTest_TestCaseReference rectTest19 =
  1556. { (SDLTest_TestCaseFp)rect_testEnclosePointsWithClipping, "rect_testEnclosePointsWithClipping", "Tests SDL_EnclosePoints with clipping", TEST_ENABLED };
  1557. static const SDLTest_TestCaseReference rectTest20 =
  1558. { (SDLTest_TestCaseFp)rect_testEnclosePointsRepeatedInput, "rect_testEnclosePointsRepeatedInput", "Tests SDL_EnclosePoints with repeated input", TEST_ENABLED };
  1559. static const SDLTest_TestCaseReference rectTest21 =
  1560. { (SDLTest_TestCaseFp)rect_testEnclosePointsParam, "rect_testEnclosePointsParam", "Negative tests against SDL_EnclosePoints with invalid parameters", TEST_ENABLED };
  1561. /* SDL_UnionRect */
  1562. static const SDLTest_TestCaseReference rectTest22 =
  1563. { (SDLTest_TestCaseFp)rect_testUnionRectInside, "rect_testUnionRectInside", "Tests SDL_UnionRect where rect B is inside rect A", TEST_ENABLED };
  1564. static const SDLTest_TestCaseReference rectTest23 =
  1565. { (SDLTest_TestCaseFp)rect_testUnionRectOutside, "rect_testUnionRectOutside", "Tests SDL_UnionRect where rect B is outside rect A", TEST_ENABLED };
  1566. static const SDLTest_TestCaseReference rectTest24 =
  1567. { (SDLTest_TestCaseFp)rect_testUnionRectEmpty, "rect_testUnionRectEmpty", "Tests SDL_UnionRect where rect A or rect B are empty", TEST_ENABLED };
  1568. static const SDLTest_TestCaseReference rectTest25 =
  1569. { (SDLTest_TestCaseFp)rect_testUnionRectParam, "rect_testUnionRectParam", "Negative tests against SDL_UnionRect with invalid parameters", TEST_ENABLED };
  1570. /* SDL_RectEmpty */
  1571. static const SDLTest_TestCaseReference rectTest26 =
  1572. { (SDLTest_TestCaseFp)rect_testRectEmpty, "rect_testRectEmpty", "Tests SDL_RectEmpty with various inputs", TEST_ENABLED };
  1573. static const SDLTest_TestCaseReference rectTest27 =
  1574. { (SDLTest_TestCaseFp)rect_testRectEmptyParam, "rect_testRectEmptyParam", "Negative tests against SDL_RectEmpty with invalid parameters", TEST_ENABLED };
  1575. /* SDL_RectEquals */
  1576. static const SDLTest_TestCaseReference rectTest28 =
  1577. { (SDLTest_TestCaseFp)rect_testRectEquals, "rect_testRectEquals", "Tests SDL_RectEquals with various inputs", TEST_ENABLED };
  1578. static const SDLTest_TestCaseReference rectTest29 =
  1579. { (SDLTest_TestCaseFp)rect_testRectEqualsParam, "rect_testRectEqualsParam", "Negative tests against SDL_RectEquals with invalid parameters", TEST_ENABLED };
  1580. /* SDL_FRectEquals */
  1581. static const SDLTest_TestCaseReference rectTest30 =
  1582. { (SDLTest_TestCaseFp)rect_testFRectEquals, "rect_testFRectEquals", "Tests SDL_FRectEquals with various inputs", TEST_ENABLED };
  1583. static const SDLTest_TestCaseReference rectTest31 =
  1584. { (SDLTest_TestCaseFp)rect_testFRectEqualsParam, "rect_testFRectEqualsParam", "Negative tests against SDL_FRectEquals with invalid parameters", TEST_ENABLED };
  1585. /* !
  1586. * \brief Sequence of Rect test cases; functions that handle simple rectangles including overlaps and merges.
  1587. *
  1588. * \sa
  1589. * http://wiki.libsdl.org/CategoryRect
  1590. */
  1591. static const SDLTest_TestCaseReference *rectTests[] = {
  1592. &rectTest1, &rectTest2, &rectTest3, &rectTest4, &rectTest5, &rectTest6, &rectTest7, &rectTest8, &rectTest9, &rectTest10, &rectTest11, &rectTest12, &rectTest13, &rectTest14,
  1593. &rectTest15, &rectTest16, &rectTest17, &rectTest18, &rectTest19, &rectTest20, &rectTest21, &rectTest22, &rectTest23, &rectTest24, &rectTest25, &rectTest26, &rectTest27,
  1594. &rectTest28, &rectTest29, &rectTest30, &rectTest31, NULL
  1595. };
  1596. /* Rect test suite (global) */
  1597. SDLTest_TestSuiteReference rectTestSuite = {
  1598. "Rect",
  1599. NULL,
  1600. rectTests,
  1601. NULL
  1602. };