proptest/arbitrary/_std/time.rs
1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Arbitrary implementations for `std::time`.
11
12use core::ops::Range;
13use std::time::*;
14
15use crate::arbitrary::*;
16use crate::num;
17use crate::strategy::statics::{self, static_map};
18
19arbitrary!(Duration, SMapped<(u64, u32), Self>;
20 static_map(any::<(u64, u32)>(), |(a, b)| Duration::new(a, b))
21);
22
23// Instant::now() "never" returns the same Instant, so no shrinking may occur!
24arbitrary!(Instant; Self::now());
25
26arbitrary!(
27 // We can't use `any::<Duration>()` because the addition to `SystemTime`
28 // can overflow and panic. To be conservative, we only allow seconds to go
29 // to i32::MAX since a certain popular OS still uses `i32` to represent the
30 // seconds counter.
31 SystemTime, statics::Map<(num::i32::Any, Range<u32>),
32 fn ((i32, u32)) -> SystemTime>;
33 static_map((num::i32::ANY, 0..1_000_000_000u32),
34 |(sec, ns)| {
35 if sec >= 0 {
36 UNIX_EPOCH + Duration::new(sec as u64, ns)
37 } else {
38 UNIX_EPOCH - Duration::new((-(sec as i64)) as u64, ns)
39 }
40 })
41);
42
43#[cfg(test)]
44mod test {
45 no_panic_test!(
46 duration => Duration,
47 instant => Instant,
48 system_time => SystemTime
49 );
50}