1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.util;
18
19import java.util.Arrays;
20import java.util.Random;
21
22public class RandomTest extends junit.framework.TestCase {
23    public void test_subclassing() throws Exception {
24        // http://b/2502231
25        // Ensure that Random's constructors call setSeed by emulating the active ingredient
26        // from the bug: the subclass' setSeed had a side-effect necessary for the correct
27        // functioning of next.
28        class MyRandom extends Random {
29            public String state;
30            public MyRandom() { super(); }
31            public MyRandom(long l) { super(l); }
32            @Override protected synchronized int next(int bits) { return state.length(); }
33            @Override public synchronized void setSeed(long seed) { state = Long.toString(seed); }
34        }
35        // Test the 0-argument constructor...
36        MyRandom r1 = new MyRandom();
37        r1.nextInt();
38        assertNotNull(r1.state);
39        // Test the 1-argument constructor...
40        MyRandom r2 = new MyRandom(123L);
41        r2.nextInt();
42        assertNotNull(r2.state);
43    }
44
45    public void test_ints$() {
46        final int limit = 128; // We can't test for every element in an infinite stream.
47
48        Random rand = new Random(0);
49        int[] rands = new int[limit];
50        for(int i = 0; i < limit; ++i) {
51            rands[i] = rand.nextInt();
52        }
53
54        int[] streamRands = new Random(0).ints().limit(limit).toArray();
55        assertTrue(Arrays.equals(rands, streamRands));
56    }
57
58    public void test_ints$L() {
59        final int size = 32;
60
61        Random rand = new Random(0);
62        int[] rands = new int[size];
63        for(int i = 0; i < size; ++i) {
64            rands[i] = rand.nextInt();
65        }
66
67        int[] streamRands = new Random(0).ints(size).toArray();
68        assertTrue(Arrays.equals(rands, streamRands));
69        assertEquals(size, new Random(0).ints(size).count());
70
71        try {
72            new Random(0).ints(-1);
73            fail();
74        } catch (IllegalArgumentException expected) {}
75    }
76
77    public void test_ints$II() {
78        final int limit = 128; // We can't test for every element in an infinite stream.
79        final int origin = 128, bound = 256;
80
81        Random rand = new Random(0);
82        int[] rands = new int[limit];
83        for(int i = 0; i < limit; ++i) {
84            rands[i] = rand.nextInt(bound - origin) + origin;
85        }
86
87        int[] streamRands = new Random(0).ints(origin, bound).limit(limit).toArray();
88        assertTrue(Arrays.equals(rands, streamRands));
89
90        try {
91            new Random(0).ints(100, 0);
92            fail();
93        } catch (IllegalArgumentException expected) {}
94    }
95
96    public void test_ints$LII() {
97        final int size = 32;
98        final int origin = 128, bound = 256;
99
100        Random rand = new Random(0);
101        int[] rands = new int[size];
102        for(int i = 0; i < size; ++i) {
103            rands[i] = rand.nextInt(bound - origin) + origin;
104        }
105
106        int[] streamRands = new Random(0).ints(size, origin, bound).toArray();
107        assertTrue(Arrays.equals(rands, streamRands));
108        assertEquals(size, new Random(0).ints(size, origin, bound).count());
109
110        try {
111            new Random(0).ints(-1, 10, 20);
112            fail();
113        } catch (IllegalArgumentException expected) {}
114        try {
115            new Random(0).ints(10, 100, 0);
116            fail();
117        } catch (IllegalArgumentException expected) {}
118    }
119
120    public void test_longs$() {
121        final int limit = 128; // We can't test for every element in an infinite stream.
122
123        Random rand = new Random(0);
124        long[] rands = new long[limit];
125        for(int i = 0; i < limit; ++i) {
126            rands[i] = rand.nextLong();
127        }
128
129        long[] streamRands = new Random(0).longs().limit(limit).toArray();
130        assertTrue(Arrays.equals(rands, streamRands));
131    }
132
133    public void test_longs$L() {
134        final int size = 32;
135
136        Random rand = new Random(0);
137        long[] rands = new long[size];
138        for(int i = 0; i < size; ++i) {
139            rands[i] = rand.nextLong();
140        }
141
142        long[] streamRands = new Random(0).longs(size).toArray();
143        assertTrue(Arrays.equals(rands, streamRands));
144        assertEquals(size, new Random(0).longs(size).count());
145
146        try {
147            new Random(0).longs(-1);
148            fail();
149        } catch (IllegalArgumentException expected) {}
150    }
151
152    public void test_longs$II() {
153        final int limit = 128; // We can't test for every element in an infinite stream.
154        final int origin = 128, bound = 256;
155
156        Random rand = new Random(0);
157        long[] rands = new long[limit];
158        for(int i = 0; i < limit; ++i) {
159            rands[i] = (rand.nextLong() & 127) + origin;
160        }
161
162        long[] streamRands = new Random(0).longs(origin, bound).limit(limit).toArray();
163        assertTrue(Arrays.equals(rands, streamRands));
164
165        try {
166            new Random(0).longs(100, 0);
167            fail();
168        } catch (IllegalArgumentException expected) {}
169    }
170
171    public void test_longs$LII() {
172        final int size = 32;
173        final int origin = 128, bound = 256;
174
175        Random rand = new Random(0);
176        long[] rands = new long[size];
177        for(int i = 0; i < size; ++i) {
178            rands[i] = (rand.nextLong() & 127) + origin;
179        }
180
181        long[] streamRands = new Random(0).longs(size, origin, bound).toArray();
182        assertTrue(Arrays.equals(rands, streamRands));
183        assertEquals(size, new Random(0).longs(size, origin, bound).count());
184
185        try {
186            new Random(0).longs(-1, 10, 20);
187            fail();
188        } catch (IllegalArgumentException expected) {}
189        try {
190            new Random(0).longs(10, 100, 0);
191            fail();
192        } catch (IllegalArgumentException expected) {}
193    }
194
195    public void test_doubles$() {
196        final int limit = 128; // We can't test for every element in an infinite stream.
197
198        Random rand = new Random(0);
199        double[] rands = new double[limit];
200        for(int i = 0; i < limit; ++i) {
201            rands[i] = rand.nextDouble();
202        }
203
204        double[] streamRands = new Random(0).doubles().limit(limit).toArray();
205        assertTrue(Arrays.equals(rands, streamRands));
206    }
207
208    public void test_doubles$L() {
209        final int size = 32;
210
211        Random rand = new Random(0);
212        double[] rands = new double[size];
213        for(int i = 0; i < size; ++i) {
214            rands[i] = rand.nextDouble();
215        }
216
217        double[] streamRands = new Random(0).doubles(size).toArray();
218        assertTrue(Arrays.equals(rands, streamRands));
219        assertEquals(size, new Random(0).doubles(size).count());
220
221        try {
222            new Random(0).ints(-1);
223            fail();
224        } catch (IllegalArgumentException expected) {}
225    }
226
227    public void test_doubles$II() {
228        final int limit = 128; // We can't test for every element in an infinite stream.
229        final int origin = 128, bound = 256;
230
231        Random rand = new Random(0);
232        double[] rands = new double[limit];
233        for(int i = 0; i < limit; ++i) {
234            double r = rand.nextDouble() * (bound - origin) + origin;
235            if (r >= bound) {
236                r = Math.nextDown(r);
237            }
238            rands[i] = r;
239        }
240
241        double[] streamRands = new Random(0).doubles(origin, bound).limit(limit).toArray();
242        assertTrue(Arrays.equals(rands, streamRands));
243
244        try {
245            new Random(0).doubles(100, 0);
246            fail();
247        } catch (IllegalArgumentException expected) {}
248    }
249
250    public void test_doubles$LII() {
251        final int size = 32;
252        final int origin = 128, bound = 256;
253
254        Random rand = new Random(0);
255        double[] rands = new double[size];
256        for(int i = 0; i < size; ++i) {
257            double r = rand.nextDouble() * (bound - origin) + origin;
258            if (r >= bound) {
259                r = Math.nextDown(r);
260            }
261            rands[i] = r;
262        }
263
264        double[] streamRands = new Random(0).doubles(size, origin, bound).toArray();
265        assertTrue(Arrays.equals(rands, streamRands));
266        assertEquals(size, new Random(0).doubles(size, origin, bound).count());
267
268        try {
269            new Random(0).doubles(-1, 10, 20);
270            fail();
271        } catch (IllegalArgumentException expected) {}
272        try {
273            new Random(0).doubles(10, 100, 0);
274            fail();
275        } catch (IllegalArgumentException expected) {}
276    }
277}
278