KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 /**
17  * Tests the CompositeGene class.
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

22 public class CompositeGeneTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.47 $";
26
27   private static int cleanedUp = 0;
28
29   public static Test suite() {
30     TestSuite suite = new TestSuite(CompositeGeneTest.class);
31     return suite;
32   }
33
34   /**
35    * @throws Exception
36    * @author Klaus Meffert
37    * @since 2.4
38    */

39   public void testCompareTo_4()
40       throws Exception JavaDoc {
41     Configuration conf = new ConfigurationForTest();
42     CompositeGene gene1 = new CompositeGene(conf);
43     Gene newGene1 = new IntegerGene(conf, 3, 5);
44     newGene1.setAllele(new Integer JavaDoc(4));
45     Gene newGene2 = new IntegerGene(conf, 3, 5);
46     newGene2.setAllele(new Integer JavaDoc(2));
47     gene1.addGene(newGene1, false);
48     gene1.addGene(newGene2, false);
49     CompositeGene gene2 = new CompositeGene(conf);
50     Gene newGene3 = new IntegerGene(conf, 3, 5);
51     newGene3.setAllele(new Integer JavaDoc(1));
52     newGene3.setAllele(new Integer JavaDoc(3));
53     gene2.addGene(newGene3, false);
54     assertEquals(1, gene1.compareTo(gene2));
55     assertEquals( -1, gene2.compareTo(gene1));
56   }
57
58   /**
59    *
60    * @author Klaus Meffert
61    * @throws Exception
62    */

63   public void testConstruct_0()
64       throws Exception JavaDoc {
65     //following should be possible without exception
66
CompositeGene gene = new CompositeGene(conf);
67     assertEquals(null, gene.getGeneTypeAllowed());
68   }
69
70   /**
71    * @throws Exception
72    *
73    * @author Klaus Meffert
74    * @since 3.1
75    */

76   public void testConstruct_1()
77       throws Exception JavaDoc {
78     Genotype.setStaticConfiguration(conf);
79     CompositeGene gene = new CompositeGene();
80     assertSame(conf, gene.getConfiguration());
81   }
82
83   /**
84    * Ensures that a CompositeGene may be added to a CompositeGene
85    *
86    * @author Audrius Meskauskas
87    * @author Klaus Meffert
88    * @since 2.0
89    * @throws Exception
90    */

91   public void testAddGene_0()
92       throws Exception JavaDoc {
93     CompositeGene gene = new CompositeGene(conf);
94     gene.addGene(new CompositeGene(conf), false);
95     assertEquals(1, gene.size());
96   }
97
98   public void testAddGene_1()
99       throws Exception JavaDoc {
100     CompositeGene gene = new CompositeGene(conf);
101     Gene newGene = new DoubleGene(conf);
102     gene.addGene(newGene, false);
103     assertEquals(1, gene.size());
104     gene.addGene(new DoubleGene(conf, 1.2d, 3.4d), false);
105     assertEquals(2, gene.size());
106     //try to remove a non existent gene
107
gene.removeGeneByIdentity(new DoubleGene(conf));
108     assertEquals(2, gene.size());
109     //try to add an already existent gene
110
try {
111       gene.addGene(newGene, false);
112       fail();
113     }
114     catch (IllegalArgumentException JavaDoc iex) {
115       ; //this is OK
116
}
117   }
118
119   /**
120    *
121    * @author Klaus Meffert
122    * @since 2.2
123    * @throws Exception
124    */

125   public void testAddGene_2()
126       throws Exception JavaDoc {
127     CompositeGene gene = new CompositeGene(conf, new DoubleGene(conf, 2, 3));
128     assertEquals(new DoubleGene(conf), gene.getGeneTypeAllowed());
129     gene.addGene(new DoubleGene(conf), false);
130     assertEquals(1, gene.size());
131   }
132
133   /**
134    *
135    * @author Klaus Meffert
136    * @since 2.2
137    * @throws Exception
138    */

139   public void testAddGene_3()
140       throws Exception JavaDoc {
141     CompositeGene gene = new CompositeGene(conf, new DoubleGene(conf, 2, 3));
142     try {
143       gene.addGene(new CompositeGene(conf), false);
144       fail();
145     }
146     catch (IllegalArgumentException JavaDoc iex) {
147       ; //this is OK
148
}
149   }
150
151   /**
152    *
153    * @author Klaus Meffert
154    * @since 2.2
155    * @throws Exception
156    */

157   public void testAddGene_4()
158       throws Exception JavaDoc {
159     CompositeGene gene = new CompositeGene(conf);
160     gene.addGene(new CompositeGene(conf), true);
161     assertEquals(1, gene.size());
162   }
163
164   /**
165    *
166    * @author Klaus Meffert
167    * @since 2.2
168    * @throws Exception
169    */

170   public void testAddGene_5()
171       throws Exception JavaDoc {
172     CompositeGene gene = new CompositeGene(conf);
173     CompositeGene gene2 = new CompositeGene(conf);
174     gene.addGene(gene2, true);
175     try {
176       gene.addGene(gene2, false);
177       fail();
178     }
179     catch (IllegalArgumentException JavaDoc iex) {
180       ; //this is OK
181
}
182   }
183
184   /**
185    * Adding two different initial genes with strict parameter set is not allowed.
186    *
187    * @author Klaus Meffert
188    * @since 2.2
189    * @throws Exception
190    */

191   public void testAddGene_6()
192       throws Exception JavaDoc {
193     CompositeGene gene = new CompositeGene(conf);
194     gene.addGene(new CompositeGene(conf), true);
195     try {
196       gene.addGene(new CompositeGene(conf), true);
197       fail();
198     }
199     catch (IllegalArgumentException JavaDoc iex) {
200       ; //this is OK
201
}
202   }
203
204   /**
205    * Adding an initial CompositeGene twice should be possible, if they are two
206    * newly created instances.
207    *
208    * @author Klaus Meffert
209    * @since 2.2
210    * @throws Exception
211    */

212   public void testAddGene_7()
213       throws Exception JavaDoc {
214     CompositeGene gene = new CompositeGene(conf);
215     gene.addGene(new CompositeGene(conf), true);
216     gene.addGene(new CompositeGene(conf), false);
217   }
218
219   /**
220    *
221    * @author Klaus Meffert
222    * @throws Exception
223    */

224   public void testToString_0()
225       throws Exception JavaDoc {
226     CompositeGene gene = new CompositeGene(conf);
227     Gene newGene1 = new DoubleGene(conf);
228     newGene1.setAllele(new Double JavaDoc(47.123d));
229     gene.addGene(newGene1, false);
230     assertEquals("CompositeGene=(" + newGene1.toString() + ")", gene.toString());
231     Gene newGene2 = new IntegerGene(conf);
232     newGene2.setAllele(new Integer JavaDoc(23456));
233     gene.addGene(newGene2, false);
234     assertEquals("CompositeGene=(" + newGene1.toString() + gene.GENE_DELIMITER
235                  + newGene2.toString() + ")", gene.toString());
236   }
237
238   /**
239    *
240    * @author Klaus Meffert
241    * @since 2.2
242    * @throws Exception
243    */

244   public void testToString_1()
245       throws Exception JavaDoc {
246     CompositeGene gene = new CompositeGene(conf);
247     assertEquals("CompositeGene=null", gene.toString());
248   }
249
250   public void testGetAllele_0()
251       throws Exception JavaDoc {
252     Gene gene = new CompositeGene(conf);
253     //this should be possible without exception
254
gene.setAllele(new Vector());
255   }
256
257   public void testGetAllele_1()
258       throws Exception JavaDoc {
259     CompositeGene gene = new CompositeGene(conf);
260     Gene newGene1 = new IntegerGene(conf, 1, 5);
261     gene.addGene(newGene1, false);
262     Vector v = new Vector();
263     v.add(new Integer JavaDoc(4));
264     gene.setAllele(v);
265     v = (Vector) gene.getAllele();
266     assertEquals(newGene1.getAllele(), v.elementAt(0));
267     assertEquals(1, v.size());
268   }
269
270   public void testGetAllele_2()
271       throws Exception JavaDoc {
272     CompositeGene gene = new CompositeGene(conf);
273     Gene newGene1 = new IntegerGene(conf, 1, 5);
274     gene.addGene(newGene1, false);
275     Gene newGene2 = new DoubleGene(conf, 77.2d, 999.0d);
276     gene.addGene(newGene2, false);
277     Vector v = new Vector();
278     v.add(new Integer JavaDoc(4));
279     v.add(new Double JavaDoc(333.5d));
280     gene.setAllele(v);
281     v = (Vector) gene.getAllele();
282     assertEquals(newGene1.getAllele(), v.elementAt(0));
283     assertEquals(newGene2.getAllele(), v.elementAt(1));
284     assertEquals(2, v.size());
285   }
286
287   public void testEquals_0()
288       throws Exception JavaDoc {
289     Gene gene1 = new CompositeGene(conf);
290     Gene gene2 = new CompositeGene(conf);
291     assertTrue(gene1.equals(gene2));
292     assertTrue(gene2.equals(gene1));
293   }
294
295   public void testEquals_1()
296       throws Exception JavaDoc {
297     Gene gene1 = new CompositeGene(conf);
298     assertFalse(gene1.equals(null));
299   }
300
301   public void testEquals_2()
302       throws Exception JavaDoc {
303     Gene gene1 = new CompositeGene(conf);
304     assertFalse(gene1.equals(new BooleanGene(conf)));
305   }
306
307   public void testEquals_3()
308       throws Exception JavaDoc {
309     Gene gene1 = new CompositeGene(conf);
310     assertFalse(gene1.equals(new Vector()));
311   }
312
313   /**
314    * Tests that no NullPointerException arises.
315    *
316    * @author Klaus Meffert
317    * @since 2.6
318    * @throws Exception
319    */

320   public void testEquals_4_2()
321       throws Exception JavaDoc {
322     CompositeGene gene1 = new CompositeGene(conf, new BooleanGene(conf));
323     try {
324       gene1.addGene(null, false);
325       fail();
326     }
327     catch (IllegalArgumentException JavaDoc iex) {
328       ; // this is expected
329
}
330   }
331
332   public void testEquals_5()
333       throws Exception JavaDoc {
334     CompositeGene gene1 = new CompositeGene(conf);
335     Gene newGene1 = new IntegerGene(conf, 3, 5);
336     gene1.addGene(newGene1, false);
337     CompositeGene gene2 = new CompositeGene(conf);
338     gene2.addGene(newGene1, false);
339     assertTrue(gene1.equals(gene2));
340     assertTrue(gene2.equals(gene1));
341   }
342
343   /**
344    *
345    * @author Klaus Meffert
346    * @since 2.4
347    * @throws Exception
348    */

349   public void testEquals_5_2()
350       throws Exception JavaDoc {
351     CompositeGene gene1 = new CompositeGene(conf);
352     Gene newGene1 = new IntegerGene(conf, 3, 5);
353     Gene newGene2 = new IntegerGene(conf, 3, 7);
354     gene1.addGene(newGene1, false);
355     gene1.addGene(newGene2, false);
356     CompositeGene gene2 = new CompositeGene(conf);
357     gene2.addGene(newGene2, false);
358     assertFalse(gene1.equals(gene2));
359     assertFalse(gene2.equals(gene1));
360   }
361
362   public void testEquals_6()
363       throws Exception JavaDoc {
364     CompositeGene gene1 = new CompositeGene(conf);
365     Gene newGene1 = new IntegerGene(conf, 3, 5);
366     gene1.addGene(newGene1, false);
367     Gene newGene2 = new IntegerGene(conf, 7, 9);
368     CompositeGene gene2 = new CompositeGene(conf);
369     gene2.addGene(newGene2, false);
370     assertEquals(newGene1.equals(newGene2), gene1.equals(gene2));
371     assertEquals(newGene2.equals(newGene1), gene2.equals(gene1));
372   }
373
374   public void testEquals_7()
375       throws Exception JavaDoc {
376     CompositeGene gene1 = new CompositeGene(conf);
377     Gene newGene1 = new IntegerGene(conf, 3, 5);
378     gene1.addGene(newGene1, false);
379     Gene newGene2 = new DoubleGene(conf, 7.2d, 59.4d);
380     CompositeGene gene2 = new CompositeGene(conf);
381     gene2.addGene(newGene2, false);
382     assertEquals(newGene1.equals(newGene2), gene1.equals(gene2));
383     assertEquals(newGene2.equals(newGene1), gene2.equals(gene1));
384   }
385
386   /**
387    *
388    * @author Klaus Meffert
389    * @since 2.4
390    * @throws Exception
391    */

392   public void testEquals_8()
393       throws Exception JavaDoc {
394     Gene newGene1 = new IntegerGene(conf, 3, 5);
395     newGene1.setAllele(new Integer JavaDoc(3));
396     Gene newGene2 = new IntegerGene(conf, 3, 15);
397     newGene2.setAllele(new Integer JavaDoc(9));
398     CompositeGene gene1 = new CompositeGene(conf);
399     gene1.addGene(newGene1, false);
400     CompositeGene gene2 = new CompositeGene(conf);
401     gene2.addGene(newGene2, false);
402     assertFalse(gene1.equals(gene2));
403     assertFalse(gene2.equals(gene1));
404   }
405
406   public void testSetAllele_0()
407       throws Exception JavaDoc {
408     Gene gene = new CompositeGene(conf);
409     try {
410       gene.setAllele(null);
411       fail();
412     }
413     catch (IllegalArgumentException JavaDoc iex) {
414       ; //this is OK
415
}
416   }
417
418   public void testSetAllele_1()
419       throws Exception JavaDoc {
420     Gene gene = new CompositeGene(conf);
421     try {
422       gene.setAllele(new Double JavaDoc(2.3d));
423       fail();
424     }
425     catch (IllegalArgumentException JavaDoc iex) {
426       ; //this is OK
427
}
428   }
429
430   /**
431    * Set Allele to empty Vector, no exception should occur.
432    *
433    * @throws Exception
434    */

435   public void testSetAllele_2()
436       throws Exception JavaDoc {
437     Gene gene1 = new CompositeGene(conf);
438     gene1.setAllele(new Vector());
439   }
440
441   public void testSetAllele_3()
442       throws Exception JavaDoc {
443     Gene gene1 = new CompositeGene(conf);
444     try {
445       gene1.setAllele("22");
446       fail();
447     }
448     catch (IllegalArgumentException JavaDoc classex) {
449       ; //this is OK
450
}
451   }
452
453   /**
454    *
455    * @author Klaus Meffert
456    * @since 2.2
457    * @throws Exception
458    */

459   public void testSetAllele_4()
460       throws Exception JavaDoc {
461     CompositeGene gene = new CompositeGene(conf);
462     DoubleGene gene2 = new DoubleGene(conf, 1.0d, 3.0d);
463     gene2.setAllele(new Double JavaDoc(1.0d));
464     gene.addGene(gene2);
465     gene.setConstraintChecker(new IGeneConstraintChecker() {
466       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
467                             IChromosome a_chrom, int a_index) {
468         return false;
469       }
470     });
471     List l = new Vector();
472     l.add(new Double JavaDoc(2.0d));
473     gene.setAllele(l);
474     assertEquals(1.0d, ( (Double JavaDoc) gene2.getAllele()).doubleValue(), DELTA);
475   }
476
477   /**
478    *
479    * @author Klaus Meffert
480    * @since 2.2
481    * @throws Exception
482    */

483   public void testSetAllele_5()
484       throws Exception JavaDoc {
485     CompositeGene gene = new CompositeGene(conf);
486     DoubleGene gene2 = new DoubleGene(conf, 1.0d, 3.0d);
487     gene2.setAllele(new Double JavaDoc(1.0d));
488     gene.addGene(gene2);
489     gene.setConstraintChecker(new IGeneConstraintChecker() {
490       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
491                             IChromosome a_chrom, int a_index) {
492         return true;
493       }
494     });
495     List l = new Vector();
496     l.add(new Double JavaDoc(2.0d));
497     gene.setAllele(l);
498     assertEquals(2.0d, ( (Double JavaDoc) gene2.getAllele()).doubleValue(), DELTA);
499   }
500
501   public void testNewGene_0()
502       throws Exception JavaDoc {
503     CompositeGene gene1 = new CompositeGene(conf);
504     IGeneConstraintChecker checker = new GeneConstraintChecker();
505     gene1.setConstraintChecker(checker);
506     gene1.addGene(new DoubleGene(conf, 2.05d, 7.53d), false);
507     gene1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false);
508     gene1.addGene(new IntegerGene(conf, 3, 8), false);
509     gene1.addGene(new BooleanGene(conf), false);
510     gene1.addGene(new StringGene(conf), false);
511     gene1.addGene(new StringGene(conf, 2, 5), false);
512     gene1.addGene(new StringGene(conf, 6, 11, "ABC"), false);
513     CompositeGene gene2 = (CompositeGene) gene1.newGene();
514     assertTrue(gene1.equals(gene2));
515     assertEquals(checker, gene2.getConstraintChecker());
516     //Remove all genes from gene2 that are contained in gene1.
517
//Because they should be equal, gene2 should then be empty.
518
//---------------------------------------------------------
519
for (int i = 0; i < gene1.size(); i++) {
520       gene2.removeGene(gene1.geneAt(i));
521     }
522     assertEquals(0, gene2.size());
523   }
524
525   public void testPersistentPresentation_0()
526       throws Exception JavaDoc {
527     CompositeGene gene1 = new CompositeGene(conf);
528     Gene gene0 = new DoubleGene(conf, 2.05d, 7.53d);
529     gene0.setAllele(new Double JavaDoc(7.52d));
530     gene1.addGene(gene0, false);
531     gene1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false);
532     gene0 = new IntegerGene(conf, 3, 8);
533     gene0.setAllele(new Integer JavaDoc(5));
534     gene1.addGene(gene0, false);
535     gene0 = new BooleanGene(conf);
536     gene0.setAllele(Boolean.valueOf(true));
537     gene1.addGene(gene0, false);
538     gene1.addGene(new StringGene(conf), false);
539     gene1.addGene(new StringGene(conf, 2, 5), false);
540     gene0 = new StringGene(conf, 6, 11, ":ABC"); // using ':'
541
gene0.setAllele("B:BCBCCA");
542     gene1.addGene(gene0, false);
543     String JavaDoc pres1 = gene1.getPersistentRepresentation();
544     CompositeGene gene2 = new CompositeGene(conf);
545     gene2.setValueFromPersistentRepresentation(pres1);
546     String JavaDoc pres2 = gene2.getPersistentRepresentation();
547     assertEquals(pres1, pres2);
548   }
549
550   /**
551    * Test a nested persistent representation, including strings
552    * @throws Exception
553    *
554    * @author Audrius Meskauskas
555    * @since 2.0
556    */

557   public void testPersistentPresentation_1()
558       throws Exception JavaDoc {
559     CompositeGene composite1 = new CompositeGene(conf);
560     Gene strgene = new DoubleGene(conf, 2.05d, 7.53d);
561     strgene.setAllele(new Double JavaDoc(7.52d));
562     composite1.addGene(strgene, false);
563     composite1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false);
564     strgene = new IntegerGene(conf, 3, 8);
565     strgene.setAllele(new Integer JavaDoc(5));
566     composite1.addGene(strgene, false);
567     strgene = new BooleanGene(conf);
568     strgene.setAllele(Boolean.valueOf(true));
569     composite1.addGene(strgene, false);
570     composite1.addGene(new StringGene(conf), false);
571     composite1.addGene(new StringGene(conf, 2, 5), false);
572     String JavaDoc string = "<!-- many:various:chars &%$§/()=<>C:CA/ -->";
573     strgene = new StringGene(conf, 6, 50, "CA! many:various:chars<>:&%$§/()-=");
574     strgene.setAllele(string);
575     // remember where, we will check the value later
576
int stringPosition = composite1.size();
577     composite1.addGene(strgene, false);
578     CompositeGene compositeInside = new CompositeGene(conf);
579     Gene istrgene = new DoubleGene(conf, 2.05d, 17.53d);
580     istrgene.setAllele(new Double JavaDoc(3.33));
581     compositeInside.addGene(istrgene, false);
582     compositeInside.addGene(new DoubleGene(conf, 128.35d, 155.90d), false);
583     istrgene = new IntegerGene(conf, 3, 8);
584     istrgene.setAllele(new Integer JavaDoc(5));
585     compositeInside.addGene(istrgene, false);
586     istrgene = new BooleanGene(conf);
587     istrgene.setAllele(Boolean.valueOf(true));
588     compositeInside.addGene(istrgene, false);
589     compositeInside.addGene(new StringGene(conf), false);
590     compositeInside.addGene(new StringGene(conf, 2, 5), false);
591     istrgene = new StringGene(conf, 6, 11, "&xyzab<:>");
592     String JavaDoc string2 = "<:yzab:>";
593     istrgene.setAllele(string2);
594     int position2 = compositeInside.size();
595     compositeInside.addGene(istrgene, false);
596     int whereCompositeGene = composite1.size();
597     composite1.addGene(compositeInside);
598     String JavaDoc pres1 = composite1.getPersistentRepresentation();
599     CompositeGene gene2 = new CompositeGene(conf);
600     gene2.setValueFromPersistentRepresentation(pres1);
601     String JavaDoc pres2 = gene2.getPersistentRepresentation();
602     assertEquals(pres1, pres2);
603     // check the string
604
StringGene s = (StringGene) gene2.geneAt(stringPosition);
605     assertTrue(string.equals(s.getAllele()));
606     // check also in the composite gene
607
CompositeGene cg = (CompositeGene) gene2.geneAt(whereCompositeGene);
608     s = (StringGene) cg.geneAt(position2);
609     assertTrue(string2.equals(s.getAllele()));
610   }
611
612   /**
613    * @throws Exception
614    *
615    * @author Klaus Meffert
616    * @since 2.2
617    */

618   public void testPersistentPresentation_2()
619       throws Exception JavaDoc {
620     CompositeGene gene1 = new CompositeGene(conf);
621     gene1.setValueFromPersistentRepresentation(null);
622     assertEquals(0, gene1.size());
623   }
624
625   /**
626    *
627    * @throws Exception
628    *
629    * @author Klaus Meffert
630    * @since 2.2
631    */

632   public void testPersistentPresentation_3()
633       throws Exception JavaDoc {
634     CompositeGene gene1 = new CompositeGene(conf);
635     try {
636       gene1.setValueFromPersistentRepresentation("1"
637                                                  + CompositeGene.GENE_DELIMITER
638                                                  + "2");
639       fail();
640     }
641     catch (UnsupportedRepresentationException uex) {
642       ; //this is OK
643
}
644   }
645
646   /**
647    * Invalid Gene class.
648    *
649    * @throws Exception
650    *
651    * @author Klaus Meffert
652    * @since 2.2
653    */

654   public void testPersistentPresentation_4()
655       throws Exception JavaDoc {
656     CompositeGene gene1 = new CompositeGene(conf);
657     try {
658       gene1.setValueFromPersistentRepresentation("<1"
659                                                  + CompositeGene.GENE_DELIMITER
660                                                  + "2>");
661       fail();
662     }
663     catch (UnsupportedRepresentationException uex) {
664       ; //this is OK
665
}
666   }
667
668   /**
669    *
670    * @throws Exception
671    *
672    * @author Klaus Meffert
673    * @since 2.2
674    */

675   public void testPersistentPresentation_5()
676       throws Exception JavaDoc {
677     CompositeGene gene1 = new CompositeGene(conf);
678     try {
679       gene1.setValueFromPersistentRepresentation("<1>");
680       fail();
681     }
682     catch (UnsupportedRepresentationException uex) {
683       ; //this is OK
684
}
685   }
686
687   /**
688    * No closing tag.
689    *
690    * @throws Exception
691    *
692    * @author Klaus Meffert
693    * @since 2.6
694    */

695   public void testPersistentPresentation_6()
696       throws Exception JavaDoc {
697     CompositeGene gene1 = new CompositeGene(conf);
698     try {
699       gene1.setValueFromPersistentRepresentation("<IntegerGene"
700                                                  + CompositeGene.GENE_DELIMITER
701                                                  + "2<");
702       fail();
703     }
704     catch (UnsupportedRepresentationException uex) {
705       ; //this is OK
706
}
707   }
708
709   /**
710    * Empty representation.
711    *
712    * @throws Exception
713    *
714    * @author Klaus Meffert
715    * @since 2.6
716    */

717   public void testPersistentPresentation_7()
718       throws Exception JavaDoc {
719     CompositeGene gene1 = new CompositeGene(conf);
720     try {
721       gene1.setValueFromPersistentRepresentation("<org.jgap.impl.IntegerGene"
722                                                  + CompositeGene.GENE_DELIMITER
723                                                  + "2:4:4"
724                                                  + CompositeGene.GENE_DELIMITER
725                                                  + "><>");
726       fail();
727     }
728     catch (UnsupportedRepresentationException uex) {
729       assertEquals(1, gene1.size());
730       ; //this is OK
731
}
732   }
733
734   /**
735    * Tests if removal from empty list returns false no matter what to remove.
736    *
737    * @throws Exception
738    *
739    * @author Klaus Meffert
740    * @since 2.2
741    */

742   public void testRemoveGeneByIdentity_0()
743       throws Exception JavaDoc {
744     CompositeGene gene1 = new CompositeGene(conf);
745     assertFalse(gene1.removeGeneByIdentity(gene1));
746     assertFalse(gene1.removeGeneByIdentity(null));
747   }
748
749   /**
750    *
751    * @throws Exception
752    *
753    * @author Klaus Meffert
754    * @since 2.2
755    */

756   public void testRemoveGeneByIdentity_1()
757       throws Exception JavaDoc {
758     CompositeGene gene1 = new CompositeGene(conf);
759     DoubleGene gene2 = new DoubleGene(conf, 1.0d, 4.0d);
760     gene1.addGene(gene2);
761     DoubleGene gene3 = new DoubleGene(conf, 1.0d, 5.0d);
762     assertFalse(gene1.removeGeneByIdentity(gene3));
763     assertTrue(gene1.removeGeneByIdentity(gene2));
764     assertEquals(0, gene1.size());
765   }
766
767   /**
768    *
769    * @throws Exception
770    *
771    * @author Klaus Meffert
772    * @since 2.2
773    */

774   public void testCleanup_0()
775       throws Exception JavaDoc {
776     CompositeGene gene = new CompositeGene(conf);
777     CompositeGene gene2 = new CompositeGene(conf) {
778       public void cleanup() {
779         cleanedUp++;
780       }
781     };
782     CompositeGene gene3 = new CompositeGene(conf) {
783       public void cleanup() {
784         cleanedUp++;
785       }
786     };
787     gene.addGene(gene2, true);
788     gene.addGene(gene3, false);
789     gene.cleanup();
790     assertEquals(2, gene.size());
791     assertEquals(2, cleanedUp);
792   }
793
794   /**
795    *
796    * @throws Exception
797    *
798    * @author Klaus Meffert
799    * @since 2.2
800    */

801   public void testCleanup_1()
802       throws Exception JavaDoc {
803     CompositeGene gene = new CompositeGene(conf);
804     gene.cleanup();
805     assertEquals(0, gene.size());
806   }
807
808   /**
809    *
810    * @throws Exception
811    *
812    * @author Klaus Meffert
813    * @since 2.2
814    */

815   public void testSetConstraintChecker_0()
816       throws Exception JavaDoc {
817     CompositeGene gene = new CompositeGene(conf);
818     assertNull(gene.getConstraintChecker());
819     gene.setConstraintChecker(new IGeneConstraintChecker() {
820       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
821                             IChromosome a_chrom, int a_index) {
822         return false;
823       }
824     });
825     assertNotNull(gene.getConstraintChecker());
826   }
827
828   /**
829    *
830    * @throws Exception
831    *
832    * @author Klaus Meffert
833    * @since 2.2
834    */

835   public void testSetToRandomValue_0()
836       throws Exception JavaDoc {
837     Gene gene1 = new CompositeGene(conf);
838     try {
839       gene1.setToRandomValue(null);
840       fail();
841     }
842     catch (IllegalArgumentException JavaDoc iex) {
843       ; //this is OK
844
}
845   }
846
847   /**
848    *
849    * @throws Exception
850    *
851    * @author Klaus Meffert
852    * @since 2.2
853    */

854   public void testSetToRandomValue_1()
855       throws Exception JavaDoc {
856     CompositeGene gene1 = new CompositeGene(conf);
857     DoubleGene d = new DoubleGene(conf, 0.0d, 1.0d);
858     gene1.addGene(d);
859     gene1.setToRandomValue(new RandomGeneratorForTest(0.23d));
860     assertEquals(0.23d, d.doubleValue(), DELTA);
861   }
862
863   /**
864    *
865    * @throws Exception
866    *
867    * @author Klaus Meffert
868    * @since 2.2
869    */

870   public void testSetToRandomValue_2()
871       throws Exception JavaDoc {
872     CompositeGene gene1 = new CompositeGene(conf);
873     DoubleGene d = new DoubleGene(conf, 0.5d, 1.8d);
874     gene1.addGene(d);
875     gene1.setToRandomValue(new RandomGeneratorForTest(0.23d));
876     assertEquals( (1.8d - 0.5d) * 0.23d + 0.5d, d.doubleValue(), DELTA);
877   }
878
879   /**
880    *
881    * @throws Exception
882    *
883    * @author Klaus Meffert
884    * @since 2.4
885    */

886   public void testSetEnergy_0()
887       throws Exception JavaDoc {
888     BaseGene gene = new CompositeGene(conf);
889     assertEquals(0.0, gene.getEnergy(), DELTA);
890   }
891
892   /**
893    *
894    * @throws Exception
895    *
896    * @author Klaus Meffert
897    * @since 2.4
898    */

899   public void testSetEnergy_1()
900       throws Exception JavaDoc {
901     BaseGene gene = new CompositeGene(conf);
902     gene.setEnergy(2.3);
903     assertEquals(2.3, gene.getEnergy(), DELTA);
904     gene.setEnergy( -55.8);
905     assertEquals( -55.8, gene.getEnergy(), DELTA);
906     gene.setEnergy(0.5);
907     gene.setEnergy(0.8);
908     assertEquals(0.8, gene.getEnergy(), DELTA);
909   }
910
911   /**
912    *
913    * @throws Exception
914    *
915    * @author Klaus Meffert
916    * @since 2.4
917    */

918   public void testCompareTo_0()
919       throws Exception JavaDoc {
920     CompositeGene gene1 = new CompositeGene(conf);
921     Gene newGene1 = new IntegerGene(conf, 3, 5);
922     Gene newGene2 = new IntegerGene(conf, 3, 7);
923     gene1.addGene(newGene1, false);
924     gene1.addGene(newGene2, false);
925     CompositeGene gene2 = new CompositeGene(conf);
926     gene2.addGene(newGene2, false);
927     assertEquals(1, gene1.compareTo(gene2));
928     assertEquals( -1, gene2.compareTo(gene1));
929   }
930
931   /**
932    *
933    * @throws Exception
934    *
935    * @author Klaus Meffert
936    * @since 2.4
937    */

938   public void testCompareTo_1()
939       throws Exception JavaDoc {
940     CompositeGene gene1 = new CompositeGene(conf);
941     Gene newGene1 = new IntegerGene(conf, 3, 5);
942     gene1.addGene(newGene1, false);
943     CompositeGene gene2 = new CompositeGene(conf);
944     gene2.addGene(newGene1, false);
945     assertEquals(0, gene1.compareTo(gene2));
946     assertEquals(0, gene2.compareTo(gene1));
947   }
948
949   /**
950    *
951    * @throws Exception
952    *
953    * @author Klaus Meffert
954    * @since 2.4
955    */

956   public void testCompareTo_2()
957       throws Exception JavaDoc {
958     CompositeGene gene1 = new CompositeGene(conf);
959     Gene newGene1 = new IntegerGene(conf, 3, 5);
960     gene1.addGene(newGene1, false);
961     CompositeGene gene2 = new CompositeGene(conf);
962     Gene newGene2 = new IntegerGene(conf, 3, 5);
963     gene2.addGene(newGene2, false);
964     assertEquals(0, gene1.compareTo(gene2));
965     assertEquals(0, gene2.compareTo(gene1));
966   }
967
968   /**
969    *
970    * @throws Exception
971    *
972    * @author Klaus Meffert
973    * @since 2.4
974    */

975   public void testCompareTo_3()
976       throws Exception JavaDoc {
977     CompositeGene gene1 = new CompositeGene(conf);
978     Gene newGene1 = new IntegerGene(conf, 3, 5);
979     gene1.addGene(newGene1, false);
980     Gene newGene2 = new IntegerGene(conf, 3, 5);
981     newGene2.setAllele(new Integer JavaDoc(2));
982     int i = ( (Integer JavaDoc) newGene2.getAllele()).intValue();
983     assertTrue(i >= 3 && i <= 5);
984   }
985
986   public void testCompareTo_3_2()
987       throws Exception JavaDoc {
988     Configuration conf = new ConfigurationForTest();
989     CompositeGene gene1 = new CompositeGene(conf);
990     Gene newGene1 = new IntegerGene(conf, 3, 5);
991     gene1.addGene(newGene1, false);
992     CompositeGene gene2 = new CompositeGene(conf);
993     Gene newGene2 = new IntegerGene(conf, 3, 5);
994     newGene2.setAllele(new Integer JavaDoc(2));
995     gene2.addGene(newGene2, false);
996     assertEquals( -1, gene1.compareTo(gene2));
997     assertEquals(1, gene2.compareTo(gene1));
998   }
999
1000  /**
1001   * Using application data.
1002   *
1003   * @throws Exception
1004   *
1005   * @author Klaus Meffert
1006   * @since 2.4
1007   */

1008  public void testCompareTo_5()
1009      throws Exception JavaDoc {
1010    CompositeGene gene1 = new CompositeGene(conf);
1011    gene1.setCompareApplicationData(true);
1012    Gene newGene1 = new IntegerGene(conf, 3, 5);
1013    gene1.addGene(newGene1, false);
1014    CompositeGene gene2 = new CompositeGene(conf);
1015    gene2.setCompareApplicationData(true);
1016    Gene newGene2 = new IntegerGene(conf, 3, 5);
1017    gene2.addGene(newGene2, false);
1018    assertEquals(0, gene1.compareTo(gene2));
1019    assertEquals(0, gene2.compareTo(gene1));
1020    gene1.setApplicationData(new Integer JavaDoc(2));
1021    assertEquals(1, gene1.compareTo(gene2));
1022    assertEquals( -1, gene2.compareTo(gene1));
1023    gene2.setApplicationData(new Integer JavaDoc(3));
1024    assertEquals( -1, gene1.compareTo(gene2));
1025    assertEquals(1, gene2.compareTo(gene1));
1026    newGene1.setApplicationData(new Integer JavaDoc(5));
1027    newGene2.setApplicationData(new Integer JavaDoc(4));
1028    assertEquals( -1, gene1.compareTo(gene2));
1029    assertEquals(1, gene2.compareTo(gene1));
1030    newGene1.setCompareApplicationData(true);
1031    newGene2.setCompareApplicationData(true);
1032    assertEquals(1, gene1.compareTo(gene2));
1033    assertEquals( -1, gene2.compareTo(gene1));
1034  }
1035
1036  /**
1037   * @throws Exception
1038   *
1039   * @author Klaus Meffert
1040   * @since 2.6
1041   */

1042  public void testCompareTo_6()
1043      throws Exception JavaDoc {
1044    Configuration conf = new ConfigurationForTest();
1045    CompositeGene gene1 = new CompositeGene(conf);
1046    Gene newGene1 = new IntegerGene(conf, 3, 5);
1047    gene1.addGene(newGene1, false);
1048    CompositeGene gene2 = new CompositeGene(conf);
1049    assertEquals(1, gene1.compareTo(gene2));
1050    assertEquals( -1, gene2.compareTo(gene1));
1051  }
1052
1053  /**
1054   *
1055   * @throws Exception
1056   *
1057   * @author Klaus Meffert
1058   * @since 2.6
1059   */

1060  public void testApplyMutation_0()
1061      throws Exception JavaDoc {
1062    CompositeGene gene = new CompositeGene(conf);
1063    try {
1064      gene.applyMutation(0, 0.5d);
1065      fail();
1066    }
1067    catch (RuntimeException JavaDoc rex) {
1068      ; //this is OK
1069
}
1070  }
1071
1072  /**
1073   *
1074   * @throws Exception
1075   *
1076   * @author Klaus Meffert
1077   * @since 2.6
1078   */

1079  public void testGetAllele_3()
1080      throws Exception JavaDoc {
1081    CompositeGene gene = new CompositeGene(conf);
1082    assertNull(gene.getInternalValue());
1083  }
1084
1085  /**
1086   * @throws Exception
1087   *
1088   * @author Klaus Meffert
1089   * @since 2.6
1090   */

1091  public void testHashCode_0()
1092      throws Exception JavaDoc {
1093    Configuration conf = new ConfigurationForTest();
1094    CompositeGene c1 = new CompositeGene(conf);
1095    CompositeGene c2 = new CompositeGene(conf);
1096    assertEquals(c1.hashCode(), c2.hashCode());
1097    Gene newGene1 = new IntegerGene(conf, 3, 5);
1098    c1.addGene(newGene1, false);
1099    assertFalse(c1.hashCode() == c2.hashCode());
1100    assertEquals(c1.hashCode(), c1.hashCode());
1101    Gene newGene2 = new IntegerGene(conf, 2, 5);
1102    c2.addGene(newGene2, false);
1103    assertTrue(c1.hashCode() == c2.hashCode());
1104    newGene1.setAllele(new Integer JavaDoc(2));
1105    assertFalse(c1.hashCode() == c2.hashCode());
1106  }
1107
1108  class GeneConstraintChecker
1109      implements IGeneConstraintChecker {
1110    public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
1111                          IChromosome a_chrom, int a_index) {
1112      return true;
1113    }
1114  }
1115}
1116
Popular Tags