KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > GaussianRandomGeneratorTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15 import sun.misc.*;
16
17 /**
18  * Tests the GaussianRandomGenerator class.
19  *
20  * @author Klaus Meffert
21  * @since 1.1
22  */

23 public class GaussianRandomGeneratorTest
24     extends JGAPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private static final String JavaDoc CVS_REVISION = "$Revision: 1.16 $";
27
28   public static Test suite() {
29     TestSuite suite = new TestSuite(GaussianRandomGeneratorTest.class);
30     return suite;
31   }
32
33   /**
34    * Check if construction and calculation in general possible
35    * @author Klaus Meffert
36    */

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   /**
48    * @author Klaus Meffert
49    */

50   public void testConstruct_0() {
51     try {
52       new GaussianRandomGenerator(0.0d);
53       fail();
54     }
55     catch (IllegalArgumentException JavaDoc iex) {
56       ; //this is OK
57
}
58   }
59
60   /**
61    * @author Klaus Meffert
62    */

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   /**
78    * @throws Exception
79    *
80    * @author Klaus Meffert
81    */

82   public void testGetGaussianStdDeviation_0()
83       throws Exception JavaDoc {
84     final double stdDeriv = 0.04d;
85     GaussianRandomGenerator calc = new GaussianRandomGenerator(stdDeriv);
86     assertEquals(stdDeriv, calc.getGaussianStdDeviation(), DELTA);
87 // Gene gene = new IntegerGene(1, 5);
88
// Chromosome chrom = new Chromosome(gene, 50);
89
// conf.setSampleChromosome(chrom);
90
/**@todo finish*/
91   }
92
93   /**
94    * @throws Exception
95    *
96    * @author Klaus Meffert
97    */

98   public void testNextInt_0()
99       throws Exception JavaDoc {
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   /**
110    *
111    * @throws Exception
112    * @author Klaus Meffert
113    */

114   public void testNextInt_1()
115       throws Exception JavaDoc {
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   /**
134    *
135    * @throws Exception
136    * @author Klaus Meffert
137    */

138   public void testNextInt_2()
139       throws Exception JavaDoc {
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   /**
158    *
159    * @throws Exception
160    * @author Klaus Meffert
161    */

162   public void testNextLong_0()
163       throws Exception JavaDoc {
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   /**
182    *
183    * @throws Exception
184    * @author Klaus Meffert
185    */

186   public void testNextDouble_0()
187       throws Exception JavaDoc {
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   /**
210    *
211    * @throws Exception
212    * @author Klaus Meffert
213    */

214   public void testNextFloat_0()
215       throws Exception JavaDoc {
216     GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d);
217 // double min=-5.57979044;double max=5.7904768;
218
// for (int i=0;i<100000000;i++) {
219
// double g = calc.nextDouble();
220
// if(g < min) {
221
// min = g;
222
// }
223
// if (g > max) {
224
// max = g;
225
// }
226
// }
227
// System.err.println("MIN: "+min);
228
// System.err.println("MAX: "+max);
229
// GaussianRandomGenerator calc = new GaussianRandomGenerator(1.0d);
230
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   /**
247    * Tests serializability capabilities with default constructor.
248    *
249    * @throws Exception
250    *
251    * @author Klaus Meffert
252    * @since 3.01
253    */

254   public void testSerialize_0()
255       throws Exception JavaDoc {
256     /**@todo fix test as Java 5 uses java.util.concurrent.AtomicLong instead
257      * of sun.misc.AtomicLong
258      */

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   /**
270    * Tests serializability capabilities with non-default constructor.
271    *
272    * @throws Exception
273    *
274    * @author Klaus Meffert
275    * @since 3.01
276    */

277   public void testSerialize_1() throws Exception JavaDoc {
278     /**@todo fix test as Java 5 uses java.util.concurrent.AtomicLong instead
279      * of sun.misc.AtomicLong
280      */

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