Craig Evans
/
Pong2
Pong for Gamepad2
Edit.
Ball/Ball-test.h@0:7423345f87c5, 2020-01-31 (annotated)
- Committer:
- eencae
- Date:
- Fri Jan 31 12:32:38 2020 +0000
- Revision:
- 0:7423345f87c5
Pong ported to Gamepad2;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:7423345f87c5 | 1 | #ifndef BALL_TEST_H |
eencae | 0:7423345f87c5 | 2 | #define BALL_TEST_H |
eencae | 0:7423345f87c5 | 3 | |
eencae | 0:7423345f87c5 | 4 | /** |
eencae | 0:7423345f87c5 | 5 | * \brief Check that Ball object goes to correct position when moved |
eencae | 0:7423345f87c5 | 6 | * |
eencae | 0:7423345f87c5 | 7 | * \returns true if all the tests passed |
eencae | 0:7423345f87c5 | 8 | */ |
eencae | 0:7423345f87c5 | 9 | bool Ball_test_movement() |
eencae | 0:7423345f87c5 | 10 | { |
eencae | 0:7423345f87c5 | 11 | // Initialise Ball object with a size of 2, and speed of 1 |
eencae | 0:7423345f87c5 | 12 | Ball ball; |
eencae | 0:7423345f87c5 | 13 | ball.init(2, 1); |
eencae | 0:7423345f87c5 | 14 | |
eencae | 0:7423345f87c5 | 15 | // Set the position to 5, 5 |
eencae | 0:7423345f87c5 | 16 | Vector2D initial_pos = {5, 5}; |
eencae | 0:7423345f87c5 | 17 | ball.set_pos(initial_pos); |
eencae | 0:7423345f87c5 | 18 | |
eencae | 0:7423345f87c5 | 19 | // Read the position |
eencae | 0:7423345f87c5 | 20 | Vector2D read_pos_1 = ball.get_pos(); |
eencae | 0:7423345f87c5 | 21 | printf("%f, %f\n", read_pos_1.x, read_pos_1.y); |
eencae | 0:7423345f87c5 | 22 | |
eencae | 0:7423345f87c5 | 23 | // Set the velocity to -2, 3 |
eencae | 0:7423345f87c5 | 24 | Vector2D velocity = {-2, 3}; |
eencae | 0:7423345f87c5 | 25 | ball.set_velocity(velocity); |
eencae | 0:7423345f87c5 | 26 | |
eencae | 0:7423345f87c5 | 27 | // Update the position |
eencae | 0:7423345f87c5 | 28 | ball.update(); |
eencae | 0:7423345f87c5 | 29 | |
eencae | 0:7423345f87c5 | 30 | // Read the position |
eencae | 0:7423345f87c5 | 31 | Vector2D read_pos_2 = ball.get_pos(); |
eencae | 0:7423345f87c5 | 32 | printf("%f, %f\n", read_pos_2.x, read_pos_2.y); |
eencae | 0:7423345f87c5 | 33 | |
eencae | 0:7423345f87c5 | 34 | // Now check that both the positions are as expected |
eencae | 0:7423345f87c5 | 35 | bool success_flag = true; |
eencae | 0:7423345f87c5 | 36 | |
eencae | 0:7423345f87c5 | 37 | // Fail the test if the initial position is wrong |
eencae | 0:7423345f87c5 | 38 | if (read_pos_1.x != 5 || read_pos_1.y != 5) { |
eencae | 0:7423345f87c5 | 39 | success_flag = false; |
eencae | 0:7423345f87c5 | 40 | } |
eencae | 0:7423345f87c5 | 41 | |
eencae | 0:7423345f87c5 | 42 | // Fail the test if the final position is wrong |
eencae | 0:7423345f87c5 | 43 | if (read_pos_2.x != 3 || read_pos_2.y != 8) { |
eencae | 0:7423345f87c5 | 44 | success_flag = false; |
eencae | 0:7423345f87c5 | 45 | } |
eencae | 0:7423345f87c5 | 46 | |
eencae | 0:7423345f87c5 | 47 | return success_flag; |
eencae | 0:7423345f87c5 | 48 | } |
eencae | 0:7423345f87c5 | 49 | #endif |