Skip to content

Commit

Permalink
Add debug assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Jun 13, 2022
1 parent 73138f4 commit 21b439f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -664,6 +670,7 @@ impl Date {
} else if duration.is_negative() {
Self::MIN
} else {
debug_assert!(duration.is_positive());
Self::MAX
}
}
Expand Down Expand Up @@ -701,6 +708,7 @@ impl Date {
} else if duration.is_negative() {
Self::MAX
} else {
debug_assert!(duration.is_positive());
Self::MIN
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Add<Duration> for Instant {
} else if duration.is_negative() {
Self(self.0 - duration.unsigned_abs())
} else {
debug_assert!(duration.is_zero());
self
}
}
Expand Down Expand Up @@ -199,6 +200,7 @@ impl Sub<Duration> for Instant {
} else if duration.is_negative() {
Self(self.0 + duration.unsigned_abs())
} else {
debug_assert!(duration.is_zero());
self
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions src/quickcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ impl Arbitrary for Weekday {
3 => Thursday,
4 => Friday,
5 => Saturday,
_ => Sunday,
val => {
debug_assert!(val == 6);
Sunday
}
}
}

Expand All @@ -203,7 +206,10 @@ impl Arbitrary for Month {
9 => September,
10 => October,
11 => November,
_ => December,
val => {
debug_assert!(val == 12);
December
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ impl Distribution<Weekday> for Standard {
3 => Thursday,
4 => Friday,
5 => Saturday,
_ => Sunday,
val => {
debug_assert!(val == 6);
Sunday
}
}
}
}
Expand All @@ -87,7 +90,10 @@ impl Distribution<Month> for Standard {
9 => September,
10 => October,
11 => November,
_ => December,
val => {
debug_assert!(val == 12);
December
}
}
}
}
5 changes: 5 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/utc_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 21b439f

Please sign in to comment.