Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Sep 24, 2023
1 parent 0995d4e commit cd12d23
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ clap = { version = "3.2", features = ["cargo"] }
ctrlc = "3.2"
anyhow = "1.0"


# ----- Utilities -----

[[bin]]
Expand Down
14 changes: 14 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
22 changes: 19 additions & 3 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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);
}
}
21 changes: 15 additions & 6 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cd12d23

Please sign in to comment.