Skip to content

Commit

Permalink
add tests for missing items
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusuMET committed Jan 6, 2020
1 parent 3c120e0 commit 1e2677c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,37 @@ impl<'g> Dimension<'g> {
}
}

pub(crate) fn from_name_toid(loc: nc_type, name: &str) -> error::Result<nc_type> {
pub(crate) fn from_name_toid(loc: nc_type, name: &str) -> error::Result<Option<nc_type>> {
let mut dimid = 0;
let cname = super::utils::short_name_to_bytes(name)?;
unsafe {
error::checked(nc_inq_dimid(loc, cname.as_ptr() as *const _, &mut dimid))?;
let e = unsafe { nc_inq_dimid(loc, cname.as_ptr() as *const _, &mut dimid) };
if e == NC_EBADDIM {
return Ok(None);
} else {
error::checked(e)?;
}
Ok(dimid)
Ok(Some(dimid))
}

pub(crate) fn from_name<'f>(loc: nc_type, name: &str) -> error::Result<Dimension<'f>> {
pub(crate) fn from_name<'f>(loc: nc_type, name: &str) -> error::Result<Option<Dimension<'f>>> {
let mut dimid = 0;
let cname = super::utils::short_name_to_bytes(name)?;
unsafe {
error::checked(nc_inq_dimid(loc, cname.as_ptr() as *const _, &mut dimid))?;
let e = unsafe { nc_inq_dimid(loc, cname.as_ptr() as *const _, &mut dimid) };
if e == NC_EBADDIM {
return Ok(None);
} else {
error::checked(e)?;
}
let mut dimlen = 0;
unsafe {
error::checked(nc_inq_dimlen(loc, dimid, &mut dimlen))?;
}

Ok(Dimension {
Ok(Some(Dimension {
len: core::num::NonZeroUsize::new(dimlen),
id: Identifier { ncid: loc, dimid },
_group: PhantomData,
})
}))
}

pub(crate) fn dimensions_from_location<'g>(
Expand Down Expand Up @@ -168,7 +174,7 @@ pub(crate) fn dimension_from_name<'f>(
let cname = super::utils::short_name_to_bytes(name)?;
let mut dimid = 0;
let e = unsafe { nc_inq_dimid(ncid, cname.as_ptr() as *const _, &mut dimid) };
if e == NC_ENOTFOUND {
if e == NC_EBADDIM {
return Ok(None);
} else {
error::checked(e)?;
Expand Down
2 changes: 1 addition & 1 deletion src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub(crate) fn group_from_name<'f>(ncid: nc_type, name: &str) -> error::Result<Op
let byte_name = super::utils::short_name_to_bytes(name)?;
let mut grpid = 0;
let e = unsafe { nc_inq_grp_ncid(ncid, byte_name.as_ptr() as *const _, &mut grpid) };
if e == NC_ENOTFOUND {
if e == NC_ENOGRP {
return Ok(None);
} else {
error::checked(e)?;
Expand Down
16 changes: 13 additions & 3 deletions src/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'g> Variable<'g> {
let cname = super::utils::short_name_to_bytes(name)?;
let mut varid = 0;
let e = unsafe { nc_inq_varid(ncid, cname.as_ptr() as *const _, &mut varid) };
if e == NC_ENOTFOUND {
if e == NC_ENOTVAR {
return Ok(None);
} else {
error::checked(e)?;
Expand Down Expand Up @@ -1121,7 +1121,13 @@ impl<'g> VariableMut<'g> {
) -> error::Result<Self> {
let dimensions = dims
.iter()
.map(|dimname| super::dimension::from_name_toid(ncid, dimname))
.map(
|dimname| match super::dimension::from_name_toid(ncid, dimname) {
Ok(Some(id)) => Ok(id),
Ok(None) => Err(error::Error::NotFound(format!("dimensions {}", dimname))),
Err(e) => Err(e),
},
)
.collect::<error::Result<Vec<_>>>()?;

let cname = super::utils::short_name_to_bytes(name)?;
Expand All @@ -1139,7 +1145,11 @@ impl<'g> VariableMut<'g> {

let dimensions = dims
.iter()
.map(|dimname| super::dimension::from_name(ncid, dimname))
.map(|dimname| match super::dimension::from_name(ncid, dimname) {
Ok(None) => Err(error::Error::NotFound(format!("dimensions {}", dimname))),
Ok(Some(dim)) => Ok(dim),
Err(e) => Err(e),
})
.collect::<error::Result<Vec<_>>>()?;

Ok(VariableMut(
Expand Down
74 changes: 74 additions & 0 deletions tests/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#[test]
fn dimensions() {
let d = tempfile::tempdir().unwrap();
let path = d.path().join("dimensions.rs");

let mut file = netcdf::create(path).unwrap();

let mut group = file.add_group("group").unwrap();

group.add_dimension("a", 5).unwrap();
group.add_dimension("b", 6).unwrap();
group.add_unlimited_dimension("c").unwrap();

let dim = group.dimension("a").unwrap().unwrap();
assert_eq!(dim.len(), 5);
let dim = group.dimension("b").unwrap().unwrap();
assert_eq!(dim.len(), 6);
let dim = group.dimension("c").unwrap().unwrap();
assert_eq!(dim.len(), 0);
assert!(group.dimension("d").unwrap().is_none());
}

#[test]
fn groups() {
let d = tempfile::tempdir().unwrap();
let path = d.path().join("groups.rs");
let mut file = netcdf::create(path).unwrap();
let mut group = file.add_group("group").unwrap();
group.add_group("g").unwrap();
group.add_group("e").unwrap();
group.add_group("f").unwrap();

assert_eq!(group.groups().unwrap().count(), 3);
assert!(group.group("w").unwrap().is_none());
assert!(group.group_mut("w").unwrap().is_none());
assert!(group.group_mut("e").unwrap().is_some());
assert!(group.group("f").unwrap().is_some());
}

#[test]
fn find_variable() {
let d = tempfile::tempdir().unwrap();
let path = d.path().join("groups.rs");
let mut file = netcdf::create(path).unwrap();
let mut group = file.add_group("group").unwrap();

group.add_variable::<u8>("v", &[]).unwrap();
group.add_variable::<u8>("w", &[]).unwrap();
group.add_dimension("d", 3).unwrap();
group.add_variable::<u8>("z", &["d"]).unwrap();

assert_eq!(group.variables_mut().unwrap().count(), 3);
assert_eq!(group.variables().unwrap().count(), 3);

let v = group.variable("v").unwrap().unwrap();
assert_eq!(v.dimensions().iter().count(), 0);
assert_eq!(v.len(), 1);
let z = group.variable_mut("z").unwrap().unwrap();
assert_eq!(z.dimensions()[0].len(), 3);
assert_eq!(z.vartype(), netcdf_sys::NC_UBYTE);
assert_eq!(z.name().unwrap(), "z");

assert!(group.variable("vvvvv").unwrap().is_none());

for var in group.variables_mut().unwrap() {
let mut var = var.unwrap();
var.compression(3).unwrap();
if var.name().unwrap() == "z" {
var.chunking(&[1]).unwrap();
} else {
var.chunking(&[]).unwrap();
}
}
}

0 comments on commit 1e2677c

Please sign in to comment.