diff --git a/src/lib.rs b/src/lib.rs index 00bbdf3..66a1b23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ mod tictoc { use pyo3::exceptions::PyException; #[pyclass] - #[derive(Clone)] + #[derive(Clone, Debug, PartialEq)] struct Results { #[pyo3(get)] nanos: u128, @@ -74,6 +74,7 @@ mod tictoc { } } } + #[test] fn test_new() { let init = Init::new(); @@ -84,17 +85,48 @@ mod tictoc { fn test_tic() { let mut init = Init::new(); let time1 = init.time; - init.tic(); + let _ = init.tic(); let time2 = init.time; assert!(time2 > time1) } - + #[test] fn test_toc() { let mut init = Init::new(); - init.tic(); + let _ = init.tic(); println!("{}","test"); - let _ = init.toc(); - assert!(init.results.nanos > 0) + let _ = init.toc(None).unwrap(); + assert!(init.results.nanos > 0); + } + + #[test] + fn test_passing_tic_to_toc() { + let mut init = Init::new(); + let tic_obj = init.tic().unwrap(); + println!("{}","test"); + let results = init.toc(Some(tic_obj)).unwrap(); + assert!(init.results.nanos > 0); + assert_eq!(init.results,results) + } + + #[test] + fn test_multiple_calls() { + let mut init = Init::new(); + let first_tic = init.tic().unwrap(); + println!("{}","test"); + let second_tic = init.tic().unwrap(); + println!("{}","test"); + let results2 = init.toc(Some(second_tic)).unwrap(); + let results = init.toc(Some(first_tic)).unwrap(); + assert!(results.nanos > results2.nanos); + } + + #[test] + fn test_toc_before_tic() { + let mut init = Init::new(); + //assert!(init.toc(None).is_err()) + pyo3::prepare_freethreaded_python(); + let e = init.toc(None).unwrap_err(); + assert_eq!(e.to_string(),"Exception: tic() must be called before toc()") } }