Skip to content

Commit 3ddd67b

Browse files
LukasKalbertodtkennytm
authored andcommitted
Move libcore/time tests from time.rs to tests/time.rs
All other tests of libcore reside in the tests/ directory, too. Apparently the tests of `time.rs` weren't run before, at least not by `x.py test src/libcore`.
1 parent be9d669 commit 3ddd67b

File tree

3 files changed

+123
-116
lines changed

3 files changed

+123
-116
lines changed

src/libcore/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ mod result;
7474
mod slice;
7575
mod str;
7676
mod str_lossy;
77+
mod time;
7778
mod tuple;

src/libcore/tests/time.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.
4+
//
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.
10+
11+
use core::time::Duration;
12+
13+
#[test]
14+
fn creation() {
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));
21+
}
22+
23+
#[test]
24+
fn secs() {
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);
29+
}
30+
31+
#[test]
32+
fn nanos() {
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);
39+
}
40+
41+
#[test]
42+
fn add() {
43+
assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
44+
Duration::new(0, 1));
45+
assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
46+
Duration::new(1, 1));
47+
}
48+
49+
#[test]
50+
fn checked_add() {
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);
56+
}
57+
58+
#[test]
59+
fn sub() {
60+
assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
61+
Duration::new(0, 1));
62+
assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
63+
Duration::new(0, 1));
64+
assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
65+
Duration::new(0, 999_999_999));
66+
}
67+
68+
#[test]
69+
fn checked_sub() {
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);
78+
}
79+
80+
#[test] #[should_panic]
81+
fn sub_bad1() {
82+
Duration::new(0, 0) - Duration::new(0, 1);
83+
}
84+
85+
#[test] #[should_panic]
86+
fn sub_bad2() {
87+
Duration::new(0, 0) - Duration::new(1, 0);
88+
}
89+
90+
#[test]
91+
fn mul() {
92+
assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
93+
assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
94+
assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
95+
assert_eq!(Duration::new(0, 500_000_001) * 4000,
96+
Duration::new(2000, 4000));
97+
}
98+
99+
#[test]
100+
fn checked_mul() {
101+
assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
102+
assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
103+
assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
104+
assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000),
105+
Some(Duration::new(2000, 4000)));
106+
assert_eq!(Duration::new(::core::u64::MAX - 1, 0).checked_mul(2), None);
107+
}
108+
109+
#[test]
110+
fn div() {
111+
assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
112+
assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
113+
assert_eq!(Duration::new(99, 999_999_000) / 100,
114+
Duration::new(0, 999_999_990));
115+
}
116+
117+
#[test]
118+
fn checked_div() {
119+
assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
120+
assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
121+
assert_eq!(Duration::new(2, 0).checked_div(0), None);
122+
}

src/libcore/time.rs

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -481,119 +481,3 @@ impl<'a> Sum<&'a Duration> for Duration {
481481
iter.fold(Duration::new(0, 0), |a, b| a + *b)
482482
}
483483
}
484-
485-
#[cfg(test)]
486-
mod tests {
487-
use super::Duration;
488-
489-
#[test]
490-
fn creation() {
491-
assert!(Duration::from_secs(1) != Duration::from_secs(0));
492-
assert_eq!(Duration::from_secs(1) + Duration::from_secs(2),
493-
Duration::from_secs(3));
494-
assert_eq!(Duration::from_millis(10) + Duration::from_secs(4),
495-
Duration::new(4, 10 * 1_000_000));
496-
assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
497-
}
498-
499-
#[test]
500-
fn secs() {
501-
assert_eq!(Duration::new(0, 0).as_secs(), 0);
502-
assert_eq!(Duration::from_secs(1).as_secs(), 1);
503-
assert_eq!(Duration::from_millis(999).as_secs(), 0);
504-
assert_eq!(Duration::from_millis(1001).as_secs(), 1);
505-
}
506-
507-
#[test]
508-
fn nanos() {
509-
assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
510-
assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
511-
assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
512-
assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
513-
assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
514-
assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
515-
}
516-
517-
#[test]
518-
fn add() {
519-
assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
520-
Duration::new(0, 1));
521-
assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
522-
Duration::new(1, 1));
523-
}
524-
525-
#[test]
526-
fn checked_add() {
527-
assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)),
528-
Some(Duration::new(0, 1)));
529-
assert_eq!(Duration::new(0, 500_000_000).checked_add(Duration::new(0, 500_000_001)),
530-
Some(Duration::new(1, 1)));
531-
assert_eq!(Duration::new(1, 0).checked_add(Duration::new(::u64::MAX, 0)), None);
532-
}
533-
534-
#[test]
535-
fn sub() {
536-
assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
537-
Duration::new(0, 1));
538-
assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
539-
Duration::new(0, 1));
540-
assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
541-
Duration::new(0, 999_999_999));
542-
}
543-
544-
#[test]
545-
fn checked_sub() {
546-
let zero = Duration::new(0, 0);
547-
let one_nano = Duration::new(0, 1);
548-
let one_sec = Duration::new(1, 0);
549-
assert_eq!(one_nano.checked_sub(zero), Some(Duration::new(0, 1)));
550-
assert_eq!(one_sec.checked_sub(one_nano),
551-
Some(Duration::new(0, 999_999_999)));
552-
assert_eq!(zero.checked_sub(one_nano), None);
553-
assert_eq!(zero.checked_sub(one_sec), None);
554-
}
555-
556-
#[test] #[should_panic]
557-
fn sub_bad1() {
558-
Duration::new(0, 0) - Duration::new(0, 1);
559-
}
560-
561-
#[test] #[should_panic]
562-
fn sub_bad2() {
563-
Duration::new(0, 0) - Duration::new(1, 0);
564-
}
565-
566-
#[test]
567-
fn mul() {
568-
assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
569-
assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
570-
assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
571-
assert_eq!(Duration::new(0, 500_000_001) * 4000,
572-
Duration::new(2000, 4000));
573-
}
574-
575-
#[test]
576-
fn checked_mul() {
577-
assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
578-
assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
579-
assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
580-
assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000),
581-
Some(Duration::new(2000, 4000)));
582-
assert_eq!(Duration::new(::u64::MAX - 1, 0).checked_mul(2), None);
583-
}
584-
585-
#[test]
586-
fn div() {
587-
assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
588-
assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
589-
assert_eq!(Duration::new(99, 999_999_000) / 100,
590-
Duration::new(0, 999_999_990));
591-
}
592-
593-
#[test]
594-
fn checked_div() {
595-
assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
596-
assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
597-
assert_eq!(Duration::new(2, 0).checked_div(0), None);
598-
}
599-
}

0 commit comments

Comments
 (0)