001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.Random;
021:
022: public class RandomTest extends junit.framework.TestCase {
023:
024: Random r;
025:
026: /**
027: * @tests java.util.Random#Random()
028: */
029: public void test_Constructor() {
030: // Test for method java.util.Random()
031: assertTrue("Used to test", true);
032: }
033:
034: /**
035: * @tests java.util.Random#Random(long)
036: */
037: public void test_ConstructorJ() {
038: Random r = new Random(8409238L);
039: Random r2 = new Random(8409238L);
040: for (int i = 0; i < 100; i++)
041: assertTrue(
042: "Values from randoms with same seed don't match", r
043: .nextInt() == r2.nextInt());
044: }
045:
046: /**
047: * @tests java.util.Random#nextBoolean()
048: */
049: public void test_nextBoolean() {
050: // Test for method boolean java.util.Random.nextBoolean()
051: boolean falseAppeared = false, trueAppeared = false;
052: for (int counter = 0; counter < 100; counter++)
053: if (r.nextBoolean())
054: trueAppeared = true;
055: else
056: falseAppeared = true;
057: assertTrue(
058: "Calling nextBoolean() 100 times resulted in all trues",
059: falseAppeared);
060: assertTrue(
061: "Calling nextBoolean() 100 times resulted in all falses",
062: trueAppeared);
063: }
064:
065: /**
066: * @tests java.util.Random#nextBytes(byte[])
067: */
068: public void test_nextBytes$B() {
069: // Test for method void java.util.Random.nextBytes(byte [])
070: boolean someDifferent = false;
071: byte[] randomBytes = new byte[100];
072: r.nextBytes(randomBytes);
073: byte firstByte = randomBytes[0];
074: for (int counter = 1; counter < randomBytes.length; counter++)
075: if (randomBytes[counter] != firstByte)
076: someDifferent = true;
077: assertTrue(
078: "nextBytes() returned an array of length 100 of the same byte",
079: someDifferent);
080: }
081:
082: /**
083: * @tests java.util.Random#nextDouble()
084: */
085: public void test_nextDouble() {
086: // Test for method double java.util.Random.nextDouble()
087: double lastNum = r.nextDouble();
088: double nextNum;
089: boolean someDifferent = false;
090: boolean inRange = true;
091: for (int counter = 0; counter < 100; counter++) {
092: nextNum = r.nextDouble();
093: if (nextNum != lastNum)
094: someDifferent = true;
095: if (!(0 <= nextNum && nextNum < 1.0))
096: inRange = false;
097: lastNum = nextNum;
098: }
099: assertTrue(
100: "Calling nextDouble 100 times resulted in same number",
101: someDifferent);
102: assertTrue(
103: "Calling nextDouble resulted in a number out of range [0,1)",
104: inRange);
105: }
106:
107: /**
108: * @tests java.util.Random#nextFloat()
109: */
110: public void test_nextFloat() {
111: // Test for method float java.util.Random.nextFloat()
112: float lastNum = r.nextFloat();
113: float nextNum;
114: boolean someDifferent = false;
115: boolean inRange = true;
116: for (int counter = 0; counter < 100; counter++) {
117: nextNum = r.nextFloat();
118: if (nextNum != lastNum)
119: someDifferent = true;
120: if (!(0 <= nextNum && nextNum < 1.0))
121: inRange = false;
122: lastNum = nextNum;
123: }
124: assertTrue(
125: "Calling nextFloat 100 times resulted in same number",
126: someDifferent);
127: assertTrue(
128: "Calling nextFloat resulted in a number out of range [0,1)",
129: inRange);
130: }
131:
132: /**
133: * @tests java.util.Random#nextGaussian()
134: */
135: public void test_nextGaussian() {
136: // Test for method double java.util.Random.nextGaussian()
137: double lastNum = r.nextGaussian();
138: double nextNum;
139: boolean someDifferent = false;
140: boolean someInsideStd = false;
141: for (int counter = 0; counter < 100; counter++) {
142: nextNum = r.nextGaussian();
143: if (nextNum != lastNum)
144: someDifferent = true;
145: if (-1.0 <= nextNum && nextNum <= 1.0)
146: someInsideStd = true;
147: lastNum = nextNum;
148: }
149: assertTrue(
150: "Calling nextGaussian 100 times resulted in same number",
151: someDifferent);
152: assertTrue(
153: "Calling nextGaussian 100 times resulted in no number within 1 std. deviation of mean",
154: someInsideStd);
155: }
156:
157: /**
158: * @tests java.util.Random#nextInt()
159: */
160: public void test_nextInt() {
161: // Test for method int java.util.Random.nextInt()
162: int lastNum = r.nextInt();
163: int nextNum;
164: boolean someDifferent = false;
165: for (int counter = 0; counter < 100; counter++) {
166: nextNum = r.nextInt();
167: if (nextNum != lastNum)
168: someDifferent = true;
169: lastNum = nextNum;
170: }
171: assertTrue("Calling nextInt 100 times resulted in same number",
172: someDifferent);
173: }
174:
175: /**
176: * @tests java.util.Random#nextInt(int)
177: */
178: public void test_nextIntI() {
179: // Test for method int java.util.Random.nextInt(int)
180: final int range = 10;
181: int lastNum = r.nextInt(range);
182: int nextNum;
183: boolean someDifferent = false;
184: boolean inRange = true;
185: for (int counter = 0; counter < 100; counter++) {
186: nextNum = r.nextInt(range);
187: if (nextNum != lastNum)
188: someDifferent = true;
189: if (!(0 <= nextNum && nextNum < range))
190: inRange = false;
191: lastNum = nextNum;
192: }
193: assertTrue(
194: "Calling nextInt (range) 100 times resulted in same number",
195: someDifferent);
196: assertTrue(
197: "Calling nextInt (range) resulted in a number outside of [0, range)",
198: inRange);
199:
200: }
201:
202: /**
203: * @tests java.util.Random#nextLong()
204: */
205: public void test_nextLong() {
206: // Test for method long java.util.Random.nextLong()
207: long lastNum = r.nextLong();
208: long nextNum;
209: boolean someDifferent = false;
210: for (int counter = 0; counter < 100; counter++) {
211: nextNum = r.nextLong();
212: if (nextNum != lastNum)
213: someDifferent = true;
214: lastNum = nextNum;
215: }
216: assertTrue(
217: "Calling nextLong 100 times resulted in same number",
218: someDifferent);
219: }
220:
221: /**
222: * @tests java.util.Random#setSeed(long)
223: */
224: public void test_setSeedJ() {
225: // Test for method void java.util.Random.setSeed(long)
226: long[] randomArray = new long[100];
227: boolean someDifferent = false;
228: final long firstSeed = 1000;
229: long aLong, anotherLong, yetAnotherLong;
230: Random aRandom = new Random();
231: Random anotherRandom = new Random();
232: Random yetAnotherRandom = new Random();
233: aRandom.setSeed(firstSeed);
234: anotherRandom.setSeed(firstSeed);
235: for (int counter = 0; counter < randomArray.length; counter++) {
236: aLong = aRandom.nextLong();
237: anotherLong = anotherRandom.nextLong();
238: assertTrue(
239: "Two randoms with same seeds gave differing nextLong values",
240: aLong == anotherLong);
241: yetAnotherLong = yetAnotherRandom.nextLong();
242: randomArray[counter] = aLong;
243: if (aLong != yetAnotherLong)
244: someDifferent = true;
245: }
246: assertTrue(
247: "Two randoms with the different seeds gave the same chain of values",
248: someDifferent);
249: aRandom.setSeed(firstSeed);
250: for (int counter = 0; counter < randomArray.length; counter++)
251: assertTrue(
252: "Reseting a random to its old seed did not result in the same chain of values as it gave before",
253: aRandom.nextLong() == randomArray[counter]);
254: }
255:
256: // two random create at a time should also generated different results
257: // regression test for Harmony 4616
258: public void test_random_generate() throws Exception {
259: for (int i = 0; i < 100; i++) {
260: Random random1 = new Random();
261: Random random2 = new Random();
262: assertFalse(random1.nextLong() == random2.nextLong());
263: }
264: }
265:
266: /**
267: * Sets up the fixture, for example, open a network connection. This method
268: * is called before a test is executed.
269: */
270: protected void setUp() {
271: r = new Random();
272: }
273:
274: /**
275: * Tears down the fixture, for example, close a network connection. This
276: * method is called after a test is executed.
277: */
278: protected void tearDown() {
279: }
280: }
|