Skip to content

Commit 5eff05b

Browse files
shinghimyancyribbens
authored andcommitted
Implement Arbitrary for units types
1 parent 293b07a commit 5eff05b

4 files changed

Lines changed: 87 additions & 2 deletions

File tree

units/src/amount.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,21 @@ impl<'a> Arbitrary<'a> for Amount {
19241924
}
19251925
}
19261926

1927+
#[cfg(feature = "arbitrary")]
1928+
impl<'a> Arbitrary<'a> for Denomination {
1929+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
1930+
let choice = u.int_in_range(0..=5)?;
1931+
match choice {
1932+
0 => Ok(Denomination::Bitcoin),
1933+
1 => Ok(Denomination::CentiBitcoin),
1934+
2 => Ok(Denomination::MilliBitcoin),
1935+
3 => Ok(Denomination::MicroBitcoin),
1936+
4 => Ok(Denomination::Bit),
1937+
_ => Ok(Denomination::Satoshi),
1938+
}
1939+
}
1940+
}
1941+
19271942
#[cfg(feature = "arbitrary")]
19281943
impl<'a> Arbitrary<'a> for SignedAmount {
19291944
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {

units/src/fee_rate.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,14 @@ crate::impl_parse_str_from_int_infallible!(FeeRate, u64, from_sat_per_kwu);
157157
#[cfg(feature = "arbitrary")]
158158
impl<'a> Arbitrary<'a> for FeeRate {
159159
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
160-
let f = u64::arbitrary(u)?;
161-
Ok(FeeRate(f))
160+
let choice = u.int_in_range(0..=4)?;
161+
match choice {
162+
0 => Ok(FeeRate::MIN),
163+
1 => Ok(FeeRate::BROADCAST_MIN),
164+
2 => Ok(FeeRate::DUST),
165+
3 => Ok(FeeRate::MAX),
166+
_ => Ok(FeeRate(u64::arbitrary(u)?)),
167+
}
162168
}
163169
}
164170

units/src/locktime/absolute.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
use core::fmt;
66

7+
#[cfg(feature = "arbitrary")]
8+
use arbitrary::{Arbitrary, Unstructured};
79
use internals::write_err;
810

911
use crate::parse::{self, ParseIntError};
@@ -366,6 +368,40 @@ impl From<ConversionError> for ParseError {
366368
fn from(value: ConversionError) -> Self { Self::Conversion(value.input.into()) }
367369
}
368370

371+
#[cfg(feature = "arbitrary")]
372+
impl<'a> Arbitrary<'a> for Height {
373+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
374+
let choice = u.int_in_range(0..=2)?;
375+
match choice {
376+
0 => Ok(Height::MIN),
377+
1 => Ok(Height::MAX),
378+
_ => {
379+
let min = Height::MIN.to_consensus_u32();
380+
let max = Height::MAX.to_consensus_u32();
381+
let h = u.int_in_range(min..=max)?;
382+
Ok(Height::from_consensus(h).unwrap())
383+
}
384+
}
385+
}
386+
}
387+
388+
#[cfg(feature = "arbitrary")]
389+
impl<'a> Arbitrary<'a> for Time {
390+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
391+
let choice = u.int_in_range(0..=2)?;
392+
match choice {
393+
0 => Ok(Time::MIN),
394+
1 => Ok(Time::MAX),
395+
_ => {
396+
let min = Time::MIN.to_consensus_u32();
397+
let max = Time::MAX.to_consensus_u32();
398+
let t = u.int_in_range(min..=max)?;
399+
Ok(Time::from_consensus(t).unwrap())
400+
}
401+
}
402+
}
403+
}
404+
369405
#[cfg(test)]
370406
mod tests {
371407
use super::*;

units/src/locktime/relative.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
use core::fmt;
66

7+
#[cfg(feature = "arbitrary")]
8+
use arbitrary::{Arbitrary, Unstructured};
79
#[cfg(feature = "serde")]
810
use serde::{Deserialize, Serialize};
911

@@ -152,3 +154,29 @@ impl fmt::Display for TimeOverflowError {
152154

153155
#[cfg(feature = "std")]
154156
impl std::error::Error for TimeOverflowError {}
157+
158+
#[cfg(feature = "arbitrary")]
159+
impl<'a> Arbitrary<'a> for Height {
160+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
161+
let choice = u.int_in_range(0..=2)?;
162+
163+
match choice {
164+
0 => Ok(Height::MIN),
165+
1 => Ok(Height::MAX),
166+
_ => Ok(Height::from_height(u16::arbitrary(u)?)),
167+
}
168+
}
169+
}
170+
171+
#[cfg(feature = "arbitrary")]
172+
impl<'a> Arbitrary<'a> for Time {
173+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
174+
let choice = u.int_in_range(0..=2)?;
175+
176+
match choice {
177+
0 => Ok(Time::MIN),
178+
1 => Ok(Time::MAX),
179+
_ => Ok(Time::from_512_second_intervals(u16::arbitrary(u)?)),
180+
}
181+
}
182+
}

0 commit comments

Comments
 (0)