Merge branch 'create-robot' into 'master'

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

See merge request mhart/DancingDroids!26
This commit is contained in:
Martin HART 2020-10-26 13:15:52 +01:00
commit 9de4c1ff6b

View File

@ -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<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.
fn move_forward(&mut self) {
match self.o {
@ -193,6 +213,32 @@ 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();
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 {