KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > CommandGeneTest


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.gp;
11
12 import junit.framework.*;
13 import org.jgap.*;
14 import org.jgap.gp.impl.*;
15
16 /**
17  * Tests the CommandGene class.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class CommandGeneTest
23     extends GPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.6 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(CommandGeneTest.class);
29     return suite;
30   }
31
32   /**
33    * Following should be possible without exception.
34    * @throws Exception
35    *
36    * @author Klaus Meffert
37    * @since 3.0
38    */

39   public void testConstruct_0()
40       throws Exception JavaDoc {
41     assertNotNull(new CommandGeneImpl(m_gpconf));
42   }
43
44   /**
45    * @throws Exception
46    *
47    * @author Klaus Meffert
48    * @since 3.0
49    */

50   public void testToString_0()
51       throws Exception JavaDoc {
52     CommandGene gene = new CommandGeneImpl(m_gpconf);
53     assertEquals("test", gene.toString());
54   }
55
56   /**
57    * @throws Exception
58    *
59    * @author Klaus Meffert
60    * @since 3.0
61    */

62   public void testSetAllele_0()
63       throws Exception JavaDoc {
64     CommandGene gene = new CommandGeneImpl(m_gpconf);
65     try {
66       gene.setAllele(new Double JavaDoc(75));
67       fail();
68     }catch (UnsupportedOperationException JavaDoc uex) {
69       ;//this is OK
70
}
71   }
72
73   /**
74    * @throws Exception
75    *
76    * @author Klaus Meffert
77    * @since 2.4
78    */

79   public void testSize_0()
80       throws Exception JavaDoc {
81     CommandGene gene = new CommandGeneImpl(m_gpconf);
82     assertEquals(1, gene.size());
83   }
84
85   /**
86    * @throws Exception
87    *
88    * @author Klaus Meffert
89    * @since 2.4
90    */

91   public void testEquals_0()
92       throws Exception JavaDoc {
93     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
94     assertFalse(gene.equals(null));
95     assertTrue(gene.equals(gene));
96     assertFalse(gene.equals(new Integer JavaDoc(2)));
97   }
98
99   /**
100    * @throws Exception
101    *
102    * @author Klaus Meffert
103    * @since 2.4
104    */

105   public void testEquals_1()
106       throws Exception JavaDoc {
107     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
108     assertFalse(gene.equals(null));
109     assertTrue(gene.equals(gene));
110     assertFalse(gene.equals(new Integer JavaDoc(2)));
111   }
112
113   /**
114    * @throws Exception
115    *
116    * @author Klaus Meffert
117    * @since 3.0
118    */

119   public void testEquals_2()
120       throws Exception JavaDoc {
121     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
122     assertFalse(gene.equals(null));
123     assertTrue(gene.equals(gene));
124     assertFalse(gene.equals(new Integer JavaDoc(2)));
125   }
126
127   /**
128    * Compare application data.
129    * @throws Exception
130    *
131    * @author Klaus Meffert
132    * @since 3.0
133    */

134   public void testEquals_3()
135       throws Exception JavaDoc {
136     Configuration conf = new ConfigurationForTest();
137     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
138     gene.setApplicationData(new AppDataForTest());
139     CommandGeneImpl gene2 = new CommandGeneImpl(m_gpconf);
140     gene2.setApplicationData(new AppDataForTest());
141     gene.setCompareApplicationData(true);
142     assertTrue(gene.equals(gene2));
143     /**@todo use other than JGAPFactory to be able to receive a null
144      * CompareToHandler for the application data object
145      */

146   }
147
148   /**
149    * Simple cleanup should be possible without exception.
150    * @throws Exception
151    *
152    *
153    * @author Klaus Meffert
154    * @since 3.0
155    */

156   public void testCleanup_0()
157       throws Exception JavaDoc {
158     CommandGene gene = new CommandGeneImpl(m_gpconf);
159     gene.cleanup();
160   }
161
162   /**
163    * @throws Exception
164    *
165    * @author Klaus Meffert
166    * @since 3.0
167    */

168   public void testHashCode_0()
169       throws Exception JavaDoc {
170     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
171     assertEquals( gene.getClass().getName().hashCode(), gene.hashCode());
172   }
173
174   /**
175    * @throws Exception
176    *
177    * @author Klaus Meffert
178    * @since 3.0
179    */

180   public void testHashCode_1()
181       throws Exception JavaDoc {
182     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
183     //TODO implement
184
// gene.setAllele(new Double(1.5d));
185
// assertEquals(new Double(1.5d).hashCode(), gene.hashCode());
186
}
187
188   /**
189    * @throws Exception
190    *
191    * @author Klaus Meffert
192    * @since 3.0
193    */

194   public void testSetEnergy_0()
195       throws Exception JavaDoc {
196     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
197     assertEquals(0.0, gene.getEnergy(), DELTA);
198     gene.setEnergy(2.3);
199     assertEquals(2.3, gene.getEnergy(), DELTA);
200   }
201
202   /**
203    * @throws Exception
204    *
205    * @author Klaus Meffert
206    * @since 3.0
207    */

208   public void testSetEnergy_1()
209       throws Exception JavaDoc {
210     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
211     gene.setEnergy(2.3);
212     assertEquals(2.3, gene.getEnergy(), DELTA);
213     gene.setEnergy( -55.8);
214     assertEquals( -55.8, gene.getEnergy(), DELTA);
215     gene.setEnergy(0.5);
216     gene.setEnergy(0.8);
217     assertEquals(0.8, gene.getEnergy(), DELTA);
218   }
219
220   /**
221    * @throws Exception
222    *
223    * @author Klaus Meffert
224    * @since 3.0
225    */

226   public void testSetApplicationData_0()
227       throws Exception JavaDoc {
228     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
229     assertNull(gene.getApplicationData());
230     Integer JavaDoc i = new Integer JavaDoc(23);
231     gene.setApplicationData(i);
232     assertSame(i, gene.getApplicationData());
233     String JavaDoc s = "Hallo";
234     gene.setApplicationData(s);
235     assertSame(s, gene.getApplicationData());
236   }
237
238   /**
239    * @throws Exception
240    *
241    * @author Klaus Meffert
242    * @since 3.0
243    */

244   public void testIsCompareApplicationData_0()
245       throws Exception JavaDoc {
246     CommandGeneImpl gene = new CommandGeneImpl(m_gpconf);
247     assertFalse(gene.isCompareApplicationData());
248     gene.setCompareApplicationData(false);
249     assertFalse(gene.isCompareApplicationData());
250     gene.setCompareApplicationData(true);
251     assertTrue(gene.isCompareApplicationData());
252   }
253
254   /**
255    * Test implementation of Gene interface extending abstract CommandGene class.
256    *
257    * @author Klaus Meffert
258    * @since 3.0
259    */

260   class CommandGeneImpl
261       extends CommandGene {
262
263     public CommandGeneImpl(final GPConfiguration a_config)
264         throws InvalidConfigurationException {
265       super(a_config,1,CommandGene.FloatClass);
266     }
267
268     protected Gene newGeneInternal() {
269       return null;
270     }
271
272     public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
273       return null;
274     }
275
276     public String JavaDoc toString() {
277       return "test";
278     }
279
280   }
281   class AppDataForTest
282       implements IApplicationData {
283     public int compareTo(Object JavaDoc o2) {
284       return 0;
285     }
286
287     public Object JavaDoc clone()
288         throws CloneNotSupportedException JavaDoc {
289       return null;
290     }
291   }
292 }
293
Popular Tags