James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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