Debian Codegraph @

debian/rust-vendor/rand_core/tests/mod.rs

raw
1 //! Main `rand_core` tests
2 use rand_core::{CryptoRng, Infallible, Rng, SeedableRng, TryCryptoRng, TryRng, UnwrapErr, utils};
3
4 #[test]
5 fn test_seed_from_u64() {
6 struct SeedableNum(u64);
7 impl SeedableRng for SeedableNum {
8 type Seed = [u8; 8];
9
10 fn from_seed(seed: Self::Seed) -> Self {
11 let x: [u64; 1] = utils::read_words(&seed);
12 SeedableNum(x[0])
13 }
14 }
15
16 const N: usize = 8;
17 const SEEDS: [u64; N] = [0u64, 1, 2, 3, 4, 8, 16, -1i64 as u64];
18 let mut results = [0u64; N];
19 for (i, seed) in SEEDS.iter().enumerate() {
20 let SeedableNum(x) = SeedableNum::seed_from_u64(*seed);
21 results[i] = x;
22 }
23
24 for (i1, r1) in results.iter().enumerate() {
25 let weight = r1.count_ones();
26 // This is the binomial distribution B(64, 0.5), so chance of
27 // weight < 20 is binocdf(19, 64, 0.5) = 7.8e-4, and same for
28 // weight > 44.
29 assert!((20..=44).contains(&weight));
30
31 for (i2, r2) in results.iter().enumerate() {
32 if i1 == i2 {
33 continue;
34 }
35 let diff_weight = (r1 ^ r2).count_ones();
36 assert!(diff_weight >= 20);
37 }
38 }
39
40 // value-breakage test:
41 assert_eq!(results[0], 5029875928683246316);
42 }
43
44 // A stub RNG.
45 struct SomeRng;
46
47 impl TryRng for SomeRng {
48 type Error = Infallible;
49
50 fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
51 unimplemented!()
52 }
53 fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
54 unimplemented!()
55 }
56 fn try_fill_bytes(&mut self, _dst: &mut [u8]) -> Result<(), Self::Error> {
57 unimplemented!()
58 }
59 }
60
61 impl TryCryptoRng for SomeRng {}
62
63 #[test]
64 fn dyn_rng_to_tryrng() {
65 // Illustrates the need for `+ ?Sized` bound in `impl<R: Rng> TryRng for R`.
66
67 // A method in another crate taking a fallible RNG
68 fn third_party_api(_rng: &mut (impl TryRng + ?Sized)) -> bool {
69 true
70 }
71
72 // A method in our crate requiring an infallible RNG
73 fn my_api(rng: &mut dyn Rng) -> bool {
74 // We want to call the method above
75 third_party_api(rng)
76 }
77
78 assert!(my_api(&mut SomeRng));
79 }
80
81 #[test]
82 fn dyn_cryptorng_to_trycryptorng() {
83 // Illustrates the need for `+ ?Sized` bound in `impl<R: CryptoRng> TryCryptoRng for R`.
84
85 // A method in another crate taking a fallible RNG
86 fn third_party_api(_rng: &mut (impl TryCryptoRng + ?Sized)) -> bool {
87 true
88 }
89
90 // A method in our crate requiring an infallible RNG
91 fn my_api(rng: &mut dyn CryptoRng) -> bool {
92 // We want to call the method above
93 third_party_api(rng)
94 }
95
96 assert!(my_api(&mut SomeRng));
97 }
98
99 #[test]
100 fn dyn_unwrap_mut_tryrng() {
101 // Illustrates that UnwrapMut may be used over &mut R where R: TryRng
102
103 fn third_party_api(_rng: &mut impl Rng) -> bool {
104 true
105 }
106
107 fn my_api(rng: &mut (impl TryRng + ?Sized)) -> bool {
108 let mut infallible_rng = UnwrapErr(rng);
109 third_party_api(&mut infallible_rng)
110 }
111
112 assert!(my_api(&mut SomeRng));
113 }
114
115 #[test]
116 fn dyn_unwrap_mut_trycryptorng() {
117 // Crypto variant of the above
118
119 fn third_party_api(_rng: &mut impl CryptoRng) -> bool {
120 true
121 }
122
123 fn my_api(rng: &mut (impl TryCryptoRng + ?Sized)) -> bool {
124 let mut infallible_rng = UnwrapErr(rng);
125 third_party_api(&mut infallible_rng)
126 }
127
128 assert!(my_api(&mut SomeRng));
129 }
130
131 #[test]
132 fn reborrow_unwrap_mut() {
133 struct FourRng;
134
135 impl TryRng for FourRng {
136 type Error = Infallible;
137 fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
138 Ok(4)
139 }
140 fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
141 unimplemented!()
142 }
143 fn try_fill_bytes(&mut self, _: &mut [u8]) -> Result<(), Self::Error> {
144 unimplemented!()
145 }
146 }
147
148 let mut rng = FourRng;
149 let mut rng = UnwrapErr(&mut rng);
150
151 assert_eq!(rng.next_u32(), 4);
152 {
153 let mut rng2 = rng.re();
154 assert_eq!(rng2.next_u32(), 4);
155 // Make sure rng2 is dropped.
156 }
157 assert_eq!(rng.next_u32(), 4);
158 }