RandomTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 tests.api.java.util; 19 20import dalvik.annotation.TestTargetNew; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.util.Random; 26 27@TestTargetClass(Random.class) 28public class RandomTest extends junit.framework.TestCase { 29 30 Random r; 31 32 /** 33 * @tests java.util.Random#Random() 34 */ 35 @TestTargetNew( 36 level = TestLevel.COMPLETE, 37 notes = "", 38 method = "Random", 39 args = {} 40 ) 41 public void test_Constructor() { 42 // Test for method java.util.Random() 43 assertTrue("Used to test", true); 44 } 45 46 /** 47 * @tests java.util.Random#Random(long) 48 */ 49 @TestTargetNew( 50 level = TestLevel.COMPLETE, 51 notes = "", 52 method = "Random", 53 args = {long.class} 54 ) 55 public void test_ConstructorJ() { 56 Random r = new Random(8409238L); 57 Random r2 = new Random(8409238L); 58 for (int i = 0; i < 100; i++) 59 assertTrue("Values from randoms with same seed don't match", r 60 .nextInt() == r2.nextInt()); 61 } 62 63 /** 64 * @tests java.util.Random#nextBoolean() 65 */ 66 @TestTargetNew( 67 level = TestLevel.COMPLETE, 68 notes = "", 69 method = "nextBoolean", 70 args = {} 71 ) 72 public void test_nextBoolean() { 73 // Test for method boolean java.util.Random.nextBoolean() 74 boolean falseAppeared = false, trueAppeared = false; 75 for (int counter = 0; counter < 100; counter++) 76 if (r.nextBoolean()) 77 trueAppeared = true; 78 else 79 falseAppeared = true; 80 assertTrue("Calling nextBoolean() 100 times resulted in all trues", 81 falseAppeared); 82 assertTrue("Calling nextBoolean() 100 times resulted in all falses", 83 trueAppeared); 84 } 85 86 /** 87 * @tests java.util.Random#nextBytes(byte[]) 88 */ 89 @TestTargetNew( 90 level = TestLevel.COMPLETE, 91 notes = "", 92 method = "nextBytes", 93 args = {byte[].class} 94 ) 95 public void test_nextBytes$B() { 96 // Test for method void java.util.Random.nextBytes(byte []) 97 boolean someDifferent = false; 98 byte[] randomBytes = new byte[100]; 99 r.nextBytes(randomBytes); 100 byte firstByte = randomBytes[0]; 101 for (int counter = 1; counter < randomBytes.length; counter++) 102 if (randomBytes[counter] != firstByte) 103 someDifferent = true; 104 assertTrue( 105 "nextBytes() returned an array of length 100 of the same byte", 106 someDifferent); 107 } 108 109 /** 110 * @tests java.util.Random#nextDouble() 111 */ 112 @TestTargetNew( 113 level = TestLevel.COMPLETE, 114 notes = "", 115 method = "nextDouble", 116 args = {} 117 ) 118 public void test_nextDouble() { 119 // Test for method double java.util.Random.nextDouble() 120 double lastNum = r.nextDouble(); 121 double nextNum; 122 boolean someDifferent = false; 123 boolean inRange = true; 124 for (int counter = 0; counter < 100; counter++) { 125 nextNum = r.nextDouble(); 126 if (nextNum != lastNum) 127 someDifferent = true; 128 if (!(0 <= nextNum && nextNum < 1.0)) 129 inRange = false; 130 lastNum = nextNum; 131 } 132 assertTrue("Calling nextDouble 100 times resulted in same number", 133 someDifferent); 134 assertTrue( 135 "Calling nextDouble resulted in a number out of range [0,1)", 136 inRange); 137 } 138 139 /** 140 * @tests java.util.Random#nextFloat() 141 */ 142 @TestTargetNew( 143 level = TestLevel.COMPLETE, 144 notes = "", 145 method = "nextFloat", 146 args = {} 147 ) 148 public void test_nextFloat() { 149 // Test for method float java.util.Random.nextFloat() 150 float lastNum = r.nextFloat(); 151 float nextNum; 152 boolean someDifferent = false; 153 boolean inRange = true; 154 for (int counter = 0; counter < 100; counter++) { 155 nextNum = r.nextFloat(); 156 if (nextNum != lastNum) 157 someDifferent = true; 158 if (!(0 <= nextNum && nextNum < 1.0)) 159 inRange = false; 160 lastNum = nextNum; 161 } 162 assertTrue("Calling nextFloat 100 times resulted in same number", 163 someDifferent); 164 assertTrue("Calling nextFloat resulted in a number out of range [0,1)", 165 inRange); 166 } 167 168 /** 169 * @tests java.util.Random#nextGaussian() 170 */ 171 @TestTargetNew( 172 level = TestLevel.COMPLETE, 173 notes = "", 174 method = "nextGaussian", 175 args = {} 176 ) 177 public void test_nextGaussian() { 178 // Test for method double java.util.Random.nextGaussian() 179 double lastNum = r.nextGaussian(); 180 double nextNum; 181 boolean someDifferent = false; 182 boolean someInsideStd = false; 183 for (int counter = 0; counter < 100; counter++) { 184 nextNum = r.nextGaussian(); 185 if (nextNum != lastNum) 186 someDifferent = true; 187 if (-1.0 <= nextNum && nextNum <= 1.0) 188 someInsideStd = true; 189 lastNum = nextNum; 190 } 191 assertTrue("Calling nextGaussian 100 times resulted in same number", 192 someDifferent); 193 assertTrue( 194 "Calling nextGaussian 100 times resulted in no number within 1 std. deviation of mean", 195 someInsideStd); 196 } 197 198 /** 199 * @tests java.util.Random#nextInt() 200 */ 201 @TestTargetNew( 202 level = TestLevel.COMPLETE, 203 notes = "", 204 method = "nextInt", 205 args = {} 206 ) 207 public void test_nextInt() { 208 // Test for method int java.util.Random.nextInt() 209 int lastNum = r.nextInt(); 210 int nextNum; 211 boolean someDifferent = false; 212 for (int counter = 0; counter < 100; counter++) { 213 nextNum = r.nextInt(); 214 if (nextNum != lastNum) 215 someDifferent = true; 216 lastNum = nextNum; 217 } 218 assertTrue("Calling nextInt 100 times resulted in same number", 219 someDifferent); 220 } 221 222 /** 223 * @tests java.util.Random#nextInt(int) 224 */ 225 @TestTargetNew( 226 level = TestLevel.COMPLETE, 227 notes = "", 228 method = "nextInt", 229 args = {int.class} 230 ) 231 public void test_nextIntI() { 232 // Test for method int java.util.Random.nextInt(int) 233 final int range = 10; 234 int lastNum = r.nextInt(range); 235 int nextNum; 236 boolean someDifferent = false; 237 boolean inRange = true; 238 for (int counter = 0; counter < 100; counter++) { 239 nextNum = r.nextInt(range); 240 if (nextNum != lastNum) 241 someDifferent = true; 242 if (!(0 <= nextNum && nextNum < range)) 243 inRange = false; 244 lastNum = nextNum; 245 } 246 assertTrue("Calling nextInt (range) 100 times resulted in same number", 247 someDifferent); 248 assertTrue( 249 "Calling nextInt (range) resulted in a number outside of [0, range)", 250 inRange); 251 252 } 253 254 /** 255 * @tests java.util.Random#nextLong() 256 */ 257 @TestTargetNew( 258 level = TestLevel.COMPLETE, 259 notes = "", 260 method = "nextLong", 261 args = {} 262 ) 263 public void test_nextLong() { 264 // Test for method long java.util.Random.nextLong() 265 long lastNum = r.nextLong(); 266 long nextNum; 267 boolean someDifferent = false; 268 for (int counter = 0; counter < 100; counter++) { 269 nextNum = r.nextLong(); 270 if (nextNum != lastNum) 271 someDifferent = true; 272 lastNum = nextNum; 273 } 274 assertTrue("Calling nextLong 100 times resulted in same number", 275 someDifferent); 276 } 277 278 /** 279 * @tests java.util.Random#setSeed(long) 280 */ 281 @TestTargetNew( 282 level = TestLevel.COMPLETE, 283 notes = "", 284 method = "setSeed", 285 args = {long.class} 286 ) 287 public void test_setSeedJ() { 288 // Test for method void java.util.Random.setSeed(long) 289 long[] randomArray = new long[100]; 290 boolean someDifferent = false; 291 final long firstSeed = 1000; 292 long aLong, anotherLong, yetAnotherLong; 293 Random aRandom = new Random(); 294 Random anotherRandom = new Random(); 295 Random yetAnotherRandom = new Random(); 296 aRandom.setSeed(firstSeed); 297 anotherRandom.setSeed(firstSeed); 298 for (int counter = 0; counter < randomArray.length; counter++) { 299 aLong = aRandom.nextLong(); 300 anotherLong = anotherRandom.nextLong(); 301 assertTrue( 302 "Two randoms with same seeds gave differing nextLong values", 303 aLong == anotherLong); 304 yetAnotherLong = yetAnotherRandom.nextLong(); 305 randomArray[counter] = aLong; 306 if (aLong != yetAnotherLong) 307 someDifferent = true; 308 } 309 assertTrue( 310 "Two randoms with the different seeds gave the same chain of values", 311 someDifferent); 312 aRandom.setSeed(firstSeed); 313 for (int counter = 0; counter < randomArray.length; counter++) 314 assertTrue( 315 "Reseting a random to its old seed did not result in the same chain of values as it gave before", 316 aRandom.nextLong() == randomArray[counter]); 317 } 318 319 class Mock_Random extends Random { 320 boolean nextCalled = false; 321 322 public boolean getFlag () { 323 boolean retVal = nextCalled; 324 nextCalled = false; 325 return retVal; 326 } 327 328 @Override 329 protected int next(int bits) { 330 nextCalled = true; 331 return super.next(bits); 332 } 333 } 334 335 @TestTargetNew( 336 level = TestLevel.COMPLETE, 337 notes = "", 338 method = "next", 339 args = {int.class} 340 ) 341 public void test_next() { 342 Mock_Random mr = new Mock_Random(); 343 assertFalse(mr.getFlag()); 344 mr.nextBoolean(); 345 assertTrue(mr.getFlag()); 346 mr.nextBytes(new byte[10]); 347 assertTrue(mr.getFlag()); 348 mr.nextDouble(); 349 assertTrue(mr.getFlag()); 350 mr.nextFloat(); 351 assertTrue(mr.getFlag()); 352 mr.nextGaussian(); 353 assertTrue(mr.getFlag()); 354 mr.nextInt(); 355 assertTrue(mr.getFlag()); 356 mr.nextInt(10); 357 assertTrue(mr.getFlag()); 358 mr.nextLong(); 359 assertTrue(mr.getFlag()); 360 } 361 362 /** 363 * Sets up the fixture, for example, open a network connection. This method 364 * is called before a test is executed. 365 */ 366 protected void setUp() { 367 r = new Random(); 368 } 369 370 /** 371 * Tears down the fixture, for example, close a network connection. This 372 * method is called after a test is executed. 373 */ 374 protected void tearDown() { 375 } 376} 377