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
4347impl 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
132136impl 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
150158impl 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