FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
124:d635e3154bf3
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 124:d635e3154bf3 1 #ifndef BALL_TEST_H
jamesheavey 124:d635e3154bf3 2 #define BALL_TEST_H
jamesheavey 124:d635e3154bf3 3
jamesheavey 124:d635e3154bf3 4 /**
jamesheavey 124:d635e3154bf3 5 * \brief Check that Ball object goes to correct position when moved
jamesheavey 124:d635e3154bf3 6 *
jamesheavey 124:d635e3154bf3 7 * \returns true if all the tests passed
jamesheavey 124:d635e3154bf3 8 */
jamesheavey 124:d635e3154bf3 9 bool Ball_test_movement()
jamesheavey 124:d635e3154bf3 10 {
jamesheavey 124:d635e3154bf3 11 // Initialise Ball object with a size of 2, and speed of 2
jamesheavey 124:d635e3154bf3 12 Ball _ball;
jamesheavey 124:d635e3154bf3 13 _ball.init(2, 2);
jamesheavey 124:d635e3154bf3 14
jamesheavey 124:d635e3154bf3 15 // Set the position to 0, 0
jamesheavey 124:d635e3154bf3 16 Vector2D initial_pos = {0, 0};
jamesheavey 124:d635e3154bf3 17 _ball.set_pos(initial_pos);
jamesheavey 124:d635e3154bf3 18
jamesheavey 124:d635e3154bf3 19 // Read the position
jamesheavey 124:d635e3154bf3 20 Vector2D read_pos_1 = _ball.get_pos();
jamesheavey 124:d635e3154bf3 21 printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
jamesheavey 124:d635e3154bf3 22
jamesheavey 124:d635e3154bf3 23 // Set the velocity to -2, 3
jamesheavey 124:d635e3154bf3 24 Vector2D velocity = {-2, 3};
jamesheavey 124:d635e3154bf3 25 _ball.set_velocity(velocity);
jamesheavey 124:d635e3154bf3 26
jamesheavey 124:d635e3154bf3 27 // Update the position
jamesheavey 124:d635e3154bf3 28 _ball.update();
jamesheavey 124:d635e3154bf3 29
jamesheavey 124:d635e3154bf3 30 // Read the position
jamesheavey 124:d635e3154bf3 31 Vector2D read_pos_2 = _ball.get_pos();
jamesheavey 124:d635e3154bf3 32 printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
jamesheavey 124:d635e3154bf3 33
jamesheavey 124:d635e3154bf3 34 // Now check that both the positions are as expected
jamesheavey 124:d635e3154bf3 35 bool success_flag = true;
jamesheavey 124:d635e3154bf3 36
jamesheavey 124:d635e3154bf3 37 // Fail the test if the initial position is wrong
jamesheavey 124:d635e3154bf3 38 if (read_pos_1.x != 0 || read_pos_1.y != 0) {
jamesheavey 124:d635e3154bf3 39 success_flag = false;
jamesheavey 124:d635e3154bf3 40 }
jamesheavey 124:d635e3154bf3 41
jamesheavey 124:d635e3154bf3 42 // Fail the test if the final position is wrong
jamesheavey 124:d635e3154bf3 43 if (read_pos_2.x != -2 || read_pos_2.y != 3) {
jamesheavey 124:d635e3154bf3 44 success_flag = false;
jamesheavey 124:d635e3154bf3 45 }
jamesheavey 124:d635e3154bf3 46
jamesheavey 124:d635e3154bf3 47 return success_flag;
jamesheavey 124:d635e3154bf3 48 }
jamesheavey 124:d635e3154bf3 49 #endif