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.
Revision 12:1c0b6796aaca, committed 2020-04-24
- Comitter:
- evanso
- Date:
- Fri Apr 24 19:09:22 2020 +0000
- Parent:
- 11:ab578a151f67
- Child:
- 13:12276eed13ac
- Commit message:
- Added unit test for map, which it passed.
Changed in this revision
--- a/Map/Map.h Thu Apr 23 18:17:28 2020 +0000
+++ b/Map/Map.h Fri Apr 24 19:09:22 2020 +0000
@@ -4,7 +4,7 @@
/////////////// Include libraries ///////////////
#include "mbed.h"
#include "N5110.h"
-#include "Gamepad.h"
+
/** Map class
@@ -66,13 +66,17 @@
void check_duplicates_map_backwards(N5110 &lcd);
/////////////// Variables ///////////////
-
+
+ // Map x postion on lcd
int position_x_map_;
+ // Map y postion on lcd
int position_y_map_;
+ // Float to store random seed for random function
float rand_seed_;
+ // Map length
int map_length_;
// Arrays to hold random heights of triangles and lengths of lines
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Map/Map_test.h Fri Apr 24 19:09:22 2020 +0000
@@ -0,0 +1,52 @@
+#ifndef MAP_TEST_H
+#define MAP_TEST_H
+
+/** Map Test
+@brief Checks Spcaceship goes to the correct postion when moved
+@author Benjamin Evans, University of Leeds
+@date April 2020
+@return true if test are passed
+*/
+
+bool map_draw_test(){
+ // Objects reqired for test
+ N5110 lcd;
+ Gamepad pad;
+ Map map;
+ Serial usb(USBTX, USBRX);
+ AnalogIn adc(PTD5);
+
+ // Flag to return if test passed
+ bool pass_flag = true;
+
+ // Initialise map in start postion of -84, 42 and draw
+ lcd.init();
+ map.init(adc);
+ map.draw_map(lcd,0);
+
+ // Reads start spaceship postion
+ int start_x_postion = map.get_position_x_map();
+ usb.printf("start postion x = %d\n", start_x_postion);
+
+// Test drap map function
+ map.draw_map(lcd,1);
+
+ // Reads end map position
+ int end_x_postion_one = map.get_position_x_map();
+ usb.printf("end postion one x = %d\n", end_x_postion_one );
+
+ //TO check if map moves in opsite direction
+ map.draw_map(lcd,-1);
+
+ // Reads end map position
+ int end_x_postion_two = map.get_position_x_map();
+ usb.printf("end postion two x = %d\n", end_x_postion_two );
+
+ // Fail test if start postision is incorrect
+ if (end_x_postion_one != -83 || end_x_postion_two != -84) {
+ pass_flag = false;
+ }
+
+ return pass_flag;
+}
+#endif
\ No newline at end of file
--- a/Spaceship/Spaceship.h Thu Apr 23 18:17:28 2020 +0000
+++ b/Spaceship/Spaceship.h Fri Apr 24 19:09:22 2020 +0000
@@ -60,8 +60,13 @@
/////////////// Variables ///////////////
+ // Spaceships x position on lcd
int position_x_spaceship_;
+
+ // Spaceships y position on lcd
int position_y_spaceship_;
+
+ // Boolean flag for if sprite direction is changed
bool spaceship_sprite_direction_;
};
--- a/Spaceship/Spaceship_test.h Thu Apr 23 18:17:28 2020 +0000
+++ b/Spaceship/Spaceship_test.h Fri Apr 24 19:09:22 2020 +0000
@@ -1,48 +1,43 @@
#ifndef SPACESHIP_TEST_H
#define SPACESHIP_TEST_H
-
-
/** Spaceship Test
-@brief Checks Spcaceship goes to the correct postion when moved
+@brief Checks Spcaceship goes to the correct postion when moved and doesnt go of map
@author Benjamin Evans, University of Leeds
@date April 2020
@return true if test are passed
*/
bool spaceship_movement_test(){
+ // Objects reqired for test
Gamepad pad;
N5110 lcd;
Spaceship spaceship;
- Serial usb(USBTX, USBRX);
+ Serial usb(USBTX, USBRX);
+
+ // Flag to return if test passed
+ bool pass_flag = true;
// Initialise spaceship in start postion of 36, 22 and draw
-
pad.init();
lcd.init();
spaceship.init();
spaceship.draw(lcd);
- // Checks start spaceship postion
+ // Reads start spaceship postion
int start_x_postion = spaceship.get_position_x_spaceship();
int start_y_postion = spaceship.get_position_y_spaceship();
usb.printf("start postion x = %d, start postion y = %d\n",start_x_postion, start_y_postion );
- // Moves spaceship: Joystick in NE postion so should move x++, y--
+// Test Moves spaceship: Joystick in NE postion so should move x++, y--
usb.printf("Move joystick to NE position\n");
wait(3); // give me time to move joystick
spaceship.movement(pad);
- // Redraws spaceship
- spaceship.draw(lcd);
-
- // Checks end spaceship postion
- int end_x_postion = spaceship.get_position_x_spaceship();
- int end_y_postion = spaceship.get_position_y_spaceship();
- usb.printf("end postion x = %d, end postion y = %d\n",end_x_postion, end_y_postion );
-
- // Check if finish x,y postions are correct
- bool pass_flag = true;
+ // Reads end spaceship postion one
+ int end_x_postion_one = spaceship.get_position_x_spaceship();
+ int end_y_postion_one = spaceship.get_position_y_spaceship();
+ usb.printf("end postion one x = %d, end postion one y = %d\n",end_x_postion_one, end_y_postion_one);
// Fail test if start postision is incorrect
if (start_x_postion != 36 || start_y_postion != 22) {
@@ -50,7 +45,37 @@
}
// Fail test if end postision is incorrect
- if (end_x_postion != 37 || end_y_postion != 21) {
+ if (end_x_postion_one != 37 || end_y_postion_one != 21) {
+ pass_flag = false;
+ }
+
+// Test spaceship off screen checker and spaceship is drawn: Hold Joystick in NE postion so should move x++, y--
+ usb.printf("Move joystick to NE position\n");
+ wait(3); // give me time to move joystick
+
+ // Move spaceship to max postions
+ for (int i = 0; i < 21;i++){
+ spaceship.movement(pad);
+ }
+
+ // Redraws spaceship, spaceship.draw calls off screen checker
+ spaceship.draw(lcd);
+
+ // Reads end spaceship postion two
+ int end_x_postion_two = spaceship.get_position_x_spaceship();
+ int end_y_postion_two = spaceship.get_position_y_spaceship();
+ usb.printf("end postion two x = %d, end postion two y = %d\n",end_x_postion_two, end_y_postion_two);
+
+ //Checks if spaceships draws
+ int did_spaceship_draw = lcd.getPixel(end_x_postion_two,end_y_postion_two);
+
+ // Fail test if pixel isnt drawn
+ if (!did_spaceship_draw) {
+ pass_flag = false;
+ }
+
+ // Fail test if end postision is incorrect
+ if (end_x_postion_two != 52 || end_y_postion_two != 1) {
pass_flag = false;
}
--- a/main.cpp Thu Apr 23 18:17:28 2020 +0000
+++ b/main.cpp Fri Apr 24 19:09:22 2020 +0000
@@ -18,7 +18,7 @@
#include "Spaceship.h"
#include "Map.h"
-#ifdef SPACESHIP_TEST
+#ifdef GAME_TEST
# include "test.h"
#endif
@@ -42,6 +42,7 @@
///////////// Global variables /////////////
+// Flag for ISR
volatile int g_lcd_frame_time_flag = 0;
///////////// prototypes ///////////////
@@ -55,8 +56,9 @@
engine.init(lcd, spaceship, map, pad, adc);
// Compile with tests
- #ifdef SPACESHIP_TEST
- run_spaceship_tests();
+ #ifdef GAME_TEST
+ //run_spaceship_tests();
+ run_map_tests();
#endif
while (1) {
--- a/test.h Thu Apr 23 18:17:28 2020 +0000
+++ b/test.h Fri Apr 24 19:09:22 2020 +0000
@@ -2,12 +2,7 @@
#define TESTS_H
#include "Spaceship_test.h"
-
-/**
- * @brief Run all the tests for this program
- *
- * @returns The number of tests that failed
- */
+#include "Map_test.h"
/** Test
@brief Runs all tests for game
@@ -23,7 +18,22 @@
// Prints spaceshp test results
usb.printf("spaceship_movement_test = ");
if (spaceship_test_result) {
- usb.printf("passed");
+ usb.printf("passed\n");
+ }
+ else {
+ usb.printf("failed\n");
+ }
+}
+
+void run_map_tests(){
+ Serial usb(USBTX, USBRX);
+ // Runs map test
+ bool map_test_result = map_draw_test();
+
+ // Prints map test results
+ usb.printf("map_draw_test = ");
+ if (map_test_result) {
+ usb.printf("passed\n");
}
else {
usb.printf("failed\n");