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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
pub mod performances {
    use colored_truecolor::Colorize;
    use std::{collections::HashMap, time::Instant};

    pub struct Performances {}

    impl Default for Performances {
        fn default() -> Self {
            Self::new("Calcul of performances")
        }
    }
    ///
    /// To run performnce test
    ///
    impl Performances {

        ///
        /// Constructor
        ///
        pub fn new(description: &str) -> Performances {
            println!("\n{}\n", description.magenta().bold());
            Self {}
        }
        
        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in f32s unit
        ///
        ///
        pub fn f32(&mut self, callbacks: HashMap<fn(), f32>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: f32 = now.elapsed().as_secs_f32();
                assert!(
                    end < v,
                    "A callback take {} f32s and the expected time is {} f32s",
                    end,
                    v
                );
            }
            self.output()
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in f64s unit
        ///
        ///
        pub fn f64(&mut self, callbacks: HashMap<fn(), f64>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: f64 = now.elapsed().as_secs_f64();
                assert!(
                    end < v,
                    "A callback take {} f64s and the expected time is {} f64s",
                    end,
                    v
                );
            }
            self.output()
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in nanos unit
        ///
        ///
        pub fn nanos(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u128 = now.elapsed().as_nanos();
                assert!(
                    end < v,
                    "A callback take {} ns and the expected time is {} ns",
                    end,
                    v
                );
            }
            self.output()
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in micros seconds unit
        ///
        pub fn micros(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u128 = now.elapsed().as_micros();
                assert!(
                    end < v,
                    "A callback take {} µs and the expected time is {} µs",
                    end,
                    v
                );
            }
            self.output()
        }
  
        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in millis seconds unit
        ///
        pub fn millis(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u128 = now.elapsed().as_millis();
                assert!(
                    end < v,
                    "A callback take {} ms and the expected time is {} ms",
                    end,
                    v
                );
            }
            self.output()
        }
        
        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in seconds unit
        ///
        pub fn secs(&mut self, callbacks: HashMap<fn(), u64>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u64 = now.elapsed().as_secs();
                assert!(
                    end < v,
                    "A callback take {} ms and the expected time is {} ms",
                    end,
                    v
                );
            }
            self.output()
        }

        ///
        /// # Print a point in console after a test runned successfully
        ///
        fn output(&mut self) -> &mut Performances {
            print!("{}", ".".white().bold());
            self
        }
        
        ///
        /// # End of the test 
        ///
        pub fn end(&mut self) -> Result<String, String> {
            println!();
            Ok(String::from("ok"))
        }
    }
}

#[cfg(test)]
mod test {
    use std::{collections::HashMap, thread::sleep, time::Duration};

    use crate::performances::Performances;

    fn live() {
        let t = Duration::from_secs_f32(5.0f32);
        sleep(t);
    }

    fn life() {
        let t = Duration::from_secs_f64(5.0f64);
        sleep(t);
    }
    fn like() {
        let t = Duration::from_micros(5_0);
        sleep(t);
    }

    fn wife() {
        let t = Duration::from_nanos(5_0);
        sleep(t);
    }

    fn knife() {
        let t = Duration::from_millis(5_0);
        sleep(t);
    }
    fn chipher() {
        let t = Duration::from_secs(1);
        sleep(t);
    }
    #[test]
    pub fn all() {
        let mut callback_f32: HashMap<fn(), f32> = HashMap::new();
        let mut callback_f64: HashMap<fn(), f64> = HashMap::new();
        let mut callback_millis: HashMap<fn(), u128> = HashMap::new();
        let mut callback_nanos: HashMap<fn(), u128> = HashMap::new();
        let mut callback_micros: HashMap<fn(), u128> = HashMap::new();
        let mut callback_secs: HashMap<fn(), u64> = HashMap::new();

        callback_f32.insert(live, 7.0f32);
        callback_f64.insert(life, 7.0f64);
        callback_millis.insert(knife, 7_000_000);
        callback_micros.insert(like, 7_000_000);
        callback_nanos.insert(wife, 7_000_000);
        callback_secs.insert(chipher, 7);

        let mut p = Performances::default();
        p.f32(callback_f32);
        p.f64(callback_f64);
        p.millis(callback_millis);
        p.nanos(callback_nanos);
        p.micros(callback_micros);
        p.secs(callback_secs);
        assert!(p.end().is_ok());
    }
}