KickJava   Java API By Example, From Geeks To Geeks.

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


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 CauchyRandomGenerator class.
19  *
20  * @author Klaus Meffert
21  * @since 1.1
22  */

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

38   public void testGeneral() {
39     RandomGenerator calc = new CauchyRandomGenerator();
40     calc.nextInt();
41     calc.nextBoolean();
42     calc.nextDouble();
43     calc.nextFloat();
44     calc.nextInt();
45     calc.nextLong();
46   }
47
48   /**
49    * @throws Exception
50    *
51    * @author Klaus Meffert
52    */

53   public void testNextCauchy_0()
54       throws Exception JavaDoc {
55     final double stdDev = 0.04d;
56     CauchyRandomGenerator calc = new CauchyRandomGenerator(0.0d, stdDev);
57     calc.nextCauchy();
58   }
59
60   /**
61    * @author Klaus Meffert
62    * @since 2.2
63    */

64   public void testGetCauchyStdDeviation_0() {
65     final double stdDev = 0.04d;
66     CauchyRandomGenerator calc = new CauchyRandomGenerator(0.0d, stdDev);
67     assertEquals(stdDev, calc.getCauchyStandardDeviation(), DELTA);
68   }
69
70   /**
71    * @author Klaus Meffert
72    * @since 2.6
73    */

74   public void testNextInt_0() {
75     final double stdDev = 0.04d;
76     CauchyRandomGenerator calc = new CauchyRandomGenerator(0.0d, stdDev);
77     int i = calc.nextInt(2);
78     assertTrue(i < 2 && i >= 0);
79     i = calc.nextInt(1);
80     assertEquals(0, i);
81   }
82
83   /**
84    * Tests serializability capabilities.
85    *
86    * @throws Exception
87    *
88    * @author Klaus Meffert
89    * @since 3.01
90    */

91   public void testSerialize_0() throws Exception JavaDoc {
92     /**@todo fix test as Java 5 uses java.util.concurrent.AtomicLong instead
93      * of sun.misc.AtomicLong
94      */

95     CauchyRandomGenerator srg = new CauchyRandomGenerator();
96     Random r1 = (Random)privateAccessor.getField(srg,"m_rn");
97     AtomicLong seed1 = (AtomicLong)privateAccessor.getField(r1,"seed");
98     long curr = System.currentTimeMillis();
99     while (curr == System.currentTimeMillis());
100     CauchyRandomGenerator srg2 = (CauchyRandomGenerator)doSerialize(srg);
101     Random r2 = (Random)privateAccessor.getField(srg2,"m_rn");
102     AtomicLong seed2 = (AtomicLong)privateAccessor.getField(r2,"seed");
103     assertFalse(seed1.get() == seed2.get());
104   }
105 }
106
Popular Tags