@@ -526,164 +526,3 @@ pub trait SeedableRng: Sized {
526526 Self :: try_from_rng ( self )
527527 }
528528}
529-
530- #[ cfg( test) ]
531- mod test {
532- use super :: * ;
533-
534- #[ test]
535- fn test_seed_from_u64 ( ) {
536- struct SeedableNum ( u64 ) ;
537- impl SeedableRng for SeedableNum {
538- type Seed = [ u8 ; 8 ] ;
539-
540- fn from_seed ( seed : Self :: Seed ) -> Self {
541- let x: [ u64 ; 1 ] = utils:: read_words ( & seed) ;
542- SeedableNum ( x[ 0 ] )
543- }
544- }
545-
546- const N : usize = 8 ;
547- const SEEDS : [ u64 ; N ] = [ 0u64 , 1 , 2 , 3 , 4 , 8 , 16 , -1i64 as u64 ] ;
548- let mut results = [ 0u64 ; N ] ;
549- for ( i, seed) in SEEDS . iter ( ) . enumerate ( ) {
550- let SeedableNum ( x) = SeedableNum :: seed_from_u64 ( * seed) ;
551- results[ i] = x;
552- }
553-
554- for ( i1, r1) in results. iter ( ) . enumerate ( ) {
555- let weight = r1. count_ones ( ) ;
556- // This is the binomial distribution B(64, 0.5), so chance of
557- // weight < 20 is binocdf(19, 64, 0.5) = 7.8e-4, and same for
558- // weight > 44.
559- assert ! ( ( 20 ..=44 ) . contains( & weight) ) ;
560-
561- for ( i2, r2) in results. iter ( ) . enumerate ( ) {
562- if i1 == i2 {
563- continue ;
564- }
565- let diff_weight = ( r1 ^ r2) . count_ones ( ) ;
566- assert ! ( diff_weight >= 20 ) ;
567- }
568- }
569-
570- // value-breakage test:
571- assert_eq ! ( results[ 0 ] , 5029875928683246316 ) ;
572- }
573-
574- // A stub RNG.
575- struct SomeRng ;
576-
577- impl TryRng for SomeRng {
578- type Error = Infallible ;
579-
580- fn try_next_u32 ( & mut self ) -> Result < u32 , Self :: Error > {
581- unimplemented ! ( )
582- }
583- fn try_next_u64 ( & mut self ) -> Result < u64 , Self :: Error > {
584- unimplemented ! ( )
585- }
586- fn try_fill_bytes ( & mut self , _dst : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
587- unimplemented ! ( )
588- }
589- }
590-
591- impl TryCryptoRng for SomeRng { }
592-
593- #[ test]
594- fn dyn_rng_to_tryrng ( ) {
595- // Illustrates the need for `+ ?Sized` bound in `impl<R: Rng> TryRng for R`.
596-
597- // A method in another crate taking a fallible RNG
598- fn third_party_api ( _rng : & mut ( impl TryRng + ?Sized ) ) -> bool {
599- true
600- }
601-
602- // A method in our crate requiring an infallible RNG
603- fn my_api ( rng : & mut dyn Rng ) -> bool {
604- // We want to call the method above
605- third_party_api ( rng)
606- }
607-
608- assert ! ( my_api( & mut SomeRng ) ) ;
609- }
610-
611- #[ test]
612- fn dyn_cryptorng_to_trycryptorng ( ) {
613- // Illustrates the need for `+ ?Sized` bound in `impl<R: CryptoRng> TryCryptoRng for R`.
614-
615- // A method in another crate taking a fallible RNG
616- fn third_party_api ( _rng : & mut ( impl TryCryptoRng + ?Sized ) ) -> bool {
617- true
618- }
619-
620- // A method in our crate requiring an infallible RNG
621- fn my_api ( rng : & mut dyn CryptoRng ) -> bool {
622- // We want to call the method above
623- third_party_api ( rng)
624- }
625-
626- assert ! ( my_api( & mut SomeRng ) ) ;
627- }
628-
629- #[ test]
630- fn dyn_unwrap_mut_tryrng ( ) {
631- // Illustrates that UnwrapMut may be used over &mut R where R: TryRng
632-
633- fn third_party_api ( _rng : & mut impl Rng ) -> bool {
634- true
635- }
636-
637- fn my_api ( rng : & mut ( impl TryRng + ?Sized ) ) -> bool {
638- let mut infallible_rng = UnwrapErr ( rng) ;
639- third_party_api ( & mut infallible_rng)
640- }
641-
642- assert ! ( my_api( & mut SomeRng ) ) ;
643- }
644-
645- #[ test]
646- fn dyn_unwrap_mut_trycryptorng ( ) {
647- // Crypto variant of the above
648-
649- fn third_party_api ( _rng : & mut impl CryptoRng ) -> bool {
650- true
651- }
652-
653- fn my_api ( rng : & mut ( impl TryCryptoRng + ?Sized ) ) -> bool {
654- let mut infallible_rng = UnwrapErr ( rng) ;
655- third_party_api ( & mut infallible_rng)
656- }
657-
658- assert ! ( my_api( & mut SomeRng ) ) ;
659- }
660-
661- #[ test]
662- fn reborrow_unwrap_mut ( ) {
663- struct FourRng ;
664-
665- impl TryRng for FourRng {
666- type Error = Infallible ;
667- fn try_next_u32 ( & mut self ) -> Result < u32 , Self :: Error > {
668- Ok ( 4 )
669- }
670- fn try_next_u64 ( & mut self ) -> Result < u64 , Self :: Error > {
671- unimplemented ! ( )
672- }
673- fn try_fill_bytes ( & mut self , _: & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
674- unimplemented ! ( )
675- }
676- }
677-
678- let mut rng = FourRng ;
679- let mut rng = UnwrapErr ( & mut rng) ;
680-
681- assert_eq ! ( rng. next_u32( ) , 4 ) ;
682- {
683- let mut rng2 = rng. re ( ) ;
684- assert_eq ! ( rng2. next_u32( ) , 4 ) ;
685- // Make sure rng2 is dropped.
686- }
687- assert_eq ! ( rng. next_u32( ) , 4 ) ;
688- }
689- }
0 commit comments