1 10 package org.jgap.impl; 11 12 import org.jgap.*; 13 import junit.framework.*; 14 import sun.misc.*; 15 16 22 public class StockRandomGeneratorTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.6 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(StockRandomGeneratorTest.class); 29 return suite; 30 } 31 32 38 public void testGeneral() { 39 StockRandomGenerator calc = new StockRandomGenerator(); 40 calc.nextInt(); 41 calc.nextBoolean(); 42 calc.nextDouble(); 43 calc.nextFloat(); 44 calc.nextInt(); 45 calc.nextLong(); 46 } 47 48 52 public void testConstruct_0() { 53 StockRandomGenerator calc = new StockRandomGenerator(); 54 int i = calc.nextInt(); 55 if (Math.abs(i) < DELTA) { 56 i = calc.nextInt(); 57 if (Math.abs(i) < DELTA) { 58 i = calc.nextInt(); 59 if (Math.abs(i) < DELTA) { 60 fail(); 61 } 62 } 63 } 64 } 65 66 73 public void testNextInt_0() 74 throws Exception { 75 StockRandomGenerator calc = new StockRandomGenerator(); 76 int res; 77 for (int i = 0; i < 100; i++) { 78 res = calc.nextInt(5); 79 assertTrue(res < 5); 80 assertTrue(res >= 0); 81 } 82 } 83 84 91 public void testNextInt_1() 92 throws Exception { 93 StockRandomGenerator calc = new StockRandomGenerator(); 94 int res; 95 int resOli = 0; 96 for (int i = 0; i < 100; i++) { 97 res = calc.nextInt(); 98 if (i > 0) { 99 if (resOli == res) { 100 fail("Two consecutive calls produced same value: " + res); 101 } 102 else { 103 resOli = res; 104 } 105 } 106 } 107 } 108 109 116 public void testNextInt_2() 117 throws Exception { 118 StockRandomGenerator calc = new StockRandomGenerator(); 119 int res; 120 int resOli = 0; 121 for (int i = 0; i < 100; i++) { 122 res = calc.nextInt(100000); 123 if (i > 0) { 124 if (resOli == res) { 125 fail("Two consecutive calls produced same value: " + res); 126 } 127 else { 128 resOli = res; 129 } 130 } 131 assertTrue(res >= 0.000d); 132 } 133 } 134 135 142 public void testNextLong_0() 143 throws Exception { 144 StockRandomGenerator calc = new StockRandomGenerator(); 145 long res; 146 long resOll = 0; 147 for (int i = 0; i < 100; i++) { 148 res = calc.nextLong(); 149 if (i > 0) { 150 if (resOll == res) { 151 fail("Two consecutive calls produced same value: " + res); 152 } 153 else { 154 resOll = res; 155 } 156 } 157 } 158 } 159 160 167 public void testNextDouble_0() 168 throws Exception { 169 StockRandomGenerator calc = new StockRandomGenerator(); 170 double res; 171 double resOld = 0.0000d; 172 for (int i = 0; i < 100; i++) { 173 res = calc.nextDouble(); 174 if (i > 0) { 175 if (Math.abs(resOld - res) < DELTA) { 176 fail("Two consecutive calls produced same value: " + res); 177 } 178 else { 179 resOld = res; 180 } 181 } 182 assertTrue(res >= 0.000d); 183 } 184 } 185 186 193 public void testNextFloat_0() 194 throws Exception { 195 StockRandomGenerator calc = new StockRandomGenerator(); 196 float res; 197 float resOlf = 0.0000f; 198 for (int i = 0; i < 100; i++) { 199 res = calc.nextFloat(); 200 if (i > 0) { 201 if (Math.abs(resOlf - res) < DELTA) { 202 fail("Two consecutive calls produced same value: " + res); 203 } 204 else { 205 resOlf = res; 206 } 207 } 208 assertTrue(res >= 0.000d); 209 } 210 } 211 212 220 public void testNextBoolean_0() 221 throws Exception { 222 StockRandomGenerator calc = new StockRandomGenerator(); 223 boolean res; 224 int trueCounter = 0; 225 int falseCounter = 0; 226 int total = 100; 227 for (int i = 0; i < total; i++) { 228 res = calc.nextBoolean(); 229 if (res) { 230 trueCounter++; 231 } 232 else { 233 falseCounter++; 234 } 235 } 236 assertTrue(trueCounter > total * 0.1); 237 assertTrue(falseCounter > total * 0.1); 238 } 239 240 248 public void testSerialize_0() throws Exception { 249 252 StockRandomGenerator srg = new StockRandomGenerator(); 253 Object o = privateAccessor.getField(srg, "seed"); 254 AtomicLong seed1 = (AtomicLong) privateAccessor.getField(srg, "seed"); 255 long curr = System.currentTimeMillis(); 256 while (curr == System.currentTimeMillis()); 257 StockRandomGenerator srg2 = (StockRandomGenerator) doSerialize(srg); 258 AtomicLong seed2 = (AtomicLong) privateAccessor.getField(srg2, "seed"); 259 assertFalse(seed1.get() == seed2.get()); 260 } 261 } 262 | Popular Tags |