Skip to content

Commit

Permalink
add to tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Oct 25, 2024
1 parent 820f139 commit 8a682e7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
19 changes: 18 additions & 1 deletion library/src/radio/rf24/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ mod test {
let pin_expectations = [
PinTransaction::set(PinState::Low),
PinTransaction::set(PinState::High),
PinTransaction::set(PinState::Low),
];
let mut pin_mock = PinMock::new(&pin_expectations);

Expand All @@ -549,7 +550,7 @@ mod test {
}

let spi_expectations = spi_test_expects![
// flush_tx() of artifact ACK payloads
// flush_tx()
(vec![commands::FLUSH_TX], vec![0xEu8]),
// clear_status_flags()
(
Expand All @@ -563,11 +564,25 @@ mod test {
(buf.to_vec(), vec![0u8; 33]),
// spoof a tx_ds event from a NOP write
(vec![commands::NOP], vec![0xE | mnemonics::MASK_TX_DS]),
// flush_tx()
(vec![commands::FLUSH_TX], vec![0xEu8]),
// clear_status_flags()
(
vec![
registers::STATUS | commands::W_REGISTER,
mnemonics::MASK_MAX_RT | mnemonics::MASK_RX_DR | mnemonics::MASK_TX_DS,
],
vec![0xFu8, 0u8],
),
];
let mut spi_mock = SpiMock::new(&spi_expectations);
let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
let payload = [0x55; 8];
assert!(radio.send(&payload, false).unwrap());
// again using simulated full TX FIFO
assert!(!radio.send(&payload, false).unwrap());
radio._config_reg |= 1; // simulate RX mode
assert!(!radio.send(&payload, false).unwrap());
spi_mock.done();
pin_mock.done();
}
Expand Down Expand Up @@ -651,6 +666,8 @@ mod test {
let mut spi_mock = SpiMock::new(&spi_expectations);
let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
assert!(radio.resend().unwrap());
radio._config_reg |= 1; // simulate RX mode
assert!(!radio.resend().unwrap());
spi_mock.done();
pin_mock.done();
}
Expand Down
104 changes: 104 additions & 0 deletions library/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,107 @@ impl Display for StatusFlags {
)
}
}

#[cfg(test)]
mod test {
use crate::StatusFlags;

use super::{CrcLength, DataRate, FifoState, PaLevel};
extern crate std;
use std::{format, string::String};

fn display_crc(param: CrcLength, expected: String) -> bool {
format!("{param}") == expected
}

#[test]
fn crc_8bit() {
assert!(display_crc(CrcLength::Bit8, String::from("8 bit")));
}

#[test]
fn crc_16bit() {
assert!(display_crc(CrcLength::Bit16, String::from("16 bit")));
}

#[test]
fn crc_disable() {
assert!(display_crc(CrcLength::Disabled, String::from("disabled")));
}

fn display_fifo_state(param: FifoState, expected: String) -> bool {
format!("{param}") == expected
}

#[test]
fn fifo_state_empty() {
assert!(display_fifo_state(FifoState::Empty, String::from("Empty")));
}

#[test]
fn fifo_state_full() {
assert!(display_fifo_state(FifoState::Full, String::from("Full")));
}

#[test]
fn fifo_state_occupied() {
assert!(display_fifo_state(
FifoState::Occupied,
String::from("Occupied")
));
}

fn display_data_rate(param: DataRate, expected: String) -> bool {
format!("{param}") == expected
}

#[test]
fn data_rate_1mbps() {
assert!(display_data_rate(DataRate::Mbps1, String::from("1 Mbps")));
}

#[test]
fn data_rate_2mbps() {
assert!(display_data_rate(DataRate::Mbps2, String::from("2 Mbps")));
}

#[test]
fn data_rate_250kbps() {
assert!(display_data_rate(
DataRate::Kbps250,
String::from("250 Kbps")
));
}

fn display_pa_level(param: PaLevel, expected: String) -> bool {
format!("{param}") == expected
}

#[test]
fn pa_level_min() {
assert!(display_pa_level(PaLevel::Min, String::from("Min")));
}

#[test]
fn pa_level_low() {
assert!(display_pa_level(PaLevel::Low, String::from("Low")));
}

#[test]
fn pa_level_high() {
assert!(display_pa_level(PaLevel::High, String::from("High")));
}

#[test]
fn pa_level_max() {
assert!(display_pa_level(PaLevel::Max, String::from("Max")));
}

#[test]
fn display_flags() {
assert_eq!(
format!("{}", StatusFlags::default()),
String::from("StatusFlags rx_dr: false, tx_ds: false, tx_df: false")
);
}
}

0 comments on commit 8a682e7

Please sign in to comment.