KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.*;
13 import junit.framework.*;
14 import sun.misc.*;
15
16 /**
17  * Tests the StockRandomGenerator class.
18  *
19  * @author Klaus Meffert
20  * @since 2.2
21  */

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

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   /**
49    * @author Klaus Meffert
50    * @since 2.2
51    */

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   /**
67    *
68    * @throws Exception
69    *
70    * @author Klaus Meffert
71    * @since 2.2
72    */

73   public void testNextInt_0()
74       throws Exception JavaDoc {
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   /**
85    *
86    * @throws Exception
87    *
88    * @author Klaus Meffert
89    * @since 2.2
90    */

91   public void testNextInt_1()
92       throws Exception JavaDoc {
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   /**
110    *
111    * @throws Exception
112    *
113    * @author Klaus Meffert
114    * @since 2.2
115    */

116   public void testNextInt_2()
117       throws Exception JavaDoc {
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   /**
136    *
137    * @throws Exception
138    *
139    * @author Klaus Meffert
140    * @since 2.2
141    */

142   public void testNextLong_0()
143       throws Exception JavaDoc {
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   /**
161    *
162    * @throws Exception
163    *
164    * @author Klaus Meffert
165    * @since 2.2
166    */

167   public void testNextDouble_0()
168       throws Exception JavaDoc {
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   /**
187    *
188    * @throws Exception
189    *
190    * @author Klaus Meffert
191    * @since 2.2
192    */

193   public void testNextFloat_0()
194       throws Exception JavaDoc {
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   /**
213    * Tests distribution of boolean random values. We should have more than 10%
214    * of each true and false when running a significant number of tests
215    * @throws Exception
216    *
217    * @author Klaus Meffert
218    * @since 2.3
219    */

220   public void testNextBoolean_0()
221       throws Exception JavaDoc {
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   /**
241    * Tests serializability capabilities.
242    *
243    * @throws Exception
244    *
245    * @author Klaus Meffert
246    * @since 3.01
247    */

248   public void testSerialize_0() throws Exception JavaDoc {
249     /**@todo fix test as Java 5 uses java.util.concurrent.AtomicLong instead
250      * of sun.misc.AtomicLong
251      */

252     StockRandomGenerator srg = new StockRandomGenerator();
253     Object JavaDoc 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