remove casts to i32

This commit is contained in:
Daniel Barlow 2023-11-03 18:49:01 +00:00
parent 4f3fb62432
commit 3af88ea712

View File

@ -33,13 +33,13 @@ mod rover {
pub fn forward(&self) -> Rover {
let Rover(lon, lat, direction) = self;
let (xoff, yoff) = velocity(&direction);
Rover(lon + xoff as i32, lat + yoff as i32, *direction)
Rover(lon + xoff, lat + yoff, *direction)
}
pub fn backward(&self) -> Rover {
let Rover(lon, lat, direction) = self;
let (xoff, yoff) = velocity(&direction);
Rover(lon - xoff as i32, lat - yoff as i32, *direction)
Rover(lon - xoff, lat - yoff, *direction)
}
pub fn rotate(&self, rotation: Rotation) -> Rover {
@ -91,53 +91,53 @@ mod tests {
#[test]
fn move_north() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::N);
let r = Rover(1, 1, rover::Facing::N);
assert_eq!(r.forward(),
Rover(1 as i32, 0 as i32, rover::Facing::N));
Rover(1, 0, rover::Facing::N));
let r = Rover(1 as i32, 3 as i32, rover::Facing::N);
let r = Rover(1, 3, rover::Facing::N);
assert_eq!(r.forward(),
Rover(1 as i32, 2 as i32, rover::Facing::N));
Rover(1, 2, rover::Facing::N));
}
#[test]
fn move_south() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
let r = Rover(1, 1, rover::Facing::S);
assert_eq!(r.forward(),
Rover(1 as i32, 2 as i32, rover::Facing::S));
Rover(1, 2, rover::Facing::S));
}
#[test]
fn move_west() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::W);
let r = Rover(1, 1, rover::Facing::W);
assert_eq!(r.forward(),
Rover(0 as i32, 1 as i32, rover::Facing::W));
Rover(0, 1, rover::Facing::W));
}
#[test]
fn move_east() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
let r = Rover(1, 1, rover::Facing::E);
assert_eq!(r.forward(),
Rover(2 as i32, 1 as i32, rover::Facing::E));
Rover(2, 1, rover::Facing::E));
}
#[test]
fn move_backward() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
let r = Rover(1, 1, rover::Facing::E);
let r2 = r.forward();
assert_eq!(r2.backward(), r);
}
#[test]
fn spin_left() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
let r = Rover(1, 1, rover::Facing::E);
match r.rotate(rover::Rotation::Left) {
Rover(_, _, facing) => assert_eq!(facing, rover::Facing::N)
}
}
#[test]
fn spin_right() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
let r = Rover(1, 1, rover::Facing::S);
match r.rotate(rover::Rotation::Right) {
Rover(_, _, facing) => assert_eq!(facing, rover::Facing::W)
}
@ -145,8 +145,8 @@ mod tests {
#[test]
fn drive() {
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
let r = Rover(1, 1, rover::Facing::S);
assert_eq!(driver::execute(&r, "ffrf"),
Rover(0 as i32, 3 as i32, rover::Facing::W));
Rover(0, 3, rover::Facing::W));
}
}