1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use core::time::Duration;
15 assert!(Duration::from_secs(1) != Duration::from_secs(0));
16 assert_eq!(Duration::from_secs(1) + Duration::from_secs(2),
17 Duration::from_secs(3));
18 assert_eq!(Duration::from_millis(10) + Duration::from_secs(4),
19 Duration::new(4, 10 * 1_000_000));
20 assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
25 assert_eq!(Duration::new(0, 0).as_secs(), 0);
26 assert_eq!(Duration::new(0, 500_000_005).as_secs(), 0);
27 assert_eq!(Duration::new(0, 1_050_000_001).as_secs(), 1);
28 assert_eq!(Duration::from_secs(1).as_secs(), 1);
29 assert_eq!(Duration::from_millis(999).as_secs(), 0);
30 assert_eq!(Duration::from_millis(1001).as_secs(), 1);
31 assert_eq!(Duration::from_micros(999_999).as_secs(), 0);
32 assert_eq!(Duration::from_micros(1_000_001).as_secs(), 1);
33 assert_eq!(Duration::from_nanos(999_999_999).as_secs(), 0);
34 assert_eq!(Duration::from_nanos(1_000_000_001).as_secs(), 1);
39 assert_eq!(Duration::new(0, 0).subsec_millis(), 0);
40 assert_eq!(Duration::new(0, 500_000_005).subsec_millis(), 500);
41 assert_eq!(Duration::new(0, 1_050_000_001).subsec_millis(), 50);
42 assert_eq!(Duration::from_secs(1).subsec_millis(), 0);
43 assert_eq!(Duration::from_millis(999).subsec_millis(), 999);
44 assert_eq!(Duration::from_millis(1001).subsec_millis(), 1);
45 assert_eq!(Duration::from_micros(999_999).subsec_millis(), 999);
46 assert_eq!(Duration::from_micros(1_001_000).subsec_millis(), 1);
47 assert_eq!(Duration::from_nanos(999_999_999).subsec_millis(), 999);
48 assert_eq!(Duration::from_nanos(1_001_000_000).subsec_millis(), 1);
53 assert_eq!(Duration::new(0, 0).subsec_micros(), 0);
54 assert_eq!(Duration::new(0, 500_000_005).subsec_micros(), 500_000);
55 assert_eq!(Duration::new(0, 1_050_000_001).subsec_micros(), 50_000);
56 assert_eq!(Duration::from_secs(1).subsec_micros(), 0);
57 assert_eq!(Duration::from_millis(999).subsec_micros(), 999_000);
58 assert_eq!(Duration::from_millis(1001).subsec_micros(), 1_000);
59 assert_eq!(Duration::from_micros(999_999).subsec_micros(), 999_999);
60 assert_eq!(Duration::from_micros(1_000_001).subsec_micros(), 1);
61 assert_eq!(Duration::from_nanos(999_999_999).subsec_micros(), 999_999);
62 assert_eq!(Duration::from_nanos(1_000_001_000).subsec_micros(), 1);
67 assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
68 assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
69 assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
70 assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
71 assert_eq!(Duration::from_millis(999).subsec_nanos(), 999_000_000);
72 assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1_000_000);
73 assert_eq!(Duration::from_micros(999_999).subsec_nanos(), 999_999_000);
74 assert_eq!(Duration::from_micros(1_000_001).subsec_nanos(), 1000);
75 assert_eq!(Duration::from_nanos(999_999_999).subsec_nanos(), 999_999_999);
76 assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1);
81 assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
83 assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
89 assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)),
90 Some(Duration::new(0, 1)));
91 assert_eq!(Duration::new(0, 500_000_000).checked_add(Duration::new(0, 500_000_001)),
92 Some(Duration::new(1, 1)));
93 assert_eq!(Duration::new(1, 0).checked_add(Duration::new(::core::u64::MAX, 0)), None);
98 assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
100 assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
101 Duration::new(0, 1));
102 assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
103 Duration::new(0, 999_999_999));
108 let zero = Duration::new(0, 0);
109 let one_nano = Duration::new(0, 1);
110 let one_sec = Duration::new(1, 0);
111 assert_eq!(one_nano.checked_sub(zero), Some(Duration::new(0, 1)));
112 assert_eq!(one_sec.checked_sub(one_nano),
113 Some(Duration::new(0, 999_999_999)));
114 assert_eq!(zero.checked_sub(one_nano), None);
115 assert_eq!(zero.checked_sub(one_sec), None);
121 let _ = Duration::new(0, 0) - Duration::new(0, 1);
127 let _ = Duration::new(0, 0) - Duration::new(1, 0);
132 assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
133 assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
134 assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
135 assert_eq!(Duration::new(0, 500_000_001) * 4000,
136 Duration::new(2000, 4000));
141 assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
142 assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
143 assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
144 assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000),
145 Some(Duration::new(2000, 4000)));
146 assert_eq!(Duration::new(::core::u64::MAX - 1, 0).checked_mul(2), None);
151 assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
152 assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
153 assert_eq!(Duration::new(99, 999_999_000) / 100,
154 Duration::new(0, 999_999_990));
159 assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
160 assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
161 assert_eq!(Duration::new(2, 0).checked_div(0), None);
167 Duration::new(1, 999_999_999),
168 Duration::new(2, 999_999_999),
169 Duration::new(0, 999_999_999),
170 Duration::new(0, 999_999_999),
171 Duration::new(0, 999_999_999),
174 let sum = durations.iter().sum::<Duration>();
175 assert_eq!(sum, Duration::new(1+2+5+4, 1_000_000_000 - 5));
179 fn debug_formatting_extreme_values() {
181 format!("{:?}", Duration::new(18_446_744_073_709_551_615, 123_456_789)),
182 "18446744073709551615.123456789s"
187 fn debug_formatting_secs() {
188 assert_eq!(format!("{:?}", Duration::new(7, 000_000_000)), "7s");
189 assert_eq!(format!("{:?}", Duration::new(7, 100_000_000)), "7.1s");
190 assert_eq!(format!("{:?}", Duration::new(7, 000_010_000)), "7.00001s");
191 assert_eq!(format!("{:?}", Duration::new(7, 000_000_001)), "7.000000001s");
192 assert_eq!(format!("{:?}", Duration::new(7, 123_456_789)), "7.123456789s");
194 assert_eq!(format!("{:?}", Duration::new(88, 000_000_000)), "88s");
195 assert_eq!(format!("{:?}", Duration::new(88, 100_000_000)), "88.1s");
196 assert_eq!(format!("{:?}", Duration::new(88, 000_010_000)), "88.00001s");
197 assert_eq!(format!("{:?}", Duration::new(88, 000_000_001)), "88.000000001s");
198 assert_eq!(format!("{:?}", Duration::new(88, 123_456_789)), "88.123456789s");
200 assert_eq!(format!("{:?}", Duration::new(999, 000_000_000)), "999s");
201 assert_eq!(format!("{:?}", Duration::new(999, 100_000_000)), "999.1s");
202 assert_eq!(format!("{:?}", Duration::new(999, 000_010_000)), "999.00001s");
203 assert_eq!(format!("{:?}", Duration::new(999, 000_000_001)), "999.000000001s");
204 assert_eq!(format!("{:?}", Duration::new(999, 123_456_789)), "999.123456789s");
208 fn debug_formatting_millis() {
209 assert_eq!(format!("{:?}", Duration::new(0, 7_000_000)), "7ms");
210 assert_eq!(format!("{:?}", Duration::new(0, 7_100_000)), "7.1ms");
211 assert_eq!(format!("{:?}", Duration::new(0, 7_000_001)), "7.000001ms");
212 assert_eq!(format!("{:?}", Duration::new(0, 7_123_456)), "7.123456ms");
214 assert_eq!(format!("{:?}", Duration::new(0, 88_000_000)), "88ms");
215 assert_eq!(format!("{:?}", Duration::new(0, 88_100_000)), "88.1ms");
216 assert_eq!(format!("{:?}", Duration::new(0, 88_000_001)), "88.000001ms");
217 assert_eq!(format!("{:?}", Duration::new(0, 88_123_456)), "88.123456ms");
219 assert_eq!(format!("{:?}", Duration::new(0, 999_000_000)), "999ms");
220 assert_eq!(format!("{:?}", Duration::new(0, 999_100_000)), "999.1ms");
221 assert_eq!(format!("{:?}", Duration::new(0, 999_000_001)), "999.000001ms");
222 assert_eq!(format!("{:?}", Duration::new(0, 999_123_456)), "999.123456ms");
226 fn debug_formatting_micros() {
227 assert_eq!(format!("{:?}", Duration::new(0, 7_000)), "7µs");
228 assert_eq!(format!("{:?}", Duration::new(0, 7_100)), "7.1µs");
229 assert_eq!(format!("{:?}", Duration::new(0, 7_001)), "7.001µs");
230 assert_eq!(format!("{:?}", Duration::new(0, 7_123)), "7.123µs");
232 assert_eq!(format!("{:?}", Duration::new(0, 88_000)), "88µs");
233 assert_eq!(format!("{:?}", Duration::new(0, 88_100)), "88.1µs");
234 assert_eq!(format!("{:?}", Duration::new(0, 88_001)), "88.001µs");
235 assert_eq!(format!("{:?}", Duration::new(0, 88_123)), "88.123µs");
237 assert_eq!(format!("{:?}", Duration::new(0, 999_000)), "999µs");
238 assert_eq!(format!("{:?}", Duration::new(0, 999_100)), "999.1µs");
239 assert_eq!(format!("{:?}", Duration::new(0, 999_001)), "999.001µs");
240 assert_eq!(format!("{:?}", Duration::new(0, 999_123)), "999.123µs");
244 fn debug_formatting_nanos() {
245 assert_eq!(format!("{:?}", Duration::new(0, 0)), "0ns");
246 assert_eq!(format!("{:?}", Duration::new(0, 1)), "1ns");
247 assert_eq!(format!("{:?}", Duration::new(0, 88)), "88ns");
248 assert_eq!(format!("{:?}", Duration::new(0, 999)), "999ns");
252 fn debug_formatting_precision_zero() {
253 assert_eq!(format!("{:.0?}", Duration::new(0, 0)), "0ns");
254 assert_eq!(format!("{:.0?}", Duration::new(0, 123)), "123ns");
256 assert_eq!(format!("{:.0?}", Duration::new(0, 1_001)), "1µs");
257 assert_eq!(format!("{:.0?}", Duration::new(0, 1_499)), "1µs");
258 assert_eq!(format!("{:.0?}", Duration::new(0, 1_500)), "2µs");
259 assert_eq!(format!("{:.0?}", Duration::new(0, 1_999)), "2µs");
261 assert_eq!(format!("{:.0?}", Duration::new(0, 1_000_001)), "1ms");
262 assert_eq!(format!("{:.0?}", Duration::new(0, 1_499_999)), "1ms");
263 assert_eq!(format!("{:.0?}", Duration::new(0, 1_500_000)), "2ms");
264 assert_eq!(format!("{:.0?}", Duration::new(0, 1_999_999)), "2ms");
266 assert_eq!(format!("{:.0?}", Duration::new(1, 000_000_001)), "1s");
267 assert_eq!(format!("{:.0?}", Duration::new(1, 499_999_999)), "1s");
268 assert_eq!(format!("{:.0?}", Duration::new(1, 500_000_000)), "2s");
269 assert_eq!(format!("{:.0?}", Duration::new(1, 999_999_999)), "2s");
273 fn debug_formatting_precision_two() {
274 assert_eq!(format!("{:.2?}", Duration::new(0, 0)), "0.00ns");
275 assert_eq!(format!("{:.2?}", Duration::new(0, 123)), "123.00ns");
277 assert_eq!(format!("{:.2?}", Duration::new(0, 1_000)), "1.00µs");
278 assert_eq!(format!("{:.2?}", Duration::new(0, 7_001)), "7.00µs");
279 assert_eq!(format!("{:.2?}", Duration::new(0, 7_100)), "7.10µs");
280 assert_eq!(format!("{:.2?}", Duration::new(0, 7_109)), "7.11µs");
281 assert_eq!(format!("{:.2?}", Duration::new(0, 7_199)), "7.20µs");
282 assert_eq!(format!("{:.2?}", Duration::new(0, 1_999)), "2.00µs");
284 assert_eq!(format!("{:.2?}", Duration::new(0, 1_000_000)), "1.00ms");
285 assert_eq!(format!("{:.2?}", Duration::new(0, 3_001_000)), "3.00ms");
286 assert_eq!(format!("{:.2?}", Duration::new(0, 3_100_000)), "3.10ms");
287 assert_eq!(format!("{:.2?}", Duration::new(0, 1_999_999)), "2.00ms");
289 assert_eq!(format!("{:.2?}", Duration::new(1, 000_000_000)), "1.00s");
290 assert_eq!(format!("{:.2?}", Duration::new(4, 001_000_000)), "4.00s");
291 assert_eq!(format!("{:.2?}", Duration::new(2, 100_000_000)), "2.10s");
292 assert_eq!(format!("{:.2?}", Duration::new(2, 104_990_000)), "2.10s");
293 assert_eq!(format!("{:.2?}", Duration::new(2, 105_000_000)), "2.11s");
294 assert_eq!(format!("{:.2?}", Duration::new(8, 999_999_999)), "9.00s");
298 fn debug_formatting_precision_high() {
299 assert_eq!(format!("{:.5?}", Duration::new(0, 23_678)), "23.67800µs");
301 assert_eq!(format!("{:.9?}", Duration::new(1, 000_000_000)), "1.000000000s");
302 assert_eq!(format!("{:.10?}", Duration::new(4, 001_000_000)), "4.0010000000s");
303 assert_eq!(format!("{:.20?}", Duration::new(4, 001_000_000)), "4.00100000000000000000s");