Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Spaceship/Spaceship_test.h

Committer:
evanso
Date:
2020-04-24
Revision:
12:1c0b6796aaca
Parent:
11:ab578a151f67
Child:
13:12276eed13ac

File content as of revision 12:1c0b6796aaca:

#ifndef SPACESHIP_TEST_H
#define SPACESHIP_TEST_H

/** Spaceship Test
@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);
   
    // 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);
    
    // 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 );
    
// 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);
    
    // 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) {
        pass_flag = false;
    }
    
    // Fail test if end postision is incorrect
    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;
    }

    return pass_flag;
}
#endif