From 997b9d8dfba97e0018ae7b6ca80a1f1f51ab17df Mon Sep 17 00:00:00 2001 From: Martin HART Date: Mon, 26 Oct 2020 13:00:30 +0100 Subject: [PATCH 1/2] Add create() and new() methods to the Robot type --- src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main.rs b/src/main.rs index 465bd1a..fb058eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,26 @@ struct Robot { } impl Robot { + /// Create a empty Robot. + fn new() -> Robot { + Robot { + id: 0, + o: Orientation::N, + p: Position { x: 0, y: 0 }, + q: queue![], + } + } + /// Initialize a new Robot. + fn create(&mut self, id: u32, o: Orientation, p: Position, s: String) { + let mut q: Queue = queue![]; + for c in s.chars() { + q.add(c); + } + self.id = id; + self.o = o; + self.p = p; + self.q = q; + } /// Apply forward instruction to the robot. fn move_forward(&mut self) { match self.o { @@ -193,6 +213,20 @@ mod tests { assert_eq!(w.map, vec!['.'; 25]); } + #[test] + fn test_create_robot() { + let mut r: Robot = Robot::new(); + r.create(1, Orientation::N, Position { x: 1, y: 1 }, "FF".to_string()); + assert_eq!(r.id, 1); + assert!(matches!(r.o, Orientation::N)); + + assert_eq!(r.p.x, 1); + assert_eq!(r.p.y, 1); + + assert_eq!(r.q.peek(), Ok('F')); + assert_eq!(r.q.peek(), Ok('F')); + } + #[test] fn test_set_robot() { let r = Robot { From 771a58544628b2155e5fbbf741c7398b8103fa0b Mon Sep 17 00:00:00 2001 From: Martin HART Date: Mon, 26 Oct 2020 13:14:44 +0100 Subject: [PATCH 2/2] add test_new_robot() --- src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main.rs b/src/main.rs index fb058eb..47f69ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -213,6 +213,18 @@ mod tests { assert_eq!(w.map, vec!['.'; 25]); } + #[test] + fn test_new_robot() { + let mut r: Robot = Robot::new(); + assert_eq!(r.id, 0); + assert!(matches!(r.o, Orientation::N)); + + assert_eq!(r.p.x, 0); + assert_eq!(r.p.y, 0); + + assert_eq!(r.q.size(), 0); + } + #[test] fn test_create_robot() { let mut r: Robot = Robot::new();