Implement Arbitrary for result types

Implement `Arbitrary` for the `NumOpResult` and `MathOp` types from the
`result` module.
This commit is contained in:
Tobin C. Harding 2025-06-19 12:37:36 +10:00
parent f034367bbc
commit a6ab5c9fd0
No known key found for this signature in database
GPG Key ID: 0AEF0A899E41F7DD
1 changed files with 28 additions and 0 deletions

View File

@ -5,6 +5,8 @@
use core::convert::Infallible;
use core::fmt;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use NumOpResult as R;
use crate::{Amount, FeeRate, SignedAmount, Weight};
@ -320,6 +322,32 @@ impl fmt::Display for MathOp {
}
}
#[cfg(feature = "arbitrary")]
impl<'a, T: Arbitrary<'a>> Arbitrary<'a> for NumOpResult<T> {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let choice = u.int_in_range(0..=1)?;
match choice {
0 => Ok(NumOpResult::Valid(T::arbitrary(u)?)),
_ => Ok(NumOpResult::Error(NumOpError(MathOp::arbitrary(u)?))),
}
}
}
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for MathOp {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let choice = u.int_in_range(0..=5)?;
match choice {
0 => Ok(MathOp::Add),
1 => Ok(MathOp::Sub),
2 => Ok(MathOp::Mul),
3 => Ok(MathOp::Div),
4 => Ok(MathOp::Rem),
_ => Ok(MathOp::Neg),
}
}
}
#[cfg(test)]
mod tests {
use crate::MathOp;