@@ -496,6 +496,11 @@ module!(native_endian, NativeEndian, "native-endian");
496
496
#[ cfg( test) ]
497
497
mod tests {
498
498
use :: byteorder:: NativeEndian ;
499
+ use rand:: {
500
+ distributions:: { Distribution , Standard } ,
501
+ rngs:: SmallRng ,
502
+ Rng , SeedableRng ,
503
+ } ;
499
504
500
505
use {
501
506
super :: * ,
@@ -507,7 +512,12 @@ mod tests {
507
512
const ZERO : Self ;
508
513
const MAX_VALUE : Self ;
509
514
510
- fn rand ( ) -> Self ;
515
+ type Distribution : Distribution < Self > ;
516
+ const DIST : Self :: Distribution ;
517
+
518
+ fn rand < R : Rng > ( rng : & mut R ) -> Self {
519
+ rng. sample ( Self :: DIST )
520
+ }
511
521
}
512
522
513
523
trait ByteArray :
@@ -570,9 +580,8 @@ mod tests {
570
580
const ZERO : $native = 0 as $native;
571
581
const MAX_VALUE : $native = $native:: MAX ;
572
582
573
- fn rand( ) -> $native {
574
- rand:: random( )
575
- }
583
+ type Distribution = Standard ;
584
+ const DIST : Standard = Standard ;
576
585
}
577
586
578
587
impl <O : ByteOrder > ByteOrderType for $name<O > {
@@ -646,6 +655,36 @@ mod tests {
646
655
#[ cfg( target_endian = "little" ) ]
647
656
type NonNativeEndian = BigEndian ;
648
657
658
+ // We use a `u64` seed so that we can use `SeedableRng::seed_from_u64`.
659
+ // `SmallRng`'s `SeedableRng::Seed` differs by platform, so if we wanted to
660
+ // call `SeedableRng::from_seed`, which takes a `Seed`, we would need
661
+ // conditional compilation by `target_pointer_width`.
662
+ const RNG_SEED : u64 = 0x7A03CAE2F32B5B8F ;
663
+
664
+ const RAND_ITERS : usize = if cfg ! ( miri) {
665
+ // The tests below which use this constant used to take a very long time
666
+ // on Miri, which slows down local development and CI jobs. We're not
667
+ // using Miri to check for the correctness of our code, but rather its
668
+ // soundness, and at least in the context of these particular tests, a
669
+ // single loop iteration is just as good for surfacing UB as multiple
670
+ // iterations are.
671
+ //
672
+ // As of the writing of this comment, here's one set of measurements:
673
+ //
674
+ // $ # RAND_ITERS == 1
675
+ // $ cargo miri test -- -Z unstable-options --report-time endian
676
+ // test byteorder::tests::test_native_endian ... ok <0.049s>
677
+ // test byteorder::tests::test_non_native_endian ... ok <0.061s>
678
+ //
679
+ // $ # RAND_ITERS == 1024
680
+ // $ cargo miri test -- -Z unstable-options --report-time endian
681
+ // test byteorder::tests::test_native_endian ... ok <25.716s>
682
+ // test byteorder::tests::test_non_native_endian ... ok <38.127s>
683
+ 1
684
+ } else {
685
+ 1024
686
+ } ;
687
+
649
688
#[ test]
650
689
fn test_zero ( ) {
651
690
fn test_zero < T : ByteOrderType > ( ) {
@@ -669,8 +708,9 @@ mod tests {
669
708
#[ test]
670
709
fn test_native_endian ( ) {
671
710
fn test_native_endian < T : ByteOrderType > ( ) {
672
- for _ in 0 ..1024 {
673
- let native = T :: Native :: rand ( ) ;
711
+ let mut r = SmallRng :: seed_from_u64 ( RNG_SEED ) ;
712
+ for _ in 0 ..RAND_ITERS {
713
+ let native = T :: Native :: rand ( & mut r) ;
674
714
let mut bytes = T :: ByteArray :: default ( ) ;
675
715
bytes. as_bytes_mut ( ) . copy_from_slice ( native. as_bytes ( ) ) ;
676
716
let mut from_native = T :: new ( native) ;
@@ -681,7 +721,7 @@ mod tests {
681
721
assert_eq ! ( from_native. into_bytes( ) , bytes) ;
682
722
assert_eq ! ( from_bytes. into_bytes( ) , bytes) ;
683
723
684
- let updated = T :: Native :: rand ( ) ;
724
+ let updated = T :: Native :: rand ( & mut r ) ;
685
725
from_native. set ( updated) ;
686
726
assert_eq ! ( from_native. get( ) , updated) ;
687
727
}
@@ -693,8 +733,9 @@ mod tests {
693
733
#[ test]
694
734
fn test_non_native_endian ( ) {
695
735
fn test_non_native_endian < T : ByteOrderType > ( ) {
696
- for _ in 0 ..1024 {
697
- let native = T :: Native :: rand ( ) ;
736
+ let mut r = SmallRng :: seed_from_u64 ( RNG_SEED ) ;
737
+ for _ in 0 ..RAND_ITERS {
738
+ let native = T :: Native :: rand ( & mut r) ;
698
739
let mut bytes = T :: ByteArray :: default ( ) ;
699
740
bytes. as_bytes_mut ( ) . copy_from_slice ( native. as_bytes ( ) ) ;
700
741
bytes = bytes. invert ( ) ;
@@ -706,7 +747,7 @@ mod tests {
706
747
assert_eq ! ( from_native. into_bytes( ) , bytes) ;
707
748
assert_eq ! ( from_bytes. into_bytes( ) , bytes) ;
708
749
709
- let updated = T :: Native :: rand ( ) ;
750
+ let updated = T :: Native :: rand ( & mut r ) ;
710
751
from_native. set ( updated) ;
711
752
assert_eq ! ( from_native. get( ) , updated) ;
712
753
}
0 commit comments