[19/11/23] Adding tests and exception for bad syntax

This commit is contained in:
Andrew Conlin 2023-11-19 21:20:09 +00:00
parent ed995856f9
commit fa055714b6
2 changed files with 47 additions and 9 deletions

View File

@ -1,6 +1,11 @@
certifi==2023.7.22 certifi==2023.7.22
charset-normalizer==3.3.2 charset-normalizer==3.3.2
coverage==7.3.2
idna==3.4 idna==3.4
iniconfig==2.0.0
maturin==1.3.1 maturin==1.3.1
packaging==23.2
pluggy==1.3.0
pytest==7.4.3
requests==2.31.0 requests==2.31.0
urllib3==2.0.7 urllib3==2.0.7

View File

@ -1,5 +1,6 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use std::time::Instant; use std::time::Instant;
use pyo3::exceptions::PyException;
#[pyclass] #[pyclass]
#[derive(Clone)] #[derive(Clone)]
@ -19,6 +20,7 @@ struct Init {
time: Instant, time: Instant,
#[pyo3(get)] #[pyo3(get)]
results: Results, results: Results,
status: bool,
} }
#[pymethods] #[pymethods]
@ -34,21 +36,28 @@ impl Init {
Init { Init {
time: Instant::now(), time: Instant::now(),
results: res, results: res,
status: false,
} }
} }
fn tic(&mut self) { fn tic(&mut self) {
self.time = Instant::now() self.time = Instant::now();
self.status = true;
} }
fn toc(&mut self) { fn toc(&mut self) -> PyResult<()> {
let elapsed_time = self.time.elapsed(); if self.status == false {
self.results = Results { Err(PyException::new_err("tic() must be called before toc()"))
nanos: elapsed_time.as_nanos(), } else {
micros: elapsed_time.as_micros(), let elapsed_time = self.time.elapsed();
millis: elapsed_time.as_millis(), self.results = Results {
seconds: elapsed_time.as_secs_f64(), 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::<Init>()?; m.add_class::<Init>()?;
Ok(()) 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)
}