Merge branch 'robot' into 'master'

create and move robot + tests

See merge request mhart/DancingDroids!33
This commit is contained in:
Martin HART 2020-10-28 19:58:00 +01:00
commit 7c935269f5

View File

@ -4,6 +4,41 @@ pub struct Robot {
id: u32,
o: Orientation,
p: Position,
i: Vec<char>,
}
impl Robot {
/// Create new `Robot` with given id, `Orientation`, `Position` and instructions.
fn new(id: u32, o: Orientation, p: Position, i: Vec<char>) -> Robot {
Robot { id, o, p, i }
}
/// Apply given instruction to a `Robot`.
fn execute_instruction(&mut self) {
match self.i.pop() {
Some(instruction) => match instruction {
'L' => match self.o {
Orientation::N => self.o = Orientation::W,
Orientation::E => self.o = Orientation::N,
Orientation::S => self.o = Orientation::E,
Orientation::W => self.o = Orientation::S,
},
'R' => match self.o {
Orientation::N => self.o = Orientation::E,
Orientation::E => self.o = Orientation::S,
Orientation::S => self.o = Orientation::W,
Orientation::W => self.o = Orientation::N,
},
'F' => match self.o {
Orientation::N => self.p.y += 1,
Orientation::E => self.p.x += 1,
Orientation::S => self.p.y -= 1,
Orientation::W => self.p.x -= 1,
},
_ => (), // never happens 😉
},
None => (),
}
}
}
/// Enum to store all possible orientations.
@ -15,7 +50,49 @@ enum Orientation {
}
/// Struct to store robot position.
#[derive(PartialEq, Eq, Hash)]
struct Position {
x: i32,
y: i32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_robot() {
let r: Robot = Robot::new(
0,
Orientation::N,
Position { x: 1, y: 2 },
vec!['L', 'L', 'F', 'R'],
);
assert_eq!(r.id, 0);
assert!(matches!(r.o, Orientation::N));
assert_eq!(r.p.x, 1);
assert_eq!(r.p.y, 2);
assert_eq!(r.i, vec!['L', 'L', 'F', 'R']);
}
#[test]
fn test_execute_instruction() {
let mut r: Robot = Robot::new(
0,
Orientation::N,
Position { x: 1, y: 2 },
vec!['R', 'F', 'L', 'F'],
);
let mut hash = std::collections::HashMap::new();
//hash.insert(&r.p, &r.id); // first insert while initializing.
hash.remove(&r.p); // remove before execute_instruction().
r.execute_instruction();
hash.insert(&r.p, &r.id); // second insert after moving.
assert_eq!(r.p.x, 1);
assert_eq!(r.p.y, 3);
r.execute_instruction();
r.execute_instruction();
assert_eq!(r.p.x, 0);
assert_eq!(r.p.y, 3);
}
}