diff --git a/Cargo.toml b/Cargo.toml index 7bc6ee8..24e314d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ clap = { version = "3.2", features = ["cargo"] } ctrlc = "3.2" anyhow = "1.0" + # ----- Utilities ----- [[bin]] diff --git a/src/buffer.rs b/src/buffer.rs index ac2edf7..767adb8 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -465,3 +465,17 @@ impl<'a> Iterator for AttrIterator<'a> { } } } + +// -------------------------------------------------------------------------- +// Unit Tests +// -------------------------------------------------------------------------- + +// Note: These tests assume that the IIO Dummy kernel module is loaded +// locally with a device created. See the `load_dummy.sh` script. + +#[cfg(test)] +mod tests { + //use super::*; + + //const DEV_ID: &str = "dummydev"; +} diff --git a/src/channel.rs b/src/channel.rs index b261abf..6e477ab 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -549,6 +549,14 @@ impl Channel { } } +impl PartialEq for Channel { + /// Two channels are the same if they refer to the same underlying + /// object in the library. + fn eq(&self, other: &Self) -> bool { + self.chan == other.chan + } +} + /// Iterator over the attributes of a Channel #[derive(Debug)] pub struct AttrIterator<'a> { @@ -584,11 +592,19 @@ impl<'a> Iterator for AttrIterator<'a> { mod tests { use super::*; + const DEV_ID: &str = "dummydev"; + // See that we get the default context. #[test] fn default_context() { - let dev = Context::new().unwrap().get_device(0).unwrap(); - let chan = dev.get_channel(0); - assert!(chan.is_ok()); + let ctx = Context::new().unwrap(); + let dev = ctx.find_device(DEV_ID).unwrap(); + + let idx_chan = dev.get_channel(0).unwrap(); + let id = idx_chan.id().unwrap(); + let output = idx_chan.is_output(); + + let id_chan = dev.find_channel(&id, output).unwrap(); + assert_eq!(id_chan, idx_chan); } } diff --git a/src/device.rs b/src/device.rs index 94b1eef..259596b 100644 --- a/src/device.rs +++ b/src/device.rs @@ -396,23 +396,32 @@ impl<'a> Iterator for AttrIterator<'a> { mod tests { use super::*; + const DEV_ID: &str = "iio:device0"; + const DEV_NAME: &str = "dummydev"; + // Make sure we get a device #[test] fn get_device() { let ctx = Context::new().unwrap(); - let dev = ctx.get_device(0); - assert!(dev.is_ok()); - let dev = dev.unwrap(); - let id = dev.id().unwrap(); - assert!(!id.is_empty()); + let id_dev = ctx.find_device(DEV_ID).unwrap(); + assert_eq!(id_dev.id(), Some(DEV_ID.to_string())); + + let name_dev = ctx.find_device(DEV_NAME).unwrap(); + assert_eq!(name_dev.name(), Some(DEV_NAME.to_string())); + + // Find by name or ID should both work and give the same device. + let id = name_dev.id().unwrap(); + let id_dev = ctx.find_device(&id).unwrap(); + assert_eq!(name_dev.name(), Some(DEV_NAME.to_string())); + assert_eq!(name_dev, id_dev); } // See that attr iterator gets the correct number of attributes #[test] fn attr_iterator_count() { let ctx = Context::new().unwrap(); - let dev = ctx.get_device(0).unwrap(); + let dev = ctx.find_device(DEV_ID).unwrap(); let n = dev.num_attrs(); assert!(n != 0);