Starter project for SDL2.
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.

245 lines
4.9 KiB

  1. #include "../../test/IZ_test.h"
  2. #include "IZ_point2d.h"
  3. #include "IZ_vector2d.h"
  4. #include "IZ_rect.h"
  5. spec("geometry") {
  6. describe("point2d") {
  7. describe("PointTranslate") {
  8. it("translates coordinates of a point") {
  9. static IZ_Point2D input = {
  10. .x = 420.f,
  11. .y = 1337.f,
  12. };
  13. static IZ_Point2D expected = {
  14. .x = 426.f,
  15. .y = 1346.f,
  16. };
  17. static IZ_Point2D actual;
  18. actual = IZ_PointTranslate(input, 6.f, 9.f);
  19. check(expected.x == actual.x, "X values do not match.");
  20. check(expected.y == actual.y, "Y values do not match.");
  21. }
  22. }
  23. }
  24. describe("vector2d") {
  25. describe("VectorAdd") {
  26. it("adds two vectors") {
  27. static IZ_Vector2D addend = {
  28. .right = 420.f,
  29. .up = 1337.f,
  30. };
  31. static IZ_Vector2D augend = {
  32. .right = 6.f,
  33. .up = 9.f,
  34. };
  35. static IZ_Vector2D expected_sum = {
  36. .right = 426.f,
  37. .up = 1346.f,
  38. };
  39. static IZ_Vector2D actual_sum;
  40. actual_sum = IZ_VectorAdd(addend, augend);
  41. check(expected_sum.right == actual_sum.right, "Right values do not match.");
  42. check(expected_sum.up == actual_sum.up, "Up values do not match.");
  43. }
  44. }
  45. describe("VectorMultiply") {
  46. it("multiplies two vectors") {
  47. static IZ_Vector2D multiplicand = {
  48. .right = 6.f,
  49. .up = 9.f,
  50. };
  51. static IZ_Vector2D multiplier = {
  52. .right = 3.f,
  53. .up = 2.f,
  54. };
  55. static IZ_Vector2D expected_product = {
  56. .right = 18.f,
  57. .up = 18.f,
  58. };
  59. static IZ_Vector2D actual_product;
  60. actual_product = IZ_VectorMultiply(multiplicand, multiplier);
  61. check(expected_product.right == actual_product.right, "Right values do not match.");
  62. check(expected_product.up == actual_product.up, "Up values do not match.");
  63. }
  64. }
  65. describe("VectorScale") {
  66. it("scales a vector") {
  67. static IZ_Vector2D v = {
  68. .right = 420.f,
  69. .up = 69.f,
  70. };
  71. static IZ_VectorMagnitude s = 2.f;
  72. static IZ_Vector2D expected = {
  73. .right = 840.f,
  74. .up = 138.f,
  75. };
  76. static IZ_Vector2D actual;
  77. actual = IZ_VectorScale(v, s);
  78. check(expected.right == actual.right, "Right values do not match.");
  79. check(expected.up == actual.up, "Up values do not match.");
  80. }
  81. }
  82. }
  83. describe("rect") {
  84. describe("RectGetBounds") {
  85. it("returns the bounds of a rectangle") {
  86. static IZ_Rect r = {
  87. .pos = {
  88. .x = 50,
  89. .y = 100,
  90. },
  91. .width = 150,
  92. .height = 200,
  93. };
  94. static IZ_Bounds expected = {
  95. .left = 50,
  96. .top = 100,
  97. .right = 200,
  98. .bottom = 300,
  99. };
  100. static IZ_Bounds actual;
  101. actual = IZ_RectGetBounds(r);
  102. check(expected.left == actual.left, "Left values do not match.");
  103. check(expected.top == actual.top, "Top values do not match.");
  104. check(expected.right == actual.right, "Right values do not match.");
  105. check(expected.bottom == actual.bottom, "Bottom values do not match.");
  106. }
  107. }
  108. describe("BoundsContainPoint") {
  109. it("returns true for points inside bounds") {
  110. static IZ_Bounds b = {
  111. .left = 50,
  112. .top = 100,
  113. .right = 200,
  114. .bottom = 300,
  115. };
  116. static IZ_Point2D p = {
  117. .x = 75,
  118. .y = 150,
  119. };
  120. check(IZ_BoundsContainPoint(b, p), "Point not found inside bounds.");
  121. }
  122. it("returns true for points along bounds edge") {
  123. static IZ_Bounds b = {
  124. .left = 50,
  125. .top = 100,
  126. .right = 200,
  127. .bottom = 300,
  128. };
  129. static IZ_Point2D p1 = {
  130. .x = 50,
  131. .y = 100,
  132. };
  133. static IZ_Point2D p2 = {
  134. .x = 200,
  135. .y = 300,
  136. };
  137. check(IZ_BoundsContainPoint(b, p1), "Point p1 not found inside bounds.");
  138. check(IZ_BoundsContainPoint(b, p2), "Point p2 not found inside bounds.");
  139. }
  140. it("returns false for points outside bounds") {
  141. static IZ_Bounds b = {
  142. .left = 50,
  143. .top = 100,
  144. .right = 200,
  145. .bottom = 300,
  146. };
  147. static IZ_Point2D p = {
  148. .x = 0,
  149. .y = 0,
  150. };
  151. check(!IZ_BoundsContainPoint(b, p), "Point found inside bounds.");
  152. }
  153. }
  154. describe("BoundsCollide") {
  155. it("returns true for bounds A inside bounds B") {
  156. static IZ_Bounds a = {
  157. .left = 100,
  158. .top = 125,
  159. .right = 150,
  160. .bottom = 200,
  161. };
  162. static IZ_Bounds b = {
  163. .left = 50,
  164. .top = 75,
  165. .right = 200,
  166. .bottom = 250,
  167. };
  168. check(IZ_BoundsCollide(a, b), "Bounds not colliding.");
  169. }
  170. it("returns true for bounds A intersecting bounds B") {
  171. static IZ_Bounds a = {
  172. .left = 0,
  173. .top = 0,
  174. .right = 150,
  175. .bottom = 200,
  176. };
  177. static IZ_Bounds b = {
  178. .left = 50,
  179. .top = 75,
  180. .right = 200,
  181. .bottom = 250,
  182. };
  183. check(IZ_BoundsCollide(a, b), "Bounds not colliding.");
  184. }
  185. it("returns true for bounds A outside bounds B") {
  186. static IZ_Bounds a = {
  187. .left = 50,
  188. .top = 75,
  189. .right = 200,
  190. .bottom = 250,
  191. };
  192. static IZ_Bounds b = {
  193. .left = 550,
  194. .top = 575,
  195. .right = 800,
  196. .bottom = 850,
  197. };
  198. check(!IZ_BoundsCollide(a, b), "Bounds colliding.");
  199. }
  200. }
  201. }
  202. }