Fix checking transactions

pull/4/head
Diego Barrios Romero 2018-10-29 18:09:02 +01:00
parent 6b2f92877b
commit 75c2988fa4
3 changed files with 154 additions and 139 deletions

View File

@ -47,33 +47,47 @@ pub fn new_ds3234(transactions: &[SpiTrans])
Ds323x::new_ds3234(SpiMock::new(&transactions), DummyOutputPin) Ds323x::new_ds3234(SpiMock::new(&transactions), DummyOutputPin)
} }
pub fn destroy_ds3231(dev: Ds323x<interface::I2cInterface<I2cMock>, ic::DS3231>) {
dev.destroy_ds3231().done();
}
pub fn destroy_ds3232(dev: Ds323x<interface::I2cInterface<I2cMock>, ic::DS3232>) {
dev.destroy_ds3232().done();
}
pub fn destroy_ds3234(dev: Ds323x<interface::SpiInterface<SpiMock, DummyOutputPin>, ic::DS3234>) {
dev.destroy_ds3234().0.done();
}
#[macro_export] #[macro_export]
macro_rules! get_test { macro_rules! get_test {
($name:ident, $method:ident, $create_method:ident, $expected:expr, $transactions:expr) => { ($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $expected:expr, $transactions:expr) => {
#[test] #[test]
fn $name() { fn $name() {
let trans = $transactions; let trans = $transactions;
let mut dev = $create_method(&trans); let mut dev = $create_method(&trans);
assert_eq!($expected, dev.$method().unwrap()); assert_eq!($expected, dev.$method().unwrap());
$destroy_method(dev);
} }
}; };
} }
#[macro_export] #[macro_export]
macro_rules! set_test { macro_rules! set_test {
($name:ident, $method:ident, $create_method:ident, $value:expr, $transactions:expr) => { ($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $value:expr, $transactions:expr) => {
#[test] #[test]
fn $name() { fn $name() {
let trans = $transactions; let trans = $transactions;
let mut dev = $create_method(&trans); let mut dev = $create_method(&trans);
dev.$method($value).unwrap(); dev.$method($value).unwrap();
$destroy_method(dev);
} }
}; };
} }
#[macro_export] #[macro_export]
macro_rules! set_invalid_test { macro_rules! set_invalid_test {
($name:ident, $method:ident, $create_method:ident, $value:expr) => { ($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $value:expr) => {
#[test] #[test]
fn $name() { fn $name() {
let mut dev = $create_method(&[]); let mut dev = $create_method(&[]);
@ -81,18 +95,71 @@ macro_rules! set_invalid_test {
Err(Error::InvalidInputData) => (), Err(Error::InvalidInputData) => (),
_ => panic!("InvalidInputData error not returned.") _ => panic!("InvalidInputData error not returned.")
} }
$destroy_method(dev);
} }
}; };
} }
#[macro_export] #[macro_export]
macro_rules! call_test { macro_rules! call_test {
($name:ident, $method:ident, $create_method:ident, $transactions:expr) => { ($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $transactions:expr) => {
#[test] #[test]
fn $name() { fn $name() {
let trans = $transactions; let trans = $transactions;
let mut dev = $create_method(&trans); let mut dev = $create_method(&trans);
dev.$method().unwrap(); dev.$method().unwrap();
$destroy_method(dev);
} }
}; };
} }
#[macro_export]
macro_rules! _get_param_test {
($name:ident, $method:ident, $value:expr, $i2c_transactions:expr, $spi_transactions:expr) => {
mod $name {
use super::*;
get_test!(can_get_ds3231, $method, new_ds3231, destroy_ds3231, $value, $i2c_transactions);
get_test!(can_get_ds3232, $method, new_ds3232, destroy_ds3232, $value, $i2c_transactions);
get_test!(can_get_ds3234, $method, new_ds3234, destroy_ds3234, $value, $spi_transactions);
}
};
}
#[macro_export]
macro_rules! get_param_test {
($name:ident, $method:ident, $register:ident, $value:expr, $binary_value:expr) => {
_get_param_test!($name, $method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value]) ],
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $binary_value]) ]);
};
}
#[macro_export]
macro_rules! get_param_read_array_test {
($name:ident, $method:ident, $value:expr, $register1:ident, [ $( $read_bin:expr ),+ ], [ $( $read_bin2:expr ),+ ]) => {
_get_param_test!($name, $method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register1], vec![$( $read_bin ),*]) ],
[ SpiTrans::transfer(vec![Register::$register1, $( $read_bin2 ),*], vec![Register::$register1, $( $read_bin ),*]) ]);
};
}
#[macro_export]
macro_rules! _set_param_test {
($name:ident, $method:ident, $value:expr, $i2c_transactions:expr, $spi_transactions:expr) => {
mod $name {
use super::*;
set_test!(can_set_ds3231, $method, new_ds3231, destroy_ds3231, $value, $i2c_transactions);
set_test!(can_set_ds3232, $method, new_ds3232, destroy_ds3232, $value, $i2c_transactions);
set_test!(can_set_ds3234, $method, new_ds3234, destroy_ds3234, $value, $spi_transactions);
}
};
}
#[macro_export]
macro_rules! set_param_test {
($name:ident, $method:ident, $register:ident, $value:expr, $binary_value:expr) => {
_set_param_test!($name, $method, $value,
[ I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value]) ],
[ SpiTrans::write(vec![Register::$register + 0x80, $binary_value]) ]);
};
}

View File

@ -4,47 +4,39 @@ extern crate embedded_hal_mock as hal;
use hal::i2c::Transaction as I2cTrans; use hal::i2c::Transaction as I2cTrans;
use hal::spi::Transaction as SpiTrans; use hal::spi::Transaction as SpiTrans;
mod common; mod common;
use common::{ DEVICE_ADDRESS as DEV_ADDR, Register as Reg, new_ds3231, use common::{ DEVICE_ADDRESS as DEV_ADDR, Register, new_ds3231,
new_ds3232, new_ds3234, BitFlags as BF }; new_ds3232, new_ds3234, destroy_ds3231, destroy_ds3232,
destroy_ds3234, BitFlags as BF };
macro_rules! call_method_test { macro_rules! call_method_test {
($method:ident, $i2c_transactions:expr, $spi_transactions:expr) => { ($name:ident, $method:ident, $i2c_transactions:expr, $spi_transactions:expr) => {
call_test!(can_call_ds3231, $method, new_ds3231, $i2c_transactions); mod $name {
call_test!(can_call_ds3232, $method, new_ds3232, $i2c_transactions); use super::*;
call_test!(can_call_ds3234, $method, new_ds3234, $spi_transactions); call_test!(can_call_ds3231, $method, new_ds3231, destroy_ds3231, $i2c_transactions);
call_test!(can_call_ds3232, $method, new_ds3232, destroy_ds3232, $i2c_transactions);
call_test!(can_call_ds3234, $method, new_ds3234, destroy_ds3234, $spi_transactions);
}
}; };
} }
mod do_nothing_if_already_enabled { macro_rules! change_if_necessary_test {
use super::*; ($name:ident, $method:ident, $register:ident, $value_enabled:expr, $value_disabled:expr) => {
call_method_test!(enable, mod $name {
[ I2cTrans::write_read(DEV_ADDR, vec![Reg::CONTROL], vec![0]) ], use super::*;
[ SpiTrans::transfer(vec![Reg::CONTROL, 0], vec![Reg::CONTROL, 0]) ]); call_method_test!(do_nothing_if_not_necessary, $method,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$value_enabled]) ],
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $value_enabled]) ]);
call_method_test!(change, $method,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$value_disabled]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $value_enabled]) ],
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $value_disabled]),
SpiTrans::write(vec![Register::$register + 0x80, $value_enabled]) ]);
}
};
} }
mod enable { change_if_necessary_test!(enable, enable, CONTROL, 0xFF & !BF::EOSC, 0xFF);
use super::*; change_if_necessary_test!(disable, disable, CONTROL, 0xFF, 0xFF & !BF::EOSC);
call_method_test!(enable,
[ I2cTrans::write_read(DEV_ADDR, vec![Reg::CONTROL], vec![BF::EOSC | 0b0101_1010]),
I2cTrans::write(DEV_ADDR, vec![Reg::CONTROL, 0b0101_1010]) ],
[ SpiTrans::transfer(vec![Reg::CONTROL, 0], vec![Reg::CONTROL, BF::EOSC | 0b0101_1010]),
SpiTrans::write(vec![Reg::CONTROL + 0x80, 0b0101_1010]) ]);
}
mod do_nothing_if_already_disabled {
use super::*;
call_method_test!(disable,
[ I2cTrans::write_read(DEV_ADDR, vec![Reg::CONTROL], vec![BF::EOSC]) ],
[ SpiTrans::transfer(vec![Reg::CONTROL, 0], vec![Reg::CONTROL, BF::EOSC]) ]);
}
mod disable {
use super::*;
call_method_test!(disable,
[ I2cTrans::write_read(DEV_ADDR, vec![Reg::CONTROL], vec![0b0101_1010]),
I2cTrans::write(DEV_ADDR, vec![Reg::CONTROL, BF::EOSC | 0b0101_1010]) ],
[ SpiTrans::transfer(vec![Reg::CONTROL, 0], vec![Reg::CONTROL, 0b0101_1010]),
SpiTrans::write(vec![Reg::CONTROL + 0x80, BF::EOSC | 0b0101_1010]) ]);
}

View File

@ -5,61 +5,22 @@ use hal::i2c::Transaction as I2cTrans;
use hal::spi::Transaction as SpiTrans; use hal::spi::Transaction as SpiTrans;
mod common; mod common;
use common::{ DEVICE_ADDRESS as DEV_ADDR, Register, new_ds3231, use common::{ DEVICE_ADDRESS as DEV_ADDR, Register, new_ds3231,
new_ds3232, new_ds3234 }; new_ds3232, new_ds3234, destroy_ds3231, destroy_ds3232,
destroy_ds3234 };
extern crate ds323x; extern crate ds323x;
use ds323x::{ Hours, DateTime, Error }; use ds323x::{ Hours, DateTime, Error };
macro_rules! _get_param_test {
($method:ident, $value:expr, $i2c_transactions:expr, $spi_transactions:expr) => {
get_test!(can_get_ds3231, $method, new_ds3231, $value, $i2c_transactions);
get_test!(can_get_ds3232, $method, new_ds3232, $value, $i2c_transactions);
get_test!(can_get_ds3234, $method, new_ds3234, $value, $spi_transactions);
};
}
macro_rules! get_param_test {
($method:ident, $register:ident, $value:expr, $binary_value:expr) => {
_get_param_test!($method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value]) ],
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $binary_value]) ]);
};
}
macro_rules! get_param_read_array_test {
($method:ident, $value:expr, $register1:ident, [ $( $read_bin:expr ),+ ], [ $( $read_bin2:expr ),+ ]) => {
_get_param_test!($method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register1], vec![$( $read_bin ),*]) ],
[ SpiTrans::transfer(vec![Register::$register1, $( $read_bin2 ),*], vec![Register::$register1, $( $read_bin ),*]) ]);
};
}
macro_rules! _set_param_test {
($method:ident, $value:expr, $i2c_transactions:expr, $spi_transactions:expr) => {
set_test!(can_set_ds3231, $method, new_ds3231, $value, $i2c_transactions);
set_test!(can_set_ds3232, $method, new_ds3232, $value, $i2c_transactions);
set_test!(can_set_ds3234, $method, new_ds3234, $value, $spi_transactions);
};
}
macro_rules! set_param_test {
($method:ident, $register:ident, $value:expr, $binary_value:expr) => {
_set_param_test!($method, $value,
[ I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value]) ],
[ SpiTrans::write(vec![Register::$register + 0x80, $binary_value]) ]);
};
}
macro_rules! set_param_write_array_test { macro_rules! set_param_write_array_test {
($method:ident, $value:expr, $register:ident, [ $( $exp_bin:expr ),+ ] ) => { ($name:ident, $method:ident, $value:expr, $register:ident, [ $( $exp_bin:expr ),+ ] ) => {
_set_param_test!($method, $value, _set_param_test!($name, $method, $value,
[ I2cTrans::write(DEV_ADDR, vec![Register::$register, $( $exp_bin ),*]) ], [ I2cTrans::write(DEV_ADDR, vec![Register::$register, $( $exp_bin ),*]) ],
[ SpiTrans::write(vec![Register::$register + 0x80, $( $exp_bin ),*]) ]); [ SpiTrans::write(vec![Register::$register + 0x80, $( $exp_bin ),*]) ]);
}; };
} }
macro_rules! read_set_param_write_two_test { macro_rules! read_set_param_write_two_test {
($method:ident, $value:expr, $register:ident, $binary_value1_read:expr, $bin1:expr, $bin2:expr) => { ($name:ident, $method:ident, $value:expr, $register:ident, $binary_value1_read:expr, $bin1:expr, $bin2:expr) => {
_set_param_test!($method, $value, _set_param_test!($name, $method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value1_read]), [ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value1_read]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $bin1, $bin2]) ], I2cTrans::write(DEV_ADDR, vec![Register::$register, $bin1, $bin2]) ],
@ -69,8 +30,8 @@ macro_rules! read_set_param_write_two_test {
} }
macro_rules! read_set_param_test { macro_rules! read_set_param_test {
($method:ident, $register:ident, $value:expr, $binary_value_read:expr, $binary_value_write:expr) => { ($name:ident, $method:ident, $register:ident, $value:expr, $binary_value_read:expr, $binary_value_write:expr) => {
_set_param_test!($method, $value, _set_param_test!($name, $method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value_read]), [ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value_read]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write]) ], I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write]) ],
@ -80,102 +41,97 @@ macro_rules! read_set_param_test {
} }
macro_rules! set_invalid_param_test { macro_rules! set_invalid_param_test {
($method:ident, $value:expr) => { ($name:ident, $method:ident, $value:expr) => {
set_invalid_test!(cannot_set_invalid_ds3231, $method, new_ds3231, $value); mod $name {
set_invalid_test!(cannot_set_invalid_ds3232, $method, new_ds3232, $value); use super::*;
set_invalid_test!(cannot_set_invalid_ds3234, $method, new_ds3234, $value); set_invalid_test!(cannot_set_invalid_ds3231, $method, new_ds3231, destroy_ds3231, $value);
set_invalid_test!(cannot_set_invalid_ds3232, $method, new_ds3232, destroy_ds3232, $value);
set_invalid_test!(cannot_set_invalid_ds3234, $method, new_ds3234, destroy_ds3234, $value);
}
}; };
} }
macro_rules! set_invalid_param_range_test { macro_rules! set_invalid_param_range_test {
($method:ident, $too_small_value:expr, $too_big_value:expr) => { ($name:ident, $method:ident, $too_small_value:expr, $too_big_value:expr) => {
mod too_small { mod $name {
use super::*; use super::*;
set_invalid_param_test!($method, $too_small_value); set_invalid_param_test!(too_small, $method, $too_small_value);
} set_invalid_param_test!(too_big, $method, $too_big_value);
mod too_big {
use super::*;
set_invalid_param_test!($method, $too_big_value);
} }
}; };
} }
mod seconds { mod seconds {
use super::*; use super::*;
get_param_test!(get_seconds, SECONDS, 1, 1); get_param_test!(get, get_seconds, SECONDS, 1, 1);
set_param_test!(set_seconds, SECONDS, 1, 1); set_param_test!(set, set_seconds, SECONDS, 1, 1);
set_invalid_param_test!(set_seconds, 60); set_invalid_param_test!(invalid, set_seconds, 60);
} }
mod minutes { mod minutes {
use super::*; use super::*;
get_param_test!(get_minutes, MINUTES, 1, 1); get_param_test!(get, get_minutes, MINUTES, 1, 1);
set_param_test!(set_minutes, MINUTES, 1, 1); set_param_test!(set, set_minutes, MINUTES, 1, 1);
set_invalid_param_test!(set_minutes, 60); set_invalid_param_test!(invalid, set_minutes, 60);
} }
mod hours_24h { mod hours_24h {
use super::*; use super::*;
get_param_test!(get_hours, HOURS, Hours::H24(21), 0b0010_0001); get_param_test!(get, get_hours, HOURS, Hours::H24(21), 0b0010_0001);
set_param_test!(set_hours, HOURS, Hours::H24(21), 0b0010_0001); set_param_test!(set, set_hours, HOURS, Hours::H24(21), 0b0010_0001);
set_invalid_param_test!(set_hours, Hours::H24(24)); set_invalid_param_test!(invalid, set_hours, Hours::H24(24));
} }
mod hours_12h_am { mod hours_12h_am {
use super::*; use super::*;
get_param_test!(get_hours, HOURS, Hours::AM(12), 0b0101_0010); get_param_test!(get, get_hours, HOURS, Hours::AM(12), 0b0101_0010);
set_param_test!(set_hours, HOURS, Hours::AM(12), 0b0101_0010); set_param_test!(set, set_hours, HOURS, Hours::AM(12), 0b0101_0010);
set_invalid_param_range_test!(set_hours, Hours::AM(0), Hours::AM(13)); set_invalid_param_range_test!(invalid, set_hours, Hours::AM(0), Hours::AM(13));
} }
mod hours_12h_pm { mod hours_12h_pm {
use super::*; use super::*;
get_param_test!(get_hours, HOURS, Hours::PM(12), 0b0111_0010); get_param_test!(get, get_hours, HOURS, Hours::PM(12), 0b0111_0010);
set_param_test!(set_hours, HOURS, Hours::PM(12), 0b0111_0010); set_param_test!(set, set_hours, HOURS, Hours::PM(12), 0b0111_0010);
set_invalid_param_range_test!(set_hours, Hours::PM(0), Hours::PM(13)); set_invalid_param_range_test!(invalid, set_hours, Hours::PM(0), Hours::PM(13));
} }
mod weekday { mod weekday {
use super::*; use super::*;
get_param_test!(get_weekday, DOW, 1, 1); get_param_test!(get, get_weekday, DOW, 1, 1);
set_param_test!(set_weekday, DOW, 1, 1); set_param_test!(set, set_weekday, DOW, 1, 1);
set_invalid_param_range_test!(set_weekday, 0, 8); set_invalid_param_range_test!(invalid, set_weekday, 0, 8);
} }
mod day { mod day {
use super::*; use super::*;
get_param_test!(get_day, DOM, 1, 1); get_param_test!(get, get_day, DOM, 1, 1);
set_param_test!(set_day, DOM, 1, 1); set_param_test!(set, set_day, DOM, 1, 1);
set_invalid_param_range_test!(set_day, 0, 8); set_invalid_param_range_test!(invalid, set_day, 0, 8);
} }
mod month { mod month {
use super::*; use super::*;
get_param_test!(get_month, MONTH, 1, 1); get_param_test!(get, get_month, MONTH, 1, 1);
read_set_param_test!(set_month, MONTH, 12, 0b0000_0010, 0b0001_0010); read_set_param_test!(set, set_month, MONTH, 12, 0b0000_0010, 0b0001_0010);
set_invalid_param_range_test!(set_month, 0, 13); set_invalid_param_range_test!(invalid, set_month, 0, 13);
mod keeps_century { mod keeps_century {
use super::*; use super::*;
get_param_test!(get_month, MONTH, 12, 0b1001_0010); get_param_test!(get, get_month, MONTH, 12, 0b1001_0010);
read_set_param_test!(set_month, MONTH, 12, 0b1000_0010, 0b1001_0010); read_set_param_test!(set, set_month, MONTH, 12, 0b1000_0010, 0b1001_0010);
} }
} }
mod year { mod year {
use super::*; use super::*;
mod century0 { get_param_read_array_test!(century0_get, get_year, 2099, MONTH, [ 0, 0b1001_1001 ], [0, 0]);
use super::*; read_set_param_write_two_test!(century0_set, set_year, 2099, MONTH, 0b1001_0010, 0b0001_0010, 0b1001_1001);
get_param_read_array_test!(get_year, 2099, MONTH, [ 0, 0b1001_1001 ], [0, 0]);
read_set_param_write_two_test!(set_year, 2099, MONTH, 0b1001_0010, 0b0001_0010, 0b1001_1001); get_param_read_array_test!(century1_get, get_year, 2100, MONTH, [ 0b1000_0000, 0 ], [0, 0]);
} read_set_param_write_two_test!(century1_set, set_year, 2100, MONTH, 0b0001_0010, 0b1001_0010, 0);
mod century1 {
use super::*; set_invalid_param_range_test!(invalid, set_year, 1999, 2101);
get_param_read_array_test!(get_year, 2100, MONTH, [ 0b1000_0000, 0 ], [0, 0]);
read_set_param_write_two_test!(set_year, 2100, MONTH, 0b0001_0010, 0b1001_0010, 0);
}
set_invalid_param_range_test!(set_year, 1999, 2101);
} }
macro_rules! invalid_dt_test { macro_rules! invalid_dt_test {
@ -185,7 +141,7 @@ macro_rules! invalid_dt_test {
use super::*; use super::*;
const DT : DateTime = DateTime { year: $year, month: $month, day: $day, weekday: $weekday, const DT : DateTime = DateTime { year: $year, month: $month, day: $day, weekday: $weekday,
hour: $hour, minute: $minute, second: $second }; hour: $hour, minute: $minute, second: $second };
set_invalid_param_test!(set_datetime, &DT); set_invalid_param_test!($name, set_datetime, &DT);
} }
}; };
} }
@ -194,12 +150,12 @@ mod datetime {
use super::*; use super::*;
const DT : DateTime = DateTime { year: 2018, month: 8, day: 13, weekday: 2, const DT : DateTime = DateTime { year: 2018, month: 8, day: 13, weekday: 2,
hour: Hours::H24(23), minute: 59, second: 58 }; hour: Hours::H24(23), minute: 59, second: 58 };
get_param_read_array_test!(get_datetime, DT, SECONDS, get_param_read_array_test!(get, get_datetime, DT, SECONDS,
[0b0101_1000, 0b0101_1001, 0b0010_0011, 0b0000_0010, [0b0101_1000, 0b0101_1001, 0b0010_0011, 0b0000_0010,
0b0001_0011, 0b0000_1000, 0b0001_1000], 0b0001_0011, 0b0000_1000, 0b0001_1000],
[0, 0, 0, 0, 0, 0, 0]); [0, 0, 0, 0, 0, 0, 0]);
set_param_write_array_test!(set_datetime, &DT, SECONDS, set_param_write_array_test!(set, set_datetime, &DT, SECONDS,
[0b0101_1000, 0b0101_1001, 0b0010_0011, 0b0000_0010, [0b0101_1000, 0b0101_1001, 0b0010_0011, 0b0000_0010,
0b0001_0011, 0b0000_1000, 0b0001_1000]); 0b0001_0011, 0b0000_1000, 0b0001_1000]);