From e1c8c8cf63b48c798c1954749e58e99c6cc50093 Mon Sep 17 00:00:00 2001 From: Michael Mc Donnell Date: Thu, 20 Feb 2020 16:01:08 -0800 Subject: [PATCH] Test `Duration::new` panics on overflow A `Duration` is created from a second and nanoseconds variable. The documentation says: "This constructor will panic if the carry from the nanoseconds overflows the seconds counter". This was, however, not tested in the tests. I doubt the behavior will ever regress, but it is usually a good idea to test all documented behavior. --- src/libcore/tests/time.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libcore/tests/time.rs b/src/libcore/tests/time.rs index 273f1258bb090..c1fbdf7df76fc 100644 --- a/src/libcore/tests/time.rs +++ b/src/libcore/tests/time.rs @@ -11,6 +11,12 @@ fn creation() { assert_eq!(Duration::from_millis(4000), Duration::new(4, 0)); } +#[test] +#[should_panic] +fn new_overflow() { + let _ = Duration::new(::core::u64::MAX, 1_000_000_000); +} + #[test] fn secs() { assert_eq!(Duration::new(0, 0).as_secs(), 0);