• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(any(feature = "quickcheck", feature = "arbitrary"))]
2 
3 use super::{BigInt, Sign};
4 use crate::BigUint;
5 
6 #[cfg(feature = "quickcheck")]
7 use alloc::boxed::Box;
8 
9 #[cfg(feature = "quickcheck")]
10 #[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
11 impl quickcheck::Arbitrary for BigInt {
arbitrary(g: &mut quickcheck::Gen) -> Self12     fn arbitrary(g: &mut quickcheck::Gen) -> Self {
13         let positive = bool::arbitrary(g);
14         let sign = if positive { Sign::Plus } else { Sign::Minus };
15         Self::from_biguint(sign, BigUint::arbitrary(g))
16     }
17 
shrink(&self) -> Box<dyn Iterator<Item = Self>>18     fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
19         let sign = self.sign();
20         let unsigned_shrink = self.data.shrink();
21         Box::new(unsigned_shrink.map(move |x| BigInt::from_biguint(sign, x)))
22     }
23 }
24 
25 #[cfg(feature = "arbitrary")]
26 #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
27 impl arbitrary::Arbitrary<'_> for BigInt {
arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self>28     fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
29         let positive = bool::arbitrary(u)?;
30         let sign = if positive { Sign::Plus } else { Sign::Minus };
31         Ok(Self::from_biguint(sign, BigUint::arbitrary(u)?))
32     }
33 
arbitrary_take_rest(mut u: arbitrary::Unstructured<'_>) -> arbitrary::Result<Self>34     fn arbitrary_take_rest(mut u: arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
35         let positive = bool::arbitrary(&mut u)?;
36         let sign = if positive { Sign::Plus } else { Sign::Minus };
37         Ok(Self::from_biguint(sign, BigUint::arbitrary_take_rest(u)?))
38     }
39 
size_hint(depth: usize) -> (usize, Option<usize>)40     fn size_hint(depth: usize) -> (usize, Option<usize>) {
41         arbitrary::size_hint::and(bool::size_hint(depth), BigUint::size_hint(depth))
42     }
43 }
44