Add create() and new() methods to the Robot type

This commit is contained in:
Martin HART 2020-10-26 13:00:30 +01:00
parent bfa328c1b4
commit 997b9d8dfb
1 changed files with 34 additions and 0 deletions

View File

@ -46,6 +46,26 @@ struct Robot {
} }
impl 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<char> = 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. /// Apply forward instruction to the robot.
fn move_forward(&mut self) { fn move_forward(&mut self) {
match self.o { match self.o {
@ -193,6 +213,20 @@ mod tests {
assert_eq!(w.map, vec!['.'; 25]); 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] #[test]
fn test_set_robot() { fn test_set_robot() {
let r = Robot { let r = Robot {