Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't enable rand/std by default #90

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ jobs:
command: test
args: --features "std,schemars"

- name: Test (rand features subset)
uses: actions-rs/cargo@v1
with:
command: test
args: --features "rand,randtest"

- name: Test (all features)
uses: actions-rs/cargo@v1
with:
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ordered-float"
version = "2.5.0"
version = "2.5.1"
authors = [
"Jonathan Reem <jonathan.reem@gmail.com>",
"Matt Brubeck <mbrubeck@limpet.net>",
Expand All @@ -24,4 +24,5 @@ serde_test = "1.0"

[features]
default = ["std"]
std = ["num-traits/std", "rand/std", "rand/std_rng"]
std = ["num-traits/std"]
randtest = ["rand/std", "rand/std_rng"]
112 changes: 58 additions & 54 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ mod impl_schemars {
}
}

#[cfg(all(feature = "std", feature = "rand"))]
#[cfg(feature = "rand")]
mod impl_rand {
use super::{NotNan, OrderedFloat};
use rand::distributions::uniform::*;
Expand Down Expand Up @@ -1720,63 +1720,67 @@ mod impl_rand {
impl_uniform_sampler! { f32 }
impl_uniform_sampler! { f64 }

#[cfg(test)]
fn sample_fuzz<T>()
where
Standard: Distribution<NotNan<T>>,
Open01: Distribution<NotNan<T>>,
OpenClosed01: Distribution<NotNan<T>>,
Standard: Distribution<OrderedFloat<T>>,
Open01: Distribution<OrderedFloat<T>>,
OpenClosed01: Distribution<OrderedFloat<T>>,
T: super::Float,
{
let mut rng = rand::thread_rng();
let f1: NotNan<T> = rng.sample(Standard);
let f2: NotNan<T> = rng.sample(Open01);
let f3: NotNan<T> = rng.sample(OpenClosed01);
let _: OrderedFloat<T> = rng.sample(Standard);
let _: OrderedFloat<T> = rng.sample(Open01);
let _: OrderedFloat<T> = rng.sample(OpenClosed01);
assert!(!f1.into_inner().is_nan());
assert!(!f2.into_inner().is_nan());
assert!(!f3.into_inner().is_nan());
}
#[cfg(all(test, feature = "randtest"))]
mod tests {
use super::*;

#[test]
fn sampling_f32_does_not_panic() {
sample_fuzz::<f32>();
}
fn sample_fuzz<T>()
where
Standard: Distribution<NotNan<T>>,
Open01: Distribution<NotNan<T>>,
OpenClosed01: Distribution<NotNan<T>>,
Standard: Distribution<OrderedFloat<T>>,
Open01: Distribution<OrderedFloat<T>>,
OpenClosed01: Distribution<OrderedFloat<T>>,
T: crate::Float,
{
let mut rng = rand::thread_rng();
let f1: NotNan<T> = rng.sample(Standard);
let f2: NotNan<T> = rng.sample(Open01);
let f3: NotNan<T> = rng.sample(OpenClosed01);
let _: OrderedFloat<T> = rng.sample(Standard);
let _: OrderedFloat<T> = rng.sample(Open01);
let _: OrderedFloat<T> = rng.sample(OpenClosed01);
assert!(!f1.into_inner().is_nan());
assert!(!f2.into_inner().is_nan());
assert!(!f3.into_inner().is_nan());
}

#[test]
fn sampling_f64_does_not_panic() {
sample_fuzz::<f64>();
}
#[test]
fn sampling_f32_does_not_panic() {
sample_fuzz::<f32>();
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_infinity_notnan() {
let (low, high) = (
NotNan::new(0f64).unwrap(),
NotNan::new(std::f64::INFINITY).unwrap(),
);
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}
#[test]
fn sampling_f64_does_not_panic() {
sample_fuzz::<f64>();
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_infinity_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(std::f64::INFINITY));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}
#[test]
#[should_panic]
fn uniform_sampling_panic_on_infinity_notnan() {
let (low, high) = (
NotNan::new(0f64).unwrap(),
NotNan::new(core::f64::INFINITY).unwrap(),
);
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_nan_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(std::f64::NAN));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
#[test]
#[should_panic]
fn uniform_sampling_panic_on_infinity_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(core::f64::INFINITY));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_nan_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(core::f64::NAN));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}
}
}