diff --git a/src/main.rs b/src/main.rs index 62906d5..2b816e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); + } }