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.tests.java.util; 19 20import java.io.Serializable; 21import java.util.Arrays; 22import java.util.Random; 23import org.apache.harmony.testframework.serialization.SerializationTest; 24 25public class RandomTest extends junit.framework.TestCase { 26 27 private Random r; 28 29 /** 30 * java.util.Random#Random() 31 */ 32 public void test_Constructor() { 33 // Test for method java.util.Random() 34 assertTrue("Used to test", true); 35 } 36 37 /** 38 * java.util.Random#Random(long) 39 */ 40 public void test_ConstructorJ() { 41 Random r = new Random(8409238L); 42 Random r2 = new Random(8409238L); 43 for (int i = 0; i < 100; i++) 44 assertTrue("Values from randoms with same seed don't match", r 45 .nextInt() == r2.nextInt()); 46 } 47 48 public void test_setSeed() { 49 Random r = new Random(); 50 r.setSeed(1337); 51 Random r2 = new Random(); 52 r2.setSeed(1337); 53 for (int i = 0; i < 100; i++) 54 assertTrue("Values from randoms with same seed don't match", r 55 .nextInt() == r2.nextInt()); 56 } 57 58 59 /** 60 * java.util.Random#nextBoolean() 61 */ 62 public void test_nextBoolean() { 63 // Test for method boolean java.util.Random.nextBoolean() 64 boolean falseAppeared = false, trueAppeared = false; 65 for (int counter = 0; counter < 100; counter++) 66 if (r.nextBoolean()) 67 trueAppeared = true; 68 else 69 falseAppeared = true; 70 assertTrue("Calling nextBoolean() 100 times resulted in all trues", 71 falseAppeared); 72 assertTrue("Calling nextBoolean() 100 times resulted in all falses", 73 trueAppeared); 74 } 75 76 /** 77 * java.util.Random#nextBytes(byte[]) 78 */ 79 public void test_nextBytes$B() { 80 // Test for method void java.util.Random.nextBytes(byte []) 81 boolean someDifferent = false; 82 byte[] randomBytes = new byte[100]; 83 r.nextBytes(randomBytes); 84 byte firstByte = randomBytes[0]; 85 for (int counter = 1; counter < randomBytes.length; counter++) 86 if (randomBytes[counter] != firstByte) 87 someDifferent = true; 88 assertTrue( 89 "nextBytes() returned an array of length 100 of the same byte", 90 someDifferent); 91 } 92 93 /** 94 * java.util.Random#nextDouble() 95 */ 96 public void test_nextDouble() { 97 // Test for method double java.util.Random.nextDouble() 98 double lastNum = r.nextDouble(); 99 double nextNum; 100 boolean someDifferent = false; 101 boolean inRange = true; 102 for (int counter = 0; counter < 100; counter++) { 103 nextNum = r.nextDouble(); 104 if (nextNum != lastNum) 105 someDifferent = true; 106 if (!(0 <= nextNum && nextNum < 1.0)) 107 inRange = false; 108 lastNum = nextNum; 109 } 110 assertTrue("Calling nextDouble 100 times resulted in same number", 111 someDifferent); 112 assertTrue( 113 "Calling nextDouble resulted in a number out of range [0,1)", 114 inRange); 115 } 116 117 /** 118 * java.util.Random#nextFloat() 119 */ 120 public void test_nextFloat() { 121 // Test for method float java.util.Random.nextFloat() 122 float lastNum = r.nextFloat(); 123 float nextNum; 124 boolean someDifferent = false; 125 boolean inRange = true; 126 for (int counter = 0; counter < 100; counter++) { 127 nextNum = r.nextFloat(); 128 if (nextNum != lastNum) 129 someDifferent = true; 130 if (!(0 <= nextNum && nextNum < 1.0)) 131 inRange = false; 132 lastNum = nextNum; 133 } 134 assertTrue("Calling nextFloat 100 times resulted in same number", 135 someDifferent); 136 assertTrue("Calling nextFloat resulted in a number out of range [0,1)", 137 inRange); 138 } 139 140 /** 141 * java.util.Random#nextGaussian() 142 */ 143 public void test_nextGaussian() { 144 // Test for method double java.util.Random.nextGaussian() 145 double lastNum = r.nextGaussian(); 146 double nextNum; 147 boolean someDifferent = false; 148 boolean someInsideStd = false; 149 for (int counter = 0; counter < 100; counter++) { 150 nextNum = r.nextGaussian(); 151 if (nextNum != lastNum) 152 someDifferent = true; 153 if (-1.0 <= nextNum && nextNum <= 1.0) 154 someInsideStd = true; 155 lastNum = nextNum; 156 } 157 assertTrue("Calling nextGaussian 100 times resulted in same number", 158 someDifferent); 159 assertTrue( 160 "Calling nextGaussian 100 times resulted in no number within 1 std. deviation of mean", 161 someInsideStd); 162 } 163 164 /** 165 * java.util.Random#nextInt() 166 */ 167 public void test_nextInt() { 168 // Test for method int java.util.Random.nextInt() 169 int lastNum = r.nextInt(); 170 int nextNum; 171 boolean someDifferent = false; 172 for (int counter = 0; counter < 100; counter++) { 173 nextNum = r.nextInt(); 174 if (nextNum != lastNum) 175 someDifferent = true; 176 lastNum = nextNum; 177 } 178 assertTrue("Calling nextInt 100 times resulted in same number", 179 someDifferent); 180 } 181 182 /** 183 * java.util.Random#nextInt(int) 184 */ 185 public void test_nextIntI() { 186 // Test for method int java.util.Random.nextInt(int) 187 final int range = 10; 188 int lastNum = r.nextInt(range); 189 int nextNum; 190 boolean someDifferent = false; 191 boolean inRange = true; 192 for (int counter = 0; counter < 100; counter++) { 193 nextNum = r.nextInt(range); 194 if (nextNum != lastNum) 195 someDifferent = true; 196 if (!(0 <= nextNum && nextNum < range)) 197 inRange = false; 198 lastNum = nextNum; 199 } 200 assertTrue("Calling nextInt (range) 100 times resulted in same number", 201 someDifferent); 202 assertTrue( 203 "Calling nextInt (range) resulted in a number outside of [0, range)", 204 inRange); 205 206 } 207 208 /** 209 * java.util.Random#nextLong() 210 */ 211 public void test_nextLong() { 212 // Test for method long java.util.Random.nextLong() 213 long lastNum = r.nextLong(); 214 long nextNum; 215 boolean someDifferent = false; 216 for (int counter = 0; counter < 100; counter++) { 217 nextNum = r.nextLong(); 218 if (nextNum != lastNum) 219 someDifferent = true; 220 lastNum = nextNum; 221 } 222 assertTrue("Calling nextLong 100 times resulted in same number", 223 someDifferent); 224 } 225 226 /** 227 * java.util.Random#setSeed(long) 228 */ 229 public void test_setSeedJ() { 230 // Test for method void java.util.Random.setSeed(long) 231 long[] random1Values = new long[100]; 232 long[] random2Values = new long[100]; 233 long[] random3Values = new long[100]; 234 235 Random random1 = new Random(); 236 Random random2 = new Random(); 237 Random random3 = new Random(); 238 239 random1.setSeed(1337); 240 random2.setSeed(1337); 241 random3.setSeed(5000); 242 243 for (int i = 0; i < 100; ++i) { 244 random1Values[i] = random1.nextLong(); 245 random2Values[i] = random2.nextLong(); 246 random3Values[i] = random3.nextLong(); 247 } 248 249 assertTrue(Arrays.equals(random1Values, random2Values)); 250 assertFalse(Arrays.equals(random2Values, random3Values)); 251 252 // Set random3's seed to 1337 and assert it results in the same sequence of 253 // values as the first two randoms. 254 random3.setSeed(1337); 255 for (int i = 0; i < 100; ++i) { 256 random3Values[i] = random3.nextLong(); 257 } 258 259 assertTrue(Arrays.equals(random1Values, random3Values)); 260 } 261 262 static final class Mock_Random extends Random { 263 private boolean nextCalled = false; 264 265 public boolean getFlag () { 266 boolean retVal = nextCalled; 267 nextCalled = false; 268 return retVal; 269 } 270 271 @Override 272 protected int next(int bits) { 273 nextCalled = true; 274 return super.next(bits); 275 } 276 } 277 278 public void test_next() { 279 Mock_Random mr = new Mock_Random(); 280 assertFalse(mr.getFlag()); 281 mr.nextBoolean(); 282 assertTrue(mr.getFlag()); 283 mr.nextBytes(new byte[10]); 284 assertTrue(mr.getFlag()); 285 mr.nextDouble(); 286 assertTrue(mr.getFlag()); 287 mr.nextFloat(); 288 assertTrue(mr.getFlag()); 289 mr.nextGaussian(); 290 assertTrue(mr.getFlag()); 291 mr.nextInt(); 292 assertTrue(mr.getFlag()); 293 mr.nextInt(10); 294 assertTrue(mr.getFlag()); 295 mr.nextLong(); 296 assertTrue(mr.getFlag()); 297 } 298 299 @Override 300 protected void setUp() { 301 r = new Random(); 302 } 303 304 @Override 305 protected void tearDown() { 306 } 307 308 public void testSerializationCompatibility() throws Exception { 309 Random rand = new Random(0x8123aea6267e055dL); 310 rand.nextGaussian(); 311 // SerializationTest.createGoldenFile("/tmp", this, rand); 312 SerializationTest.verifyGolden(this, rand, comparator); 313 314 rand = new Random(0x8123aea6267e055dL); 315 rand.nextGaussian(); 316 SerializationTest.verifySelf(rand, comparator); 317 } 318 319 public static final SerializationTest.SerializableAssert comparator = 320 new SerializationTest.SerializableAssert() { 321 public void assertDeserialized(Serializable initial, Serializable deserialized) { 322 Random initialRand = (Random) initial; 323 Random deserializedRand = (Random) deserialized; 324 assertEquals("should be equal", initialRand.nextInt(), deserializedRand.nextInt()); 325 } 326 }; 327} 328