Skip to content

Commit

Permalink
add tests for boxed link
Browse files Browse the repository at this point in the history
  • Loading branch information
s5suzuki committed Jan 29, 2025
1 parent a881d71 commit 071a9a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
25 changes: 7 additions & 18 deletions autd3/src/async/controller/sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ mod tests {

use super::*;

#[derive(Default)]
struct MockAsyncLink {
pub is_open: bool,
pub send_cnt: usize,
Expand Down Expand Up @@ -199,12 +200,8 @@ mod tests {

#[tokio::test]
async fn test_close() -> anyhow::Result<()> {
let mut link = MockAsyncLink {
is_open: true,
send_cnt: 0,
recv_cnt: 0,
down: false,
};
let mut link = MockAsyncLink::default();
link.open(&Geometry::new(Vec::new())).await?;

assert!(link.is_open());

Expand All @@ -222,16 +219,12 @@ mod tests {
#[cfg_attr(target_os = "windows", case(WaitableSleeper::new().unwrap()))]
#[tokio::test]
async fn test_send_receive(#[case] sleeper: impl AsyncSleep) {
let mut link = MockAsyncLink {
is_open: true,
send_cnt: 0,
recv_cnt: 0,
down: false,
};
let mut link = MockAsyncLink::default();
let mut geometry = create_geometry(1);
let mut tx = vec![];
let mut rx = Vec::new();

assert!(link.open(&geometry).await.is_ok());
let mut sender = Sender {
link: &mut link,
geometry: &mut geometry,
Expand Down Expand Up @@ -272,17 +265,13 @@ mod tests {
#[cfg_attr(target_os = "windows", case(WaitableSleeper::new().unwrap()))]
#[tokio::test]
async fn test_wait_msg_processed(#[case] sleeper: impl AsyncSleep) {
let mut link = MockAsyncLink {
is_open: true,
send_cnt: 0,
recv_cnt: 0,
down: false,
};
let mut link = MockAsyncLink::default();
let mut geometry = create_geometry(1);
let mut tx = vec![TxMessage::new_zeroed(); 1];
tx[0].header.msg_id = 2;
let mut rx = vec![RxMessage::new(0, 0)];

assert!(link.open(&geometry).await.is_ok());
let mut sender = Sender {
link: &mut link,
geometry: &mut geometry,
Expand Down
4 changes: 2 additions & 2 deletions autd3/src/controller/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ mod tests {
option: Default::default(),
},
),
)? // GRCOV_EXCL_LINE
)?
.send()?;

assert_eq!(
Expand Down Expand Up @@ -421,7 +421,7 @@ mod tests {
option: Default::default(),
},
),
)? // GRCOV_EXCL_LINE
)?
.send()?;

assert_eq!(
Expand Down
19 changes: 17 additions & 2 deletions autd3/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ pub(crate) mod tests {
config: 1. * Hz,
option: Default::default(),
},
))?; // GRCOV_EXCL_LINE
))?;

autd.iter().try_for_each(|dev| {
assert_eq!(
Expand Down Expand Up @@ -563,6 +563,21 @@ pub(crate) mod tests {
Ok(())
}

#[test]
fn with_boxed_link() -> anyhow::Result<()> {
let link: Box<dyn Link> = Box::new(Audit::new(AuditOption::default()));
let mut autd = Controller::open([AUTD3::default()], link)?;

autd.send(Sine {
freq: 150. * Hz,
option: Default::default(),
})?;

autd.close()?;

Ok(())
}

#[test]
fn into_boxed_link_unsafe() -> anyhow::Result<()> {
let option = SenderOption {
Expand Down Expand Up @@ -598,7 +613,7 @@ pub(crate) mod tests {
config: 1. * Hz,
option: Default::default(),
},
))?; // GRCOV_EXCL_LINE
))?;

let mut autd = unsafe { Controller::<Audit>::from_boxed_link(autd) };

Expand Down
25 changes: 7 additions & 18 deletions autd3/src/controller/sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ mod tests {
assert_eq!(expect, mode.is_parallel(num_devices, threshold));
}

#[derive(Default)]
struct MockLink {
pub is_open: bool,
pub send_cnt: usize,
Expand Down Expand Up @@ -271,12 +272,8 @@ mod tests {

#[test]
fn test_close() -> anyhow::Result<()> {
let mut link = MockLink {
is_open: true,
send_cnt: 0,
recv_cnt: 0,
down: false,
};
let mut link = MockLink::default();
link.open(&Geometry::new(Vec::new()))?;

assert!(link.is_open());

Expand All @@ -293,16 +290,12 @@ mod tests {
#[cfg_attr(target_os = "windows", case(WaitableSleeper::new().unwrap()))]
#[test]
fn test_send_receive(#[case] sleeper: impl Sleep) {
let mut link = MockLink {
is_open: true,
send_cnt: 0,
recv_cnt: 0,
down: false,
};
let mut link = MockLink::default();
let mut geometry = create_geometry(1);
let mut tx = vec![];
let mut rx = Vec::new();

assert!(link.open(&geometry).is_ok());
let mut sender = Sender {
link: &mut link,
geometry: &mut geometry,
Expand Down Expand Up @@ -342,17 +335,13 @@ mod tests {
#[cfg_attr(target_os = "windows", case(WaitableSleeper::new().unwrap()))]
#[test]
fn test_wait_msg_processed(#[case] sleeper: impl Sleep) {
let mut link = MockLink {
is_open: true,
send_cnt: 0,
recv_cnt: 0,
down: false,
};
let mut link = MockLink::default();
let mut geometry = create_geometry(1);
let mut tx = vec![TxMessage::new_zeroed(); 1];
tx[0].header.msg_id = 2;
let mut rx = vec![RxMessage::new(0, 0)];

assert!(link.open(&geometry).is_ok());
let mut sender = Sender {
link: &mut link,
geometry: &mut geometry,
Expand Down

0 comments on commit 071a9a8

Please sign in to comment.