One of the key reasons people learn Rust is because they want to write code that doesn’t fail at runtime. Exercism problems should reflect this desire by teaching students how to write Rust that can’t panic at runtime. The Clock in Rust exercise, in this case, has a weakness – you can pass all the tests and still have code that panics at runtim if too large of an integer is passed into the clock. I propose adding a set of integer overflow tests to make sure the exercise can’t be passed without accounting for this possibility.
The following are my proposed additions:
//
// Test Overflow
//
#[test]
fn positive_overflow_hours() {
Clock::new(i32::MAX, 0);
}
#[test]
fn negative_overflow_hours() {
Clock::new(i32::MIN, 0);
}
#[test]
fn positive_overflow_minutes() {
Clock::new(23, i32::MAX);
}
#[test]
fn negative_overflow_minutes() {
Clock::new(23, i32::MIN);
}
#[test]
fn positive_overflow_minutes_hours() {
Clock::new(i32::MAX, i32::MAX);
}
#[test]
fn negative_overflow_minutes_hours() {
Clock::new(i32::MIN, i32::MIN);
}
#[test]
fn positive_overflow_add_minutes() {
let clock = Clock::new(i32::MAX, i32::MAX);
clock.add_minutes(i32::MAX);
}
#[test]
fn negative_overflow_add_minutes() {
let clock = Clock::new(i32::MIN, i32::MIN);
clock.add_minutes(i32::MIN);
}