diff --git a/src/main.rs b/src/main.rs
index 850f528..814bd7b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,12 +14,37 @@
// along with this program. If not, see .
use clap::{App, Arg};
+use std::collections::HashMap;
use std::fs;
use std::io;
mod robot;
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) {
+ Err(format!("The robot {} is off map", r.id))
+ } else {
+ Ok(())
+ }
+}
+
+/// Check if the robot collide with another one at the given position.
+fn check_collisions(
+ r: &robot::Robot,
+ w: &world::World,
+ h: &HashMap<&robot::Position, &u32>,
+) -> Result<(), String> {
+ match h.get(&r.p) {
+ Some(&x) => Err(format!(
+ "The robot id: {} collided with robot id: {} in position: ({};{}) !",
+ &r.id, x, &r.p.x, &r.p.y
+ )),
+ None => Ok(()),
+ }
+}
+
/// Parse the config file, generate the world and robot pool.
fn parse_config(conf: String, pool: &mut Vec) -> Result {
let mut lines = conf.lines();
@@ -84,4 +109,47 @@ 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());
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_check_collisions() {
+ 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 };
+
+ let mut h: HashMap<&robot::Position, &u32> = HashMap::new();
+ h.insert(&robot::Position { x: 2, y: 3 }, &1);
+ assert!(check_collisions(&r, &w, &h).is_ok());
+ }
}