Skip to content

Commit

Permalink
Improve tests slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Sep 6, 2024
1 parent ae9fe3f commit 8a60c83
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions hil-test/tests/delay_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![no_std]
#![no_main]

use embedded_hal_async::delay::DelayNs as _;
use embedded_hal_async::delay::DelayNs;
use esp_hal::{
peripherals::Peripherals,
timer::{
Expand All @@ -22,6 +22,19 @@ struct Context {
peripherals: Peripherals,
}

async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) {
let t1 = esp_hal::time::current_time();
timer.delay_ns(duration).await;
let t2 = esp_hal::time::current_time();

assert!(t2 > t1);
assert!(
(t2 - t1).to_nanos() >= duration as u64,
"diff: {:?}",
(t2 - t1).to_nanos()
);
}

#[cfg(test)]
#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())]
mod tests {
Expand All @@ -39,35 +52,29 @@ mod tests {
async fn test_systimer_async_delay_ns(ctx: Context) {
let mut alarms = SystemTimer::new(ctx.peripherals.SYSTIMER);
let unit = FrozenUnit::new(&mut alarms.unit0);
let mut alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic();

let t1 = esp_hal::time::current_time();
alarm0.delay_ns(600_000_000).await;
let t2 = esp_hal::time::current_time();
let alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic();

assert!(t2 > t1);
assert!(
(t2 - t1).to_nanos() >= 600_000_000u64,
"diff: {:?}",
(t2 - t1).to_nanos()
);
test_async_delay_ns(alarm0, 600_000_000).await;
}

#[test]
#[timeout(2)]
async fn test_timg0_async_delay_ns(ctx: Context) {
let timg0 = TimerGroup::new_async(ctx.peripherals.TIMG0);
let mut timer0 = timg0.timer0;

let t1 = esp_hal::time::current_time();
timer0.delay_ns(600_000_000).await;
let t2 = esp_hal::time::current_time();
test_async_delay_ns(timg0.timer0, 600_000_000).await;
#[cfg(timg_timer1)]
test_async_delay_ns(timg0.timer1, 600_000_000).await;
}

#[cfg(timg1)]
#[test]
#[timeout(2)]
async fn test_timg1_async_delay_ns(ctx: Context) {
let timg1 = TimerGroup::new_async(ctx.peripherals.TIMG1);

assert!(t2 > t1);
assert!(
(t2 - t1).to_nanos() >= 600_000_000u64,
"diff: {:?}",
(t2 - t1).to_nanos()
);
test_async_delay_ns(timg1.timer0, 600_000_000).await;
#[cfg(timg_timer1)]
test_async_delay_ns(timg1.timer1, 600_000_000).await;
}
}

0 comments on commit 8a60c83

Please sign in to comment.