1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#![deny(warnings)]
#![deny(missing_docs)]
use std::time::Duration;
#[cfg(unix)]
#[path = "unix.rs"]
mod platform;
#[cfg(windows)]
#[path = "windows.rs"]
mod platform;
use platform::Clock as Inner;
#[derive(Debug)]
pub struct Clock {
inner: Inner,
}
impl Clock {
pub fn new() -> Self {
Self {
inner: Inner::new(),
}
}
pub fn elapsed(&mut self) -> Duration {
self.inner.elapsed()
}
}
#[cfg(test)]
mod tests {
use crate::Clock;
use std::{
thread::sleep,
time::Duration,
};
#[test]
fn a_clock_is_instantiated() {
let _ = Clock::new();
}
#[test]
fn clock_is_monotonic() {
let mut clock = Clock::new();
let t0 = clock.elapsed();
sleep(Duration::from_millis(100));
let t1 = clock.elapsed();
assert!(t0 <= t1);
}
#[test]
fn clock_reports_correct_duration() {
let mut clock = Clock::new();
let t0 = clock.elapsed();
let sleep_duration = Duration::from_millis(100);
sleep(sleep_duration);
let t1 = clock.elapsed();
assert!(t1 - t0 >= sleep_duration);
}
}