ds323x-rs/tests/datetime.rs

287 lines
8.2 KiB
Rust
Raw Normal View History

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;
2019-05-16 17:18:07 +00:00
use common::{
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234, Register,
DEVICE_ADDRESS as DEV_ADDR,
};
2018-10-28 11:46:12 +00:00
extern crate ds323x;
2019-05-16 17:18:07 +00:00
use ds323x::{DateTime, Error, Hours};
2018-10-28 10:52:07 +00:00
macro_rules! set_param_write_array_test {
2018-10-29 17:09:02 +00:00
($name:ident, $method:ident, $value:expr, $register:ident, [ $( $exp_bin:expr ),+ ] ) => {
_set_param_test!($name, $method, $value,
[ I2cTrans::write(DEV_ADDR, vec![Register::$register, $( $exp_bin ),*]) ],
[ SpiTrans::write(vec![Register::$register + 0x80, $( $exp_bin ),*]) ]);
};
}
2018-10-28 16:28:55 +00:00
macro_rules! read_set_param_write_two_test {
2018-10-29 17:09:02 +00:00
($name:ident, $method:ident, $value:expr, $register:ident, $binary_value1_read:expr, $bin1:expr, $bin2:expr) => {
2019-05-16 17:18:07 +00:00
_set_param_test!(
$name,
$method,
$value,
[
I2cTrans::write_read(
DEV_ADDR,
vec![Register::$register],
vec![$binary_value1_read]
),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $bin1, $bin2])
],
[
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 {
2018-10-29 17:09:02 +00:00
($name:ident, $method:ident, $register:ident, $value:expr, $binary_value_read:expr, $binary_value_write:expr) => {
2019-05-16 17:18:07 +00:00
_set_param_test!(
$name,
$method,
$value,
[
I2cTrans::write_read(
DEV_ADDR,
vec![Register::$register],
vec![$binary_value_read]
),
I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write])
],
[
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 {
2018-10-29 17:09:02 +00:00
($name:ident, $method:ident, $value:expr) => {
mod $name {
use super::*;
2019-05-16 17:18:07 +00:00
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
);
2018-10-29 17:09:02 +00:00
}
2018-10-28 12:14:12 +00:00
};
}
2018-10-28 11:52:24 +00:00
2018-10-28 12:26:46 +00:00
macro_rules! set_invalid_param_range_test {
2018-10-29 17:09:02 +00:00
($name:ident, $method:ident, $too_small_value:expr, $too_big_value:expr) => {
mod $name {
2018-10-28 12:26:46 +00:00
use super::*;
2018-10-29 17:09:02 +00:00
set_invalid_param_test!(too_small, $method, $too_small_value);
2019-05-16 17:18:07 +00:00
set_invalid_param_test!(too_big, $method, $too_big_value);
2018-10-28 12:26:46 +00:00
}
};
}
2018-10-28 12:14:12 +00:00
mod seconds {
use super::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_seconds, SECONDS, 1, 1);
set_param_test!(set, set_seconds, SECONDS, 1, 1);
set_invalid_param_test!(invalid, set_seconds, 60);
2018-10-28 11:27:40 +00:00
}
mod minutes {
use super::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_minutes, MINUTES, 1, 1);
set_param_test!(set, set_minutes, MINUTES, 1, 1);
set_invalid_param_test!(invalid, 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-29 17:09:02 +00:00
get_param_test!(get, get_hours, HOURS, Hours::H24(21), 0b0010_0001);
set_param_test!(set, set_hours, HOURS, Hours::H24(21), 0b0010_0001);
set_invalid_param_test!(invalid, set_hours, Hours::H24(24));
2018-10-28 12:14:12 +00:00
}
2018-10-28 11:52:24 +00:00
2018-10-28 12:14:12 +00:00
mod hours_12h_am {
use super::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_hours, HOURS, Hours::AM(12), 0b0101_0010);
set_param_test!(set, set_hours, HOURS, Hours::AM(12), 0b0101_0010);
set_invalid_param_range_test!(invalid, 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::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_hours, HOURS, Hours::PM(12), 0b0111_0010);
set_param_test!(set, set_hours, HOURS, Hours::PM(12), 0b0111_0010);
set_invalid_param_range_test!(invalid, 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::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_weekday, DOW, 1, 1);
set_param_test!(set, set_weekday, DOW, 1, 1);
set_invalid_param_range_test!(invalid, set_weekday, 0, 8);
2018-10-28 12:28:11 +00:00
}
mod day {
use super::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_day, DOM, 1, 1);
set_param_test!(set, set_day, DOM, 1, 1);
set_invalid_param_range_test!(invalid, set_day, 0, 8);
}
2018-10-28 14:44:17 +00:00
mod month {
use super::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_month, MONTH, 1, 1);
read_set_param_test!(set, set_month, MONTH, 12, 0b0000_0010, 0b0001_0010);
set_invalid_param_range_test!(invalid, set_month, 0, 13);
2018-10-28 14:44:17 +00:00
mod keeps_century {
use super::*;
2018-10-29 17:09:02 +00:00
get_param_test!(get, get_month, MONTH, 12, 0b1001_0010);
read_set_param_test!(set, set_month, MONTH, 12, 0b1000_0010, 0b1001_0010);
2018-10-28 14:44:17 +00:00
}
}
2018-10-28 16:28:55 +00:00
mod year {
use super::*;
2019-05-16 17:18:07 +00:00
get_param_read_array_test!(
century0_get,
get_year,
2099,
MONTH,
[0, 0b1001_1001],
[0, 0]
);
read_set_param_write_two_test!(
century0_set,
set_year,
2099,
MONTH,
0b1001_0010,
0b0001_0010,
0b1001_1001
);
2018-10-29 17:09:02 +00:00
2019-05-16 17:18:07 +00:00
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
);
2018-10-29 17:09:02 +00:00
set_invalid_param_range_test!(invalid, set_year, 1999, 2101);
2018-10-28 17:55:56 +00:00
}
2018-10-28 18:45:06 +00:00
macro_rules! invalid_dt_test {
($name:ident, $year:expr, $month:expr, $day:expr, $weekday:expr,
$hour:expr, $minute:expr, $second:expr) => {
mod $name {
use super::*;
2019-05-16 17:18:07 +00:00
const DT: DateTime = DateTime {
year: $year,
month: $month,
day: $day,
weekday: $weekday,
hour: $hour,
minute: $minute,
second: $second,
};
2018-10-29 17:09:02 +00:00
set_invalid_param_test!($name, set_datetime, &DT);
2018-10-28 18:45:06 +00:00
}
};
}
mod datetime {
use super::*;
2019-05-16 17:18:07 +00:00
const DT: DateTime = DateTime {
year: 2018,
month: 8,
day: 13,
weekday: 2,
hour: Hours::H24(23),
minute: 59,
second: 58,
};
get_param_read_array_test!(
get,
get_datetime,
DT,
SECONDS,
[
0b0101_1000,
0b0101_1001,
0b0010_0011,
0b0000_0010,
0b0001_0011,
0b0000_1000,
0b0001_1000
],
[0, 0, 0, 0, 0, 0, 0]
);
set_param_write_array_test!(
set,
set_datetime,
&DT,
SECONDS,
[
0b0101_1000,
0b0101_1001,
0b0010_0011,
0b0000_0010,
0b0001_0011,
0b0000_1000,
0b0001_1000
]
);
invalid_dt_test!(too_small_year, 1999, 8, 13, 2, Hours::H24(23), 59, 58);
invalid_dt_test!(too_big_year, 2101, 8, 13, 2, Hours::H24(23), 59, 58);
invalid_dt_test!(too_small_month, 2018, 0, 13, 2, Hours::H24(23), 59, 58);
invalid_dt_test!(too_big_month, 2018, 13, 13, 2, Hours::H24(23), 59, 58);
invalid_dt_test!(too_small_day, 2018, 8, 0, 2, Hours::H24(23), 59, 58);
invalid_dt_test!(too_big_day, 2018, 8, 32, 2, Hours::H24(23), 59, 58);
invalid_dt_test!(too_small_wd, 2018, 8, 13, 0, Hours::H24(23), 59, 58);
invalid_dt_test!(too_big_wd, 2018, 8, 13, 8, Hours::H24(23), 59, 58);
invalid_dt_test!(too_big_hours, 2018, 8, 13, 2, Hours::H24(24), 59, 58);
invalid_dt_test!(too_big_min, 2018, 8, 13, 2, Hours::H24(24), 60, 58);
invalid_dt_test!(too_big_seconds, 2018, 8, 13, 2, Hours::H24(24), 59, 60);
2018-10-28 16:28:55 +00:00
}