ds323x-rs/tests/ds323x.rs

172 lines
6.2 KiB
Rust
Raw Normal View History

2018-10-28 11:07:55 +00:00
#[deny(warnings)]
2018-10-28 10:52:07 +00:00
extern crate embedded_hal_mock as hal;
2018-10-28 11:06:25 +00:00
use hal::i2c::Transaction as I2cTrans;
use hal::spi::Transaction as SpiTrans;
2018-10-28 10:52:07 +00:00
mod common;
2018-10-28 12:14:12 +00:00
use common::{ DEVICE_ADDRESS as DEV_ADDR, Register, new_ds3231,
2018-10-28 11:06:25 +00:00
new_ds3232, new_ds3234 };
2018-10-28 11:46:12 +00:00
extern crate ds323x;
2018-10-28 11:52:24 +00:00
use ds323x::{ Hours, Error };
2018-10-28 10:52:07 +00:00
2018-10-28 16:28:55 +00:00
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);
};
}
2018-10-28 12:14:12 +00:00
macro_rules! get_param_test {
($method:ident, $register:ident, $value:expr, $binary_value:expr) => {
2018-10-28 16:28:55 +00:00
_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]) ]);
};
}
2018-10-28 10:52:07 +00:00
2018-10-28 16:28:55 +00:00
macro_rules! get_param_read_two_test {
($method:ident, $value:expr, $register1:ident, $bin1:expr, $bin2:expr) => {
_get_param_test!($method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register1], vec![$bin1, $bin2]) ],
[ SpiTrans::transfer(vec![Register::$register1, 0, 0], vec![Register::$register1, $bin1, $bin2]) ]);
};
}
2018-10-28 10:52:07 +00:00
2018-10-28 16:28:55 +00:00
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);
2018-10-28 12:14:12 +00:00
};
}
2018-10-28 10:52:07 +00:00
2018-10-28 12:14:12 +00:00
macro_rules! set_param_test {
($method:ident, $register:ident, $value:expr, $binary_value:expr) => {
2018-10-28 16:28:55 +00:00
_set_param_test!($method, $value,
[ I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value]) ],
[ SpiTrans::write(vec![Register::$register + 0x80, $binary_value]) ]);
};
}
2018-10-28 10:52:07 +00:00
2018-10-28 16:28:55 +00:00
macro_rules! read_set_param_write_two_test {
($method:ident, $value:expr, $register:ident, $binary_value1_read:expr, $bin1:expr, $bin2:expr) => {
_set_param_test!($method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value1_read]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $bin1, $bin2]) ],
2018-10-28 10:52:07 +00:00
2018-10-28 16:28:55 +00:00
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $binary_value1_read]),
SpiTrans::write(vec![Register::$register + 0x80, $bin1, $bin2]) ]);
2018-10-28 14:43:06 +00:00
};
}
2018-10-28 14:44:17 +00:00
macro_rules! read_set_param_test {
($method:ident, $register:ident, $value:expr, $binary_value_read:expr, $binary_value_write:expr) => {
2018-10-28 16:28:55 +00:00
_set_param_test!($method, $value,
[ I2cTrans::write_read(DEV_ADDR, vec![Register::$register], vec![$binary_value_read]),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write]) ],
2018-10-28 14:44:17 +00:00
2018-10-28 16:28:55 +00:00
[ SpiTrans::transfer(vec![Register::$register, 0], vec![Register::$register, $binary_value_read]),
SpiTrans::write(vec![Register::$register + 0x80, $binary_value_write]) ]);
2018-10-28 12:14:12 +00:00
};
}
2018-10-28 10:52:07 +00:00
2018-10-28 12:14:12 +00:00
macro_rules! set_invalid_param_test {
($method:ident, $value:expr) => {
set_invalid_test!(cannot_set_invalid_ds3231, $method, new_ds3231, $value);
set_invalid_test!(cannot_set_invalid_ds3232, $method, new_ds3232, $value);
set_invalid_test!(cannot_set_invalid_ds3234, $method, new_ds3234, $value);
};
}
2018-10-28 11:52:24 +00:00
2018-10-28 12:26:46 +00:00
macro_rules! set_invalid_param_range_test {
($method:ident, $too_small_value:expr, $too_big_value:expr) => {
mod too_small {
use super::*;
set_invalid_param_test!($method, $too_small_value);
}
mod too_big {
use super::*;
set_invalid_param_test!($method, $too_big_value);
}
};
}
2018-10-28 12:14:12 +00:00
mod seconds {
use super::*;
get_param_test!(get_seconds, SECONDS, 1, 1);
set_param_test!(set_seconds, SECONDS, 1, 1);
set_invalid_param_test!(set_seconds, 60);
2018-10-28 11:27:40 +00:00
}
mod minutes {
use super::*;
2018-10-28 12:14:12 +00:00
get_param_test!(get_minutes, MINUTES, 1, 1);
set_param_test!(set_minutes, MINUTES, 1, 1);
set_invalid_param_test!(set_minutes, 60);
2018-10-28 11:52:24 +00:00
}
2018-10-28 12:14:12 +00:00
mod hours_24h {
2018-10-28 11:46:12 +00:00
use super::*;
2018-10-28 12:14:12 +00:00
get_param_test!(get_hours, HOURS, Hours::H24(21), 0b0010_0001);
set_param_test!(set_hours, HOURS, Hours::H24(21), 0b0010_0001);
set_invalid_param_test!(set_hours, Hours::H24(24));
}
2018-10-28 11:52:24 +00:00
2018-10-28 12:14:12 +00:00
mod hours_12h_am {
use super::*;
get_param_test!(get_hours, HOURS, Hours::AM(12), 0b0101_0010);
set_param_test!(set_hours, HOURS, Hours::AM(12), 0b0101_0010);
2018-10-28 12:26:46 +00:00
set_invalid_param_range_test!(set_hours, Hours::AM(0), Hours::AM(13));
2018-10-28 11:46:12 +00:00
}
2018-10-28 12:15:18 +00:00
mod hours_12h_pm {
use super::*;
get_param_test!(get_hours, HOURS, Hours::PM(12), 0b0111_0010);
set_param_test!(set_hours, HOURS, Hours::PM(12), 0b0111_0010);
2018-10-28 12:26:46 +00:00
set_invalid_param_range_test!(set_hours, Hours::PM(0), Hours::PM(13));
2018-10-28 12:15:18 +00:00
}
2018-10-28 12:26:46 +00:00
2018-10-28 12:28:11 +00:00
mod weekday {
use super::*;
get_param_test!(get_weekday, DOW, 1, 1);
set_param_test!(set_weekday, DOW, 1, 1);
set_invalid_param_range_test!(set_weekday, 0, 8);
}
mod day {
use super::*;
get_param_test!(get_day, DOM, 1, 1);
set_param_test!(set_day, DOM, 1, 1);
set_invalid_param_range_test!(set_day, 0, 8);
}
2018-10-28 14:44:17 +00:00
mod month {
use super::*;
get_param_test!(get_month, MONTH, 1, 1);
read_set_param_test!(set_month, MONTH, 12, 0b0000_0010, 0b0001_0010);
set_invalid_param_range_test!(set_month, 0, 13);
mod keeps_century {
use super::*;
get_param_test!(get_month, MONTH, 12, 0b1001_0010);
read_set_param_test!(set_month, MONTH, 12, 0b1000_0010, 0b1001_0010);
}
}
2018-10-28 16:28:55 +00:00
mod year {
use super::*;
mod century0 {
use super::*;
get_param_read_two_test!(get_year, 2099, MONTH, 0, 0b1001_1001);
read_set_param_write_two_test!(set_year, 2099, MONTH, 0b1001_0010, 0b0001_0010, 0b1001_1001);
}
mod century1 {
use super::*;
get_param_read_two_test!(get_year, 2100, MONTH, 0b1000_0000, 0);
read_set_param_write_two_test!(set_year, 2100, MONTH, 0b0001_0010, 0b1001_0010, 0);
}
set_invalid_param_test!(set_year, 2101);
}