KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > supergenes > SupergenePersistentRepresentationTest


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.supergenes;
11
12 import java.util.*;
13 import org.jgap.*;
14 import org.jgap.impl.*;
15 import junit.framework.*;
16
17 /**
18  * Test persistent representation of the AbstractSupergene.
19  *
20  * @author Meskauskas Audrius
21  * @since 2.0
22  */

23 public class SupergenePersistentRepresentationTest
24     extends JGAPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.15 $";
27
28   public static Test suite() {
29     TestSuite suite =
30         new TestSuite(SupergenePersistentRepresentationTest.class);
31     return suite;
32   }
33
34   public void setUp() {
35     super.setUp();
36     Configuration.reset();
37   }
38
39   public void testRepresentation()
40       throws Exception JavaDoc {
41     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
42     Gene i1 = new IntegerGene(conf, 1, 12);
43     Gene i2 = new DoubleGene(conf, 3, 4);
44     i1.setAllele(new Integer JavaDoc(7));
45     i2.setAllele(new Double JavaDoc(3.2));
46     gene.addGene(i1);
47     gene.addGene(i2);
48     InstantiableSupergeneForTest nested = new InstantiableSupergeneForTest(conf);
49     Gene n1 = new IntegerGene(conf, 1, 12);
50     Gene n2 = new DoubleGene(conf, 3, 4);
51     n1.setAllele(new Integer JavaDoc(5));
52     n2.setAllele(new Double JavaDoc(3.6));
53     nested.addGene(n1);
54     nested.addGene(n2);
55     gene.addGene(nested);
56     InstantiableSupergeneForTest nested2 = new InstantiableSupergeneForTest(conf);
57     nested2.setValidator(new TestValidator(conf));
58     Gene nn1 = new IntegerGene(conf, 1, 1000);
59     Gene nn2 = new DoubleGene(conf, 0, 1000);
60     nn1.setAllele(new Integer JavaDoc(22));
61     nn2.setAllele(new Double JavaDoc(44));
62     nested2.addGene(nn1);
63     nested2.addGene(nn2);
64     gene.addGene(nested2);
65     InstantiableSupergeneForTest nested3 = new InstantiableSupergeneForTest(conf);
66     nested3.setValidator(null);
67     Gene nnn1 = new IntegerGene(conf, 1, 1000);
68     Gene nnn2 = new DoubleGene(conf, 0, 1000);
69     nnn1.setAllele(new Integer JavaDoc(555));
70     nnn2.setAllele(new Double JavaDoc(777));
71     nested3.addGene(nnn1);
72     nested3.addGene(nnn2);
73     nested2.addGene(nested3);
74     String JavaDoc representation = gene.getPersistentRepresentation();
75     InstantiableSupergeneForTest restored = new InstantiableSupergeneForTest(conf);
76     restored.setValueFromPersistentRepresentation(representation);
77     assertTrue(gene.equals(restored));
78   }
79
80   public void testValidatorPers_0() {
81     Validator val = new TestValidator(conf);
82     String JavaDoc pers = val.getPersistent();
83     val.setFromPersistent(null);
84     assertSame(pers, val.getPersistent());
85     assertSame(conf, val.getConfiguration());
86   }
87
88   public void testGetGenes_0()
89       throws Exception JavaDoc {
90     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
91     assertEquals(0, gene.getGenes().length);
92     Gene subGene = new BooleanGene(conf);
93     gene.addGene(subGene);
94     assertEquals(1, gene.getGenes().length);
95     assertSame(subGene, gene.geneAt(0));
96   }
97
98   public void testSize_0()
99       throws Exception JavaDoc {
100     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
101     assertEquals(0, gene.size());
102     Gene subGene = new BooleanGene(conf);
103     gene.addGene(subGene);
104     assertEquals(1, gene.size());
105   }
106
107   public void testIsValid_0()
108       throws Exception JavaDoc {
109     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
110     gene.m_validator = null;
111     assertTrue(gene.isValid());
112     gene.m_validator = gene;
113     try {
114       assertTrue(gene.isValid());
115       fail();
116     }
117     catch (Error JavaDoc e) {
118       ; //this is OK
119
}
120   }
121
122   public void testReset_0()
123       throws Exception JavaDoc {
124     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
125     /**@todo care that m_immutable is filled*/
126     gene.reset();
127     Set[] m = (Set[]) privateAccessor.getField(gene, "m_immutable");
128     assertEquals(1, m.length);
129   }
130
131   public void testPers_0()
132       throws Exception JavaDoc {
133     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
134     try {
135       gene.setValueFromPersistentRepresentation(null);
136       fail();
137     }
138     catch (UnsupportedRepresentationException uex) {
139       ; //this is OK
140
}
141   }
142
143   public void testToString_0()
144       throws Exception JavaDoc {
145     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
146     gene.m_validator = null;
147     String JavaDoc s = gene.toString();
148     assertEquals("Supergene " + gene.getClass().getName()
149                  + " {"
150                  + " non validating"
151                  + "}"
152                  , s);
153   }
154
155   public void testToString_1()
156       throws Exception JavaDoc {
157     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
158     gene.m_validator = gene;
159     String JavaDoc s = gene.toString();
160     assertEquals("Supergene " + gene.getClass().getName()
161                  + " {"
162                  + " validator: " + gene.getClass().getName()
163                  + "}"
164                  , s);
165   }
166
167   public void testToString_2()
168       throws Exception JavaDoc {
169     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
170     Gene bgene = new BooleanGene(conf);
171     gene.addGene(bgene);
172     gene.m_validator = gene;
173     String JavaDoc s = gene.toString();
174     assertEquals("Supergene " + gene.getClass().getName()
175                  + " {|"
176                  + bgene.toString()
177                  + "|"
178                  + " validator: " + gene.getClass().getName()
179                  + "}"
180                  , s);
181   }
182
183   public void testCompareTo_0()
184       throws Exception JavaDoc {
185     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
186     assertEquals(0, gene.compareTo(gene));
187     InstantiableSupergeneForTest gene2 = new InstantiableSupergeneForTest(conf);
188     assertEquals(0, gene.compareTo(gene2));
189   }
190
191   public void testCompareTo_1()
192       throws Exception JavaDoc {
193     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
194     try {
195       gene.compareTo(new Vector());
196       fail();
197     }
198     catch (ClassCastException JavaDoc ex) {
199       ; //this is OK
200
}
201   }
202
203   public void testCompareTo_2()
204       throws Exception JavaDoc {
205     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
206     InstantiableSupergeneForTest gene2 = new InstantiableSupergeneForTest(conf);
207     gene2.addGene(new BooleanGene(conf));
208     assertEquals( -1, gene.compareTo(gene2));
209     assertEquals(1, gene2.compareTo(gene));
210   }
211
212   public void testCompareTo_3()
213       throws Exception JavaDoc {
214     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
215     AbstractSupergene gene2 = new TestClass(conf);
216     assertTrue(gene.compareTo(gene2) != 0);
217     assertTrue(gene2.compareTo(gene) != 0);
218     gene2.addGene(new BooleanGene(conf));
219     assertEquals( -1, gene.compareTo(gene2));
220     assertEquals(1, gene2.compareTo(gene));
221   }
222
223   public void testEquals_0()
224       throws Exception JavaDoc {
225     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
226     assertEquals(0, gene.compareTo(gene));
227     assertFalse(gene.equals(null));
228     assertFalse(gene.equals(new Vector()));
229     InstantiableSupergeneForTest gene2 = new InstantiableSupergeneForTest(conf);
230     assertTrue(gene.equals(gene2));
231   }
232
233   public void testNewGene_0()
234       throws Exception JavaDoc {
235     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
236     gene.m_validator = null;
237     InstantiableSupergeneForTest gene2 = (InstantiableSupergeneForTest) gene.newGene();
238     assertEquals(gene, gene2);
239     assertEquals(gene2, gene);
240   }
241
242   public void testNewGene_1()
243       throws Exception JavaDoc {
244     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
245     for (int i = 0; i < 5; i++) {
246       Gene aGene = new DoubleGene(conf);
247       gene.addGene(aGene);
248     }
249     InstantiableSupergeneForTest gene2 = (InstantiableSupergeneForTest) gene.newGene();
250     assertEquals(gene, gene2);
251     assertEquals(gene2, gene);
252   }
253
254   public void testNewGene_2()
255       throws Exception JavaDoc {
256     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
257     gene.m_validator = gene;
258     InstantiableSupergeneForTest gene2 = (InstantiableSupergeneForTest) gene.newGene();
259     assertEquals(gene, gene2);
260     assertEquals(gene2, gene);
261   }
262
263   public void testSetAllele_0()
264       throws Exception JavaDoc {
265     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
266     gene.setAllele(new Double JavaDoc(23.4));
267     assertEquals(0, gene.size());
268   }
269
270   public void testSetAllele_1()
271       throws Exception JavaDoc {
272     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
273     for (int i = 0; i < 2; i++) {
274       Gene aGene = new DoubleGene(conf);
275       gene.addGene(aGene);
276     }
277     try {
278       gene.setAllele(new Double JavaDoc[] {new Double JavaDoc(23.569)});
279       fail();
280     }
281     catch (IllegalArgumentException JavaDoc iex) {
282       ; //this is OK
283
}
284   }
285
286   public void testSetAllele_2()
287       throws Exception JavaDoc {
288     InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf);
289     for (int i = 0; i < 5; i++) {
290       Gene aGene = new DoubleGene(conf);
291       gene.addGene(aGene);
292     }
293     gene.setAllele(new Double JavaDoc[] {new Double JavaDoc(03.569), new Double JavaDoc(13.569),
294                    new Double JavaDoc(23.569), new Double JavaDoc(33.569), new Double JavaDoc(43.569)});
295     for (int i = 0; i < 5; i++) {
296       DoubleGene aGene = (DoubleGene) gene.geneAt(i);
297       assertEquals(i * 10 + 3.569, aGene.doubleValue(), DELTA);
298     }
299   }
300
301 //
302
// public class InstantiableSupergeneForTest
303
// extends AbstractSupergene {
304
// public InstantiableSupergeneForTest(final Configuration a_config, Gene[] a_genes)
305
// throws InvalidConfigurationException {
306
// super(a_config, a_genes);
307
// }
308
//
309
// public InstantiableSupergeneForTest(final Configuration a_config)
310
// throws InvalidConfigurationException {
311
// super(a_config, new Gene[]{});
312
// }
313
//
314
// public InstantiableSupergeneForTest()
315
// throws InvalidConfigurationException {
316
// this(Genotype.getConfiguration());
317
// }
318
//
319
// public boolean isValid(Gene[] a_gene) {
320
// return true;
321
// };
322
// }
323
//
324
class TestClass
325       extends AbstractSupergene {
326     public TestClass(final Configuration a_conf)
327         throws InvalidConfigurationException {
328       super(a_conf, new Gene[] {});
329     }
330
331     public boolean isValid(Gene[] a) {
332       throw new Error JavaDoc("Should never be called.");
333     }
334
335     protected Gene newGeneInternal() {
336       throw new Error JavaDoc("Should never be called.");
337     }
338   }
339 }
340
Popular Tags