2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
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.

geometry.test.c 4.4 KiB

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