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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#[cfg(test)]
mod tests {
use {Rng, thread_rng, Open01, Closed01};
struct ConstantRng(u64);
impl Rng for ConstantRng {
fn next_u32(&mut self) -> u32 {
let ConstantRng(v) = *self;
v as u32
}
fn next_u64(&mut self) -> u64 {
let ConstantRng(v) = *self;
v
}
}
#[test]
fn floating_point_edge_cases() {
assert!(ConstantRng(0xffff_ffff).gen::<f32>() != 1.0);
assert!(ConstantRng(0xffff_ffff_ffff_ffff).gen::<f64>() != 1.0);
}
#[test]
fn rand_open() {
let mut rng = thread_rng();
for _ in 0..1_000 {
let Open01(f) = rng.gen::<Open01<f64>>();
assert!(0.0 < f && f < 1.0);
let Open01(f) = rng.gen::<Open01<f32>>();
assert!(0.0 < f && f < 1.0);
}
}
#[test]
fn rand_closed() {
let mut rng = thread_rng();
for _ in 0..1_000 {
let Closed01(f) = rng.gen::<Closed01<f64>>();
assert!(0.0 <= f && f <= 1.0);
let Closed01(f) = rng.gen::<Closed01<f32>>();
assert!(0.0 <= f && f <= 1.0);
}
}
}