Last active
February 11, 2019 11:20
-
-
Save pobiega/f1d699180e43b54d499c36ced70c7788 to your computer and use it in GitHub Desktop.
Simple elevator implementation in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cmp; | |
#[derive(Debug)] | |
struct Elevator { | |
max_floor: u16, | |
current_floor: u16 | |
} | |
impl Elevator { | |
fn new(max_floors: u16) -> Elevator { | |
Elevator { | |
max_floor: cmp::max(2, cmp::min(max_floors, 100u16)), | |
current_floor: 0u16 | |
} | |
} | |
fn move_elevator(&mut self, target_floor: u16) -> bool { | |
if target_floor <= self.max_floor | |
{ | |
self.current_floor = target_floor; | |
return true | |
} | |
false | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn check_floor_cap() { | |
let tall_elevator = Elevator::new(200); | |
assert_eq!(tall_elevator.max_floor, 100); | |
let short_elevator = Elevator::new(1); | |
assert_eq!(short_elevator.max_floor, 2); | |
} | |
#[test] | |
fn check_start_floor() { | |
let my_elevator = Elevator::new(100); | |
assert_eq!(my_elevator.current_floor, 0); | |
} | |
#[test] | |
fn try_invalid_move() { | |
let mut my_elevator = Elevator::new(100); | |
assert_eq!(my_elevator.move_elevator(200), false); | |
} | |
#[test] | |
fn try_valid_move() { | |
let mut my_elevator = Elevator::new(100); | |
assert_eq!(my_elevator.move_elevator(50), true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment