Merge branch 'master' of https://framagit.org/mhart/DancingDroids into copyright
sync changes
This commit is contained in:
		
						commit
						190423aacb
					
				
							
								
								
									
										54
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								src/main.rs
									
									
									
									
									
								
							@ -48,8 +48,19 @@ impl World {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/// Struct to store robot position.
 | 
					/// Struct to store robot position.
 | 
				
			||||||
struct Position {
 | 
					struct Position {
 | 
				
			||||||
    x: u32,
 | 
					    x: i32,
 | 
				
			||||||
    y: u32,
 | 
					    y: i32,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Position {
 | 
				
			||||||
 | 
					    /// Check if position is in the map.
 | 
				
			||||||
 | 
					    fn is_valid(self, w: World) -> Result<(), String> {
 | 
				
			||||||
 | 
					        if (self.x, self.y) > (0, 0) && (self.x, self.y) < (w.x as i32, w.y as i32) {
 | 
				
			||||||
 | 
					            Ok(())
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            Err(String::from("Invalid position."))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// A Robot *aka droid* is represented here.
 | 
					/// A Robot *aka droid* is represented here.
 | 
				
			||||||
@ -273,6 +284,22 @@ mod tests {
 | 
				
			|||||||
        assert_eq!(w.map, vec!['↑', '.', '.', '.']);
 | 
					        assert_eq!(w.map, vec!['↑', '.', '.', '.']);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn test_move_robot() {
 | 
				
			||||||
 | 
					        let mut r: Robot = Robot {
 | 
				
			||||||
 | 
					            id: 0,
 | 
				
			||||||
 | 
					            o: Orientation::N,
 | 
				
			||||||
 | 
					            p: Position { x: 1, y: 1 },
 | 
				
			||||||
 | 
					            q: Queue::new(),
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        r.move_forward();
 | 
				
			||||||
 | 
					        assert_eq!(0, r.p.y);
 | 
				
			||||||
 | 
					        r.move_right();
 | 
				
			||||||
 | 
					        assert_eq!(2, r.p.x);
 | 
				
			||||||
 | 
					        r.move_left();
 | 
				
			||||||
 | 
					        assert_eq!(1, r.p.x);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn test_empty_position() {
 | 
					    fn test_empty_position() {
 | 
				
			||||||
        let mut w: World = World {
 | 
					        let mut w: World = World {
 | 
				
			||||||
@ -283,4 +310,27 @@ mod tests {
 | 
				
			|||||||
        w.create();
 | 
					        w.create();
 | 
				
			||||||
        assert!(w.empty_position(Position { x: 0, y: 0 }));
 | 
					        assert!(w.empty_position(Position { x: 0, y: 0 }));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn test_valid_position() {
 | 
				
			||||||
 | 
					        let p: Position = Position { x: 1, y: 1 };
 | 
				
			||||||
 | 
					        let w: World = World {
 | 
				
			||||||
 | 
					            x: 5,
 | 
				
			||||||
 | 
					            y: 5,
 | 
				
			||||||
 | 
					            map: Vec::new(),
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        assert!(p.is_valid(w).is_ok());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    #[should_panic]
 | 
				
			||||||
 | 
					    fn test_invalid_position() {
 | 
				
			||||||
 | 
					        let p: Position = Position { x: -1, y: 1 };
 | 
				
			||||||
 | 
					        let w: World = World {
 | 
				
			||||||
 | 
					            x: 5,
 | 
				
			||||||
 | 
					            y: 5,
 | 
				
			||||||
 | 
					            map: Vec::new(),
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        assert!(p.is_valid(w).is_ok());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user