Debian Codegraph @

src/tests/minkowski_hex_test.c

raw symbols SCIP
1 #define SDL_MAIN_HANDLED 1
2 #include <cbehave/cbehave.h>
3
4 #include <collision/minkowski_hex.h>
5
6 // Stubs
7 const char *JoyName(const int deviceIndex)
8 {
9 UNUSED(deviceIndex);
10 return NULL;
11 }
12
13
14 FEATURE(minkowski_hex, "Minkowski Hex")
15 SCENARIO("Same position")
16 GIVEN("two rectangles with the same position")
17 const struct vec2 rectPos1 = svec2(3, 4);
18 const struct vec2 rectVel1 = svec2(-4, 5);
19 const struct vec2i rectSize1 = svec2i(1, 3);
20 const struct vec2 rectPos2 = rectPos1;
21 const struct vec2 rectVel2 = svec2(7, -8);
22 const struct vec2i rectSize2 = svec2i(2, 1);
23
24 WHEN("I check for their collision")
25 struct vec2 collide1, collide2, normal;
26 const bool result = MinkowskiHexCollide(
27 rectPos1, rectVel1, rectSize1,
28 rectPos2, rectVel2, rectSize2,
29 &collide1, &collide2, &normal);
30
31 THEN("the result should be true");
32 SHOULD_BE_TRUE(result);
33 AND("the collision points should be at the start")
34 SHOULD_INT_EQUAL((int)collide1.x, (int)rectPos1.x);
35 SHOULD_INT_EQUAL((int)collide1.y, (int)rectPos1.y);
36 SHOULD_INT_EQUAL((int)collide2.x, (int)rectPos2.x);
37 SHOULD_INT_EQUAL((int)collide2.y, (int)rectPos2.y);
38 AND("the normal should be (0, 0)")
39 SHOULD_INT_EQUAL((int)normal.x, 0);
40 SHOULD_INT_EQUAL((int)normal.y, 0);
41 SCENARIO_END
42
43 SCENARIO("No overlap, no movement")
44 GIVEN("two non-overlapping rectangles with no movement")
45 const struct vec2 rectPos1 = svec2(3, 4);
46 const struct vec2 rectVel1 = svec2_zero();
47 const struct vec2i rectSize1 = svec2i(1, 3);
48 const struct vec2 rectPos2 = svec2(8, 9);
49 const struct vec2 rectVel2 = svec2_zero();
50 const struct vec2i rectSize2 = svec2i(2, 1);
51
52 WHEN("I check for their collision")
53 struct vec2 collide1, collide2, normal;
54 const bool result = MinkowskiHexCollide(
55 rectPos1, rectVel1, rectSize1,
56 rectPos2, rectVel2, rectSize2,
57 &collide1, &collide2, &normal);
58
59 THEN("the result should be false");
60 SHOULD_BE_FALSE(result);
61 SCENARIO_END
62
63 SCENARIO("Single axis single movement")
64 GIVEN("two rectangles, one moving into the other")
65 const struct vec2 rectPos1 = svec2_zero();
66 const struct vec2 rectVel1 = svec2(10, 0);
67 const struct vec2i rectSize1 = svec2i(2, 2);
68 const struct vec2 rectPos2 = svec2(5, 0);
69 const struct vec2 rectVel2 = svec2_zero();
70 const struct vec2i rectSize2 = svec2i(2, 2);
71
72 WHEN("I check for their collision")
73 struct vec2 collide1, collide2, normal;
74 const bool result = MinkowskiHexCollide(
75 rectPos1, rectVel1, rectSize1,
76 rectPos2, rectVel2, rectSize2,
77 &collide1, &collide2, &normal);
78
79 THEN("the result should be true");
80 SHOULD_BE_TRUE(result);
81 AND("the collision point for the moving rectangle should not overlap "
82 "with the stationary rectangle")
83 SHOULD_INT_EQUAL((int)collide1.x, 3);
84 SHOULD_INT_EQUAL((int)collide1.y, 0);
85 AND("the collision point for the stationary rectangle should be the "
86 "same as its position")
87 SHOULD_INT_EQUAL((int)collide2.x, (int)rectPos2.x);
88 SHOULD_INT_EQUAL((int)collide2.y, (int)rectPos2.y);
89 AND("the normal should be (-1, 0)")
90 SHOULD_INT_EQUAL((int)normal.x, -1);
91 SHOULD_INT_EQUAL((int)normal.y, 0);
92 SCENARIO_END
93 FEATURE_END
94
95 CBEHAVE_RUN(
96 "minkowski_hex features are:",
97 TEST_FEATURE(minkowski_hex)
98 )