Skip to content

Commit

Permalink
Build rand without default features (no_std)
Browse files Browse the repository at this point in the history
The tests using rand/std have been moved to ci/big_rand/.
  • Loading branch information
cuviper committed Jan 14, 2020
1 parent 4bc6306 commit bee58ab
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ matrix:
before_script:
- rustup target add $TARGET
script:
- cargo build --verbose --target $TARGET --no-default-features --features "serde"
- cargo build --verbose --target $TARGET --no-default-features --features "serde rand"
- name: "rustfmt"
rust: 1.31.0
before_script:
Expand Down
10 changes: 0 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ features = ["i128"]
optional = true
version = "0.7"
default-features = false
features = ["std", "small_rng"]

[dependencies.serde]
optional = true
Expand All @@ -65,15 +64,6 @@ optional = true
version = "0.9"
default-features = false

[dev-dependencies.rand_xorshift]
version = "0.2.0"

[dev-dependencies.rand_chacha]
version = "0.2.0"

[dev-dependencies.rand_isaac]
version = "0.2.0"

[features]
default = ["std"]
std = ["num-integer/std", "num-traits/std"]
Expand Down
18 changes: 18 additions & 0 deletions ci/big_rand/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "big_rand"
version = "0.1.0"
authors = ["Josh Stone <cuviper@gmail.com>"]

[dependencies]
num-traits = "0.2.11"
rand_chacha = "0.2"
rand_isaac = "0.2"
rand_xorshift = "0.2"

[dependencies.num-bigint]
features = ["rand"]
path = "../.."

[dependencies.rand]
features = ["small_rng"]
version = "0.7"
68 changes: 66 additions & 2 deletions tests/rand.rs → ci/big_rand/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![cfg(feature = "rand")]
//! Test randomization of `BigUint` and `BigInt`
//!
//! This test is in a completely separate crate so `rand::thread_rng()`
//! can be available without "infecting" the rest of the build with
//! `rand`'s default features, especially not `rand/std`.
#![cfg(test)]

extern crate num_bigint;
extern crate num_traits;
Expand All @@ -7,9 +13,11 @@ extern crate rand_chacha;
extern crate rand_isaac;
extern crate rand_xorshift;

mod torture;

mod biguint {
use num_bigint::{BigUint, RandBigInt, RandomBits};
use num_traits::Zero;
use num_traits::{Pow, Zero};
use rand::distributions::Uniform;
use rand::thread_rng;
use rand::{Rng, SeedableRng};
Expand Down Expand Up @@ -162,6 +170,44 @@ mod biguint {
use rand_xorshift::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED);
}

#[test]
fn test_roots_rand() {
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);
}

let mut rng = 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);
}
}
}

mod bigint {
Expand Down Expand Up @@ -324,4 +370,22 @@ mod bigint {
use rand_xorshift::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED);
}

#[test]
fn test_random_shr() {
use rand::distributions::Standard;
use rand::Rng;
let rng = rand::thread_rng();

for p in rng.sample_iter::<i64, _>(&Standard).take(1000) {
let big = BigInt::from(p);
let bigger = &big << 1000;
assert_eq!(&bigger >> 1000, big);
for i in 0..64 {
let answer = BigInt::from(p >> i);
assert_eq!(&big >> i, answer);
assert_eq!(&bigger >> (1000 + i), answer);
}
}
}
}
6 changes: 0 additions & 6 deletions tests/torture.rs → ci/big_rand/src/torture.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#![cfg(feature = "rand")]

extern crate num_bigint;
extern crate num_traits;
extern crate rand;

use num_bigint::RandBigInt;
use num_traits::Zero;
use rand::prelude::*;
Expand Down
5 changes: 3 additions & 2 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ esac

case "$TRAVIS_RUST_VERSION" in
1.3[1-5].*) ;;
*) NO_STD_FEATURES="serde" ;;
*) NO_STD_FEATURES="serde rand" ;;
esac

# num-bigint should build and test everywhere.
Expand Down Expand Up @@ -55,5 +55,6 @@ if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
fi

case "$STD_FEATURES" in
*serde*) cargo test --manifest-path ci/big_serde/Cargo.toml
*serde*) cargo test --manifest-path ci/big_serde/Cargo.toml ;;&
*rand*) cargo test --manifest-path ci/big_rand/Cargo.toml ;;&
esac
14 changes: 3 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@
//!
//! It's easy to generate large random numbers:
//!
//! ```rust
//! # #[cfg(feature = "rand")]
//! ```rust,ignore
//! extern crate rand;
//! extern crate num_bigint as bigint;
//! extern crate num_bigint;
//!
//! # #[cfg(feature = "rand")]
//! # fn main() {
//! use bigint::{ToBigInt, RandBigInt};
//! use num_bigint::{ToBigInt, RandBigInt};
//!
//! let mut rng = rand::thread_rng();
//! let a = rng.gen_bigint(1000);
Expand All @@ -64,11 +61,6 @@
//!
//! // Probably an even larger number.
//! println!("{}", a * b);
//! # }
//!
//! # #[cfg(not(feature = "rand"))]
//! # fn main() {
//! # }
//! ```
//!
//! See the "Features" section for instructions for enabling random number generation.
Expand Down
21 changes: 0 additions & 21 deletions tests/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
extern crate num_bigint;
extern crate num_integer;
extern crate num_traits;
#[cfg(feature = "rand")]
extern crate rand;

use num_bigint::BigUint;
use num_bigint::Sign::{Minus, NoSign, Plus};
Expand Down Expand Up @@ -1083,25 +1081,6 @@ fn test_negative_shr() {
assert_eq!(BigInt::from(-3) >> 2, BigInt::from(-1));
}

#[test]
#[cfg(feature = "rand")]
fn test_random_shr() {
use rand::distributions::Standard;
use rand::Rng;
let rng = rand::thread_rng();

for p in rng.sample_iter::<i64, _>(&Standard).take(1000) {
let big = BigInt::from(p);
let bigger = &big << 1000;
assert_eq!(&bigger >> 1000, big);
for i in 0..64 {
let answer = BigInt::from(p >> i);
assert_eq!(&big >> i, answer);
assert_eq!(&bigger >> (1000 + i), answer);
}
}
}

#[test]
fn test_iter_sum() {
let result: BigInt = FromPrimitive::from_isize(-1234567).unwrap();
Expand Down
22 changes: 0 additions & 22 deletions tests/roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ extern crate num_bigint;
extern crate num_integer;
extern crate num_traits;

#[cfg(feature = "rand")]
extern crate rand;

mod biguint {
use num_bigint::BigUint;
use num_traits::{One, Pow, Zero};
Expand Down Expand Up @@ -101,25 +98,6 @@ mod biguint {
assert!(x.nth_root(u32::MAX).is_one());
}

#[cfg(feature = "rand")]
#[test]
fn test_roots_rand() {
use num_bigint::RandBigInt;
use rand::distributions::Uniform;
use rand::{thread_rng, Rng};

let mut rng = 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
Expand Down

0 comments on commit bee58ab

Please sign in to comment.