🛠️🐜 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.

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