Naive approach to clock
Will fail tests, starting with negative inputs
This commit is contained in:
parent
e76ddc2fe4
commit
988ca31e2b
2 changed files with 28 additions and 21 deletions
|
@ -1,15 +1,36 @@
|
|||
pub struct Clock;
|
||||
pub struct Clock{
|
||||
hours:i32,
|
||||
minutes:i32
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Clock{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f,"{:0>2}:{:0>2}", self.hours, self.minutes)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Clock{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f,"{:0>2}:{:0>2}", self.hours, self.minutes)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Clock{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.minutes == other.minutes && self.hours == other.hours
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Clock{}
|
||||
|
||||
impl Clock {
|
||||
pub fn new(hours: i32, minutes: i32) -> Self {
|
||||
unimplemented!(
|
||||
"Construct a new Clock from {} hours and {} minutes",
|
||||
hours,
|
||||
minutes
|
||||
);
|
||||
Self{hours,minutes}
|
||||
}
|
||||
|
||||
pub fn add_minutes(&self, minutes: i32) -> Self {
|
||||
unimplemented!("Add {} minutes to existing Clock time", minutes);
|
||||
let new_minutes = (self.minutes + minutes) % 60;
|
||||
let new_hours = (self.hours + ((self.minutes + minutes) / 60)) % 24;
|
||||
Self{hours:new_hours,minutes:new_minutes}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,85 +10,71 @@ fn test_on_the_hour() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_past_the_hour() {
|
||||
assert_eq!(Clock::new(11, 9).to_string(), "11:09");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_midnight_is_zero_hours() {
|
||||
assert_eq!(Clock::new(24, 0).to_string(), "00:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_hour_rolls_over() {
|
||||
assert_eq!(Clock::new(25, 0).to_string(), "01:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_hour_rolls_over_continuously() {
|
||||
assert_eq!(Clock::new(100, 0).to_string(), "04:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_sixty_minutes_is_next_hour() {
|
||||
assert_eq!(Clock::new(1, 60).to_string(), "02:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_minutes_roll_over() {
|
||||
assert_eq!(Clock::new(0, 160).to_string(), "02:40");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_minutes_roll_over_continuously() {
|
||||
assert_eq!(Clock::new(0, 1723).to_string(), "04:43");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_hours_and_minutes_roll_over() {
|
||||
assert_eq!(Clock::new(25, 160).to_string(), "03:40");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_hours_and_minutes_roll_over_continuously() {
|
||||
assert_eq!(Clock::new(201, 3001).to_string(), "11:01");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_hours_and_minutes_roll_over_to_exactly_midnight() {
|
||||
assert_eq!(Clock::new(72, 8640).to_string(), "00:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_negative_hour() {
|
||||
assert_eq!(Clock::new(-1, 15).to_string(), "23:15");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_negative_hour_roll_over() {
|
||||
assert_eq!(Clock::new(-25, 00).to_string(), "23:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_negative_hour_roll_over_continuously() {
|
||||
assert_eq!(Clock::new(-91, 00).to_string(), "05:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_negative_minutes() {
|
||||
assert_eq!(Clock::new(1, -40).to_string(), "00:20");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue