chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

1234
vendor/num-bigint-dig/tests/bigint.rs vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,181 @@
extern crate num_bigint_dig as num_bigint;
extern crate num_traits;
use crate::num_bigint::{BigInt, Sign, ToBigInt};
use num_traits::ToPrimitive;
use std::{i32, i64, u32};
enum ValueVec {
N,
P(&'static [u32]),
M(&'static [u32]),
}
use crate::ValueVec::*;
impl ToBigInt for ValueVec {
fn to_bigint(&self) -> Option<BigInt> {
match self {
&N => Some(BigInt::from_slice(Sign::NoSign, &[])),
&P(s) => Some(BigInt::from_slice(Sign::Plus, s)),
&M(s) => Some(BigInt::from_slice(Sign::Minus, s)),
}
}
}
// a, !a
const NOT_VALUES: &'static [(ValueVec, ValueVec)] = &[
(N, M(&[1])),
(P(&[1]), M(&[2])),
(P(&[2]), M(&[3])),
(P(&[!0 - 2]), M(&[!0 - 1])),
(P(&[!0 - 1]), M(&[!0])),
(P(&[!0]), M(&[0, 1])),
(P(&[0, 1]), M(&[1, 1])),
(P(&[1, 1]), M(&[2, 1])),
];
// a, b, a & b, a | b, a ^ b
const BITWISE_VALUES: &'static [(ValueVec, ValueVec, ValueVec, ValueVec, ValueVec)] = &[
(N, N, N, N, N),
(N, P(&[1]), N, P(&[1]), P(&[1])),
(N, P(&[!0]), N, P(&[!0]), P(&[!0])),
(N, P(&[0, 1]), N, P(&[0, 1]), P(&[0, 1])),
(N, M(&[1]), N, M(&[1]), M(&[1])),
(N, M(&[!0]), N, M(&[!0]), M(&[!0])),
(N, M(&[0, 1]), N, M(&[0, 1]), M(&[0, 1])),
(P(&[1]), P(&[!0]), P(&[1]), P(&[!0]), P(&[!0 - 1])),
(P(&[!0]), P(&[!0]), P(&[!0]), P(&[!0]), N),
(P(&[!0]), P(&[1, 1]), P(&[1]), P(&[!0, 1]), P(&[!0 - 1, 1])),
(P(&[1]), M(&[!0]), P(&[1]), M(&[!0]), M(&[0, 1])),
(P(&[!0]), M(&[1]), P(&[!0]), M(&[1]), M(&[0, 1])),
(P(&[!0]), M(&[!0]), P(&[1]), M(&[1]), M(&[2])),
(P(&[!0]), M(&[1, 1]), P(&[!0]), M(&[1, 1]), M(&[0, 2])),
(P(&[1, 1]), M(&[!0]), P(&[1, 1]), M(&[!0]), M(&[0, 2])),
(M(&[1]), M(&[!0]), M(&[!0]), M(&[1]), P(&[!0 - 1])),
(M(&[!0]), M(&[!0]), M(&[!0]), M(&[!0]), N),
(M(&[!0]), M(&[1, 1]), M(&[!0, 1]), M(&[1]), P(&[!0 - 1, 1])),
];
const I32_MIN: i64 = i32::MIN as i64;
const I32_MAX: i64 = i32::MAX as i64;
const U32_MAX: i64 = u32::MAX as i64;
// some corner cases
const I64_VALUES: &'static [i64] = &[
i64::MIN,
i64::MIN + 1,
i64::MIN + 2,
i64::MIN + 3,
-U32_MAX - 3,
-U32_MAX - 2,
-U32_MAX - 1,
-U32_MAX,
-U32_MAX + 1,
-U32_MAX + 2,
-U32_MAX + 3,
I32_MIN - 3,
I32_MIN - 2,
I32_MIN - 1,
I32_MIN,
I32_MIN + 1,
I32_MIN + 2,
I32_MIN + 3,
-3,
-2,
-1,
0,
1,
2,
3,
I32_MAX - 3,
I32_MAX - 2,
I32_MAX - 1,
I32_MAX,
I32_MAX + 1,
I32_MAX + 2,
I32_MAX + 3,
U32_MAX - 3,
U32_MAX - 2,
U32_MAX - 1,
U32_MAX,
U32_MAX + 1,
U32_MAX + 2,
U32_MAX + 3,
i64::MAX - 3,
i64::MAX - 2,
i64::MAX - 1,
i64::MAX,
];
#[test]
fn test_not() {
for &(ref a, ref not) in NOT_VALUES.iter() {
let a = a.to_bigint().unwrap();
let not = not.to_bigint().unwrap();
// sanity check for tests that fit in i64
if let (Some(prim_a), Some(prim_not)) = (a.to_i64(), not.to_i64()) {
assert_eq!(!prim_a, prim_not);
}
assert_eq!(!a.clone(), not, "!{:x}", a);
assert_eq!(!not.clone(), a, "!{:x}", not);
}
}
#[test]
fn test_not_i64() {
for &prim_a in I64_VALUES.iter() {
let a = prim_a.to_bigint().unwrap();
let not = (!prim_a).to_bigint().unwrap();
assert_eq!(!a.clone(), not, "!{:x}", a);
}
}
#[test]
fn test_bitwise() {
for &(ref a, ref b, ref and, ref or, ref xor) in BITWISE_VALUES.iter() {
let a = a.to_bigint().unwrap();
let b = b.to_bigint().unwrap();
let and = and.to_bigint().unwrap();
let or = or.to_bigint().unwrap();
let xor = xor.to_bigint().unwrap();
// sanity check for tests that fit in i64
if let (Some(prim_a), Some(prim_b)) = (a.to_i64(), b.to_i64()) {
if let Some(prim_and) = and.to_i64() {
assert_eq!(prim_a & prim_b, prim_and);
}
if let Some(prim_or) = or.to_i64() {
assert_eq!(prim_a | prim_b, prim_or);
}
if let Some(prim_xor) = xor.to_i64() {
assert_eq!(prim_a ^ prim_b, prim_xor);
}
}
assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
assert_eq!(b.clone() & &a, and, "{:x} & {:x}", b, a);
assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
assert_eq!(b.clone() | &a, or, "{:x} | {:x}", b, a);
assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
assert_eq!(b.clone() ^ &a, xor, "{:x} ^ {:x}", b, a);
}
}
#[test]
fn test_bitwise_i64() {
for &prim_a in I64_VALUES.iter() {
let a = prim_a.to_bigint().unwrap();
for &prim_b in I64_VALUES.iter() {
let b = prim_b.to_bigint().unwrap();
let and = (prim_a & prim_b).to_bigint().unwrap();
let or = (prim_a | prim_b).to_bigint().unwrap();
let xor = (prim_a ^ prim_b).to_bigint().unwrap();
assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
}
}
}

View File

@@ -0,0 +1,145 @@
extern crate num_bigint_dig as num_bigint;
extern crate num_traits;
use crate::num_bigint::BigInt;
use crate::num_bigint::Sign::Plus;
use num_traits::{Signed, ToPrimitive, Zero};
use std::ops::Neg;
mod consts;
use crate::consts::*;
#[macro_use]
mod macros;
#[test]
fn test_scalar_add() {
fn check(x: &BigInt, y: &BigInt, z: &BigInt) {
let (x, y, z) = (x.clone(), y.clone(), z.clone());
assert_signed_scalar_op!(x + y == z);
}
for elm in SUM_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigInt::from_slice(Plus, a_vec);
let b = BigInt::from_slice(Plus, b_vec);
let c = BigInt::from_slice(Plus, c_vec);
let (na, nb, nc) = (-&a, -&b, -&c);
check(&a, &b, &c);
check(&b, &a, &c);
check(&c, &na, &b);
check(&c, &nb, &a);
check(&a, &nc, &nb);
check(&b, &nc, &na);
check(&na, &nb, &nc);
check(&a, &na, &Zero::zero());
}
}
#[test]
fn test_scalar_sub() {
fn check(x: &BigInt, y: &BigInt, z: &BigInt) {
let (x, y, z) = (x.clone(), y.clone(), z.clone());
assert_signed_scalar_op!(x - y == z);
}
for elm in SUM_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigInt::from_slice(Plus, a_vec);
let b = BigInt::from_slice(Plus, b_vec);
let c = BigInt::from_slice(Plus, c_vec);
let (na, nb, nc) = (-&a, -&b, -&c);
check(&c, &a, &b);
check(&c, &b, &a);
check(&nb, &a, &nc);
check(&na, &b, &nc);
check(&b, &na, &c);
check(&a, &nb, &c);
check(&nc, &na, &nb);
check(&a, &a, &Zero::zero());
}
}
#[test]
fn test_scalar_mul() {
fn check(x: &BigInt, y: &BigInt, z: &BigInt) {
let (x, y, z) = (x.clone(), y.clone(), z.clone());
assert_signed_scalar_op!(x * y == z);
}
for elm in MUL_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigInt::from_slice(Plus, a_vec);
let b = BigInt::from_slice(Plus, b_vec);
let c = BigInt::from_slice(Plus, c_vec);
let (na, nb, nc) = (-&a, -&b, -&c);
check(&a, &b, &c);
check(&b, &a, &c);
check(&na, &nb, &c);
check(&na, &b, &nc);
check(&nb, &a, &nc);
}
}
#[test]
fn test_scalar_div_rem() {
fn check_sub(a: &BigInt, b: u32, ans_q: &BigInt, ans_r: &BigInt) {
let (q, r) = (a / b, a % b);
if !r.is_zero() {
assert_eq!(r.sign(), a.sign());
}
assert!(r.abs() <= From::from(b));
assert!(*a == b * &q + &r);
assert!(q == *ans_q);
assert!(r == *ans_r);
let (a, b, ans_q, ans_r) = (a.clone(), b.clone(), ans_q.clone(), ans_r.clone());
assert_op!(a / b == ans_q);
assert_op!(a % b == ans_r);
if b <= i32::max_value() as u32 {
let nb = -(b as i32);
assert_op!(a / nb == -ans_q.clone());
assert_op!(a % nb == ans_r);
}
}
fn check(a: &BigInt, b: u32, q: &BigInt, r: &BigInt) {
check_sub(a, b, q, r);
check_sub(&a.neg(), b, &q.neg(), &r.neg());
}
for elm in MUL_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigInt::from_slice(Plus, a_vec);
let b = BigInt::from_slice(Plus, b_vec);
let c = BigInt::from_slice(Plus, c_vec);
if a_vec.len() == 1 && a_vec[0] != 0 {
let a = a_vec[0];
check(&c, a, &b, &Zero::zero());
}
if b_vec.len() == 1 && b_vec[0] != 0 {
let b = b_vec[0];
check(&c, b, &a, &Zero::zero());
}
}
for elm in DIV_REM_QUADRUPLES.iter() {
let (a_vec, b_vec, c_vec, d_vec) = *elm;
let a = BigInt::from_slice(Plus, a_vec);
let c = BigInt::from_slice(Plus, c_vec);
let d = BigInt::from_slice(Plus, d_vec);
if b_vec.len() == 1 && b_vec[0] != 0 {
let b = b_vec[0];
check(&a, b, &c, &d);
}
}
}

1714
vendor/num-bigint-dig/tests/biguint.rs vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,103 @@
extern crate num_bigint_dig as num_bigint;
extern crate num_traits;
use crate::num_bigint::BigUint;
use num_traits::{ToPrimitive, Zero};
mod consts;
use crate::consts::*;
#[macro_use]
mod macros;
#[test]
fn test_scalar_add() {
fn check(x: &BigUint, y: &BigUint, z: &BigUint) {
let (x, y, z) = (x.clone(), y.clone(), z.clone());
assert_unsigned_scalar_op!(x + y == z);
}
for elm in SUM_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigUint::from_slice(a_vec);
let b = BigUint::from_slice(b_vec);
let c = BigUint::from_slice(c_vec);
check(&a, &b, &c);
check(&b, &a, &c);
}
}
#[test]
fn test_scalar_sub() {
fn check(x: &BigUint, y: &BigUint, z: &BigUint) {
let (x, y, z) = (x.clone(), y.clone(), z.clone());
assert_unsigned_scalar_op!(x - y == z);
}
for elm in SUM_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigUint::from_slice(a_vec);
let b = BigUint::from_slice(b_vec);
let c = BigUint::from_slice(c_vec);
check(&c, &a, &b);
check(&c, &b, &a);
}
}
#[test]
fn test_scalar_mul() {
fn check(x: &BigUint, y: &BigUint, z: &BigUint) {
let (x, y, z) = (x.clone(), y.clone(), z.clone());
assert_unsigned_scalar_op!(x * y == z);
}
for elm in MUL_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigUint::from_slice(a_vec);
let b = BigUint::from_slice(b_vec);
let c = BigUint::from_slice(c_vec);
check(&a, &b, &c);
check(&b, &a, &c);
}
}
#[test]
fn test_scalar_div_rem() {
fn check(x: &BigUint, y: &BigUint, z: &BigUint, r: &BigUint) {
let (x, y, z, r) = (x.clone(), y.clone(), z.clone(), r.clone());
assert_unsigned_scalar_op!(x / y == z);
assert_unsigned_scalar_op!(x % y == r);
}
for elm in MUL_TRIPLES.iter() {
let (a_vec, b_vec, c_vec) = *elm;
let a = BigUint::from_slice(a_vec);
let b = BigUint::from_slice(b_vec);
let c = BigUint::from_slice(c_vec);
if !a.is_zero() {
check(&c, &a, &b, &Zero::zero());
}
if !b.is_zero() {
check(&c, &b, &a, &Zero::zero());
}
}
for elm in DIV_REM_QUADRUPLES.iter() {
let (a_vec, b_vec, c_vec, d_vec) = *elm;
let a = BigUint::from_slice(a_vec);
let b = BigUint::from_slice(b_vec);
let c = BigUint::from_slice(c_vec);
let d = BigUint::from_slice(d_vec);
if !b.is_zero() {
check(&a, &b, &c, &d);
assert_unsigned_scalar_op!(a / b == c);
assert_unsigned_scalar_op!(a % b == d);
}
}
}

View File

@@ -0,0 +1,56 @@
#![allow(unused)]
pub const N1: u32 = -1i32 as u32;
pub const N2: u32 = -2i32 as u32;
pub const SUM_TRIPLES: &'static [(&'static [u32], &'static [u32], &'static [u32])] = &[
(&[], &[], &[]),
(&[], &[1], &[1]),
(&[1], &[1], &[2]),
(&[1], &[1, 1], &[2, 1]),
(&[1], &[N1], &[0, 1]),
(&[1], &[N1, N1], &[0, 0, 1]),
(&[N1, N1], &[N1, N1], &[N2, N1, 1]),
(&[1, 1, 1], &[N1, N1], &[0, 1, 2]),
(&[2, 2, 1], &[N1, N2], &[1, 1, 2]),
(&[1, 2, 2, 1], &[N1, N2], &[0, 1, 3, 1]),
];
pub const M: u32 = ::std::u32::MAX;
pub const MUL_TRIPLES: &'static [(&'static [u32], &'static [u32], &'static [u32])] = &[
(&[], &[], &[]),
(&[], &[1], &[]),
(&[2], &[], &[]),
(&[1], &[1], &[1]),
(&[2], &[3], &[6]),
(&[1], &[1, 1, 1], &[1, 1, 1]),
(&[1, 2, 3], &[3], &[3, 6, 9]),
(&[1, 1, 1], &[N1], &[N1, N1, N1]),
(&[1, 2, 3], &[N1], &[N1, N2, N2, 2]),
(&[1, 2, 3, 4], &[N1], &[N1, N2, N2, N2, 3]),
(&[N1], &[N1], &[1, N2]),
(&[N1, N1], &[N1], &[1, N1, N2]),
(&[N1, N1, N1], &[N1], &[1, N1, N1, N2]),
(&[N1, N1, N1, N1], &[N1], &[1, N1, N1, N1, N2]),
(&[M / 2 + 1], &[2], &[0, 1]),
(&[0, M / 2 + 1], &[2], &[0, 0, 1]),
(&[1, 2], &[1, 2, 3], &[1, 4, 7, 6]),
(&[N1, N1], &[N1, N1, N1], &[1, 0, N1, N2, N1]),
(&[N1, N1, N1], &[N1, N1, N1, N1], &[1, 0, 0, N1, N2, N1, N1]),
(&[0, 0, 1], &[1, 2, 3], &[0, 0, 1, 2, 3]),
(&[0, 0, 1], &[0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]),
];
pub const DIV_REM_QUADRUPLES: &'static [(
&'static [u32],
&'static [u32],
&'static [u32],
&'static [u32],
)] = &[
(&[1], &[2], &[], &[1]),
(&[3], &[2], &[1], &[1]),
(&[1, 1], &[2], &[M / 2 + 1], &[1]),
(&[1, 1, 1], &[2], &[M / 2 + 1, M / 2 + 1], &[1]),
(&[0, 1], &[N1], &[1], &[1]),
(&[N1, N1], &[N2], &[2, 1], &[3]),
];

View File

@@ -0,0 +1,70 @@
#![allow(unused)]
/// Assert that an op works for all val/ref combinations
macro_rules! assert_op {
($left:ident $op:tt $right:ident == $expected:expr) => {
assert_eq!((&$left) $op (&$right), $expected);
assert_eq!((&$left) $op $right.clone(), $expected);
assert_eq!($left.clone() $op (&$right), $expected);
assert_eq!($left.clone() $op $right.clone(), $expected);
};
}
/// Assert that an assign-op works for all val/ref combinations
macro_rules! assert_assign_op {
($left:ident $op:tt $right:ident == $expected:expr) => {{
let mut left = $left.clone();
assert_eq!({ left $op &$right; left}, $expected);
let mut left = $left.clone();
assert_eq!({ left $op $right.clone(); left}, $expected);
}};
}
/// Assert that an op works for scalar left or right
macro_rules! assert_scalar_op {
(($($to:ident),*) $left:ident $op:tt $right:ident == $expected:expr) => {
$(
if let Some(left) = $left.$to() {
assert_op!(left $op $right == $expected);
}
if let Some(right) = $right.$to() {
assert_op!($left $op right == $expected);
}
)*
};
}
#[cfg(not(has_i128))]
macro_rules! assert_unsigned_scalar_op {
($left:ident $op:tt $right:ident == $expected:expr) => {
assert_scalar_op!((to_u8, to_u16, to_u32, to_u64, to_usize)
$left $op $right == $expected);
};
}
#[cfg(has_i128)]
macro_rules! assert_unsigned_scalar_op {
($left:ident $op:tt $right:ident == $expected:expr) => {
assert_scalar_op!((to_u8, to_u16, to_u32, to_u64, to_usize, to_u128)
$left $op $right == $expected);
};
}
#[cfg(not(has_i128))]
macro_rules! assert_signed_scalar_op {
($left:ident $op:tt $right:ident == $expected:expr) => {
assert_scalar_op!((to_u8, to_u16, to_u32, to_u64, to_usize,
to_i8, to_i16, to_i32, to_i64, to_isize)
$left $op $right == $expected);
};
}
#[cfg(has_i128)]
macro_rules! assert_signed_scalar_op {
($left:ident $op:tt $right:ident == $expected:expr) => {
assert_scalar_op!((to_u8, to_u16, to_u32, to_u64, to_usize, to_u128,
to_i8, to_i16, to_i32, to_i64, to_isize, to_i128)
$left $op $right == $expected);
};
}

160
vendor/num-bigint-dig/tests/modpow.rs vendored Normal file

File diff suppressed because one or more lines are too long

467
vendor/num-bigint-dig/tests/rand.rs vendored Normal file
View File

@@ -0,0 +1,467 @@
#![cfg(feature = "rand")]
extern crate num_bigint_dig as num_bigint;
extern crate num_traits;
extern crate rand;
extern crate rand_chacha;
extern crate rand_isaac;
extern crate rand_xorshift;
mod biguint {
use crate::num_bigint::{BigUint, RandBigInt, RandomBits};
use num_traits::Zero;
use rand::distributions::Uniform;
use rand::{Rng, SeedableRng};
#[cfg(feature = "std")]
fn thread_rng() -> impl Rng {
rand::thread_rng()
}
#[cfg(not(feature = "std"))]
fn thread_rng() -> impl Rng {
// Chosen by fair dice roll
rand::rngs::StdRng::seed_from_u64(4)
}
#[test]
fn test_rand() {
let mut rng = thread_rng();
let n: BigUint = rng.gen_biguint(137);
assert!(n.bits() <= 137);
assert!(rng.gen_biguint(0).is_zero());
}
#[test]
fn test_rand_bits() {
let mut rng = thread_rng();
let n: BigUint = rng.sample(&RandomBits::new(137));
assert!(n.bits() <= 137);
let z: BigUint = rng.sample(&RandomBits::new(0));
assert!(z.is_zero());
}
#[test]
fn test_rand_range() {
let mut rng = thread_rng();
for _ in 0..10 {
assert_eq!(
rng.gen_biguint_range(&BigUint::from(236u32), &BigUint::from(237u32)),
BigUint::from(236u32)
);
}
let l = BigUint::from(403469000u32 + 2352);
let u = BigUint::from(403469000u32 + 3513);
for _ in 0..1000 {
let n: BigUint = rng.gen_biguint_below(&u);
assert!(n < u);
let n: BigUint = rng.gen_biguint_range(&l, &u);
assert!(n >= l);
assert!(n < u);
}
}
#[test]
#[should_panic]
fn test_zero_rand_range() {
thread_rng().gen_biguint_range(&BigUint::from(54u32), &BigUint::from(54u32));
}
#[test]
#[should_panic]
fn test_negative_rand_range() {
let mut rng = thread_rng();
let l = BigUint::from(2352u32);
let u = BigUint::from(3513u32);
// Switching u and l should fail:
let _n: BigUint = rng.gen_biguint_range(&u, &l);
}
#[test]
fn test_rand_uniform() {
let mut rng = thread_rng();
let tiny = Uniform::new(BigUint::from(236u32), BigUint::from(237u32));
for _ in 0..10 {
assert_eq!(rng.sample(&tiny), BigUint::from(236u32));
}
let l = BigUint::from(403469000u32 + 2352);
let u = BigUint::from(403469000u32 + 3513);
let below = Uniform::new(BigUint::zero(), u.clone());
let range = Uniform::new(l.clone(), u.clone());
for _ in 0..1000 {
let n: BigUint = rng.sample(&below);
assert!(n < u);
let n: BigUint = rng.sample(&range);
assert!(n >= l);
assert!(n < u);
}
}
fn seeded_value_stability<R: SeedableRng + RandBigInt>(expected: &[&str]) {
let mut seed = <R::Seed>::default();
for (i, x) in seed.as_mut().iter_mut().enumerate() {
*x = (i as u8).wrapping_mul(191);
}
let mut rng = R::from_seed(seed);
for (i, &s) in expected.iter().enumerate() {
let n: BigUint = s.parse().unwrap();
let r = rng.gen_biguint((1 << i) + i);
assert_eq!(n, r, "expected {}, got {}", n, r);
}
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"0",
"52",
"84",
"23780",
"86502865016",
"187057847319509867386",
"34045731223080904464438757488196244981910",
"23813754422987836414755953516143692594193066497413249270287126597896871975915808",
"57401636903146945411652549098818446911814352529449356393690984105383482703074355\
67088360974672291353736011718191813678720755501317478656550386324355699624671",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"0",
"8",
"1861",
"172076",
"5194801951",
"259202797457072892019",
"2806086822955830608275100562233284760859",
"28771276448190704455825316568337256462972770861366848469339788407170414346460023",
"501572804396703231264118826164515310701005506447150057229826006447721882571235378\
4765127362270091441643338804096337494157874113908470083557122824480944132407",
];
#[test]
fn test_chacha_value_stability() {
use rand_chacha::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED_CHACHA);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_ISAAC: &[&str] = &[
"1",
"4",
"3",
"649",
"89116",
"7730042024",
"20773149082453254949",
"35999009049239918667571895439206839620281",
"10191757312714088681302309313551624007714035309632506837271600807524767413673006",
"37805949268912387809989378008822038725134260145886913321084097194957861133272558\
43458183365174899239251448892645546322463253898288141861183340823194379722556",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_ISAAC: &[&str] = &[
"1",
"2",
"51",
"1198",
"29707",
"35688018574",
"365090200586541225112",
"14051533166427520604648370582738617763816",
"26319846996091585801307964353534339679417889504909644767909019559631059772127122",
"14567336733062747693583250833667292276083519237160662196899060257293814346680656\
30951609693408423310563908301065751714778956255122249041917698392245727713420",
];
#[test]
fn test_isaac_value_stability() {
use rand_isaac::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED_ISAAC);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_XOR: &[&str] = &[
"1",
"0",
"37",
"395",
"181116",
"122718231117",
"1068467172329355695001",
"28246925743544411614293300167064395633287",
"12750053187017853048648861493745244146555950255549630854523304068318587267293038",
"53041498719137109355568081064978196049094604705283682101683207799515709404788873\
53417136457745727045473194367732849819278740266658219147356315674940229288531",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_XOR: &[&str] = &[
"0",
"1",
"36",
"970",
"940965",
"61158366130",
"590484965100191554896",
"34050066418951688801044382442803594076612",
"29147581645599998811521651062569705291155276949983132826461704326818089074318948",
"4990842894093964353439376569956547459232523176881032246435842690389845516810345611554402412893818283310117202233021355634125020654279500443420515862554775828",
];
#[test]
fn test_xorshift_value_stability() {
use rand_xorshift::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED_XOR);
}
}
mod bigint {
use crate::num_bigint::{BigInt, RandBigInt, RandomBits};
use num_traits::Zero;
use rand::distributions::Uniform;
use rand::{Rng, SeedableRng};
#[cfg(feature = "std")]
fn thread_rng() -> impl Rng {
rand::thread_rng()
}
#[cfg(not(feature = "std"))]
fn thread_rng() -> impl Rng {
// Chosen by fair dice roll
rand::rngs::StdRng::seed_from_u64(4)
}
#[test]
fn test_rand() {
let mut rng = thread_rng();
let n: BigInt = rng.gen_bigint(137);
assert!(n.bits() <= 137);
assert!(rng.gen_bigint(0).is_zero());
}
#[test]
fn test_rand_bits() {
let mut rng = thread_rng();
let n: BigInt = rng.sample(&RandomBits::new(137));
assert!(n.bits() <= 137);
let z: BigInt = rng.sample(&RandomBits::new(0));
assert!(z.is_zero());
}
#[test]
fn test_rand_range() {
let mut rng = thread_rng();
for _ in 0..10 {
assert_eq!(
rng.gen_bigint_range(&BigInt::from(236), &BigInt::from(237)),
BigInt::from(236)
);
}
fn check(l: BigInt, u: BigInt) {
let mut rng = thread_rng();
for _ in 0..1000 {
let n: BigInt = rng.gen_bigint_range(&l, &u);
assert!(n >= l);
assert!(n < u);
}
}
let l: BigInt = BigInt::from(403469000 + 2352);
let u: BigInt = BigInt::from(403469000 + 3513);
check(l.clone(), u.clone());
check(-l.clone(), u.clone());
check(-u.clone(), -l.clone());
}
#[test]
#[should_panic]
fn test_zero_rand_range() {
thread_rng().gen_bigint_range(&BigInt::from(54), &BigInt::from(54));
}
#[test]
#[should_panic]
fn test_negative_rand_range() {
let mut rng = thread_rng();
let l = BigInt::from(2352);
let u = BigInt::from(3513);
// Switching u and l should fail:
let _n: BigInt = rng.gen_bigint_range(&u, &l);
}
#[test]
fn test_rand_uniform() {
let mut rng = thread_rng();
let tiny = Uniform::new(BigInt::from(236u32), BigInt::from(237u32));
for _ in 0..10 {
assert_eq!(rng.sample(&tiny), BigInt::from(236u32));
}
fn check(l: BigInt, u: BigInt) {
let mut rng = thread_rng();
let range = Uniform::new(l.clone(), u.clone());
for _ in 0..1000 {
let n: BigInt = rng.sample(&range);
assert!(n >= l);
assert!(n < u);
}
}
let l: BigInt = BigInt::from(403469000 + 2352);
let u: BigInt = BigInt::from(403469000 + 3513);
check(l.clone(), u.clone());
check(-l.clone(), u.clone());
check(-u.clone(), -l.clone());
}
fn seeded_value_stability<R: SeedableRng + RandBigInt>(expected: &[&str]) {
let mut seed = <R::Seed>::default();
for (i, x) in seed.as_mut().iter_mut().enumerate() {
*x = (i as u8).wrapping_mul(191);
}
let mut rng = R::from_seed(seed);
for (i, &s) in expected.iter().enumerate() {
let n: BigInt = s.parse().unwrap();
let r = rng.gen_bigint((1 << i) + i);
assert_eq!(n, r, "expected {}, got {}", n, r);
}
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"-6",
"-1",
"1321",
"-147247",
"8486373526",
"-272736656290199720696",
"2731152629387534140535423510744221288522",
"-28820024790651190394679732038637785320661450462089347915910979466834461433196572",
"501454570554170484799723603981439288209930393334472085317977614690773821680884844\
8530978478667288338327570972869032358120588620346111979053742269317702532328",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"-7",
"-62",
"105",
"13025",
"-33857814162",
"768483926407291599143",
"-42356168828789885585553598574661841382586",
"28813250216034838684899917677182169473483558650956121225920149068989083656174824",
"27056553770481404639717657695702187062015359344716548489861498121037858109133467\
99640556108506718020020878739044048067894089601665199172215093468287730555599",
];
#[test]
fn test_chacha_value_stability() {
use rand_chacha::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED_CHACHA);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_ISAAC: &[&str] = &[
"1",
"0",
"5",
"113",
"-132240",
"-36348760761",
"-365690596708430705434",
"-14090753008246284277803606722552430292432",
"-26313941628626248579319341019368550803676255307056857978955881718727601479436059",
"-14563174552421101848999036239003801073335703811160945137332228646111920972691151\
88341090358094331641182310792892459091016794928947242043358702692294695845817",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_ISAAC: &[&str] = &[
"-1",
"-4",
"-29",
"1621",
"23872",
"-40371956434",
"-350815272425024298187",
"-38554888817044546636456097200348998322457",
"7474426176220721712055446211154990065592106428397966654956172383998793852911545",
"6168955541726830487961394166772329653532583907235825721475483003506842180688827\
391385624898257369023912466314791483731902392667906094226608113824795883754631",
];
#[test]
fn test_isaac_value_stability() {
use rand_isaac::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED_ISAAC);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_XOR: &[&str] = &[
"-1",
"-4",
"11",
"-1802",
"966495",
"-62592045703",
"-602281783447192077116",
"-34335811410223060575607987996861632509125",
"29156580925282215857325937227200350542000244609280383263289720243118706105351199",
"49920038676141573457451407325930326489996232208489690499754573826911037849083623\
24546142615325187412887314466195222441945661833644117700809693098722026764846",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_XOR: &[&str] = &[
"-1",
"-3",
"4",
"-228",
"377276",
"32032893086",
"885120221048601050706",
"33404877924318663206979407569537287223622",
"-15253093455306269007559295940333933266263385975865952571271093251749752787075084",
"4502641950394305250103130679458759592222756470562408577296380915684757985604969904\
381774527626485128207406911227296090734227576935034372181808818486328078978",
];
#[test]
fn test_xorshift_value_stability() {
use rand_xorshift::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED_XOR);
}
}
#[cfg(feature = "prime")]
mod prime {
use num_bigint::prime::probably_prime;
use num_bigint::RandPrime;
use rand::prelude::*;
#[test]
fn test_prime_small() {
let mut rng = StdRng::from_seed([0u8; 32]);
for n in 2..10 {
let p = rng.gen_prime(n);
assert_eq!(p.bits(), n);
assert!(probably_prime(&p, 32));
}
}
#[test]
fn test_gen_prime_1024() {
let mut rng = StdRng::from_seed([0u8; 32]);
let p = rng.gen_prime(1024);
assert_eq!(p.bits(), 1024);
}
}

197
vendor/num-bigint-dig/tests/roots.rs vendored Normal file
View File

@@ -0,0 +1,197 @@
extern crate num_bigint_dig as num_bigint;
extern crate num_integer;
extern crate num_traits;
#[cfg(feature = "rand")]
extern crate rand;
mod biguint {
use crate::num_bigint::BigUint;
use num_traits::{One, Pow, Zero};
use std::{i32, u32};
fn check<T: Into<BigUint>>(x: T, n: u32) {
let x: BigUint = x.into();
let root = x.nth_root(n);
//println!("check {}.nth_root({}) = {}", x, n, root);
if n == 2 {
assert_eq!(root, x.sqrt())
} else if n == 3 {
assert_eq!(root, x.cbrt())
}
let lo = root.pow(n);
assert!(lo <= x);
assert_eq!(lo.nth_root(n), root);
if !lo.is_zero() {
assert_eq!((&lo - 1u32).nth_root(n), &root - 1u32);
}
let hi = (&root + 1u32).pow(n);
assert!(hi > x);
assert_eq!(hi.nth_root(n), &root + 1u32);
assert_eq!((&hi - 1u32).nth_root(n), root);
}
#[test]
fn test_sqrt() {
check(99u32, 2);
check(100u32, 2);
check(120u32, 2);
}
#[test]
fn test_cbrt() {
check(8u32, 3);
check(26u32, 3);
}
#[test]
fn test_nth_root() {
check(0u32, 1);
check(10u32, 1);
check(100u32, 4);
}
#[test]
#[should_panic]
fn test_nth_root_n_is_zero() {
check(4u32, 0);
}
#[test]
fn test_nth_root_big() {
let x = BigUint::from(123_456_789_u32);
let expected = BigUint::from(6u32);
assert_eq!(x.nth_root(10), expected);
check(x, 10);
}
#[test]
fn test_nth_root_googol() {
let googol = BigUint::from(10u32).pow(100u32);
// perfect divisors of 100
for &n in &[2, 4, 5, 10, 20, 25, 50, 100] {
let expected = BigUint::from(10u32).pow(100u32 / n);
assert_eq!(googol.nth_root(n), expected);
check(googol.clone(), n);
}
}
#[test]
fn test_nth_root_twos() {
const EXP: u32 = 12;
const LOG2: usize = 1 << EXP;
let x = BigUint::one() << LOG2;
// the perfect divisors are just powers of two
for exp in 1..EXP + 1 {
let n = 2u32.pow(exp);
let expected = BigUint::one() << (LOG2 / n as usize);
assert_eq!(x.nth_root(n), expected);
check(x.clone(), n);
}
// degenerate cases should return quickly
assert!(x.nth_root(x.bits() as u32).is_one());
assert!(x.nth_root(i32::MAX as u32).is_one());
assert!(x.nth_root(u32::MAX).is_one());
}
#[cfg(feature = "rand")]
#[test]
fn test_roots_rand() {
#[cfg(feature = "std")]
fn thread_rng() -> impl rand::Rng {
rand::thread_rng()
}
#[cfg(not(feature = "std"))]
fn thread_rng() -> impl rand::Rng {
use rand::SeedableRng;
// Chosen by fair dice roll
rand::rngs::StdRng::seed_from_u64(4)
}
use crate::num_bigint::RandBigInt;
use rand::distributions::Uniform;
use rand::Rng;
let rng = &mut thread_rng();
let bit_range = Uniform::new(0, 2048);
let sample_bits: Vec<_> = rng.sample_iter(&bit_range).take(100).collect();
for bits in sample_bits {
let x = rng.gen_biguint(bits);
for n in 2..11 {
check(x.clone(), n);
}
check(x.clone(), 100);
}
}
#[test]
fn test_roots_rand1() {
// A random input that found regressions
let s = "575981506858479247661989091587544744717244516135539456183849\
986593934723426343633698413178771587697273822147578889823552\
182702908597782734558103025298880194023243541613924361007059\
353344183590348785832467726433749431093350684849462759540710\
026019022227591412417064179299354183441181373862905039254106\
4781867";
let x: BigUint = s.parse().unwrap();
check(x.clone(), 2);
check(x.clone(), 3);
check(x.clone(), 10);
check(x.clone(), 100);
}
}
mod bigint {
use crate::num_bigint::BigInt;
use num_traits::{Pow, Signed};
fn check(x: i64, n: u32) {
let big_x = BigInt::from(x);
let res = big_x.nth_root(n);
if n == 2 {
assert_eq!(&res, &big_x.sqrt())
} else if n == 3 {
assert_eq!(&res, &big_x.cbrt())
}
if big_x.is_negative() {
assert!(res.pow(n) >= big_x);
assert!((res - 1u32).pow(n) < big_x);
} else {
assert!(res.pow(n) <= big_x);
assert!((res + 1u32).pow(n) > big_x);
}
}
#[test]
fn test_nth_root() {
check(-100, 3);
}
#[test]
#[should_panic]
fn test_nth_root_x_neg_n_even() {
check(-100, 4);
}
#[test]
#[should_panic]
fn test_sqrt_x_neg() {
check(-4, 2);
}
#[test]
fn test_cbrt() {
check(8, 3);
check(-8, 3);
}
}

103
vendor/num-bigint-dig/tests/serde.rs vendored Normal file
View File

@@ -0,0 +1,103 @@
//! Test serialization and deserialization of `BigUint` and `BigInt`
//!
//! The serialized formats should not change, even if we change our
//! internal representation, because we want to preserve forward and
//! backward compatibility of serialized data!
#![cfg(feature = "serde")]
extern crate num_bigint_dig as num_bigint;
extern crate num_traits;
extern crate serde_test;
use crate::num_bigint::{BigInt, BigUint};
use num_traits::{One, Zero};
use serde_test::{assert_tokens, Token};
#[test]
fn biguint_zero() {
let tokens = [Token::Seq { len: Some(0) }, Token::SeqEnd];
assert_tokens(&BigUint::zero(), &tokens);
}
#[test]
fn bigint_zero() {
let tokens = [
Token::Tuple { len: 2 },
Token::I8(0),
Token::Seq { len: Some(0) },
Token::SeqEnd,
Token::TupleEnd,
];
assert_tokens(&BigInt::zero(), &tokens);
}
#[test]
fn biguint_one() {
let tokens = [Token::Seq { len: Some(1) }, Token::U32(1), Token::SeqEnd];
assert_tokens(&BigUint::one(), &tokens);
}
#[test]
fn bigint_one() {
let tokens = [
Token::Tuple { len: 2 },
Token::I8(1),
Token::Seq { len: Some(1) },
Token::U32(1),
Token::SeqEnd,
Token::TupleEnd,
];
assert_tokens(&BigInt::one(), &tokens);
}
#[test]
fn bigint_negone() {
let tokens = [
Token::Tuple { len: 2 },
Token::I8(-1),
Token::Seq { len: Some(1) },
Token::U32(1),
Token::SeqEnd,
Token::TupleEnd,
];
assert_tokens(&-BigInt::one(), &tokens);
}
// Generated independently from python `hex(factorial(100))`
const FACTORIAL_100: &'static [u32] = &[
0x00000000, 0x00000000, 0x00000000, 0x2735c61a, 0xee8b02ea, 0xb3b72ed2, 0x9420c6ec, 0x45570cca,
0xdf103917, 0x943a321c, 0xeb21b5b2, 0x66ef9a70, 0xa40d16e9, 0x28d54bbd, 0xdc240695, 0x964ec395,
0x1b30,
];
#[test]
fn biguint_factorial_100() {
let n: BigUint = (1u8..101).product();
let mut tokens = vec![];
tokens.push(Token::Seq {
len: Some(FACTORIAL_100.len()),
});
tokens.extend(FACTORIAL_100.iter().map(|&u| Token::U32(u)));
tokens.push(Token::SeqEnd);
assert_tokens(&n, &tokens);
}
#[test]
fn bigint_factorial_100() {
let n: BigInt = (1i8..101).product();
let mut tokens = vec![];
tokens.push(Token::Tuple { len: 2 });
tokens.push(Token::I8(1));
tokens.push(Token::Seq {
len: Some(FACTORIAL_100.len()),
});
tokens.extend(FACTORIAL_100.iter().map(|&u| Token::U32(u)));
tokens.push(Token::SeqEnd);
tokens.push(Token::TupleEnd);
assert_tokens(&n, &tokens);
}

49
vendor/num-bigint-dig/tests/torture.rs vendored Normal file
View File

@@ -0,0 +1,49 @@
#![cfg(feature = "rand")]
extern crate num_bigint_dig as num_bigint;
extern crate num_traits;
extern crate rand;
use crate::num_bigint::RandBigInt;
use num_traits::Zero;
use rand::prelude::*;
fn test_mul_divide_torture_count(count: usize) {
let bits_max = 1 << 12;
#[cfg(target_pointer_width = "32")]
let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
#[cfg(target_pointer_width = "64")]
let seed = [
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,
];
let mut rng = rand::rngs::SmallRng::from_seed(seed);
for _ in 0..count {
// Test with numbers of random sizes:
let xbits = rng.gen_range(0..bits_max);
let ybits = rng.gen_range(0..bits_max);
let x = rng.gen_biguint(xbits);
let y = rng.gen_biguint(ybits);
if x.is_zero() || y.is_zero() {
continue;
}
let prod = &x * &y;
assert_eq!(&prod / &x, y);
assert_eq!(&prod / &y, x);
}
}
#[test]
fn test_mul_divide_torture() {
test_mul_divide_torture_count(1000);
}
#[test]
#[ignore]
fn test_mul_divide_torture_long() {
test_mul_divide_torture_count(1000000);
}