1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! Support for the [`arbitrary`](https://crates.io/crates/arbitrary) crate.
#![cfg(feature = "arbitrary")]
#![cfg_attr(has_doc_cfg, doc(cfg(feature = "arbitrary")))]
use crate::Uint;
use arbitrary::{Arbitrary, Result, Unstructured};

impl<'a, const BITS: usize, const LIMBS: usize> Arbitrary<'a> for Uint<BITS, LIMBS> {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let mut limbs = [0; LIMBS];
        if let Some((last, rest)) = limbs.split_last_mut() {
            for limb in rest.iter_mut() {
                *limb = u64::arbitrary(u)?;
            }
            *last = u.int_in_range(0..=Self::MASK)?;
        }
        Ok(Self::from_limbs(limbs))
    }

    fn size_hint(_depth: usize) -> (usize, Option<usize>) {
        let bytes = (BITS + 7) / 8;
        (bytes, Some(bytes))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{const_for, nlimbs};
    use std::iter::repeat;

    #[test]
    fn test_arbitrary() {
        const_for!(BITS in NON_ZERO {
            const LIMBS: usize = nlimbs(BITS);
            let (num_bytes, _) = Uint::<BITS, LIMBS>::size_hint(0);
            let bytes = repeat(0x55u8).take(num_bytes).collect::<Vec<_>>();
            let mut u = arbitrary::Unstructured::new(&bytes);
            Uint::<BITS, LIMBS>::arbitrary(&mut u).unwrap();
        });
    }
}