1 16 package org.apache.commons.lang.math; 17 18 import java.util.Random ; 19 20 import junit.framework.Test; 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 30 31 public final class RandomUtilsTest extends TestCase { 32 33 public RandomUtilsTest(String name) { 34 super(name); 35 } 36 37 public void setUp() { 38 } 39 40 public static Test suite() { 41 TestSuite suite = new TestSuite(RandomUtilsTest.class); 42 suite.setName("RandomUtils Tests"); 43 return suite; 44 } 45 46 47 public void testNextInt() { 48 tstNextInt(null); 49 } 50 51 52 public void testNextInt2() { 53 Random rnd = new Random (); 54 rnd.setSeed(System.currentTimeMillis()); 55 tstNextInt(rnd); 56 } 57 58 59 public void testJvmRandomNextInt() { 60 tstNextInt(RandomUtils.JVM_RANDOM); 61 } 62 63 64 70 private void tstNextInt(Random rnd) { 71 int bound = 0; 72 int result = 0; 73 bound = Integer.MAX_VALUE; 75 if (rnd == null) { 76 result = RandomUtils.nextInt(bound); 77 } else { 78 result = RandomUtils.nextInt(rnd,bound); 79 } 80 assertTrue("result less than bound",result < bound); 81 assertTrue("result non-negative",result >= 0); 82 83 bound = 4; 85 int[] expected = new int[] {250,250,250,250}; 86 int[] observed = new int[] {0,0,0,0}; 87 for (int i = 0; i < 1000; i ++) { 88 if (rnd == null) { 89 result = RandomUtils.nextInt(bound); 90 } else { 91 result = RandomUtils.nextInt(rnd,bound); 92 } 93 assertTrue(result < bound); 94 assertTrue(result >= 0); 95 observed[result]++; 96 } 97 100 assertTrue( 101 "chi-square test -- will fail about 1 in 1000 times", 102 chiSquare(expected,observed) < 16.27); 103 } 104 105 106 public void testNextLong() { 107 tstNextLong(null); 108 } 109 110 113 public void testNextLong2() { 114 Random rnd = new Random (); 115 rnd.setSeed(System.currentTimeMillis()); 116 tstNextLong(rnd); 117 } 118 119 125 private void tstNextLong(Random rnd) { 126 int[] expected = new int[] {500,500}; 127 int[] observed = new int[] {0,0}; 128 long result = 0; 129 long midPoint = Long.MAX_VALUE/2; 130 for (int i = 0; i < 1000; i ++) { 131 if (rnd == null) { 132 result = Math.abs(RandomUtils.nextLong()); 133 } else { 134 result = Math.abs(RandomUtils.nextLong(rnd)); 135 } 136 if (result < midPoint) { 137 observed[0]++; 138 } else { 139 observed[1]++; 140 } 141 } 142 145 assertTrue( 146 "chi-square test -- will fail about 1 in 1000 times", 147 chiSquare(expected,observed) < 10.83); 148 } 149 150 151 152 public void testNextBoolean() { 153 tstNextBoolean(null); 154 } 155 156 157 public void testNextBoolean2() { 158 Random rnd = new Random (); 159 rnd.setSeed(System.currentTimeMillis()); 160 tstNextBoolean(rnd); 161 } 162 163 169 private void tstNextBoolean(Random rnd) { 170 int[] expected = new int[] {500,500}; 171 int[] observed = new int[] {0,0}; 172 boolean result = false; 173 for (int i = 0; i < 1000; i ++) { 174 if (rnd == null) { 175 result = RandomUtils.nextBoolean(); 176 } else { 177 result = RandomUtils.nextBoolean(rnd); 178 } 179 if (result) { 180 observed[0]++; 181 } else { 182 observed[1]++; 183 } 184 } 185 188 assertTrue( 189 "chi-square test -- will fail about 1 in 1000 times", 190 chiSquare(expected,observed) < 10.83 ); 191 } 192 193 194 public void testNextFloat() { 195 tstNextFloat(null); 196 } 197 198 199 public void testNextFloat2() { 200 Random rnd = new Random (); 201 rnd.setSeed(System.currentTimeMillis()); 202 tstNextFloat(rnd); 203 } 204 205 211 private void tstNextFloat(Random rnd) { 212 int[] expected = new int[] {500,500}; 213 int[] observed = new int[] {0,0}; 214 float result = 0; 215 for (int i = 0; i < 1000; i ++) { 216 if (rnd == null) { 217 result = RandomUtils.nextFloat(); 218 } else { 219 result = RandomUtils.nextFloat(rnd); 220 } 221 if (result < 0.5) { 222 observed[0]++; 223 } else { 224 observed[1]++; 225 } 226 } 227 230 assertTrue( 231 "chi-square test -- will fail about 1 in 1000 times", 232 chiSquare(expected,observed) < 10.83); 233 } 234 235 236 public void testNextDouble() { 237 tstNextDouble(null); 238 } 239 240 241 public void testNextDouble2() { 242 Random rnd = new Random (); 243 rnd.setSeed(System.currentTimeMillis()); 244 tstNextDouble(rnd); 245 } 246 247 253 private void tstNextDouble(Random rnd) { 254 int[] expected = new int[] {500,500}; 255 int[] observed = new int[] {0,0}; 256 double result = 0; 257 for (int i = 0; i < 1000; i ++) { 258 if (rnd == null) { 259 result = RandomUtils.nextDouble(); 260 } else { 261 result = RandomUtils.nextDouble(rnd); 262 } 263 if (result < 0.5) { 264 observed[0]++; 265 } else { 266 observed[1]++; 267 } 268 } 269 272 assertTrue( 273 "chi-square test -- will fail about 1 in 1000 times", 274 chiSquare(expected,observed) < 10.83); 275 } 276 277 278 public void testUnimplementedMethods() { 279 280 try { 281 RandomUtils.JVM_RANDOM.setSeed(1000); 282 fail("expecting UnsupportedOperationException"); 283 } catch (UnsupportedOperationException ex) { 284 } 286 287 try { 288 RandomUtils.JVM_RANDOM.nextGaussian(); 289 fail("expecting UnsupportedOperationException"); 290 } catch (UnsupportedOperationException ex) { 291 } 293 294 try { 295 RandomUtils.JVM_RANDOM.nextBytes(null); 296 fail("expecting UnsupportedOperationException"); 297 } catch (UnsupportedOperationException ex) { 298 } 300 301 } 302 303 304 public void testIllegalArguments() { 305 306 try { 307 RandomUtils.JVM_RANDOM.nextInt(-1); 308 fail("expecting IllegalArgumentException"); 309 } catch (IllegalArgumentException ex) { 310 } 312 313 try { 314 JVMRandom.nextLong( -1L ); 315 fail("expecting IllegalArgumentException"); 316 } catch (IllegalArgumentException ex) { 317 } 319 320 } 321 322 327 private double chiSquare(int[] expected, int[] observed) { 328 double sumSq = 0.0d; 329 double dev = 0.0d; 330 for (int i = 0; i< observed.length; i++) { 331 dev = (double)(observed[i] - expected[i]); 332 sumSq += dev*dev/(double)expected[i]; 333 } 334 return sumSq; 335 } 336 337 } 338 339 | Popular Tags |