Skip to content

Commit b85db60

Browse files
committed
Add test against possible undefined behavior
1 parent 1199976 commit b85db60

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/bool.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![cfg(feature = "std")]
2+
3+
extern crate rand;
4+
extern crate libc;
5+
6+
use rand::{FromEntropy, SmallRng};
7+
use rand::distributions::{Distribution, Bernoulli};
8+
9+
#[link_name = "m"]
10+
extern "C" {
11+
fn nextafter(x: f64, y: f64) -> f64;
12+
}
13+
14+
/// This test should make sure that we don't accidentally have undefined
15+
/// behavior for large propabilties due to
16+
/// https://github.com/rust-lang/rust/issues/10184.
17+
#[test]
18+
fn large_probability() {
19+
let p = unsafe { nextafter(1., 0.) };
20+
assert!(p < 1.);
21+
let d = Bernoulli::new(p);
22+
let mut rng = SmallRng::from_entropy();
23+
for _ in 0..10 {
24+
assert!(d.sample(&mut rng));
25+
}
26+
}

0 commit comments

Comments
 (0)