[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
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

View File

@ -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::<Init>()?;
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)
}