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::from_secs(1).as_secs(), 1);
27 assert_eq!(Duration::from_millis(999).as_secs(), 0);
28 assert_eq!(Duration::from_millis(1001).as_secs(), 1);
33 assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
34 assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
35 assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
36 assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
37 assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
38 assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
43 assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
45 assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
51 assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)),
52 Some(Duration::new(0, 1)));
53 assert_eq!(Duration::new(0, 500_000_000).checked_add(Duration::new(0, 500_000_001)),
54 Some(Duration::new(1, 1)));
55 assert_eq!(Duration::new(1, 0).checked_add(Duration::new(::core::u64::MAX, 0)), None);
60 assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
62 assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
64 assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
65 Duration::new(0, 999_999_999));
70 let zero = Duration::new(0, 0);
71 let one_nano = Duration::new(0, 1);
72 let one_sec = Duration::new(1, 0);
73 assert_eq!(one_nano.checked_sub(zero), Some(Duration::new(0, 1)));
74 assert_eq!(one_sec.checked_sub(one_nano),
75 Some(Duration::new(0, 999_999_999)));
76 assert_eq!(zero.checked_sub(one_nano), None);
77 assert_eq!(zero.checked_sub(one_sec), None);
83 let _ = Duration::new(0, 0) - Duration::new(0, 1);
89 let _ = Duration::new(0, 0) - Duration::new(1, 0);
94 assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
95 assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
96 assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
97 assert_eq!(Duration::new(0, 500_000_001) * 4000,
98 Duration::new(2000, 4000));
103 assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
104 assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
105 assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
106 assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000),
107 Some(Duration::new(2000, 4000)));
108 assert_eq!(Duration::new(::core::u64::MAX - 1, 0).checked_mul(2), None);
113 assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
114 assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
115 assert_eq!(Duration::new(99, 999_999_000) / 100,
116 Duration::new(0, 999_999_990));
121 assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
122 assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
123 assert_eq!(Duration::new(2, 0).checked_div(0), None);