Removed unwrap()s, refactored iterator, removed memleak

This commit is contained in:
Josh Mitchell
2020-11-08 17:23:08 +11:00
parent 05b309e4bd
commit f9cf479baf
8 changed files with 223 additions and 197 deletions

View File

@@ -111,8 +111,8 @@ mod tests {
use std::ffi::CString;
#[test]
fn test_xdr_tell() {
let path = CString::new("tests/1l2y.xtc").unwrap();
fn test_xdr_tell() -> Result<(), Box<dyn std::error::Error>> {
let path = CString::new("tests/1l2y.xtc")?;
let num_atoms = 304;
let mut time: f32 = 2.0;
let mut step: i32 = 5;
@@ -121,7 +121,7 @@ mod tests {
let mut prec: f32 = 0.0;
unsafe {
let mode = CString::new("r").unwrap();
let mode = CString::new("r")?;
let xdr = xdrfile_open(path.as_ptr(), mode.as_ptr());
assert!(!xdr.is_null());
@@ -140,15 +140,16 @@ mod tests {
let tell = xdr_tell(xdr);
assert!(tell > 0, "{}", tell);
}
};
Ok(())
}
#[test]
fn test_xdr_seek() {
let path = CString::new("tests/1l2y.xtc").unwrap();
fn test_xdr_seek() -> Result<(), Box<dyn std::error::Error>> {
let path = CString::new("tests/1l2y.xtc")?;
unsafe {
let mode = CString::new("r").unwrap();
let mode = CString::new("r")?;
let xdr = xdrfile_open(path.as_ptr(), mode.as_ptr());
assert!(!xdr.is_null());
@@ -160,5 +161,6 @@ mod tests {
let tell = xdr_tell(xdr);
assert!(tell == 500, "{}", tell);
}
Ok(())
}
}

View File

@@ -47,19 +47,20 @@ mod tests {
use tempfile::NamedTempFile;
#[test]
fn test_read_trr_natoms() {
let path = CString::new("tests/1l2y.trr").unwrap();
fn test_read_trr_natoms() -> Result<(), Box<dyn std::error::Error>> {
let path = CString::new("tests/1l2y.trr")?;
let mut natoms = 0;
unsafe {
read_trr_natoms(path.as_ptr() as *const i8, &mut natoms);
}
assert!(natoms == 304);
Ok(())
}
#[test]
fn test_read_trr_nframes() {
let path = CString::new("tests/1l2y.trr").unwrap();
fn test_read_trr_nframes() -> Result<(), Box<dyn std::error::Error>> {
let path = CString::new("tests/1l2y.trr")?;
let mut nframes: u64 = 0;
unsafe {
@@ -67,12 +68,18 @@ mod tests {
assert!(code as u32 == exdrOK);
}
assert!(nframes == 38, "{:?}", nframes);
Ok(())
}
#[test]
fn test_read_write_trr() {
let tempfile = NamedTempFile::new().unwrap();
let tmp_path = CString::new(tempfile.path().to_str().unwrap()).unwrap();
fn test_read_write_trr() -> Result<(), Box<dyn std::error::Error>> {
let tempfile = NamedTempFile::new()?;
let tmp_path = CString::new(
tempfile
.path()
.to_str()
.expect("Could not convert path to str"),
)?;
// write atoms to tempfile
let natoms: i32 = 2;
@@ -86,7 +93,7 @@ mod tests {
let f: Vec<Rvec> = vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]];
unsafe {
let mode = CString::new("w").unwrap();
let mode = CString::new("w")?;
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let write_code = write_trr(
xdr,
@@ -114,7 +121,7 @@ mod tests {
let f2: Vec<Rvec> = vec![[0.0, 0.0, 0.0]; 2];
unsafe {
let mode = CString::new("r").unwrap();
let mode = CString::new("r")?;
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let read_code = read_trr(
xdr,
@@ -139,5 +146,6 @@ mod tests {
assert!(x2 == x);
assert!(v2 == v);
assert!(f2 == f);
Ok(())
}
}

View File

@@ -43,19 +43,20 @@ mod tests {
use tempfile::NamedTempFile;
#[test]
fn test_read_xtc_natoms() {
let path = CString::new("tests/1l2y.xtc").unwrap();
fn test_read_xtc_natoms() -> Result<(), Box<dyn std::error::Error>> {
let path = CString::new("tests/1l2y.xtc")?;
let mut natoms = 0;
unsafe {
read_xtc_natoms(path.as_ptr() as *mut i8, &mut natoms);
}
assert!(natoms == 304);
Ok(())
}
#[test]
fn test_read_xtc_nframes() {
let path = CString::new("tests/1l2y.xtc").unwrap();
fn test_read_xtc_nframes() -> Result<(), Box<dyn std::error::Error>> {
let path = CString::new("tests/1l2y.xtc")?;
let mut nframes: u64 = 0;
unsafe {
@@ -63,12 +64,18 @@ mod tests {
assert!(code as u32 == exdrOK);
}
assert!(nframes == 38, "{:?}", nframes);
Ok(())
}
#[test]
fn test_read_write_xtc() {
let tempfile = NamedTempFile::new().unwrap();
let tmp_path = CString::new(tempfile.path().to_str().unwrap()).unwrap();
fn test_read_write_xtc() -> Result<(), Box<dyn std::error::Error>> {
let tempfile = NamedTempFile::new()?;
let tmp_path = CString::new(
tempfile
.path()
.to_str()
.expect("Could not convert path to str"),
)?;
// write atoms to tempfile
let natoms: i32 = 2;
@@ -78,7 +85,7 @@ mod tests {
let x: Vec<Rvec> = vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]];
unsafe {
let mode = CString::new("w").unwrap();
let mode = CString::new("w")?;
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let write_code = write_xtc(
xdr,
@@ -101,7 +108,7 @@ mod tests {
let mut prec: f32 = 0.0;
unsafe {
let mode = CString::new("r").unwrap();
let mode = CString::new("r")?;
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let read_code = read_xtc(
xdr,
@@ -121,5 +128,6 @@ mod tests {
assert!(time2 == time);
assert!(box_vec2 == box_vec);
assert!(x2 == x);
Ok(())
}
}