diff --git a/requirements.txt b/requirements.txt index 2b076d0..db9c234 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,11 @@ certifi==2023.7.22 charset-normalizer==3.3.2 +coverage==7.3.2 idna==3.4 +iniconfig==2.0.0 maturin==1.3.1 +packaging==23.2 +pluggy==1.3.0 +pytest==7.4.3 requests==2.31.0 urllib3==2.0.7 diff --git a/src/lib.rs b/src/lib.rs index d3cf904..4974904 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ use pyo3::prelude::*; use std::time::Instant; +use pyo3::exceptions::PyException; #[pyclass] #[derive(Clone)] @@ -19,6 +20,7 @@ struct Init { time: Instant, #[pyo3(get)] results: Results, + status: bool, } #[pymethods] @@ -34,21 +36,28 @@ impl Init { Init { time: Instant::now(), results: res, + status: false, } } fn tic(&mut self) { - self.time = Instant::now() + self.time = Instant::now(); + self.status = true; } - fn toc(&mut self) { - let elapsed_time = self.time.elapsed(); - self.results = Results { - nanos: elapsed_time.as_nanos(), - micros: elapsed_time.as_micros(), - millis: elapsed_time.as_millis(), - seconds: elapsed_time.as_secs_f64(), - }; + fn toc(&mut self) -> PyResult<()> { + if self.status == false { + Err(PyException::new_err("tic() must be called before toc()")) + } else { + let elapsed_time = self.time.elapsed(); + self.results = Results { + nanos: elapsed_time.as_nanos(), + micros: elapsed_time.as_micros(), + millis: elapsed_time.as_millis(), + seconds: elapsed_time.as_secs_f64(), + }; + Ok(()) + } } } @@ -57,3 +66,27 @@ fn tictoc(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_class::()?; Ok(()) } + +#[test] +fn test_new() { + let init = Init::new(); + assert_eq!(init.results.nanos,0); +} + +#[test] +fn test_tic() { + let mut init = Init::new(); + let time1 = init.time; + init.tic(); + let time2 = init.time; + assert!(time2 > time1) +} + +#[test] +fn test_toc() { + let mut init = Init::new(); + init.tic(); + println!("{}","test"); + let _ = init.toc(); + assert!(init.results.nanos > 0) +}