test of the function check_map

This commit is contained in:
EliasCubz 2020-10-29 10:16:09 +01:00
parent 2eaf624137
commit d35da41c90
1 changed files with 28 additions and 1 deletions

View File

@ -22,7 +22,7 @@ mod world;
/// Check if the robot is in the map.
fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> {
if (r.p.x, r.p.y) < (0, 0) && (r.p.x, r.p.y) > (w.x, w.y) {
if (r.p.x, r.p.y) < (0, 0) || (r.p.x, r.p.y) > (w.x, w.y) {
Err(format!("The robot {} is off map", r.id))
} else {
Ok(())
@ -93,4 +93,31 @@ mod tests {
assert!(open_file("two_robots.txt").is_ok());
assert!(open_file("test_unexisting_file.extension").is_err());
}
#[test]
fn test_check_map() {
let mut r = robot::Robot::new(
0,
robot::Orientation::N,
robot::Position { x: 2, y: 3 },
vec!['F'],
);
let w = world::World { x: 10, y: 10 };
assert!(check_map(&r, &w).is_ok());
}
#[test]
#[should_panic]
fn test_check_map_fail() {
let mut r = robot::Robot::new(
0,
robot::Orientation::N,
robot::Position { x: 2, y: 3 },
vec!['F'],
);
let w = world::World { x: 1, y: 1 };
assert!(check_map(&r, &w).is_ok());
}
}