Naive approach to clock

Will fail tests, starting with negative inputs
This commit is contained in:
Blizzard Finnegan 2023-08-21 15:03:19 -04:00
parent e76ddc2fe4
commit 988ca31e2b
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
2 changed files with 28 additions and 21 deletions

View file

@ -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 { impl Clock {
pub fn new(hours: i32, minutes: i32) -> Self { pub fn new(hours: i32, minutes: i32) -> Self {
unimplemented!( Self{hours,minutes}
"Construct a new Clock from {} hours and {} minutes",
hours,
minutes
);
} }
pub fn add_minutes(&self, minutes: i32) -> Self { 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}
} }
} }

View file

@ -10,85 +10,71 @@ fn test_on_the_hour() {
} }
#[test] #[test]
#[ignore]
fn test_past_the_hour() { fn test_past_the_hour() {
assert_eq!(Clock::new(11, 9).to_string(), "11:09"); assert_eq!(Clock::new(11, 9).to_string(), "11:09");
} }
#[test] #[test]
#[ignore]
fn test_midnight_is_zero_hours() { fn test_midnight_is_zero_hours() {
assert_eq!(Clock::new(24, 0).to_string(), "00:00"); assert_eq!(Clock::new(24, 0).to_string(), "00:00");
} }
#[test] #[test]
#[ignore]
fn test_hour_rolls_over() { fn test_hour_rolls_over() {
assert_eq!(Clock::new(25, 0).to_string(), "01:00"); assert_eq!(Clock::new(25, 0).to_string(), "01:00");
} }
#[test] #[test]
#[ignore]
fn test_hour_rolls_over_continuously() { fn test_hour_rolls_over_continuously() {
assert_eq!(Clock::new(100, 0).to_string(), "04:00"); assert_eq!(Clock::new(100, 0).to_string(), "04:00");
} }
#[test] #[test]
#[ignore]
fn test_sixty_minutes_is_next_hour() { fn test_sixty_minutes_is_next_hour() {
assert_eq!(Clock::new(1, 60).to_string(), "02:00"); assert_eq!(Clock::new(1, 60).to_string(), "02:00");
} }
#[test] #[test]
#[ignore]
fn test_minutes_roll_over() { fn test_minutes_roll_over() {
assert_eq!(Clock::new(0, 160).to_string(), "02:40"); assert_eq!(Clock::new(0, 160).to_string(), "02:40");
} }
#[test] #[test]
#[ignore]
fn test_minutes_roll_over_continuously() { fn test_minutes_roll_over_continuously() {
assert_eq!(Clock::new(0, 1723).to_string(), "04:43"); assert_eq!(Clock::new(0, 1723).to_string(), "04:43");
} }
#[test] #[test]
#[ignore]
fn test_hours_and_minutes_roll_over() { fn test_hours_and_minutes_roll_over() {
assert_eq!(Clock::new(25, 160).to_string(), "03:40"); assert_eq!(Clock::new(25, 160).to_string(), "03:40");
} }
#[test] #[test]
#[ignore]
fn test_hours_and_minutes_roll_over_continuously() { fn test_hours_and_minutes_roll_over_continuously() {
assert_eq!(Clock::new(201, 3001).to_string(), "11:01"); assert_eq!(Clock::new(201, 3001).to_string(), "11:01");
} }
#[test] #[test]
#[ignore]
fn test_hours_and_minutes_roll_over_to_exactly_midnight() { fn test_hours_and_minutes_roll_over_to_exactly_midnight() {
assert_eq!(Clock::new(72, 8640).to_string(), "00:00"); assert_eq!(Clock::new(72, 8640).to_string(), "00:00");
} }
#[test] #[test]
#[ignore]
fn test_negative_hour() { fn test_negative_hour() {
assert_eq!(Clock::new(-1, 15).to_string(), "23:15"); assert_eq!(Clock::new(-1, 15).to_string(), "23:15");
} }
#[test] #[test]
#[ignore]
fn test_negative_hour_roll_over() { fn test_negative_hour_roll_over() {
assert_eq!(Clock::new(-25, 00).to_string(), "23:00"); assert_eq!(Clock::new(-25, 00).to_string(), "23:00");
} }
#[test] #[test]
#[ignore]
fn test_negative_hour_roll_over_continuously() { fn test_negative_hour_roll_over_continuously() {
assert_eq!(Clock::new(-91, 00).to_string(), "05:00"); assert_eq!(Clock::new(-91, 00).to_string(), "05:00");
} }
#[test] #[test]
#[ignore]
fn test_negative_minutes() { fn test_negative_minutes() {
assert_eq!(Clock::new(1, -40).to_string(), "00:20"); assert_eq!(Clock::new(1, -40).to_string(), "00:20");
} }