feat: add arbitrary support

adds arbitrary support to BigInt and BigUint
This commit is contained in:
aumetra 2022-11-18 17:47:25 +01:00 committed by GitHub
parent f6b46ffe32
commit 4e3ff9884c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 8 deletions

View File

@ -21,7 +21,9 @@ autobenches = false
[package.metadata.docs.rs]
features = ["std", "serde", "rand", "prime"]
[dependencies]
[dependencies.arbitrary]
version = "1.1.0"
optional = true
[dependencies.smallvec]
version = "1.0.0"
@ -82,6 +84,7 @@ version = "1.0"
[features]
default = ["std", "u64_digit"]
fuzz = ["arbitrary", "smallvec/arbitrary"]
i128 = []
std = [
"num-integer/std",

View File

@ -1,6 +1,6 @@
use core::cmp::Ordering;
use num_traits::{One, Zero};
use smallvec::SmallVec;
use core::cmp::Ordering;
use crate::algorithms::{add2, cmp_slice, sub2};
use crate::big_digit::{self, BigDigit, DoubleBigDigit};

View File

@ -3,9 +3,9 @@ use crate::bigint::Sign::*;
use crate::bigint::{BigInt, ToBigInt};
use crate::biguint::{BigUint, IntDigits};
use crate::integer::Integer;
use num_traits::{One, Signed, Zero};
use alloc::borrow::Cow;
use core::ops::Neg;
use num_traits::{One, Signed, Zero};
/// XGCD sets z to the greatest common divisor of a and b and returns z.
/// If extended is true, XGCD returns their value such that z = a*x + b*y.

View File

@ -97,5 +97,4 @@ mod tests {
assert_eq!(case[2] as isize, jacobi(&x, &y), "jacobi({}, {})", x, y);
}
}
}

View File

@ -3311,6 +3311,24 @@ impl<'a, 'b> ExtendedGcd<&'b BigUint> for &'a BigInt {
}
}
// arbitrary support
#[cfg(feature = "fuzz")]
impl arbitrary::Arbitrary<'_> for BigInt {
fn arbitrary(src: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let sign = if bool::arbitrary(src)? {
Sign::Plus
} else {
Sign::Minus
};
let data = BigUint::arbitrary(src)?;
Ok(Self::from_biguint(sign, data))
}
fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(BigUint::size_hint(depth), bool::size_hint(depth))
}
}
#[test]
fn test_from_biguint() {
fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) {

View File

@ -3382,3 +3382,16 @@ fn test_set_digit() {
assert_eq!(a.data.len(), 1);
assert_eq!(a.data[0], 4);
}
// arbitrary support
#[cfg(feature = "fuzz")]
impl arbitrary::Arbitrary<'_> for BigUint {
fn arbitrary(src: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let data = SmallVec::arbitrary(src)?;
Ok(Self { data })
}
fn size_hint(depth: usize) -> (usize, Option<usize>) {
SmallVec::<[BigDigit; VEC_SIZE]>::size_hint(depth)
}
}

View File

@ -1,8 +1,8 @@
#![allow(clippy::many_single_char_names)]
use num_traits::{One, Zero};
use core::ops::Shl;
use alloc::vec::Vec;
use core::ops::Shl;
use num_traits::{One, Zero};
use crate::big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
use crate::biguint::BigUint;

View File

@ -20,8 +20,7 @@ use std::{i128, u128};
use std::{u16, u32, u64, u8, usize};
use num_traits::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow,
ToPrimitive, Zero,
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, ToPrimitive, Zero,
};
use num_traits::float::FloatCore;