feat: add arbitrary support
adds arbitrary support to BigInt and BigUint
This commit is contained in:
parent
f6b46ffe32
commit
4e3ff9884c
@ -21,7 +21,9 @@ autobenches = false
|
|||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = ["std", "serde", "rand", "prime"]
|
features = ["std", "serde", "rand", "prime"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies.arbitrary]
|
||||||
|
version = "1.1.0"
|
||||||
|
optional = true
|
||||||
|
|
||||||
[dependencies.smallvec]
|
[dependencies.smallvec]
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -82,6 +84,7 @@ version = "1.0"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std", "u64_digit"]
|
default = ["std", "u64_digit"]
|
||||||
|
fuzz = ["arbitrary", "smallvec/arbitrary"]
|
||||||
i128 = []
|
i128 = []
|
||||||
std = [
|
std = [
|
||||||
"num-integer/std",
|
"num-integer/std",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
use core::cmp::Ordering;
|
||||||
use num_traits::{One, Zero};
|
use num_traits::{One, Zero};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use core::cmp::Ordering;
|
|
||||||
|
|
||||||
use crate::algorithms::{add2, cmp_slice, sub2};
|
use crate::algorithms::{add2, cmp_slice, sub2};
|
||||||
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
|
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
|
||||||
|
@ -3,9 +3,9 @@ use crate::bigint::Sign::*;
|
|||||||
use crate::bigint::{BigInt, ToBigInt};
|
use crate::bigint::{BigInt, ToBigInt};
|
||||||
use crate::biguint::{BigUint, IntDigits};
|
use crate::biguint::{BigUint, IntDigits};
|
||||||
use crate::integer::Integer;
|
use crate::integer::Integer;
|
||||||
use num_traits::{One, Signed, Zero};
|
|
||||||
use alloc::borrow::Cow;
|
use alloc::borrow::Cow;
|
||||||
use core::ops::Neg;
|
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.
|
/// 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.
|
/// If extended is true, XGCD returns their value such that z = a*x + b*y.
|
||||||
|
@ -97,5 +97,4 @@ mod tests {
|
|||||||
assert_eq!(case[2] as isize, jacobi(&x, &y), "jacobi({}, {})", x, y);
|
assert_eq!(case[2] as isize, jacobi(&x, &y), "jacobi({}, {})", x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn test_from_biguint() {
|
fn test_from_biguint() {
|
||||||
fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) {
|
fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) {
|
||||||
|
@ -3382,3 +3382,16 @@ fn test_set_digit() {
|
|||||||
assert_eq!(a.data.len(), 1);
|
assert_eq!(a.data.len(), 1);
|
||||||
assert_eq!(a.data[0], 4);
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#![allow(clippy::many_single_char_names)]
|
#![allow(clippy::many_single_char_names)]
|
||||||
|
|
||||||
use num_traits::{One, Zero};
|
|
||||||
use core::ops::Shl;
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
use core::ops::Shl;
|
||||||
|
use num_traits::{One, Zero};
|
||||||
|
|
||||||
use crate::big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
|
use crate::big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
|
||||||
use crate::biguint::BigUint;
|
use crate::biguint::BigUint;
|
||||||
|
@ -20,8 +20,7 @@ use std::{i128, u128};
|
|||||||
use std::{u16, u32, u64, u8, usize};
|
use std::{u16, u32, u64, u8, usize};
|
||||||
|
|
||||||
use num_traits::{
|
use num_traits::{
|
||||||
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow,
|
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, ToPrimitive, Zero,
|
||||||
ToPrimitive, Zero,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use num_traits::float::FloatCore;
|
use num_traits::float::FloatCore;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user