2727/**
2828 * @test
2929 * @summary RandomSupport.convertSeedBytesToLongs sign extension overwrites previous bytes.
30- * @bug 8282144
30+ * @bug 8282144 8294509
3131 * @modules java.base/jdk.internal.util.random
3232 * @run main T8282144
3333 * @key randomness
3636
3737public class T8282144 {
3838 public static void main (String [] args ) {
39+ testLongs ();
40+ testInts ();
41+ }
42+
43+ private static void testLongs () {
3944 RandomGenerator rng = RandomGeneratorFactory .of ("L64X128MixRandom" ).create (42 );
4045
4146 for (int i = 1 ; i < 8 ; i ++) {
@@ -56,6 +61,27 @@ public static void main(String[] args) {
5661 }
5762 }
5863
64+ private static void testInts () {
65+ RandomGenerator rng = RandomGeneratorFactory .of ("L64X128MixRandom" ).create (42 );
66+
67+ for (int i = 1 ; i < 8 ; i ++) {
68+ byte [] seed = new byte [i ];
69+
70+ for (int j = 0 ; j < 10 ; j ++) {
71+ rng .nextBytes (seed );
72+
73+ int [] existing = RandomSupport .convertSeedBytesToInts (seed , 1 , 1 );
74+ int [] testing = convertSeedBytesToIntsFixed (seed , 1 , 1 );
75+
76+ for (int k = 0 ; k < existing .length ; k ++) {
77+ if (existing [k ] != testing [k ]) {
78+ throw new RuntimeException ("convertSeedBytesToInts incorrect" );
79+ }
80+ }
81+ }
82+ }
83+ }
84+
5985
6086 public static long [] convertSeedBytesToLongsFixed (byte [] seed , int n , int z ) {
6187 final long [] result = new long [n ];
@@ -68,4 +94,17 @@ public static long[] convertSeedBytesToLongsFixed(byte[] seed, int n, int z) {
6894
6995 return result ;
7096 }
97+
98+ public static int [] convertSeedBytesToIntsFixed (byte [] seed , int n , int z ) {
99+ final int [] result = new int [n ];
100+ final int m = Math .min (seed .length , n << 2 );
101+
102+ // Distribute seed bytes into the words to be formed.
103+ for (int j = 0 ; j < m ; j ++) {
104+ result [j >> 2 ] = (result [j >> 2 ] << 8 ) | (seed [j ] & 0xff );
105+ }
106+
107+ return result ;
108+ }
109+
71110}
0 commit comments