From 66e83785a255a7934842c44c5b26e85651a87b8a Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 21 Nov 2018 08:14:06 +0100 Subject: Rename files --- tests/mux.rs | 73 +++++++++++++++++++++ tests/mux_i2c.rs | 73 --------------------- tests/tier1.rs | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tier1_i2c.rs | 185 ----------------------------------------------------- tests/tier2.rs | 101 +++++++++++++++++++++++++++++ tests/tier2_i2c.rs | 101 ----------------------------- 6 files changed, 359 insertions(+), 359 deletions(-) create mode 100644 tests/mux.rs delete mode 100644 tests/mux_i2c.rs create mode 100644 tests/tier1.rs delete mode 100644 tests/tier1_i2c.rs create mode 100644 tests/tier2.rs delete mode 100644 tests/tier2_i2c.rs diff --git a/tests/mux.rs b/tests/mux.rs new file mode 100644 index 0000000..f448f04 --- /dev/null +++ b/tests/mux.rs @@ -0,0 +1,73 @@ +#[macro_use(block)] +extern crate nb; +extern crate embedded_hal; +extern crate embedded_hal_mock as hal; +use hal::i2c::Transaction as I2cTrans; +extern crate ads1x1x; +use ads1x1x::channel; + +#[macro_use] +mod common; +use common::{ new_ads1015 as new, destroy_ads1015 as destroy, + DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags as BF, Config }; + + +macro_rules! mux_test { + ($name:ident, $CS:ident, $config_bits:expr, $other_CS:ident, $other_config_bits:expr) => { + mod $name { + use embedded_hal::adc::OneShot; + use super::*; + + #[test] + fn can_read() { + let default_config = Config::default(); + let config = Config::default().with_high(BF::OS).with_high($config_bits); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let mut dev = new(&transactions); + let measurement = block!(dev.read(&mut channel::$CS)).unwrap(); + assert_eq!(-2048, measurement); + destroy(dev); + } + + #[test] + fn read_then_read_different_triggers_new_measurement() { + let default_config = Config::default(); + let config = Config::default().with_high(BF::OS).with_high($config_bits); + let other_config = Config::default().with_high($other_config_bits); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, other_config.msb(), other_config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![other_config.msb(), other_config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let mut dev = new(&transactions); + assert_would_block!(dev.read(&mut channel::$CS)); + let measurement = block!(dev.read(&mut channel::$other_CS)).unwrap(); + assert_eq!(-2048, measurement); + destroy(dev); + } + + #[test] + fn continuous_can_select_channel() { + let config = Config::default().with_high($config_bits); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; + let dev = new(&transactions); + let mut dev = dev.into_continuous().unwrap(); + dev.select_channel(&mut channel::$CS).unwrap(); + destroy(dev); + } + } + }; +} + +mux_test!(diffa0a1, DifferentialA0A1, 0, SingleA0, BF::MUX2); +mux_test!(diffa0a3, DifferentialA0A3, BF::MUX0, SingleA0, BF::MUX2); +mux_test!(diffa1a3, DifferentialA1A3, BF::MUX1, SingleA0, BF::MUX2); +mux_test!(diffa2a3, DifferentialA2A3, BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); +mux_test!(singlea0, SingleA0, BF::MUX2, DifferentialA0A1, 0); +mux_test!(singlea1, SingleA1, BF::MUX2 | BF::MUX0, SingleA0, BF::MUX2); +mux_test!(singlea2, SingleA2, BF::MUX2 | BF::MUX1, SingleA0, BF::MUX2); +mux_test!(singlea3, SingleA3, BF::MUX2 | BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); diff --git a/tests/mux_i2c.rs b/tests/mux_i2c.rs deleted file mode 100644 index f448f04..0000000 --- a/tests/mux_i2c.rs +++ /dev/null @@ -1,73 +0,0 @@ -#[macro_use(block)] -extern crate nb; -extern crate embedded_hal; -extern crate embedded_hal_mock as hal; -use hal::i2c::Transaction as I2cTrans; -extern crate ads1x1x; -use ads1x1x::channel; - -#[macro_use] -mod common; -use common::{ new_ads1015 as new, destroy_ads1015 as destroy, - DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags as BF, Config }; - - -macro_rules! mux_test { - ($name:ident, $CS:ident, $config_bits:expr, $other_CS:ident, $other_config_bits:expr) => { - mod $name { - use embedded_hal::adc::OneShot; - use super::*; - - #[test] - fn can_read() { - let default_config = Config::default(); - let config = Config::default().with_high(BF::OS).with_high($config_bits); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; - let mut dev = new(&transactions); - let measurement = block!(dev.read(&mut channel::$CS)).unwrap(); - assert_eq!(-2048, measurement); - destroy(dev); - } - - #[test] - fn read_then_read_different_triggers_new_measurement() { - let default_config = Config::default(); - let config = Config::default().with_high(BF::OS).with_high($config_bits); - let other_config = Config::default().with_high($other_config_bits); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, other_config.msb(), other_config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![other_config.msb(), other_config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; - let mut dev = new(&transactions); - assert_would_block!(dev.read(&mut channel::$CS)); - let measurement = block!(dev.read(&mut channel::$other_CS)).unwrap(); - assert_eq!(-2048, measurement); - destroy(dev); - } - - #[test] - fn continuous_can_select_channel() { - let config = Config::default().with_high($config_bits); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; - let dev = new(&transactions); - let mut dev = dev.into_continuous().unwrap(); - dev.select_channel(&mut channel::$CS).unwrap(); - destroy(dev); - } - } - }; -} - -mux_test!(diffa0a1, DifferentialA0A1, 0, SingleA0, BF::MUX2); -mux_test!(diffa0a3, DifferentialA0A3, BF::MUX0, SingleA0, BF::MUX2); -mux_test!(diffa1a3, DifferentialA1A3, BF::MUX1, SingleA0, BF::MUX2); -mux_test!(diffa2a3, DifferentialA2A3, BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); -mux_test!(singlea0, SingleA0, BF::MUX2, DifferentialA0A1, 0); -mux_test!(singlea1, SingleA1, BF::MUX2 | BF::MUX0, SingleA0, BF::MUX2); -mux_test!(singlea2, SingleA2, BF::MUX2 | BF::MUX1, SingleA0, BF::MUX2); -mux_test!(singlea3, SingleA3, BF::MUX2 | BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); diff --git a/tests/tier1.rs b/tests/tier1.rs new file mode 100644 index 0000000..b91575e --- /dev/null +++ b/tests/tier1.rs @@ -0,0 +1,185 @@ +#[macro_use(block)] +extern crate nb; +extern crate embedded_hal; +extern crate embedded_hal_mock as hal; +use hal::i2c::Transaction as I2cTrans; +extern crate ads1x1x; +use ads1x1x::{ channel, DataRate12Bit, DataRate16Bit, Error }; + +#[macro_use] +mod common; +use common::{ new_ads1013, destroy_ads1013, new_ads1113, destroy_ads1113, + DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; + +macro_rules! measure_tests { + ($IC:ident, $create:ident, $destroy:ident, $expected:expr) => { + mod $IC { + use embedded_hal::adc::OneShot; + use super::*; + + mod would_block { + use super::*; + + #[test] + fn read_if_measurement_in_progress() { + let config = Config::default().with_low(BitFlags::OS); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()] ) ]; + let mut dev = $create(&transactions); + assert_would_block!(dev.read(&mut channel::DifferentialA0A1)); + $destroy(dev); + } + } + + #[test] + fn can_measure() { + let default_config = Config::default(); + let config_with_os = Config::default().with_high(BitFlags::OS); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_with_os.msb(), config_with_os.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_with_os.msb(), config_with_os.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let mut dev = $create(&transactions); + let measurement = block!(dev.read(&mut channel::DifferentialA0A1)).unwrap(); + assert_eq!($expected, measurement); + $destroy(dev); + } + + #[test] + fn can_measure_continuous() { + let config = Config::default().with_low(BitFlags::OP_MODE); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let dev = $create(&transactions); + let mut dev = dev.into_continuous().unwrap(); + dev.start().unwrap(); + let measurement = dev.read().unwrap(); + assert_eq!($expected, measurement); + $destroy(dev); + } + } + } +} + +measure_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); +measure_tests!(ads1113, new_ads1113, destroy_ads1113, -32768); + + +mod data_rate_12bit { + macro_rules! test { + ($name:ident, $variant:ident, $config:expr) => { + #[test] + fn $name() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let mut dev = new_ads1013(&transactions); + dev.set_data_rate(DataRate12Bit::$variant).unwrap(); + destroy_ads1013(dev); + } + } + } + + use super::*; + test!(sps128, Sps128, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps250, Sps250, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps490, Sps490, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps920, Sps920, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps1600, Sps1600, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps2400, Sps2400, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps3300, Sps3300, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); +} + +mod data_rate_16bit { + macro_rules! test { + ($name:ident, $variant:ident, $config:expr) => { + #[test] + fn $name() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let mut dev = new_ads1113(&transactions); + dev.set_data_rate(DataRate16Bit::$variant).unwrap(); + destroy_ads1113(dev); + } + } + } + + use super::*; + test!(sps8, Sps8, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps16, Sps16, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps32, Sps32, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps64, Sps64, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps128, Sps128, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps250, Sps250, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps475, Sps475, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps860, Sps860, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); +} + +#[test] +fn can_read_measurement_in_progress() { + let config_os = Config::default().with_low(BitFlags::OS); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_os.msb(), config_os.lsb()]) ]; + let mut dev = new_ads1013(&transactions); + assert!(dev.is_measurement_in_progress().unwrap()); + destroy_ads1013(dev); +} + +#[test] +fn can_read_measurement_not_in_progress() { + let config_os = Config::default().with_high(BitFlags::OS); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_os.msb(), config_os.lsb()]) ]; + let mut dev = new_ads1013(&transactions); + assert!(!dev.is_measurement_in_progress().unwrap()); + destroy_ads1013(dev); +} + +#[test] +fn can_convert_to_continuous() { + let dev = new_ads1013(&[]); + let dev = dev.into_continuous().unwrap(); + destroy_ads1013(dev); +} + +#[test] +fn can_convert_to_one_shot() { + let config_os = Config::default(); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_os.msb(), config_os.lsb()]) ]; + let dev = new_ads1013(&transactions); + let dev = dev.into_continuous().unwrap(); + let dev = dev.into_one_shot().unwrap(); + destroy_ads1013(dev); +} + +mod continuous { + use super::*; + #[test] + fn can_start() { + let config = Config::default().with_low(BitFlags::OP_MODE); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; + let dev = new_ads1013(&transactions); + let mut dev = dev.into_continuous().unwrap(); + dev.start().unwrap(); + destroy_ads1013(dev); + } + + fn assert_not_started(result: Result>) { + match result { + Err(Error::NotStarted) => (), + _ => panic!("Error::NotStarted not returned.") + } + } + #[test] + fn check_assert_matches() { + assert_not_started::<(), ()>(Err(Error::NotStarted)); + } + + #[test] + #[should_panic] + fn check_assert_fails() { + assert_not_started::<(), ()>(Ok(())); + } + + #[test] + fn cannot_read_if_not_started() { + let dev = new_ads1013(&[]); + let mut dev = dev.into_continuous().unwrap(); + assert_not_started(dev.read()); + destroy_ads1013(dev); + } +} diff --git a/tests/tier1_i2c.rs b/tests/tier1_i2c.rs deleted file mode 100644 index b91575e..0000000 --- a/tests/tier1_i2c.rs +++ /dev/null @@ -1,185 +0,0 @@ -#[macro_use(block)] -extern crate nb; -extern crate embedded_hal; -extern crate embedded_hal_mock as hal; -use hal::i2c::Transaction as I2cTrans; -extern crate ads1x1x; -use ads1x1x::{ channel, DataRate12Bit, DataRate16Bit, Error }; - -#[macro_use] -mod common; -use common::{ new_ads1013, destroy_ads1013, new_ads1113, destroy_ads1113, - DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; - -macro_rules! measure_tests { - ($IC:ident, $create:ident, $destroy:ident, $expected:expr) => { - mod $IC { - use embedded_hal::adc::OneShot; - use super::*; - - mod would_block { - use super::*; - - #[test] - fn read_if_measurement_in_progress() { - let config = Config::default().with_low(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()] ) ]; - let mut dev = $create(&transactions); - assert_would_block!(dev.read(&mut channel::DifferentialA0A1)); - $destroy(dev); - } - } - - #[test] - fn can_measure() { - let default_config = Config::default(); - let config_with_os = Config::default().with_high(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_with_os.msb(), config_with_os.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_with_os.msb(), config_with_os.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; - let mut dev = $create(&transactions); - let measurement = block!(dev.read(&mut channel::DifferentialA0A1)).unwrap(); - assert_eq!($expected, measurement); - $destroy(dev); - } - - #[test] - fn can_measure_continuous() { - let config = Config::default().with_low(BitFlags::OP_MODE); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; - let dev = $create(&transactions); - let mut dev = dev.into_continuous().unwrap(); - dev.start().unwrap(); - let measurement = dev.read().unwrap(); - assert_eq!($expected, measurement); - $destroy(dev); - } - } - } -} - -measure_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); -measure_tests!(ads1113, new_ads1113, destroy_ads1113, -32768); - - -mod data_rate_12bit { - macro_rules! test { - ($name:ident, $variant:ident, $config:expr) => { - #[test] - fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; - let mut dev = new_ads1013(&transactions); - dev.set_data_rate(DataRate12Bit::$variant).unwrap(); - destroy_ads1013(dev); - } - } - } - - use super::*; - test!(sps128, Sps128, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps250, Sps250, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps490, Sps490, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps920, Sps920, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps1600, Sps1600, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps2400, Sps2400, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps3300, Sps3300, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); -} - -mod data_rate_16bit { - macro_rules! test { - ($name:ident, $variant:ident, $config:expr) => { - #[test] - fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; - let mut dev = new_ads1113(&transactions); - dev.set_data_rate(DataRate16Bit::$variant).unwrap(); - destroy_ads1113(dev); - } - } - } - - use super::*; - test!(sps8, Sps8, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps16, Sps16, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps32, Sps32, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps64, Sps64, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps128, Sps128, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps250, Sps250, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps475, Sps475, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps860, Sps860, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); -} - -#[test] -fn can_read_measurement_in_progress() { - let config_os = Config::default().with_low(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_os.msb(), config_os.lsb()]) ]; - let mut dev = new_ads1013(&transactions); - assert!(dev.is_measurement_in_progress().unwrap()); - destroy_ads1013(dev); -} - -#[test] -fn can_read_measurement_not_in_progress() { - let config_os = Config::default().with_high(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_os.msb(), config_os.lsb()]) ]; - let mut dev = new_ads1013(&transactions); - assert!(!dev.is_measurement_in_progress().unwrap()); - destroy_ads1013(dev); -} - -#[test] -fn can_convert_to_continuous() { - let dev = new_ads1013(&[]); - let dev = dev.into_continuous().unwrap(); - destroy_ads1013(dev); -} - -#[test] -fn can_convert_to_one_shot() { - let config_os = Config::default(); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_os.msb(), config_os.lsb()]) ]; - let dev = new_ads1013(&transactions); - let dev = dev.into_continuous().unwrap(); - let dev = dev.into_one_shot().unwrap(); - destroy_ads1013(dev); -} - -mod continuous { - use super::*; - #[test] - fn can_start() { - let config = Config::default().with_low(BitFlags::OP_MODE); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; - let dev = new_ads1013(&transactions); - let mut dev = dev.into_continuous().unwrap(); - dev.start().unwrap(); - destroy_ads1013(dev); - } - - fn assert_not_started(result: Result>) { - match result { - Err(Error::NotStarted) => (), - _ => panic!("Error::NotStarted not returned.") - } - } - #[test] - fn check_assert_matches() { - assert_not_started::<(), ()>(Err(Error::NotStarted)); - } - - #[test] - #[should_panic] - fn check_assert_fails() { - assert_not_started::<(), ()>(Ok(())); - } - - #[test] - fn cannot_read_if_not_started() { - let dev = new_ads1013(&[]); - let mut dev = dev.into_continuous().unwrap(); - assert_not_started(dev.read()); - destroy_ads1013(dev); - } -} diff --git a/tests/tier2.rs b/tests/tier2.rs new file mode 100644 index 0000000..4e43e90 --- /dev/null +++ b/tests/tier2.rs @@ -0,0 +1,101 @@ +extern crate embedded_hal_mock as hal; +use hal::i2c::Transaction as I2cTrans; +extern crate ads1x1x; +use ads1x1x::{ FullScaleRange, ComparatorMode, ComparatorPolarity, + ComparatorLatching, ComparatorQueue }; + +#[macro_use] +mod common; +use common::{ new_ads1014, destroy_ads1014, + DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; + +macro_rules! set_value_test { + ($name:ident, $method:ident, $value:expr, $reg:ident, $msb:expr, $lsb:expr) => { + #[test] + fn $name() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::$reg, $msb, $lsb]) ]; + let mut dev = new_ads1014(&transactions); + dev.$method($value).unwrap(); + destroy_ads1014(dev); + } + } +} + +macro_rules! config_test { + ($name:ident, $method:ident, $value:expr, $config:expr) => { + set_value_test!($name, $method, $value, CONFIG, $config.msb(), $config.lsb()); + } +} + +mod can_set_comparator_thresholds { + use super::*; + set_value_test!(low, set_low_threshold_raw, 2047, LOW_TH, 0x7F, 0xF0); + set_value_test!(high, set_high_threshold_raw, 2047, HIGH_TH, 0x7F, 0xF0); +} + +mod can_set_comparator_mode { + use super::*; + config_test!(traditional, set_comparator_mode, ComparatorMode::Traditional, Config::default().with_low( BitFlags::COMP_MODE)); + config_test!(window, set_comparator_mode, ComparatorMode::Window, Config::default().with_high(BitFlags::COMP_MODE)); +} + +mod can_set_comparator_polarity { + use super::*; + config_test!(low, set_comparator_polarity, ComparatorPolarity::ActiveLow, Config::default().with_low( BitFlags::COMP_POL)); + config_test!(high, set_comparator_polarity, ComparatorPolarity::ActiveHigh, Config::default().with_high(BitFlags::COMP_POL)); +} + +mod can_set_comparator_latching { + use super::*; + config_test!(non, set_comparator_latching, ComparatorLatching::Nonlatching, Config::default().with_low( BitFlags::COMP_LAT)); + config_test!(lat, set_comparator_latching, ComparatorLatching::Latching, Config::default().with_high(BitFlags::COMP_LAT)); +} + +#[test] +fn can_disable_comparator() { + let config = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; + let mut dev = new_ads1014(&transactions); + dev.disable_comparator().unwrap(); + destroy_ads1014(dev); +} + +mod can_set_comparator_queue { + use super::*; + config_test!(one, set_comparator_queue, ComparatorQueue::One, Config::default().with_low( BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); + config_test!(two, set_comparator_queue, ComparatorQueue::Two, Config::default().with_low( BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0)); + config_test!(four, set_comparator_queue, ComparatorQueue::Four, Config::default().with_high(BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); +} + +#[test] +fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), + I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; + let mut dev = new_ads1014(&transactions); + dev.use_alert_rdy_pin_as_ready().unwrap(); + destroy_ads1014(dev); +} + +#[test] +fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { + let config = Config::default().with_low(BitFlags::COMP_QUE1).with_low(BitFlags::COMP_QUE0); + let config_disabled_comp = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_disabled_comp.msb(), config_disabled_comp.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), + I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; + let mut dev = new_ads1014(&transactions); + dev.set_comparator_queue(ComparatorQueue::One).unwrap(); + dev.use_alert_rdy_pin_as_ready().unwrap(); + destroy_ads1014(dev); +} + +mod can_set_full_scale_range { + use super::*; + config_test!(fsr6, set_full_scale_range, FullScaleRange::Within6_144V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); + config_test!(fsr4, set_full_scale_range, FullScaleRange::Within4_096V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_high(BitFlags::PGA0)); + config_test!(fsr2, set_full_scale_range, FullScaleRange::Within2_048V, Config::default().with_low( BitFlags::PGA2).with_high(BitFlags::PGA1).with_low( BitFlags::PGA0)); + config_test!(fsr1, set_full_scale_range, FullScaleRange::Within1_024V, Config::default().with_low( BitFlags::PGA2).with_high(BitFlags::PGA1).with_high(BitFlags::PGA0)); + config_test!(fsr0_5, set_full_scale_range, FullScaleRange::Within0_512V, Config::default().with_high(BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); + config_test!(fsr0_2, set_full_scale_range, FullScaleRange::Within0_256V, Config::default().with_high(BitFlags::PGA2).with_low( BitFlags::PGA1).with_high(BitFlags::PGA0)); +} diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs deleted file mode 100644 index 4e43e90..0000000 --- a/tests/tier2_i2c.rs +++ /dev/null @@ -1,101 +0,0 @@ -extern crate embedded_hal_mock as hal; -use hal::i2c::Transaction as I2cTrans; -extern crate ads1x1x; -use ads1x1x::{ FullScaleRange, ComparatorMode, ComparatorPolarity, - ComparatorLatching, ComparatorQueue }; - -#[macro_use] -mod common; -use common::{ new_ads1014, destroy_ads1014, - DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; - -macro_rules! set_value_test { - ($name:ident, $method:ident, $value:expr, $reg:ident, $msb:expr, $lsb:expr) => { - #[test] - fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::$reg, $msb, $lsb]) ]; - let mut dev = new_ads1014(&transactions); - dev.$method($value).unwrap(); - destroy_ads1014(dev); - } - } -} - -macro_rules! config_test { - ($name:ident, $method:ident, $value:expr, $config:expr) => { - set_value_test!($name, $method, $value, CONFIG, $config.msb(), $config.lsb()); - } -} - -mod can_set_comparator_thresholds { - use super::*; - set_value_test!(low, set_low_threshold_raw, 2047, LOW_TH, 0x7F, 0xF0); - set_value_test!(high, set_high_threshold_raw, 2047, HIGH_TH, 0x7F, 0xF0); -} - -mod can_set_comparator_mode { - use super::*; - config_test!(traditional, set_comparator_mode, ComparatorMode::Traditional, Config::default().with_low( BitFlags::COMP_MODE)); - config_test!(window, set_comparator_mode, ComparatorMode::Window, Config::default().with_high(BitFlags::COMP_MODE)); -} - -mod can_set_comparator_polarity { - use super::*; - config_test!(low, set_comparator_polarity, ComparatorPolarity::ActiveLow, Config::default().with_low( BitFlags::COMP_POL)); - config_test!(high, set_comparator_polarity, ComparatorPolarity::ActiveHigh, Config::default().with_high(BitFlags::COMP_POL)); -} - -mod can_set_comparator_latching { - use super::*; - config_test!(non, set_comparator_latching, ComparatorLatching::Nonlatching, Config::default().with_low( BitFlags::COMP_LAT)); - config_test!(lat, set_comparator_latching, ComparatorLatching::Latching, Config::default().with_high(BitFlags::COMP_LAT)); -} - -#[test] -fn can_disable_comparator() { - let config = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; - let mut dev = new_ads1014(&transactions); - dev.disable_comparator().unwrap(); - destroy_ads1014(dev); -} - -mod can_set_comparator_queue { - use super::*; - config_test!(one, set_comparator_queue, ComparatorQueue::One, Config::default().with_low( BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); - config_test!(two, set_comparator_queue, ComparatorQueue::Two, Config::default().with_low( BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0)); - config_test!(four, set_comparator_queue, ComparatorQueue::Four, Config::default().with_high(BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); -} - -#[test] -fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), - I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; - let mut dev = new_ads1014(&transactions); - dev.use_alert_rdy_pin_as_ready().unwrap(); - destroy_ads1014(dev); -} - -#[test] -fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { - let config = Config::default().with_low(BitFlags::COMP_QUE1).with_low(BitFlags::COMP_QUE0); - let config_disabled_comp = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_disabled_comp.msb(), config_disabled_comp.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), - I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; - let mut dev = new_ads1014(&transactions); - dev.set_comparator_queue(ComparatorQueue::One).unwrap(); - dev.use_alert_rdy_pin_as_ready().unwrap(); - destroy_ads1014(dev); -} - -mod can_set_full_scale_range { - use super::*; - config_test!(fsr6, set_full_scale_range, FullScaleRange::Within6_144V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); - config_test!(fsr4, set_full_scale_range, FullScaleRange::Within4_096V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_high(BitFlags::PGA0)); - config_test!(fsr2, set_full_scale_range, FullScaleRange::Within2_048V, Config::default().with_low( BitFlags::PGA2).with_high(BitFlags::PGA1).with_low( BitFlags::PGA0)); - config_test!(fsr1, set_full_scale_range, FullScaleRange::Within1_024V, Config::default().with_low( BitFlags::PGA2).with_high(BitFlags::PGA1).with_high(BitFlags::PGA0)); - config_test!(fsr0_5, set_full_scale_range, FullScaleRange::Within0_512V, Config::default().with_high(BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); - config_test!(fsr0_2, set_full_scale_range, FullScaleRange::Within0_256V, Config::default().with_high(BitFlags::PGA2).with_low( BitFlags::PGA1).with_high(BitFlags::PGA0)); -} -- cgit v1.2.3-54-g00ecf