mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-04 00:06:28 +00:00
Implement Arbitrary for Duration
This commit is contained in:
parent
44cee53a18
commit
87b04c5b91
@ -482,6 +482,24 @@ fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
|
|||||||
(this / other, this % other)
|
(this / other, this % other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "arbitrary")]
|
||||||
|
impl arbitrary::Arbitrary<'_> for Duration {
|
||||||
|
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Duration> {
|
||||||
|
const MIN_SECS: i64 = i64::MIN / MILLIS_PER_SEC - 1;
|
||||||
|
const MAX_SECS: i64 = i64::MAX / MILLIS_PER_SEC;
|
||||||
|
|
||||||
|
let secs: i64 = u.int_in_range(MIN_SECS..=MAX_SECS)?;
|
||||||
|
let nanos: i32 = u.int_in_range(0..=(NANOS_PER_SEC - 1))?;
|
||||||
|
let duration = Duration { secs, nanos };
|
||||||
|
|
||||||
|
if duration < MIN || duration > MAX {
|
||||||
|
Err(arbitrary::Error::IncorrectFormat)
|
||||||
|
} else {
|
||||||
|
Ok(duration)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{Duration, OutOfRangeError, MAX, MIN};
|
use super::{Duration, OutOfRangeError, MAX, MIN};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user