|
1 | 1 | use super::{BasicGenerator, Generator, TestCase}; |
2 | 2 | use crate::cbor_utils::cbor_map; |
3 | | -use std::time::{Duration, Instant}; |
| 3 | +use std::time::Duration; |
4 | 4 |
|
5 | 5 | /// Generator for [`Duration`] values. Created by [`durations()`]. |
6 | 6 | /// |
@@ -77,64 +77,6 @@ pub fn durations() -> DurationGenerator { |
77 | 77 | } |
78 | 78 | } |
79 | 79 |
|
80 | | -/// Generator for [`Instant`] values. Created by [`instants()`]. |
81 | | -/// |
82 | | -/// Generates instants by adding a random [`Duration`] offset to a fixed base |
83 | | -/// instant captured when the generator is created. The offsets are deterministic |
84 | | -/// (controlled by the test engine), while the base varies between runs. |
85 | | -pub struct InstantGenerator { |
86 | | - base: Instant, |
87 | | - max_offset_nanos: u64, |
88 | | -} |
89 | | - |
90 | | -impl InstantGenerator { |
91 | | - /// Set the maximum offset from the base instant (inclusive). |
92 | | - pub fn max_offset(mut self, max: Duration) -> Self { |
93 | | - self.max_offset_nanos = duration_to_nanos(max); |
94 | | - self |
95 | | - } |
96 | | -} |
97 | | - |
98 | | -impl Generator<Instant> for InstantGenerator { |
99 | | - fn do_draw(&self, tc: &TestCase) -> Instant { |
100 | | - let schema = cbor_map! { |
101 | | - "type" => "integer", |
102 | | - "min_value" => 0u64, |
103 | | - "max_value" => self.max_offset_nanos |
104 | | - }; |
105 | | - let nanos: u64 = super::generate_from_schema(tc, &schema); |
106 | | - self.base + Duration::from_nanos(nanos) |
107 | | - } |
108 | | -} |
109 | | - |
110 | | -/// Generate [`Instant`] values. |
111 | | -/// |
112 | | -/// Produces instants offset from a fixed base (`Instant::now()` at call time) |
113 | | -/// by a random duration. The default maximum offset is one hour. Use |
114 | | -/// `max_offset` to change it. |
115 | | -/// |
116 | | -/// The base is captured once when `instants()` is called, so all generated |
117 | | -/// values within a test share the same reference point. The offsets are |
118 | | -/// deterministic and shrinkable. |
119 | | -/// |
120 | | -/// # Example |
121 | | -/// |
122 | | -/// ```no_run |
123 | | -/// use std::time::Duration; |
124 | | -/// |
125 | | -/// #[hegel::test] |
126 | | -/// fn my_test(tc: hegel::TestCase) { |
127 | | -/// let i = tc.draw(hegel::generators::instants() |
128 | | -/// .max_offset(Duration::from_secs(3600))); |
129 | | -/// } |
130 | | -/// ``` |
131 | | -pub fn instants() -> InstantGenerator { |
132 | | - InstantGenerator { |
133 | | - base: Instant::now(), |
134 | | - max_offset_nanos: 3_600_000_000_000, |
135 | | - } |
136 | | -} |
137 | | - |
138 | 80 | fn duration_to_nanos(d: Duration) -> u64 { |
139 | 81 | d.as_nanos().try_into().unwrap_or(u64::MAX) |
140 | 82 | } |
0 commit comments