Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: GameObjects/Snake/Snake-test.h
- Revision:
- 68:b9cfd27987ac
- Child:
- 69:55e309da7efd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/GameObjects/Snake/Snake-test.h Sat May 04 16:56:04 2019 +0000
@@ -0,0 +1,55 @@
+#ifndef SNAKE_TEST_H
+#define SNAKE_TEST_H
+
+/**
+ * \brief Check that Snake object goes to correct position when moved
+ *
+ * \returns true if all the tests passed
+ */
+bool Snake_test_movement()
+{
+ // Create Snake object with a length of 15, and x axis speed of 2.
+ Snake snake;
+ snake.init(15, 2);
+
+ // Set the position to WIDTH/2, HEIGHT - 3
+
+ Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen.
+ snake.set_pos(initial_pos);
+
+ // Read the position
+ Vector2D read_pos_1 = snake.get_pos();
+ printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
+
+ // Set the direction to W and set snake motion free by setting b as 1;
+ Direction d = W;
+
+ int b[15]; //each element in this array represents the beed number in the snake.
+ for(int i=0; i<=13; i++) {
+ b[i] = 1;
+ }
+ b[14] = 1;
+
+ // Update the snake position
+ snake.update(d, b);
+
+ // Read the position
+ Vector2D read_pos_2 = snake.get_pos();
+ printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
+
+ // Now check that both the positions are as expected
+ bool success_flag = true;
+
+ // Fail the test if the initial position is wrong
+ if (read_pos_1.x != WIDTH/2 || read_pos_1.y != 18) { //18 because I'm testing the position of the last snake beed.
+ success_flag = false;
+ }
+
+ // Fail the test if the final position is wrong
+ if (read_pos_2.x != ((WIDTH/2)-2) || read_pos_2.y != 18) { //18 because I'm testing the position of the last snake beed and ((WIDTH/2)-2) because speed is set to 2.
+ success_flag = false;
+ }
+
+ return success_flag;
+}
+#endif
\ No newline at end of file