Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
improves some line and region coverage
  • Loading branch information
2bndy5 committed Oct 29, 2024
1 parent fc29509 commit 6b84c93
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 82 deletions.
64 changes: 59 additions & 5 deletions crates/rf24-rs/src/radio/rf24/auto_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,16 @@ mod test {
vec![registers::DYNPD | commands::W_REGISTER, 3u8],
vec![0xEu8, 0u8],
),
// write_ack_payload()
// read FEATURE register
(
vec![registers::FEATURE, 0u8],
vec![0xEu8, mnemonics::EN_ACK_PAY | mnemonics::EN_DPL]
),
(
vec![registers::DYNPD | commands::W_REGISTER, 0x3Fu8],
vec![0xEu8, 0u8],
),
// write_ack_payload() and also marks TX FIFO as full
(ack_buf.to_vec(), vec![0u8; 3]),
// read dynamic payload length invalid value
(vec![commands::R_RX_PL_WID, 0u8], vec![0xEu8, 0xFFu8]),
Expand All @@ -139,22 +148,39 @@ mod test {
// set EN_AA register with pipe 0 disabled
(
vec![registers::EN_AA | commands::W_REGISTER, 0x3Eu8],
vec![0xEu8, 0x3Fu8],
vec![0xEu8, 0u8],
),
// read EN_AA register value
(vec![registers::EN_AA, 0u8], vec![0u8, 0x3Eu8]),
// set EN_AA register with pipe 0 disabled
(
vec![registers::EN_AA | commands::W_REGISTER, 0x3Cu8],
vec![0xEu8, 0u8],
),
];
let mut spi_mock = SpiMock::new(&spi_expectations);
let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
radio.allow_ack_payloads(true).unwrap();
let buf = [0x55; 2];
assert!(!radio.write_ack_payload(9, &buf).unwrap());
assert!(radio.write_ack_payload(2, &buf).unwrap());
// do again for region coverage (should result in Ok non-op)
radio.allow_ack_payloads(true).unwrap();
// explicitly enable dynamic payloads (again) for region coverage
radio.set_dynamic_payloads(true).unwrap();
let buf = &ack_buf[1..3];
// write ACK payload to invalid pipe (results in Ok non-op)
assert!(!radio.write_ack_payload(9, buf).unwrap());
// write ACK payload to valid pipe (test will also mark TX FIFO as full)
assert!(radio.write_ack_payload(2, buf).unwrap());
assert_eq!(
radio.get_dynamic_payload_length(),
Err(Nrf24Error::BinaryCorruption)
);
assert_eq!(radio.get_dynamic_payload_length().unwrap(), 32u8);
// disable invalid pipe number (results in Ok non-op)
radio.set_auto_ack_pipe(false, 9).unwrap();
// disable auto-ack on pipe 0 (also disables allow_ack_payloads)
radio.set_auto_ack_pipe(false, 0).unwrap();
// disable pipe 1 for region coverage
radio.set_auto_ack_pipe(false, 1).unwrap();
spi_mock.done();
pin_mock.done();
}
Expand Down Expand Up @@ -211,6 +237,34 @@ mod test {
pin_mock.done();
}

/* #[test]
fn set_auto_ack_pipe() {
// Create pin
let pin_expectations = [];
let mut pin_mock = PinMock::new(&pin_expectations);
// create delay fn
let delay_mock = NoopDelay::new();
let spi_expectations = spi_test_expects![
// read EN_AA register value
(
vec![registers::EN_AA, 0x3Fu8],
vec![0xEu8, 0u8],
),
// write EN_AA register value
(
vec![registers::EN_AA | commands::W_REGISTER, 0x3Eu8],
vec![0xEu8, 0u8],
),
];
let mut spi_mock = SpiMock::new(&spi_expectations);
let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
radio.set_auto_ack_pipe(false, 0).unwrap();
spi_mock.done();
pin_mock.done();
} */

#[test]
pub fn allow_ask_no_ack() {
// Create pin
Expand Down
27 changes: 27 additions & 0 deletions crates/rf24-rs/src/radio/rf24/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,30 @@ where
Ok(())
}
}

#[cfg(test)]
mod test {
use crate::radio::prelude::EsbDetails;
use crate::radio::RF24;
use embedded_hal_mock::eh1::delay::NoopDelay;
use embedded_hal_mock::eh1::digital::Mock as PinMock;
use embedded_hal_mock::eh1::spi::Mock as SpiMock;

#[test]
fn print_nothing() {
// Create pin
let pin_expectations = [];
let mut pin_mock = PinMock::new(&pin_expectations);

// create delay fn
let delay_mock = NoopDelay::new();

// create spi device
let spi_expectations = [];
let mut spi_mock = SpiMock::new(&spi_expectations);
let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
assert!(radio.print_details().is_ok());
pin_mock.done();
spi_mock.done();
}
}
112 changes: 69 additions & 43 deletions crates/rf24-rs/src/radio/rf24/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,11 @@ where
self.set_auto_retries(0, 0)?;
let buf = [0xFF; 32];

// use write_register() instead of openWritingPipe() to bypass
// use spi_write_buf() instead of open_tx_pipe() to bypass
// truncation of the address with the current RF24::addr_width value
self.spi_write_buf(registers::TX_ADDR, &buf[..5])?;
self.flush_tx()?; // so we can write to top level

// use write_register() instead of write_payload() to bypass
// truncation of the payload with the current RF24::payload_size value
self.spi_write_buf(commands::W_TX_PAYLOAD, &buf)?;

self.set_crc_length(CrcLength::Disabled)?;
Expand Down Expand Up @@ -241,15 +239,20 @@ mod test {
pin_mock.done();
}

#[test]
pub fn start_carrier_wave() {
pub fn start_carrier_wave_parametrized(is_plus_variant: bool) {
// Create pin
let pin_expectations = [
PinTransaction::set(PinState::Low),
PinTransaction::set(PinState::High),
let mut pin_expectations = [
PinTransaction::set(PinState::Low),
PinTransaction::set(PinState::High),
];
]
.to_vec();
if is_plus_variant {
pin_expectations.extend([
PinTransaction::set(PinState::Low),
PinTransaction::set(PinState::High),
]);
}

let mut pin_mock = PinMock::new(&pin_expectations);

// create delay fn
Expand All @@ -260,7 +263,7 @@ mod test {
let mut address = [0xFFu8; 6];
address[0] = registers::TX_ADDR | commands::W_REGISTER;

let spi_expectations = spi_test_expects![
let mut spi_expectations = spi_test_expects![
// as_tx()
// clear PRIM_RX flag
(
Expand All @@ -279,28 +282,35 @@ mod test {
vec![registers::RF_SETUP | commands::W_REGISTER, 0x90u8],
vec![0xEu8, 0u8],
),
// disable auto-ack
(
vec![registers::EN_AA | commands::W_REGISTER, 0u8],
vec![0xEu8, 0u8],
),
// disable auto-retries
(
vec![registers::SETUP_RETR | commands::W_REGISTER, 0u8],
vec![0xEu8, 0u8],
),
// set TX address
(address.to_vec(), vec![0u8; 6]),
// flush_tx()
(vec![commands::FLUSH_TX], vec![0xEu8]),
// set TX payload
(buf.to_vec(), vec![0u8; 33]),
// set_crc_length(disabled)
(vec![registers::CONFIG, 0u8], vec![0xEu8, 0xCu8]),
(
vec![registers::CONFIG | commands::W_REGISTER, 0u8],
vec![0xEu8, 0u8],
),
]
.to_vec();
if is_plus_variant {
spi_expectations.extend(spi_test_expects![
// disable auto-ack
(
vec![registers::EN_AA | commands::W_REGISTER, 0u8],
vec![0xEu8, 0u8],
),
// disable auto-retries
(
vec![registers::SETUP_RETR | commands::W_REGISTER, 0u8],
vec![0xEu8, 0u8],
),
// set TX address
(address.to_vec(), vec![0u8; 6]),
// flush_tx()
(vec![commands::FLUSH_TX], vec![0xEu8]),
// set TX payload
(buf.to_vec(), vec![0u8; 33]),
// set_crc_length(disabled)
(vec![registers::CONFIG, 0u8], vec![0xEu8, 0xCu8]),
(
vec![registers::CONFIG | commands::W_REGISTER, 0u8],
vec![0xEu8, 0u8],
),
]);
}
spi_expectations.extend(spi_test_expects![
// set_pa_level()
// set special flags in RF_SETUP register value
(vec![registers::RF_SETUP, 0u8], vec![0xEu8, 0x91u8]),
Expand All @@ -313,24 +323,40 @@ mod test {
vec![registers::RF_CH | commands::W_REGISTER, 125u8],
vec![0xEu8, 0u8],
),
// clear the tx_df and tx_ds events
(
vec![
registers::STATUS | commands::W_REGISTER,
mnemonics::MASK_MAX_RT | mnemonics::MASK_TX_DS,
],
vec![0xEu8, 0u8],
),
// assert the REUSE_TX_PL flag
(vec![commands::REUSE_TX_PL], vec![0xEu8]),
];
]);
if is_plus_variant {
spi_expectations.extend(spi_test_expects![
// clear the tx_df and tx_ds events
(
vec![
registers::STATUS | commands::W_REGISTER,
mnemonics::MASK_MAX_RT | mnemonics::MASK_TX_DS,
],
vec![0xEu8, 0u8],
),
// assert the REUSE_TX_PL flag
(vec![commands::REUSE_TX_PL], vec![0xEu8]),
]);
}

let mut spi_mock = SpiMock::new(&spi_expectations);
let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
radio._is_plus_variant = is_plus_variant;
radio.start_carrier_wave(crate::PaLevel::Max, 0xFF).unwrap();
spi_mock.done();
pin_mock.done();
}

#[test]
fn start_carrier_wave_plus_variant() {
start_carrier_wave_parametrized(true);
}

#[test]
fn start_carrier_wave_non_plus_variant() {
start_carrier_wave_parametrized(false);
}

#[test]
pub fn stop_carrier_wave() {
// Create pin
Expand Down
2 changes: 1 addition & 1 deletion crates/rf24-rs/src/radio/rf24/payload_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod test {
use embedded_hal_mock::eh1::spi::{Mock as SpiMock, Transaction as SpiTransaction};
use std::vec;

// dynamic payloads are already tested in EsbAutoAck trait as
// NOTE: dynamic payloads are already tested in EsbAutoAck trait as
// these features' behaviors are interdependent.

#[test]
Expand Down
Loading

0 comments on commit 6b84c93

Please sign in to comment.