diff --git a/Cargo.toml b/Cargo.toml index 720512a6..f8072314 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,14 @@ categories = [ "algorithms", "data-structures", "science" ] license = "MIT OR Apache-2.0" name = "num-bigint" repository = "https://github.com/rust-num/num-bigint" -version = "0.4.6" +version = "0.5.0" readme = "README.md" exclude = ["/ci/*", "/.github/*"] edition = "2021" rust-version = "1.60" [features] -default = ["std"] +default = ["std", "rand"] std = ["num-integer/std", "num-traits/std"] arbitrary = ["dep:arbitrary"] quickcheck = ["dep:quickcheck"] @@ -56,7 +56,7 @@ features = ["i128"] [dependencies.rand] optional = true -version = "0.8" +version = " =0.9.0" default-features = false [dependencies.serde] @@ -73,3 +73,6 @@ default-features = false optional = true version = "1" default-features = false + +[dev-dependencies] +rand = { version = " =0.9.0", features = ["thread_rng"]} \ No newline at end of file diff --git a/README.md b/README.md index cce185b9..f933ac35 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Add this to your `Cargo.toml`: ```toml [dependencies] -num-bigint = "0.4" +num-bigint = "0.5" ``` ## Features @@ -29,12 +29,12 @@ if your compiler is not new enough. feature is enabled. To enable it include rand as ```toml -rand = "0.8" -num-bigint = { version = "0.4", features = ["rand"] } +rand = " =0.9.0-beta.3" +num-bigint = { version = "0.5", features = ["rand"] } ``` Note that you must use the version of `rand` that `num-bigint` is compatible -with: `0.8`. +with: ` =0.9.0-beta.3`. ## Releases diff --git a/benches/bigint.rs b/benches/bigint.rs index 22795274..f2fedc9d 100644 --- a/benches/bigint.rs +++ b/benches/bigint.rs @@ -13,24 +13,24 @@ use rng::get_rng; fn multiply_bench(b: &mut Bencher, xbits: u64, ybits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(xbits); - let y = rng.gen_bigint(ybits); + let x = rng.random_bigint(xbits); + let y = rng.random_bigint(ybits); b.iter(|| &x * &y); } fn divide_bench(b: &mut Bencher, xbits: u64, ybits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(xbits); - let y = rng.gen_bigint(ybits); + let x = rng.random_bigint(xbits); + let y = rng.random_bigint(ybits); b.iter(|| &x / &y); } fn remainder_bench(b: &mut Bencher, xbits: u64, ybits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(xbits); - let y = rng.gen_bigint(ybits); + let x = rng.random_bigint(xbits); + let y = rng.random_bigint(ybits); b.iter(|| &x % &y); } @@ -186,7 +186,7 @@ fn fib_to_string(b: &mut Bencher) { fn to_str_radix_bench(b: &mut Bencher, radix: u32, bits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(bits); + let x = rng.random_bigint(bits); b.iter(|| x.to_str_radix(radix)); } @@ -222,7 +222,7 @@ fn to_str_radix_36(b: &mut Bencher) { fn from_str_radix_bench(b: &mut Bencher, radix: u32) { let mut rng = get_rng(); - let x = rng.gen_bigint(1009); + let x = rng.random_bigint(1009); let s = x.to_str_radix(radix); assert_eq!(x, BigInt::from_str_radix(&s, radix).unwrap()); b.iter(|| BigInt::from_str_radix(&s, radix)); @@ -256,7 +256,7 @@ fn from_str_radix_36(b: &mut Bencher) { fn rand_bench(b: &mut Bencher, bits: u64) { let mut rng = get_rng(); - b.iter(|| rng.gen_bigint(bits)); + b.iter(|| rng.random_bigint(bits)); } #[bench] @@ -327,7 +327,7 @@ fn shr(b: &mut Bencher) { fn hash(b: &mut Bencher) { use std::collections::HashSet; let mut rng = get_rng(); - let v: Vec = (1000..2000).map(|bits| rng.gen_bigint(bits)).collect(); + let v: Vec = (1000..2000).map(|bits| rng.random_bigint(bits)).collect(); b.iter(|| { let h: HashSet<&BigInt> = v.iter().collect(); assert_eq!(h.len(), v.len()); @@ -399,8 +399,8 @@ const RFC3526_2048BIT_MODP_GROUP: &str = "\ #[bench] fn modpow(b: &mut Bencher) { let mut rng = get_rng(); - let base = rng.gen_biguint(2048); - let e = rng.gen_biguint(2048); + let base = rng.random_biguint(2048); + let e = rng.random_biguint(2048); let m = BigUint::from_str_radix(RFC3526_2048BIT_MODP_GROUP, 16).unwrap(); b.iter(|| base.modpow(&e, &m)); @@ -409,8 +409,8 @@ fn modpow(b: &mut Bencher) { #[bench] fn modpow_even(b: &mut Bencher) { let mut rng = get_rng(); - let base = rng.gen_biguint(2048); - let e = rng.gen_biguint(2048); + let base = rng.random_biguint(2048); + let e = rng.random_biguint(2048); // Make the modulus even, so monty (base-2^32) doesn't apply. let m = BigUint::from_str_radix(RFC3526_2048BIT_MODP_GROUP, 16).unwrap() - 1u32; @@ -420,7 +420,7 @@ fn modpow_even(b: &mut Bencher) { #[bench] fn to_u32_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.to_u32_digits()); } @@ -428,7 +428,7 @@ fn to_u32_digits(b: &mut Bencher) { #[bench] fn iter_u32_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.iter_u32_digits().max()); } @@ -436,7 +436,7 @@ fn iter_u32_digits(b: &mut Bencher) { #[bench] fn to_u64_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.to_u64_digits()); } @@ -444,7 +444,7 @@ fn to_u64_digits(b: &mut Bencher) { #[bench] fn iter_u64_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.iter_u64_digits().max()); } diff --git a/benches/gcd.rs b/benches/gcd.rs index c211b6ef..f015aa0c 100644 --- a/benches/gcd.rs +++ b/benches/gcd.rs @@ -13,8 +13,8 @@ use rng::get_rng; fn bench(b: &mut Bencher, bits: u64, gcd: fn(&BigUint, &BigUint) -> BigUint) { let mut rng = get_rng(); - let x = rng.gen_biguint(bits); - let y = rng.gen_biguint(bits); + let x = rng.random_biguint(bits); + let y = rng.random_biguint(bits); assert_eq!(euclid(&x, &y), x.gcd(&y)); diff --git a/benches/rng/mod.rs b/benches/rng/mod.rs index 33e4f0fa..4228ec01 100644 --- a/benches/rng/mod.rs +++ b/benches/rng/mod.rs @@ -32,7 +32,7 @@ impl RngCore for XorShiftStar { } } - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { - Ok(self.fill_bytes(dest)) - } + // fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { + // Ok(self.fill_bytes(dest)) + // } } diff --git a/benches/roots.rs b/benches/roots.rs index 7afc4f76..3c329b4b 100644 --- a/benches/roots.rs +++ b/benches/roots.rs @@ -37,7 +37,7 @@ fn check(x: &BigUint, n: u32) { } fn bench_sqrt(b: &mut Bencher, bits: u64) { - let x = get_rng().gen_biguint(bits); + let x = get_rng().random_biguint(bits); eprintln!("bench_sqrt({})", x); check(&x, 2); @@ -65,7 +65,7 @@ fn big4k_sqrt(b: &mut Bencher) { } fn bench_cbrt(b: &mut Bencher, bits: u64) { - let x = get_rng().gen_biguint(bits); + let x = get_rng().random_biguint(bits); eprintln!("bench_cbrt({})", x); check(&x, 3); @@ -93,7 +93,7 @@ fn big4k_cbrt(b: &mut Bencher) { } fn bench_nth_root(b: &mut Bencher, bits: u64, n: u32) { - let x = get_rng().gen_biguint(bits); + let x = get_rng().random_biguint(bits); eprintln!("bench_{}th_root({})", n, x); check(&x, n); diff --git a/src/bigrand.rs b/src/bigrand.rs index e5cbacdb..618ec2d8 100644 --- a/src/bigrand.rs +++ b/src/bigrand.rs @@ -2,7 +2,7 @@ #![cfg(feature = "rand")] #![cfg_attr(docsrs, doc(cfg(feature = "rand")))] -use rand::distributions::uniform::{SampleBorrow, SampleUniform, UniformSampler}; +use rand::distr::uniform::{Error as RandError, SampleBorrow, SampleUniform, UniformSampler}; use rand::prelude::*; use crate::BigInt; @@ -19,28 +19,28 @@ use num_traits::{ToPrimitive, Zero}; /// The `rand` feature must be enabled to use this. See crate-level documentation for details. pub trait RandBigInt { /// Generate a random [`BigUint`] of the given bit size. - fn gen_biguint(&mut self, bit_size: u64) -> BigUint; + fn random_biguint(&mut self, bit_size: u64) -> BigUint; /// Generate a random [ BigInt`] of the given bit size. - fn gen_bigint(&mut self, bit_size: u64) -> BigInt; + fn random_bigint(&mut self, bit_size: u64) -> BigInt; /// Generate a random [`BigUint`] less than the given bound. Fails /// when the bound is zero. - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint; + fn random_biguint_below(&mut self, bound: &BigUint) -> BigUint; /// Generate a random [`BigUint`] within the given range. The lower /// bound is inclusive; the upper bound is exclusive. Fails when /// the upper bound is not greater than the lower bound. - fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; + fn random_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; /// Generate a random [`BigInt`] within the given range. The lower /// bound is inclusive; the upper bound is exclusive. Fails when /// the upper bound is not greater than the lower bound. - fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt; + fn random_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt; } -fn gen_bits(rng: &mut R, data: &mut [u32], rem: u64) { - // `fill` is faster than many `gen::` calls +fn random_bits(rng: &mut R, data: &mut [u32], rem: u64) { + // `fill` is faster than many `random::` calls rng.fill(data); if rem > 0 { let last = data.len() - 1; @@ -48,19 +48,19 @@ fn gen_bits(rng: &mut R, data: &mut [u32], rem: u64) { } } -impl RandBigInt for R { +impl RandBigInt for R { cfg_digit!( - fn gen_biguint(&mut self, bit_size: u64) -> BigUint { + fn random_biguint(&mut self, bit_size: u64) -> BigUint { let (digits, rem) = bit_size.div_rem(&32); let len = (digits + (rem > 0) as u64) .to_usize() .expect("capacity overflow"); let mut data = vec![0u32; len]; - gen_bits(self, &mut data, rem); + random_bits(self, &mut data, rem); biguint_from_vec(data) } - fn gen_biguint(&mut self, bit_size: u64) -> BigUint { + fn random_biguint(&mut self, bit_size: u64) -> BigUint { use core::slice; let (digits, rem) = bit_size.div_rem(&32); @@ -75,7 +75,7 @@ impl RandBigInt for R { let ptr = data.as_mut_ptr() as *mut u32; debug_assert!(native_len * 2 >= len); let data = slice::from_raw_parts_mut(ptr, len); - gen_bits(self, data, rem); + random_bits(self, data, rem); } #[cfg(target_endian = "big")] for digit in &mut data { @@ -86,22 +86,22 @@ impl RandBigInt for R { } ); - fn gen_bigint(&mut self, bit_size: u64) -> BigInt { + fn random_bigint(&mut self, bit_size: u64) -> BigInt { loop { // Generate a random BigUint... - let biguint = self.gen_biguint(bit_size); + let biguint = self.random_biguint(bit_size); // ...and then randomly assign it a Sign... let sign = if biguint.is_zero() { // ...except that if the BigUint is zero, we need to try // again with probability 0.5. This is because otherwise, // the probability of generating a zero BigInt would be // double that of any other number. - if self.gen() { + if self.random() { continue; } else { NoSign } - } else if self.gen() { + } else if self.random() { Plus } else { Minus @@ -110,35 +110,35 @@ impl RandBigInt for R { } } - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint { + fn random_biguint_below(&mut self, bound: &BigUint) -> BigUint { assert!(!bound.is_zero()); let bits = bound.bits(); loop { - let n = self.gen_biguint(bits); + let n = self.random_biguint(bits); if n < *bound { return n; } } } - fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint { + fn random_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint { assert!(*lbound < *ubound); if lbound.is_zero() { - self.gen_biguint_below(ubound) + self.random_biguint_below(ubound) } else { - lbound + self.gen_biguint_below(&(ubound - lbound)) + lbound + self.random_biguint_below(&(ubound - lbound)) } } - fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt { + fn random_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt { assert!(*lbound < *ubound); if lbound.is_zero() { - BigInt::from(self.gen_biguint_below(ubound.magnitude())) + BigInt::from(self.random_biguint_below(ubound.magnitude())) } else if ubound.is_zero() { - lbound + BigInt::from(self.gen_biguint_below(lbound.magnitude())) + lbound + BigInt::from(self.random_biguint_below(lbound.magnitude())) } else { let delta = ubound - lbound; - lbound + BigInt::from(self.gen_biguint_below(delta.magnitude())) + lbound + BigInt::from(self.random_biguint_below(delta.magnitude())) } } } @@ -154,44 +154,54 @@ impl UniformSampler for UniformBigUint { type X = BigUint; #[inline] - fn new(low_b: B1, high_b: B2) -> Self + fn new(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low < high); - UniformBigUint { - len: high - low, - base: low.clone(), + if low < high { + Ok(UniformBigUint { + len: high - low, + base: low.clone(), + }) + } else { + Err(RandError::EmptyRange) } } #[inline] - fn new_inclusive(low_b: B1, high_b: B2) -> Self + fn new_inclusive(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low <= high); - Self::new(low, high + 1u32) + if low <= high { + Self::new(low, high + 1u32) + } else { + Err(RandError::EmptyRange) + } } #[inline] fn sample(&self, rng: &mut R) -> Self::X { - &self.base + rng.gen_biguint_below(&self.len) + &self.base + rng.random_biguint_below(&self.len) } #[inline] - fn sample_single(low: B1, high: B2, rng: &mut R) -> Self::X + fn sample_single( + low: B1, + high: B2, + rng: &mut R, + ) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { - rng.gen_biguint_range(low.borrow(), high.borrow()) + Ok(rng.random_biguint_range(low.borrow(), high.borrow())) } } @@ -210,44 +220,54 @@ impl UniformSampler for UniformBigInt { type X = BigInt; #[inline] - fn new(low_b: B1, high_b: B2) -> Self + fn new(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low < high); - UniformBigInt { - len: (high - low).into_parts().1, - base: low.clone(), + if low < high { + Ok(UniformBigInt { + len: (high - low).into_parts().1, + base: low.clone(), + }) + } else { + Err(RandError::EmptyRange) } } #[inline] - fn new_inclusive(low_b: B1, high_b: B2) -> Self + fn new_inclusive(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low <= high); - Self::new(low, high + 1u32) + if low <= high { + Self::new(low, high + 1u32) + } else { + Err(RandError::EmptyRange) + } } #[inline] fn sample(&self, rng: &mut R) -> Self::X { - &self.base + BigInt::from(rng.gen_biguint_below(&self.len)) + &self.base + BigInt::from(rng.random_biguint_below(&self.len)) } #[inline] - fn sample_single(low: B1, high: B2, rng: &mut R) -> Self::X + fn sample_single( + low: B1, + high: B2, + rng: &mut R, + ) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { - rng.gen_bigint_range(low.borrow(), high.borrow()) + Ok(rng.random_bigint_range(low.borrow(), high.borrow())) } } @@ -273,13 +293,38 @@ impl RandomBits { impl Distribution for RandomBits { #[inline] fn sample(&self, rng: &mut R) -> BigUint { - rng.gen_biguint(self.bits) + rng.random_biguint(self.bits) } } impl Distribution for RandomBits { #[inline] fn sample(&self, rng: &mut R) -> BigInt { - rng.gen_bigint(self.bits) + rng.random_bigint(self.bits) + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_rand_biguint_range_works() { + let mut rng = rand::rng(); + let minval = BigUint::zero(); + let maxval = BigUint::from(420u16); + let a_random_biguint = rng.random_biguint_range(&minval, &maxval); + assert!(a_random_biguint >= minval); + assert!(a_random_biguint < maxval); + } + + #[test] + fn test_rand_bigint_range_works() { + let mut rng = rand::rng(); + let minval = BigInt::from(-420i16); + let maxval = BigInt::from(420i16); + let a_random_bigint = rng.random_bigint_range(&minval, &maxval); + assert!(a_random_bigint >= minval); + assert!(a_random_bigint < maxval); } } diff --git a/tests/bigint.rs b/tests/bigint.rs index fea5232e..78b739b9 100644 --- a/tests/bigint.rs +++ b/tests/bigint.rs @@ -187,10 +187,10 @@ fn test_cmp() { let vs: [&[u32]; 4] = [&[2_u32], &[1, 1], &[2, 1], &[1, 1, 1]]; let mut nums = Vec::new(); for s in vs.iter().rev() { - nums.push(BigInt::from_slice(Minus, *s)); + nums.push(BigInt::from_slice(Minus, s)); } nums.push(Zero::zero()); - nums.extend(vs.iter().map(|s| BigInt::from_slice(Plus, *s))); + nums.extend(vs.iter().map(|s| BigInt::from_slice(Plus, s))); for (i, ni) in nums.iter().enumerate() { for (j0, nj) in nums[i..].iter().enumerate() { @@ -199,26 +199,26 @@ fn test_cmp() { assert_eq!(ni.cmp(nj), Equal); assert_eq!(nj.cmp(ni), Equal); assert_eq!(ni, nj); - assert!(!(ni != nj)); + assert!((ni == nj)); assert!(ni <= nj); assert!(ni >= nj); - assert!(!(ni < nj)); - assert!(!(ni > nj)); + assert!((ni >= nj)); + assert!((ni <= nj)); } else { assert_eq!(ni.cmp(nj), Less); assert_eq!(nj.cmp(ni), Greater); - assert!(!(ni == nj)); + assert!((ni != nj)); assert!(ni != nj); assert!(ni <= nj); - assert!(!(ni >= nj)); + assert!((ni < nj)); assert!(ni < nj); - assert!(!(ni > nj)); + assert!((ni <= nj)); - assert!(!(nj <= ni)); + assert!((nj > ni)); assert!(nj >= ni); - assert!(!(nj < ni)); + assert!((nj >= ni)); assert!(nj > ni); } } @@ -1217,9 +1217,7 @@ fn test_from_str_radix() { // issue 10522, this hit an edge case that caused it to // attempt to allocate a vector of size (-1u) == huge. - let x: BigInt = format!("1{}", repeat("0").take(36).collect::()) - .parse() - .unwrap(); + let x: BigInt = format!("1{}", "0".repeat(36)).parse().unwrap(); let _y = x.to_string(); } @@ -1318,7 +1316,7 @@ fn test_iter_product() { FromPrimitive::from_i32(-1004).unwrap(), FromPrimitive::from_i32(1005).unwrap(), ]; - let result = data.get(0).unwrap() + let result = data.first().unwrap() * data.get(1).unwrap() * data.get(2).unwrap() * data.get(3).unwrap() diff --git a/tests/bigint_bitwise.rs b/tests/bigint_bitwise.rs index 94d92e1d..bba16ff6 100644 --- a/tests/bigint_bitwise.rs +++ b/tests/bigint_bitwise.rs @@ -11,10 +11,10 @@ use crate::ValueVec::*; impl ToBigInt for ValueVec { fn to_bigint(&self) -> Option { - 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)), + 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)), } } } @@ -106,7 +106,7 @@ const I64_VALUES: &[i64] = &[ #[test] fn test_not() { - for &(ref a, ref not) in NOT_VALUES.iter() { + for (a, not) in NOT_VALUES.iter() { let a = a.to_bigint().unwrap(); let not = not.to_bigint().unwrap(); @@ -131,7 +131,7 @@ fn test_not_i64() { #[test] fn test_bitwise() { - for &(ref a, ref b, ref and, ref or, ref xor) in BITWISE_VALUES.iter() { + for (a, b, and, or, xor) in BITWISE_VALUES.iter() { let a = a.to_bigint().unwrap(); let b = b.to_bigint().unwrap(); let and = and.to_bigint().unwrap(); diff --git a/tests/biguint.rs b/tests/biguint.rs index 4e2c6259..36955ff3 100644 --- a/tests/biguint.rs +++ b/tests/biguint.rs @@ -90,7 +90,7 @@ fn test_to_bytes_le() { #[test] fn test_cmp() { let data: [&[_]; 7] = [&[], &[1], &[2], &[!0], &[0, 1], &[2, 1], &[1, 1, 1]]; - let data: Vec = data.iter().map(|v| BigUint::from_slice(*v)).collect(); + let data: Vec = data.iter().map(|v| BigUint::from_slice(v)).collect(); for (i, ni) in data.iter().enumerate() { for (j0, nj) in data[i..].iter().enumerate() { let j = j0 + i; @@ -98,26 +98,26 @@ fn test_cmp() { assert_eq!(ni.cmp(nj), Equal); assert_eq!(nj.cmp(ni), Equal); assert_eq!(ni, nj); - assert!(!(ni != nj)); + assert!((ni == nj)); assert!(ni <= nj); assert!(ni >= nj); - assert!(!(ni < nj)); - assert!(!(ni > nj)); + assert!((ni >= nj)); + assert!((ni <= nj)); } else { assert_eq!(ni.cmp(nj), Less); assert_eq!(nj.cmp(ni), Greater); - assert!(!(ni == nj)); + assert!((ni != nj)); assert!(ni != nj); assert!(ni <= nj); - assert!(!(ni >= nj)); + assert!((ni < nj)); assert!(ni < nj); - assert!(!(ni > nj)); + assert!((ni <= nj)); - assert!(!(nj <= ni)); + assert!((nj > ni)); assert!(nj >= ni); - assert!(!(nj < ni)); + assert!((nj >= ni)); assert!(nj > ni); } } @@ -146,6 +146,7 @@ fn test_hash() { } // LEFT, RIGHT, AND, OR, XOR +#[allow(clippy::type_complexity)] const BIT_TESTS: &[(&[u32], &[u32], &[u32], &[u32], &[u32])] = &[ (&[], &[], &[], &[], &[]), (&[1, 0, 1], &[1, 1], &[1], &[1, 1, 1], &[0, 1, 1]), @@ -1220,14 +1221,8 @@ fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> { ( BigUint::from_slice(&[1, 2]), vec![ - ( - 2, - format!("10{}1", repeat("0").take(bits - 1).collect::()), - ), - ( - 4, - format!("2{}1", repeat("0").take(bits / 2 - 1).collect::()), - ), + (2, format!("10{}1", "0".repeat(bits - 1))), + (4, format!("2{}1", "0".repeat(bits / 2 - 1))), ( 10, match bits { @@ -1237,10 +1232,7 @@ fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> { _ => panic!(), }, ), - ( - 16, - format!("2{}1", repeat("0").take(bits / 4 - 1).collect::()), - ), + (16, format!("2{}1", "0".repeat(bits / 4 - 1))), ], ), ( @@ -1248,18 +1240,14 @@ fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> { vec![ ( 2, - format!( - "11{}10{}1", - repeat("0").take(bits - 2).collect::(), - repeat("0").take(bits - 1).collect::() - ), + format!("11{}10{}1", "0".repeat(bits - 2), "0".repeat(bits - 1)), ), ( 4, format!( "3{}2{}1", - repeat("0").take(bits / 2 - 1).collect::(), - repeat("0").take(bits / 2 - 1).collect::() + "0".repeat(bits / 2 - 1), + "0".repeat(bits / 2 - 1) ), ), ( @@ -1284,8 +1272,8 @@ fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> { 16, format!( "3{}2{}1", - repeat("0").take(bits / 4 - 1).collect::(), - repeat("0").take(bits / 4 - 1).collect::() + "0".repeat(bits / 4 - 1), + "0".repeat(bits / 4 - 1) ), ), ], @@ -1297,9 +1285,9 @@ fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> { fn test_to_str_radix() { let r = to_str_pairs(); for num_pair in r.iter() { - let &(ref n, ref rs) = num_pair; + let (n, rs) = num_pair; for str_pair in rs.iter() { - let &(ref radix, ref str) = str_pair; + let (radix, str) = str_pair; assert_eq!(n.to_str_radix(*radix), *str); } } @@ -1630,9 +1618,9 @@ fn test_from_and_to_radix() { fn test_from_str_radix() { let r = to_str_pairs(); for num_pair in r.iter() { - let &(ref n, ref rs) = num_pair; + let (n, rs) = num_pair; for str_pair in rs.iter() { - let &(ref radix, ref str) = str_pair; + let (radix, str) = str_pair; assert_eq!(n, &BigUint::from_str_radix(str, *radix).unwrap()); } } @@ -1800,7 +1788,7 @@ fn test_iter_product() { FromPrimitive::from_u32(1004).unwrap(), FromPrimitive::from_u32(1005).unwrap(), ]; - let result = data.get(0).unwrap() + let result = data.first().unwrap() * data.get(1).unwrap() * data.get(2).unwrap() * data.get(3).unwrap() diff --git a/tests/consts/mod.rs b/tests/consts/mod.rs index 457fbd36..33ed5203 100644 --- a/tests/consts/mod.rs +++ b/tests/consts/mod.rs @@ -41,6 +41,7 @@ pub const MUL_TRIPLES: &[(&[u32], &[u32], &[u32])] = &[ (&[0, 0, 1], &[0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]), ]; +#[allow(clippy::type_complexity)] pub const DIV_REM_QUADRUPLES: &[(&[u32], &[u32], &[u32], &[u32])] = &[ (&[1], &[2], &[], &[1]), (&[3], &[2], &[1], &[1]),