Skip to content

Commit d639908

Browse files
authored
Merge pull request #2883 from ljedrz/test/counting_rng
[Feat] Count the number of calls to TestRng
2 parents 98998c2 + 090a458 commit d639908

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

utilities/src/rand.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ where
3838
}
3939

4040
/// A fast RNG used **solely** for testing and benchmarking, **not** for any real world purposes.
41-
pub struct TestRng(XorShiftRng);
41+
pub struct TestRng {
42+
seed: u64,
43+
rng: XorShiftRng,
44+
calls: usize,
45+
}
4246

4347
impl Default for TestRng {
4448
fn default() -> Self {
@@ -63,7 +67,7 @@ impl TestRng {
6367
// been initialized in a test or benchmark and an auxiliary one is desired without
6468
// spamming the stdout.
6569
pub fn from_seed(seed: u64) -> Self {
66-
Self(XorShiftRng::seed_from_u64(seed))
70+
Self { seed, rng: XorShiftRng::seed_from_u64(seed), calls: 0 }
6771
}
6872

6973
/// Returns a randomly-sampled `String`, given the maximum size in bytes and an RNG.
@@ -131,20 +135,30 @@ impl TestRng {
131135

132136
impl rand::RngCore for TestRng {
133137
fn next_u32(&mut self) -> u32 {
134-
self.0.next_u32()
138+
self.calls += 1;
139+
self.rng.next_u32()
135140
}
136141

137142
fn next_u64(&mut self) -> u64 {
138-
self.0.next_u64()
143+
self.calls += 1;
144+
self.rng.next_u64()
139145
}
140146

141147
fn fill_bytes(&mut self, dest: &mut [u8]) {
142-
self.0.fill_bytes(dest)
148+
self.calls += 1;
149+
self.rng.fill_bytes(dest)
143150
}
144151

145152
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
146-
self.0.try_fill_bytes(dest)
153+
self.calls += 1;
154+
self.rng.try_fill_bytes(dest)
147155
}
148156
}
149157

150158
impl rand::CryptoRng for TestRng {}
159+
160+
impl Drop for TestRng {
161+
fn drop(&mut self) {
162+
println!("Called TestRng with seed {} {} times", self.seed, self.calls);
163+
}
164+
}

0 commit comments

Comments
 (0)