Skip to content

Instantly share code, notes, and snippets.

@la10736
Last active July 25, 2018 22:18
Show Gist options
  • Save la10736/d7eca95fac3532d203cea5c794c1acd9 to your computer and use it in GitHub Desktop.
Save la10736/d7eca95fac3532d203cea5c794c1acd9 to your computer and use it in GitHub Desktop.
Milano Rust MeetUp 25/7/2018 MikamaiMars Rover
.idea
/target
**/*.rs.bk
Cargo.lock
mr.iml
[package]
name = "mr"
version = "0.1.0"
authors = ["michele <michele.damico@gmail.com>"]
[dependencies]
[dev-dependencies]
rstest = { git = "https://github.com/la10736/rstest" }
#![feature(proc_macro)]
#[cfg(test)]
extern crate rstest;
use std::ops::Add;
use std::ops::Neg;
fn main() {
println!("Hello, world!");
}
type X = i32;
type Y = i32;
#[derive(Clone, Copy, Debug)]
struct Coord(X, Y);
#[derive(PartialEq, Debug, Copy, Clone)]
enum Direction {
North,
East,
South,
West,
}
impl Direction {
fn reverse(self) -> Direction {
use Direction::*;
match self {
North => South,
East => West,
South => North,
West => East,
}
}
}
impl Into<Coord> for (X, Y) {
fn into(self) -> Coord {
Coord(self.0, self.1)
}
}
impl Into<Coord> for Direction {
fn into(self) -> Coord {
match self {
Direction::North => (0, 1),
Direction::East => (1, 0),
Direction::South => (0, -1),
Direction::West => (-1, 0),
}.into()
}
}
impl Add for Coord {
type Output = Coord;
fn add(self, rhs: Coord) -> <Self as Add<Coord>>::Output {
(self.0 + rhs.0, self.1 + rhs.1).into()
}
}
impl Neg for Coord {
type Output = Coord;
fn neg(self) -> <Self as Neg>::Output {
(-self.0, -self.1).into()
}
}
impl PartialEq<(X, Y)> for Coord {
fn eq(&self, other: &(X, Y)) -> bool {
self.0 == other.0 && self.1 == other.1
}
}
struct Rover {
position: Coord,
direction: Direction,
}
impl Rover {
fn exec(&mut self, cmd: char) {
use Direction::*;
let step = match cmd {
'f' => self.direction,
'b' => self.direction.reverse(),
_ => panic!("Comando Sconosciuto '{}'", cmd)
};
self.position = self.position + step.into();
}
fn position(&self) -> Coord {
self.position
}
fn direction(&self) -> Direction {
self.direction
}
}
#[cfg(test)]
mod test {
use super::{*, Direction::*};
use rstest::*;
#[test]
fn should_create_a_rover() {
let rover = Rover { position: (2, 4).into(), direction: North };
assert_eq!(rover.position(), (2, 4));
assert_eq!(rover.direction(), North)
}
#[rstest_parametrize(
direction, expected,
case(North, Unwrap("(1, 6)")),
case(East, Unwrap("(2, 5)")),
case(South, Unwrap("(1, 4)")),
case(West, Unwrap("(0, 5)")),
)]
fn should_move_forward(direction: Direction, expected: (X, Y)) {
let mut rover = Rover { position: (1, 5).into(), direction };
rover.exec('f');
assert_eq!(rover.position(), expected)
}
#[rstest_parametrize(
direction, expected,
case(North, Unwrap("(1, 4)")),
case(East, Unwrap("(0, 5)")),
case(South, Unwrap("(1, 6)")),
case(West, Unwrap("(2, 5)")),
)]
fn should_move_backward(direction: Direction, expected: (X, Y)) {
let mut rover = Rover { position: (1, 5).into(), direction };
rover.exec('b');
assert_eq!(rover.position(), expected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment