1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.util;
19
20import java.util.Random;
21
22public class RandomTest extends junit.framework.TestCase {
23
24	Random r;
25
26	/**
27	 * @tests java.util.Random#Random()
28	 */
29	public void test_Constructor() {
30		// Test for method java.util.Random()
31		assertTrue("Used to test", true);
32	}
33
34	/**
35	 * @tests java.util.Random#Random(long)
36	 */
37	public void test_ConstructorJ() {
38		Random r = new Random(8409238L);
39		Random r2 = new Random(8409238L);
40		for (int i = 0; i < 100; i++)
41			assertTrue("Values from randoms with same seed don't match", r
42					.nextInt() == r2.nextInt());
43	}
44
45	/**
46	 * @tests java.util.Random#nextBoolean()
47	 */
48	public void test_nextBoolean() {
49		// Test for method boolean java.util.Random.nextBoolean()
50		boolean falseAppeared = false, trueAppeared = false;
51		for (int counter = 0; counter < 100; counter++)
52			if (r.nextBoolean())
53				trueAppeared = true;
54			else
55				falseAppeared = true;
56		assertTrue("Calling nextBoolean() 100 times resulted in all trues",
57				falseAppeared);
58		assertTrue("Calling nextBoolean() 100 times resulted in all falses",
59				trueAppeared);
60	}
61
62	/**
63	 * @tests java.util.Random#nextBytes(byte[])
64	 */
65	public void test_nextBytes$B() {
66		// Test for method void java.util.Random.nextBytes(byte [])
67		boolean someDifferent = false;
68		byte[] randomBytes = new byte[100];
69		r.nextBytes(randomBytes);
70		byte firstByte = randomBytes[0];
71		for (int counter = 1; counter < randomBytes.length; counter++)
72			if (randomBytes[counter] != firstByte)
73				someDifferent = true;
74		assertTrue(
75				"nextBytes() returned an array of length 100 of the same byte",
76				someDifferent);
77	}
78
79	/**
80	 * @tests java.util.Random#nextDouble()
81	 */
82	public void test_nextDouble() {
83		// Test for method double java.util.Random.nextDouble()
84		double lastNum = r.nextDouble();
85		double nextNum;
86		boolean someDifferent = false;
87		boolean inRange = true;
88		for (int counter = 0; counter < 100; counter++) {
89			nextNum = r.nextDouble();
90			if (nextNum != lastNum)
91				someDifferent = true;
92			if (!(0 <= nextNum && nextNum < 1.0))
93				inRange = false;
94			lastNum = nextNum;
95		}
96		assertTrue("Calling nextDouble 100 times resulted in same number",
97				someDifferent);
98		assertTrue(
99				"Calling nextDouble resulted in a number out of range [0,1)",
100				inRange);
101	}
102
103	/**
104	 * @tests java.util.Random#nextFloat()
105	 */
106	public void test_nextFloat() {
107		// Test for method float java.util.Random.nextFloat()
108		float lastNum = r.nextFloat();
109		float nextNum;
110		boolean someDifferent = false;
111		boolean inRange = true;
112		for (int counter = 0; counter < 100; counter++) {
113			nextNum = r.nextFloat();
114			if (nextNum != lastNum)
115				someDifferent = true;
116			if (!(0 <= nextNum && nextNum < 1.0))
117				inRange = false;
118			lastNum = nextNum;
119		}
120		assertTrue("Calling nextFloat 100 times resulted in same number",
121				someDifferent);
122		assertTrue("Calling nextFloat resulted in a number out of range [0,1)",
123				inRange);
124	}
125
126	/**
127	 * @tests java.util.Random#nextGaussian()
128	 */
129	public void test_nextGaussian() {
130		// Test for method double java.util.Random.nextGaussian()
131		double lastNum = r.nextGaussian();
132		double nextNum;
133		boolean someDifferent = false;
134		boolean someInsideStd = false;
135		for (int counter = 0; counter < 100; counter++) {
136			nextNum = r.nextGaussian();
137			if (nextNum != lastNum)
138				someDifferent = true;
139			if (-1.0 <= nextNum && nextNum <= 1.0)
140				someInsideStd = true;
141			lastNum = nextNum;
142		}
143		assertTrue("Calling nextGaussian 100 times resulted in same number",
144				someDifferent);
145		assertTrue(
146				"Calling nextGaussian 100 times resulted in no number within 1 std. deviation of mean",
147				someInsideStd);
148	}
149
150	/**
151	 * @tests java.util.Random#nextInt()
152	 */
153	public void test_nextInt() {
154		// Test for method int java.util.Random.nextInt()
155		int lastNum = r.nextInt();
156		int nextNum;
157		boolean someDifferent = false;
158		for (int counter = 0; counter < 100; counter++) {
159			nextNum = r.nextInt();
160			if (nextNum != lastNum)
161				someDifferent = true;
162			lastNum = nextNum;
163		}
164		assertTrue("Calling nextInt 100 times resulted in same number",
165				someDifferent);
166	}
167
168	/**
169	 * @tests java.util.Random#nextInt(int)
170	 */
171	public void test_nextIntI() {
172		// Test for method int java.util.Random.nextInt(int)
173		final int range = 10;
174		int lastNum = r.nextInt(range);
175		int nextNum;
176		boolean someDifferent = false;
177		boolean inRange = true;
178		for (int counter = 0; counter < 100; counter++) {
179			nextNum = r.nextInt(range);
180			if (nextNum != lastNum)
181				someDifferent = true;
182			if (!(0 <= nextNum && nextNum < range))
183				inRange = false;
184			lastNum = nextNum;
185		}
186		assertTrue("Calling nextInt (range) 100 times resulted in same number",
187				someDifferent);
188		assertTrue(
189				"Calling nextInt (range) resulted in a number outside of [0, range)",
190				inRange);
191
192	}
193
194	/**
195	 * @tests java.util.Random#nextLong()
196	 */
197	public void test_nextLong() {
198		// Test for method long java.util.Random.nextLong()
199		long lastNum = r.nextLong();
200		long nextNum;
201		boolean someDifferent = false;
202		for (int counter = 0; counter < 100; counter++) {
203			nextNum = r.nextLong();
204			if (nextNum != lastNum)
205				someDifferent = true;
206			lastNum = nextNum;
207		}
208		assertTrue("Calling nextLong 100 times resulted in same number",
209				someDifferent);
210	}
211
212	/**
213	 * @tests java.util.Random#setSeed(long)
214	 */
215	public void test_setSeedJ() {
216		// Test for method void java.util.Random.setSeed(long)
217		long[] randomArray = new long[100];
218		boolean someDifferent = false;
219		final long firstSeed = 1000;
220		long aLong, anotherLong, yetAnotherLong;
221		Random aRandom = new Random();
222		Random anotherRandom = new Random();
223		Random yetAnotherRandom = new Random();
224		aRandom.setSeed(firstSeed);
225		anotherRandom.setSeed(firstSeed);
226		for (int counter = 0; counter < randomArray.length; counter++) {
227			aLong = aRandom.nextLong();
228			anotherLong = anotherRandom.nextLong();
229			assertTrue(
230					"Two randoms with same seeds gave differing nextLong values",
231					aLong == anotherLong);
232			yetAnotherLong = yetAnotherRandom.nextLong();
233			randomArray[counter] = aLong;
234			if (aLong != yetAnotherLong)
235				someDifferent = true;
236		}
237		assertTrue(
238				"Two randoms with the different seeds gave the same chain of values",
239				someDifferent);
240		aRandom.setSeed(firstSeed);
241		for (int counter = 0; counter < randomArray.length; counter++)
242			assertTrue(
243					"Reseting a random to its old seed did not result in the same chain of values as it gave before",
244					aRandom.nextLong() == randomArray[counter]);
245	}
246
247    // two random create at a time should also generated different results
248    // regression test for Harmony 4616
249    public void test_random_generate() throws Exception {
250        for (int i = 0; i < 100; i++) {
251            Random random1 = new Random();
252            Random random2 = new Random();
253            assertFalse(random1.nextLong() == random2.nextLong());
254        }
255    }
256
257
258	/**
259	 * Sets up the fixture, for example, open a network connection. This method
260	 * is called before a test is executed.
261	 */
262	protected void setUp() {
263		r = new Random();
264	}
265
266	/**
267	 * Tears down the fixture, for example, close a network connection. This
268	 * method is called after a test is executed.
269	 */
270	protected void tearDown() {
271	}
272}
273