1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 import sun.misc.*; 16 17 23 public class GaussianRandomGeneratorTest 24 extends JGAPTestCase { 25 26 private static final String CVS_REVISION = "$Revision: 1.16 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(GaussianRandomGeneratorTest.class); 30 return suite; 31 } 32 33 37 public void testGeneral() { 38 GaussianRandomGenerator calc = new GaussianRandomGenerator(); 39 calc.nextInt(); 40 calc.nextBoolean(); 41 calc.nextDouble(); 42 calc.nextFloat(); 43 calc.nextInt(); 44 calc.nextLong(); 45 } 46 47 50 public void testConstruct_0() { 51 try { 52 new GaussianRandomGenerator(0.0d); 53 fail(); 54 } 55 catch (IllegalArgumentException iex) { 56 ; } 58 } 59 60 63 public void testConstruct_1() { 64 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 65 int i = calc.nextInt(); 66 if (Math.abs(i) < DELTA) { 67 i = calc.nextInt(); 68 if (Math.abs(i) < DELTA) { 69 i = calc.nextInt(); 70 if (Math.abs(i) < DELTA) { 71 fail(); 72 } 73 } 74 } 75 } 76 77 82 public void testGetGaussianStdDeviation_0() 83 throws Exception { 84 final double stdDeriv = 0.04d; 85 GaussianRandomGenerator calc = new GaussianRandomGenerator(stdDeriv); 86 assertEquals(stdDeriv, calc.getGaussianStdDeviation(), DELTA); 87 91 } 92 93 98 public void testNextInt_0() 99 throws Exception { 100 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 101 int res; 102 for (int i = 0; i < 100; i++) { 103 res = calc.nextInt(5); 104 assertTrue(res < 5.00d); 105 assertTrue(res >= 0.000d); 106 } 107 } 108 109 114 public void testNextInt_1() 115 throws Exception { 116 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 117 int res; 118 int resOli = 0; 119 for (int i = 0; i < 100; i++) { 120 res = calc.nextInt(); 121 if (i > 0) { 122 if (resOli == res) { 123 fail("Two consecutive calls produced same value: " + res); 124 } 125 else { 126 resOli = res; 127 } 128 } 129 assertTrue(res >= 0.000d); 130 } 131 } 132 133 138 public void testNextInt_2() 139 throws Exception { 140 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 141 int res; 142 int resOli = 0; 143 for (int i = 0; i < 100; i++) { 144 res = calc.nextInt(100000); 145 if (i > 0) { 146 if (resOli == res) { 147 fail("Two consecutive calls produced same value: " + res); 148 } 149 else { 150 resOli = res; 151 } 152 } 153 assertTrue(res >= 0.000d); 154 } 155 } 156 157 162 public void testNextLong_0() 163 throws Exception { 164 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 165 long res; 166 long resOll = 0; 167 for (int i = 0; i < 100; i++) { 168 res = calc.nextLong(); 169 if (i > 0) { 170 if (resOll == res) { 171 fail("Two consecutive calls produced same value: " + res); 172 } 173 else { 174 resOll = res; 175 } 176 } 177 assertTrue(res >= 0.000d); 178 } 179 } 180 181 186 public void testNextDouble_0() 187 throws Exception { 188 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 189 double res; 190 double resOld = 0.0000d; 191 for (int i = 0; i < 100; i++) { 192 res = calc.nextDouble(); 193 if (i > 0) { 194 if (Math.abs(resOld - res) < DELTA) { 195 fail("Two consecutive calls produced same value: " + res); 196 } 197 else { 198 resOld = res; 199 } 200 } 201 assertTrue(res >= 0.000d); 202 if (res >= 1.0d) { 203 System.err.println(res); 204 assertTrue(res < 1.000d); 205 } 206 } 207 } 208 209 214 public void testNextFloat_0() 215 throws Exception { 216 GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d); 217 float res; 231 float resOlf = 0.0000f; 232 for (int i = 0; i < 100; i++) { 233 res = calc.nextFloat(); 234 if (i > 0) { 235 if (Math.abs(resOlf - res) < DELTA) { 236 fail("Two consecutive calls produced same value: " + res); 237 } 238 else { 239 resOlf = res; 240 } 241 } 242 assertTrue(res >= 0.000d); 243 } 244 } 245 246 254 public void testSerialize_0() 255 throws Exception { 256 259 GaussianRandomGenerator srg = new GaussianRandomGenerator(); 260 Random r1 = (Random) privateAccessor.getField(srg, "m_rn"); 261 AtomicLong seed1 = (AtomicLong) privateAccessor.getField(r1, "seed"); 262 long curr = System.currentTimeMillis(); 263 while (curr == System.currentTimeMillis()); 264 GaussianRandomGenerator srg2 = (GaussianRandomGenerator) doSerialize(srg); 265 Random r2 = (Random) privateAccessor.getField(srg2, "m_rn"); 266 AtomicLong seed2 = (AtomicLong) privateAccessor.getField(r2, "seed"); 267 assertFalse(seed1.get() == seed2.get()); 268 } 269 277 public void testSerialize_1() throws Exception { 278 281 GaussianRandomGenerator srg = new GaussianRandomGenerator(0.5d); 282 Random r1 = (Random) privateAccessor.getField(srg, "m_rn"); 283 AtomicLong seed1 = (AtomicLong) privateAccessor.getField(r1, "seed"); 284 long curr = System.currentTimeMillis(); 285 while (curr == System.currentTimeMillis()); 286 GaussianRandomGenerator srg2 = (GaussianRandomGenerator) doSerialize(srg); 287 Random r2 = (Random) privateAccessor.getField(srg2, "m_rn"); 288 AtomicLong seed2 = (AtomicLong) privateAccessor.getField(r2, "seed"); 289 assertFalse(seed1.get() == seed2.get()); 290 } 291 } 292 | Popular Tags |