diff --git a/src/date.rs b/src/date.rs index e28b037d9..efbdfe1e0 100644 --- a/src/date.rs +++ b/src/date.rs @@ -198,6 +198,9 @@ impl Date { pub(crate) const fn from_julian_day_unchecked(julian_day: i32) -> Self { #![allow(trivial_numeric_casts)] // cast depends on type alias + debug_assert!(julian_day >= Self::MIN.to_julian_day()); + debug_assert!(julian_day <= Self::MAX.to_julian_day()); + /// A type that is either `i32` or `i64`. This subtle difference allows for optimization /// based on the valid values. #[cfg(feature = "large-dates")] @@ -451,7 +454,10 @@ impl Date { -3 | 4 => Weekday::Friday, -2 | 5 => Weekday::Saturday, -1 | 6 => Weekday::Sunday, - _ => Weekday::Monday, + val => { + debug_assert!(val == 0); + Weekday::Monday + } } } @@ -664,6 +670,7 @@ impl Date { } else if duration.is_negative() { Self::MIN } else { + debug_assert!(duration.is_positive()); Self::MAX } } @@ -701,6 +708,7 @@ impl Date { } else if duration.is_negative() { Self::MAX } else { + debug_assert!(duration.is_positive()); Self::MIN } } diff --git a/src/duration.rs b/src/duration.rs index a765dffbe..91aaeb938 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -201,6 +201,16 @@ impl Duration { // region: constructors /// Create a new `Duration` without checking the validity of the components. pub(crate) const fn new_unchecked(seconds: i64, nanoseconds: i32) -> Self { + if seconds < 0 { + debug_assert!(nanoseconds <= 0); + debug_assert!(nanoseconds > -1_000_000_000); + } else if seconds > 0 { + debug_assert!(nanoseconds >= 0); + debug_assert!(nanoseconds < 1_000_000_000); + } else { + debug_assert!(nanoseconds.unsigned_abs() < 1_000_000_000); + } + Self { seconds, nanoseconds, diff --git a/src/instant.rs b/src/instant.rs index f2044a39b..7f95a694e 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -166,6 +166,7 @@ impl Add for Instant { } else if duration.is_negative() { Self(self.0 - duration.unsigned_abs()) } else { + debug_assert!(duration.is_zero()); self } } @@ -199,6 +200,7 @@ impl Sub for Instant { } else if duration.is_negative() { Self(self.0 + duration.unsigned_abs()) } else { + debug_assert!(duration.is_zero()); self } } diff --git a/src/offset_date_time.rs b/src/offset_date_time.rs index 0342eb548..59860b58c 100644 --- a/src/offset_date_time.rs +++ b/src/offset_date_time.rs @@ -760,6 +760,7 @@ impl OffsetDateTime { .assume_utc() .replace_offset(self.offset) } else { + debug_assert!(duration.is_positive()); PrimitiveDateTime::MAX .assume_utc() .replace_offset(self.offset) @@ -794,6 +795,7 @@ impl OffsetDateTime { .assume_utc() .replace_offset(self.offset) } else { + debug_assert!(duration.is_positive()); PrimitiveDateTime::MIN .assume_utc() .replace_offset(self.offset) diff --git a/src/quickcheck.rs b/src/quickcheck.rs index 17e34df2b..22241914d 100644 --- a/src/quickcheck.rs +++ b/src/quickcheck.rs @@ -176,7 +176,10 @@ impl Arbitrary for Weekday { 3 => Thursday, 4 => Friday, 5 => Saturday, - _ => Sunday, + val => { + debug_assert!(val == 6); + Sunday + } } } @@ -203,7 +206,10 @@ impl Arbitrary for Month { 9 => September, 10 => October, 11 => November, - _ => December, + val => { + debug_assert!(val == 12); + December + } } } diff --git a/src/rand.rs b/src/rand.rs index ff26d8af0..8afefe507 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -67,7 +67,10 @@ impl Distribution for Standard { 3 => Thursday, 4 => Friday, 5 => Saturday, - _ => Sunday, + val => { + debug_assert!(val == 6); + Sunday + } } } } @@ -87,7 +90,10 @@ impl Distribution for Standard { 9 => September, 10 => October, 11 => November, - _ => December, + val => { + debug_assert!(val == 12); + December + } } } } diff --git a/src/time.rs b/src/time.rs index e635dadce..7ef6fa87c 100644 --- a/src/time.rs +++ b/src/time.rs @@ -81,6 +81,11 @@ impl Time { second: u8, nanosecond: u32, ) -> Self { + debug_assert!(hour < 24); + debug_assert!(minute < 60); + debug_assert!(second < 60); + debug_assert!(nanosecond < 1_000_000_000); + Self { hour, minute, diff --git a/src/utc_offset.rs b/src/utc_offset.rs index 65ae5fa41..5f3b72f9b 100644 --- a/src/utc_offset.rs +++ b/src/utc_offset.rs @@ -45,6 +45,22 @@ impl UtcOffset { /// sign. #[doc(hidden)] pub const fn __from_hms_unchecked(hours: i8, minutes: i8, seconds: i8) -> Self { + if hours < 0 { + debug_assert!(minutes <= 0); + debug_assert!(seconds <= 0); + } else if hours > 0 { + debug_assert!(minutes >= 0); + debug_assert!(seconds >= 0); + } + if minutes < 0 { + debug_assert!(seconds <= 0); + } else if minutes > 0 { + debug_assert!(seconds >= 0); + } + debug_assert!(hours.unsigned_abs() < 24); + debug_assert!(minutes.unsigned_abs() < 60); + debug_assert!(seconds.unsigned_abs() < 60); + Self { hours, minutes,