Skip to content

Commit a29ff01

Browse files
committed
add tests
1 parent 6794e37 commit a29ff01

File tree

8 files changed

+66
-34
lines changed

8 files changed

+66
-34
lines changed

autd3-core/src/datagram/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub trait DatagramL: std::fmt::Debug {
3939
#[doc(hidden)]
4040
type G;
4141
#[doc(hidden)]
42-
type Error: std::error::Error;
42+
type Error;
4343

4444
#[doc(hidden)]
4545
fn operation_generator_with_loop_behavior(
@@ -52,17 +52,15 @@ pub trait DatagramL: std::fmt::Debug {
5252
) -> Result<Self::G, Self::Error>;
5353

5454
/// Returns the option of the datagram.
55-
fn option(&self) -> DatagramOption {
56-
DatagramOption::default()
57-
}
55+
fn option(&self) -> DatagramOption;
5856
}
5957

6058
/// [`DatagramS`] is a [`Datagram`] with [`Segment`].
6159
pub trait DatagramS: std::fmt::Debug {
6260
#[doc(hidden)]
6361
type G;
6462
#[doc(hidden)]
65-
type Error: std::error::Error;
63+
type Error;
6664

6765
#[doc(hidden)]
6866
fn operation_generator_with_segment(
@@ -74,9 +72,7 @@ pub trait DatagramS: std::fmt::Debug {
7472
) -> Result<Self::G, Self::Error>;
7573

7674
/// Returns the option of the datagram.
77-
fn option(&self) -> DatagramOption {
78-
DatagramOption::default()
79-
}
75+
fn option(&self) -> DatagramOption;
8076
}
8177

8278
impl<D: DatagramL> DatagramS for D {
@@ -109,7 +105,7 @@ pub trait Datagram: std::fmt::Debug {
109105
#[doc(hidden)]
110106
type G;
111107
#[doc(hidden)]
112-
type Error: std::error::Error;
108+
type Error;
113109

114110
#[doc(hidden)]
115111
fn operation_generator(

autd3-core/src/datagram/operation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub trait Operation: Send + Sync {
1212
#[doc(hidden)]
1313
pub struct NullOp;
1414

15+
// GRCOV_EXCL_START
1516
impl Operation for NullOp {
1617
type Error = std::convert::Infallible;
1718

@@ -33,3 +34,4 @@ impl Default for Box<dyn Operation<Error = std::convert::Infallible>> {
3334
Box::new(NullOp)
3435
}
3536
}
37+
// GRCOV_EXCL_STOP

autd3-core/src/datagram/tuple.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::geometry::Geometry;
44

55
use super::{Datagram, DatagramOption};
66

7+
#[derive(Debug, PartialEq)]
78
#[doc(hidden)]
89
pub struct CombinedOperationGenerator<O1, O2> {
910
pub o1: O1,
@@ -12,11 +13,7 @@ pub struct CombinedOperationGenerator<O1, O2> {
1213

1314
#[derive(Error, Debug, PartialEq)]
1415
#[doc(hidden)]
15-
pub enum CombinedError<E1, E2>
16-
where
17-
E1: std::error::Error,
18-
E2: std::error::Error,
19-
{
16+
pub enum CombinedError<E1, E2> {
2017
#[error("{0}")]
2118
E1(E1),
2219
#[error("{0}")]
@@ -27,8 +24,6 @@ impl<G1, G2, D1, D2, E1, E2> Datagram for (D1, D2)
2724
where
2825
D1: Datagram<G = G1, Error = E1>,
2926
D2: Datagram<G = G2, Error = E2>,
30-
E1: std::error::Error,
31-
E2: std::error::Error,
3227
{
3328
type G = CombinedOperationGenerator<D1::G, D2::G>;
3429
type Error = CombinedError<E1, E2>;
@@ -67,27 +62,57 @@ mod tests {
6762
use std::time::Duration;
6863

6964
#[derive(Debug)]
70-
pub struct NullDatagram {
65+
pub struct TestDatagram {
7166
pub option: DatagramOption,
67+
pub result: Result<(), ()>,
7268
}
7369

74-
impl Datagram for NullDatagram {
70+
impl Datagram for TestDatagram {
7571
type G = ();
76-
type Error = std::convert::Infallible;
72+
type Error = ();
7773

7874
fn operation_generator(
7975
self,
8076
_: &Geometry,
8177
_: &DatagramOption,
8278
) -> Result<Self::G, Self::Error> {
83-
Ok(())
79+
self.result
8480
}
8581

8682
fn option(&self) -> DatagramOption {
8783
self.option
8884
}
8985
}
9086

87+
#[rstest::rstest]
88+
#[case(Ok(CombinedOperationGenerator { o1: (), o2: () }), Ok(()), Ok(()))]
89+
#[case(Err(CombinedError::E1(())), Err(()), Ok(()))]
90+
#[case(Err(CombinedError::E2(())), Ok(()), Err(()))]
91+
#[test]
92+
fn operation_generator(
93+
#[case] expect: Result<CombinedOperationGenerator<(), ()>, CombinedError<(), ()>>,
94+
#[case] result1: Result<(), ()>,
95+
#[case] result2: Result<(), ()>,
96+
) {
97+
assert_eq!(
98+
expect,
99+
(
100+
TestDatagram {
101+
option: DatagramOption::default(),
102+
result: result1,
103+
},
104+
TestDatagram {
105+
option: DatagramOption::default(),
106+
result: result2,
107+
}
108+
)
109+
.operation_generator(
110+
&Geometry::new(Default::default()),
111+
&DatagramOption::default()
112+
)
113+
);
114+
}
115+
91116
#[rstest::rstest]
92117
#[case(
93118
Duration::from_millis(200),
@@ -104,17 +129,19 @@ mod tests {
104129
assert_eq!(
105130
expect,
106131
(
107-
NullDatagram {
132+
TestDatagram {
108133
option: DatagramOption {
109134
timeout: timeout1,
110135
parallel_threshold: 0,
111136
},
137+
result: Ok(()),
112138
},
113-
NullDatagram {
139+
TestDatagram {
114140
option: DatagramOption {
115141
timeout: timeout2,
116142
parallel_threshold: 0,
117143
},
144+
result: Ok(()),
118145
}
119146
)
120147
.option()
@@ -134,17 +161,19 @@ mod tests {
134161
assert_eq!(
135162
expect,
136163
(
137-
NullDatagram {
164+
TestDatagram {
138165
option: DatagramOption {
139166
timeout: Duration::ZERO,
140167
parallel_threshold: threshold1,
141168
},
169+
result: Ok(()),
142170
},
143-
NullDatagram {
171+
TestDatagram {
144172
option: DatagramOption {
145173
timeout: Duration::ZERO,
146174
parallel_threshold: threshold2,
147175
},
176+
result: Ok(()),
148177
}
149178
)
150179
.option()

autd3-core/src/gain/phase.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ impl Phase {
2727
}
2828
}
2929

30-
impl From<u8> for Phase {
31-
fn from(v: u8) -> Self {
32-
Self(v)
33-
}
34-
}
35-
3630
impl From<Angle> for Phase {
3731
fn from(v: Angle) -> Self {
3832
Self((((v.radian() / (2.0 * PI) * 256.0).round() as i32) & 0xFF) as _)

autd3-core/src/geometry/transducer.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ mod tests {
4646
};
4747
}
4848

49-
#[rstest::fixture]
50-
fn tr() -> Transducer {
51-
Transducer::new(0, 0, Point3::origin())
49+
#[test]
50+
fn idx() {
51+
let tr = Transducer::new(1, 2, Point3::origin());
52+
assert_eq!(1, tr.idx());
53+
assert_eq!(2, tr.dev_idx());
5254
}
5355

5456
#[rstest::rstest]
5557
#[test]
56-
fn affine(mut tr: Transducer) {
58+
fn affine() {
59+
let mut tr = Transducer::new(0, 0, Point3::origin());
60+
5761
let vector = Vector3::new(40., 50., 60.);
5862
let rotation = UnitQuaternion::from_axis_angle(&Vector3::x_axis(), 0.)
5963
* UnitQuaternion::from_axis_angle(&Vector3::y_axis(), 0.)

autd3-core/src/modulation/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub enum SamplingConfigError {
3939
SamplingPeriodOutOfRange(Duration, Duration, Duration),
4040
}
4141

42+
// GRCOV_EXCL_START
4243
impl From<Infallible> for SamplingConfigError {
4344
fn from(_: Infallible) -> Self {
4445
unreachable!()
@@ -56,3 +57,4 @@ impl From<SamplingConfigError> for ModulationError {
5657
Self::new(e.to_string())
5758
}
5859
}
60+
// GRCOV_EXCL_STOP

autd3-core/src/modulation/sampling_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ mod tests {
252252
#[case(Ok(10), 4000 * Hz)]
253253
#[case(Ok(1), 40000. * Hz)]
254254
#[case(Ok(10), 4000. * Hz)]
255+
#[case(Err(SamplingConfigError::SamplingDivisionInvalid), 0)]
255256
#[case(Err(SamplingConfigError::SamplingFreqInvalid(ultrasound_freq() - 1 * Hz)), ultrasound_freq() - 1 * Hz)]
256257
#[case(Err(SamplingConfigError::SamplingFreqOutOfRange(0 * Hz, 1 * Hz, ultrasound_freq())), 0 * Hz)]
257258
#[case(Err(SamplingConfigError::SamplingFreqOutOfRange(ultrasound_freq() + 1 * Hz, 1 * Hz, ultrasound_freq())), ultrasound_freq() + 1 * Hz)]

autd3-derive/src/modulation.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pub(crate) fn impl_mod_macro(input: syn::DeriveInput) -> TokenStream {
2525
transition_mode,
2626
})
2727
}
28+
29+
fn option(&self) -> DatagramOption {
30+
DatagramOption::default()
31+
}
2832
}
2933
};
3034

0 commit comments

Comments
 (0)