rusty_timer/
lib.rs

1mod stopwatch;
2mod timer;
3// public exports
4pub use stopwatch::*;
5pub use timer::*;
6
7// TESTS
8
9
10#[cfg(test)]
11mod tests {
12    use crate::{Stopwatch, Timer};
13    use std::time::Duration;
14    use std::thread::sleep;
15    use crate::SystemStopwatch;
16
17    #[test]
18    fn test_stopwatch() {
19        println!("--- Testing normal stopwatch ---");
20        let mut stopwatch = Stopwatch::new();
21        let time_1 = stopwatch.elapsed(); // ~ 0
22        sleep(Duration::new(2, 0));
23        let time_2 = stopwatch.elapsed(); // ~ 2.0
24        stopwatch.pause();
25        sleep(Duration::new(1, 0));
26        let time_3 = stopwatch.elapsed(); // ~ 2.0
27        stopwatch.unpause();
28        sleep(Duration::new(1, 0));
29        let time_4 = stopwatch.elapsed(); // ~ 3.0
30        stopwatch.restart();
31        sleep(Duration::new(1, 0));
32        let time_5 = stopwatch.elapsed(); // ~ 1.0
33
34        println!("time1: {:?}", time_1);
35        println!("time2: {:?}", time_2);
36        println!("time3: {:?}", time_3);
37        println!("time4: {:?}", time_4);
38        println!("time5: {:?}", time_5);
39        println!("--- *** ---");
40    }
41
42    #[test]
43    fn test_system_stopwatch() {
44        println!("Testing system stopwatch");
45        let mut sys_stopwatch = SystemStopwatch::new();
46        let time_1 = sys_stopwatch.elapsed(); // ~ 0
47        sleep(Duration::new(2, 0));
48        let time_2 = sys_stopwatch.elapsed(); // ~ 2.0
49        sys_stopwatch.pause();
50        sleep(Duration::new(1, 0));
51        let time_3 = sys_stopwatch.elapsed(); // ~ 2.0
52        sys_stopwatch.unpause();
53        sleep(Duration::new(1, 0));
54        let time_4 = sys_stopwatch.elapsed(); // ~ 3.0
55        sys_stopwatch.restart();
56        sleep(Duration::new(1, 0));
57        let time_5 = sys_stopwatch.elapsed(); // ~ 1.0
58
59        println!("time1: {:?}", time_1);
60        println!("time2: {:?}", time_2);
61        println!("time3: {:?}", time_3);
62        println!("time4: {:?}", time_4);
63        println!("time5: {:?}", time_5);
64        println!("--- *** ---");
65    }
66
67    #[test]
68    fn test_timer() {
69        let mut timer = Timer::new();
70        let mut x = 0;
71        loop {
72            timer.update_delta_time();
73            println!("delta_time: {:?}", timer.delta_time());
74
75            x += 1;
76            if x >= 1000 {
77                break
78            }
79        }
80    }
81
82    #[test]
83    fn test_fixed_update() {
84        use crate::{Timer};
85        let mut timer = Timer::new();
86        timer.set_fixed_interval(Duration::from_secs_f32(1.0 / 120.0));
87        let mut fixed_update_counter: u32 = 0;
88
89        loop {
90            timer.update_delta_time();
91            timer.update_fixed_time();
92
93            if timer.should_fixed_update() {
94                fixed_update_counter += 1;
95            }
96
97            // println!("delta_time: {:?}", timer.delta_time());
98
99            if timer.time_since_start() >= Duration::from_secs_f32(5.0) {
100                println!("time: {:?}", timer.time_since_start());
101                println!("fixed_updates amount: {}", fixed_update_counter);
102                assert_eq!(fixed_update_counter, 120 * 5);
103                break
104            }
105        }
106    }
107}