@@ -522,7 +522,9 @@ impl UniformSampler for UniformUsize {
522
522
#[ cfg( test) ]
523
523
mod tests {
524
524
use super :: * ;
525
- use crate :: distr:: Uniform ;
525
+ use crate :: distr:: { Distribution , Uniform } ;
526
+ use core:: fmt:: Debug ;
527
+ use core:: ops:: Add ;
526
528
527
529
#[ test]
528
530
fn test_uniform_bad_limits_equal_int ( ) {
@@ -652,4 +654,95 @@ mod tests {
652
654
assert ! ( Uniform :: try_from( 100 ..=10 ) . is_err( ) ) ;
653
655
assert ! ( Uniform :: try_from( 100 ..=99 ) . is_err( ) ) ;
654
656
}
657
+
658
+ #[ test]
659
+ fn value_stability ( ) {
660
+ fn test_samples < T : SampleUniform + Copy + Debug + PartialEq + Add < T > > (
661
+ lb : T ,
662
+ ub : T ,
663
+ ub_excl : T ,
664
+ expected : & [ T ] ,
665
+ ) where
666
+ Uniform < T > : Distribution < T > ,
667
+ {
668
+ let mut rng = crate :: test:: rng ( 897 ) ;
669
+ let mut buf = [ lb; 6 ] ;
670
+
671
+ for x in & mut buf[ 0 ..3 ] {
672
+ * x = T :: Sampler :: sample_single_inclusive ( lb, ub, & mut rng) . unwrap ( ) ;
673
+ }
674
+
675
+ let distr = Uniform :: new_inclusive ( lb, ub) . unwrap ( ) ;
676
+ for x in & mut buf[ 3 ..6 ] {
677
+ * x = rng. sample ( & distr) ;
678
+ }
679
+ assert_eq ! ( & buf, expected) ;
680
+
681
+ let mut rng = crate :: test:: rng ( 897 ) ;
682
+
683
+ for x in & mut buf[ 0 ..3 ] {
684
+ * x = T :: Sampler :: sample_single ( lb, ub_excl, & mut rng) . unwrap ( ) ;
685
+ }
686
+
687
+ let distr = Uniform :: new ( lb, ub_excl) . unwrap ( ) ;
688
+ for x in & mut buf[ 3 ..6 ] {
689
+ * x = rng. sample ( & distr) ;
690
+ }
691
+ assert_eq ! ( & buf, expected) ;
692
+ }
693
+
694
+ test_samples ( -105i8 , 111 , 112 , & [ -99 , -48 , 107 , 72 , -19 , 56 ] ) ;
695
+ test_samples ( 2i16 , 1352 , 1353 , & [ 43 , 361 , 1325 , 1109 , 539 , 1005 ] ) ;
696
+ test_samples (
697
+ -313853i32 ,
698
+ 13513 ,
699
+ 13514 ,
700
+ & [ -303803 , -226673 , 6912 , -45605 , -183505 , -70668 ] ,
701
+ ) ;
702
+ test_samples (
703
+ 131521i64 ,
704
+ 6542165 ,
705
+ 6542166 ,
706
+ & [ 1838724 , 5384489 , 4893692 , 3712948 , 3951509 , 4094926 ] ,
707
+ ) ;
708
+ test_samples (
709
+ -0x8000_0000_0000_0000_0000_0000_0000_0000i128 ,
710
+ -1 ,
711
+ 0 ,
712
+ & [
713
+ -30725222750250982319765550926688025855 ,
714
+ -75088619368053423329503924805178012357 ,
715
+ -64950748766625548510467638647674468829 ,
716
+ -41794017901603587121582892414659436495 ,
717
+ -63623852319608406524605295913876414006 ,
718
+ -17404679390297612013597359206379189023 ,
719
+ ] ,
720
+ ) ;
721
+ test_samples ( 11u8 , 218 , 219 , & [ 17 , 66 , 214 , 181 , 93 , 165 ] ) ;
722
+ test_samples ( 11u16 , 218 , 219 , & [ 17 , 66 , 214 , 181 , 93 , 165 ] ) ;
723
+ test_samples ( 11u32 , 218 , 219 , & [ 17 , 66 , 214 , 181 , 93 , 165 ] ) ;
724
+ test_samples ( 11u64 , 218 , 219 , & [ 66 , 181 , 165 , 127 , 134 , 139 ] ) ;
725
+ test_samples ( 11u128 , 218 , 219 , & [ 181 , 127 , 139 , 167 , 141 , 197 ] ) ;
726
+ test_samples ( 11usize , 218 , 219 , & [ 17 , 66 , 214 , 181 , 93 , 165 ] ) ;
727
+
728
+ #[ cfg( feature = "simd_support" ) ]
729
+ {
730
+ let lb = Simd :: from ( [ 11u8 , 0 , 128 , 127 ] ) ;
731
+ let ub = Simd :: from ( [ 218 , 254 , 254 , 254 ] ) ;
732
+ let ub_excl = ub + Simd :: splat ( 1 ) ;
733
+ test_samples (
734
+ lb,
735
+ ub,
736
+ ub_excl,
737
+ & [
738
+ Simd :: from ( [ 13 , 5 , 237 , 130 ] ) ,
739
+ Simd :: from ( [ 126 , 186 , 149 , 161 ] ) ,
740
+ Simd :: from ( [ 103 , 86 , 234 , 252 ] ) ,
741
+ Simd :: from ( [ 35 , 18 , 225 , 231 ] ) ,
742
+ Simd :: from ( [ 106 , 153 , 246 , 177 ] ) ,
743
+ Simd :: from ( [ 195 , 168 , 149 , 222 ] ) ,
744
+ ] ,
745
+ ) ;
746
+ }
747
+ }
655
748
}
0 commit comments