KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

37   public void testConstruct_0()
38       throws Exception JavaDoc {
39     StringGene gene = new StringGene(conf, 1, 100);
40     //following should be possible without exception
41
gene.setAllele("ABC");
42     assertEquals(null, gene.getAlphabet());
43     assertEquals(1, gene.getMinLength());
44     assertEquals(100, gene.getMaxLength());
45   }
46
47   /**
48    * @throws Exception
49    *
50    * @author Klaus Meffert
51    */

52   public void testConstruct_1()
53       throws Exception JavaDoc {
54     try {
55       new StringGene(conf, 2, 1);
56       fail();
57     }
58     catch (IllegalArgumentException JavaDoc iex) {
59       ; //this is OK
60
}
61   }
62
63   /**
64    * @throws Exception
65    *
66    * @author Klaus Meffert
67    */

68   public void testConstruct_2()
69       throws Exception JavaDoc {
70     try {
71       new StringGene(conf, -1, 3);
72       fail();
73     }
74     catch (IllegalArgumentException JavaDoc iex) {
75       ; //this is OK
76
}
77   }
78
79   /**
80    * @throws Exception
81    *
82    * @author Klaus Meffert
83    */

84   public void testConstruct_3()
85       throws Exception JavaDoc {
86     try {
87       Gene gene = new StringGene(conf, 1, 3);
88       gene.setAllele("ABCD");
89     }
90     catch (IllegalArgumentException JavaDoc iex) {
91       ; //this is OK
92
}
93   }
94
95   /**
96    * @throws Exception
97    *
98    * @author Klaus Meffert
99    */

100   public void testConstruct_4()
101       throws Exception JavaDoc {
102     try {
103       Gene gene = new StringGene(conf, 1, 3);
104       gene.setAllele(new Double JavaDoc(2.3d));
105     }
106     catch (ClassCastException JavaDoc castex) {
107       ; //this is OK
108
}
109   }
110
111   /**
112    * @throws Exception
113    *
114    * @author Klaus Meffert
115    * @since 3.1
116    */

117   public void testConstruct_5()
118       throws Exception JavaDoc {
119     Genotype.setStaticConfiguration(conf);
120     StringGene gene = new StringGene();
121     assertSame(conf, gene.getConfiguration());
122   }
123
124   /**
125    * @throws Exception
126    *
127    * @author Klaus Meffert
128    */

129   public void testSetAlphabet_1()
130       throws Exception JavaDoc {
131     StringGene gene = new StringGene(conf, 3, 5);
132     final String JavaDoc alphabet_const = "1234";
133     gene.setAlphabet(alphabet_const);
134     String JavaDoc alphabet = (String JavaDoc) privateAccessor.getField(gene, "m_alphabet");
135     assertEquals(alphabet, alphabet_const);
136     assertEquals(alphabet, gene.getAlphabet());
137   }
138
139   /**
140    * @throws Exception
141    *
142    * @author Klaus Meffert
143    */

144   public void testToString_0()
145       throws Exception JavaDoc {
146     Gene gene = new StringGene(conf, 3, 7);
147     gene.setAllele("ABC");
148     assertEquals("StringGene=ABC", gene.toString());
149   }
150
151   /**
152    * @throws Exception
153    *
154    * @author Klaus Meffert
155    * @since 2.4
156    */

157   public void testToString_1()
158       throws Exception JavaDoc {
159     StringGene gene = new StringGene(conf, 3, 7);
160     assertEquals("StringGene=null", gene.toString());
161   }
162
163   /**
164    * @throws Exception
165    *
166    * @author Klaus Meffert
167    * @since 2.4
168    */

169   public void testToString_2()
170       throws Exception JavaDoc {
171     StringGene gene = new StringGene(conf, 0, 7);
172     gene.setAllele("");
173     assertEquals("StringGene=\"\"", gene.toString());
174   }
175
176   /**
177    * @throws Exception
178    *
179    * @author Klaus Meffert
180    */

181   public void testGetAllele_0()
182       throws Exception JavaDoc {
183     Gene gene = new StringGene(conf, 3, 5);
184     gene.setAllele("BCD");
185     assertEquals("BCD", gene.getAllele());
186   }
187
188   /**
189    * @throws Exception
190    *
191    * @author Klaus Meffert
192    */

193   public void testEquals_0()
194       throws Exception JavaDoc {
195     Gene gene1 = new StringGene(conf, 1, 100);
196     Gene gene2 = new StringGene(conf, 1, 100);
197     assertTrue(gene1.equals(gene2));
198     assertTrue(gene2.equals(gene1));
199   }
200
201   /**
202    * @throws Exception
203    *
204    * @author Klaus Meffert
205    */

206   public void testEquals_1()
207       throws Exception JavaDoc {
208     Gene gene1 = new StringGene(conf, 3, 100);
209     assertFalse(gene1.equals(null));
210   }
211
212   /**
213    * @throws Exception
214    *
215    * @author Klaus Meffert
216    */

217   public void testEquals_2()
218       throws Exception JavaDoc {
219     Gene gene1 = new StringGene(conf, 11, 77);
220     assertTrue(gene1.equals(new StringGene(conf)));
221   }
222
223   /**
224    * @throws Exception
225    *
226    * @author Klaus Meffert
227    */

228   public void testEquals_3()
229       throws Exception JavaDoc {
230     Gene gene1 = new StringGene(conf, 11, 17);
231     assertFalse(gene1.equals(new Vector()));
232   }
233
234   /**
235    * @throws Exception
236    *
237    * @author Klaus Meffert
238    */

239   public void testEquals_4()
240       throws Exception JavaDoc {
241     Gene gene1 = new StringGene(conf, 12, 100);
242     Gene gene2 = new StringGene(conf, 12, 99);
243     assertTrue(gene1.equals(gene2));
244     assertTrue(gene2.equals(gene1));
245   }
246
247   /**
248    * @throws Exception
249    *
250    * @author Klaus Meffert
251    */

252   public void testEquals_5()
253       throws Exception JavaDoc {
254     Gene gene1 = new StringGene(conf, 2, 5);
255     Gene gene2 = new StringGene(conf, 1, 5);
256     gene1.setAllele("ABC");
257     gene2.setAllele("AB");
258     assertFalse(gene1.equals(gene2));
259     assertFalse(gene2.equals(gene1));
260   }
261
262   /**
263    * @throws Exception
264    *
265    * @author Klaus Meffert
266    */

267   public void testEquals_6()
268       throws Exception JavaDoc {
269     Gene gene1 = new StringGene(conf, 2, 5);
270     Gene gene2 = new DoubleGene(conf, 1, 5);
271     assertFalse(gene1.equals(gene2));
272     assertFalse(gene2.equals(gene1));
273   }
274
275   /**
276    * @throws Exception
277    *
278    * @author Klaus Meffert
279    */

280   public void testEquals_7()
281       throws Exception JavaDoc {
282     Gene gene1 = new StringGene(conf, 2, 5);
283     Gene gene2 = new BooleanGene(conf);
284     assertFalse(gene1.equals(gene2));
285     assertFalse(gene2.equals(gene1));
286   }
287
288   /**
289    * @throws Exception
290    *
291    * @author Klaus Meffert
292    * @since 2.4
293    */

294   public void testEquals_8()
295       throws Exception JavaDoc {
296     Gene gene1 = new StringGene(conf, 2, 6);
297     gene1.setAllele("hallo");
298     Gene gene2 = new StringGene(conf, 2, 6);
299     gene2.setAllele("hello");
300     assertFalse(gene1.equals(gene2));
301     assertFalse(gene2.equals(gene1));
302     gene1.setAllele("hello1");
303     assertFalse(gene1.equals(gene2));
304     assertFalse(gene2.equals(gene1));
305     gene2.setAllele("HELLO1");
306     assertFalse(gene1.equals(gene2));
307     assertFalse(gene2.equals(gene1));
308   }
309
310   /**
311    * Using application data.
312    * @throws Exception
313    *
314    *
315    * @author Klaus Meffert
316    * @since 2.4
317    */

318   public void testEquals_9()
319       throws Exception JavaDoc {
320     Gene gene1 = new StringGene(conf, 2, 6);
321     gene1.setAllele("hallo");
322     gene1.setApplicationData(new Double JavaDoc(2.3d));
323     Gene gene2 = new StringGene(conf, 2, 6);
324     gene2.setAllele("hallo");
325     assertTrue(gene1.equals(gene2));
326     assertTrue(gene2.equals(gene1));
327     gene1.setCompareApplicationData(true);
328     assertFalse(gene1.equals(gene2));
329     assertTrue(gene2.equals(gene1));
330     gene2.setCompareApplicationData(true);
331     assertFalse(gene1.equals(gene2));
332     assertFalse(gene2.equals(gene1));
333     gene2.setApplicationData(new Double JavaDoc(2.3d));
334     assertTrue(gene1.equals(gene2));
335     assertTrue(gene2.equals(gene1));
336   }
337
338   /**
339    * Comparation using application data.
340    * @throws Exception
341    *
342    *
343    * @author Klaus Meffert
344    * @since 2.4
345    */

346   public void testCompareTo_0()
347       throws Exception JavaDoc {
348     Gene gene1 = new StringGene(conf, 2, 6);
349     gene1.setAllele("hallo");
350     gene1.setApplicationData(new Double JavaDoc(2.3d));
351     Gene gene2 = new StringGene(conf, 2, 6);
352     gene2.setAllele("hallo");
353     assertEquals(0, gene1.compareTo(gene2));
354     assertEquals(0, gene2.compareTo(gene1));
355     gene1.setCompareApplicationData(true);
356     assertEquals(1, gene1.compareTo(gene2));
357     assertEquals(0, gene2.compareTo(gene1));
358     gene2.setCompareApplicationData(true);
359     assertEquals(1, gene1.compareTo(gene2));
360     assertEquals( -1, gene2.compareTo(gene1));
361     gene2.setApplicationData(new Double JavaDoc(2.3d));
362     assertEquals(0, gene1.compareTo(gene2));
363     assertEquals(0, gene2.compareTo(gene1));
364     gene2.setAllele(null);
365     assertEquals(1, gene1.compareTo(gene2));
366     gene2.setApplicationData(null);
367     assertEquals(1, gene1.compareTo(gene2));
368   }
369
370   /**
371    * class cast exception.
372    * @throws Exception
373    *
374    *
375    * @author Klaus Meffert
376    * @since 2.6
377    */

378   public void testCompareTo_1()
379       throws Exception JavaDoc {
380     Gene gene1 = new StringGene(conf, 2, 6);
381     try {
382       gene1.compareTo(new Chromosome(conf));
383       fail();
384     }
385     catch (ClassCastException JavaDoc cex) {
386       ; // this is OK
387
}
388   }
389
390   /**
391    * Comparation using application data and null allele.
392    * @throws Exception
393    *
394    *
395    * @author Klaus Meffert
396    * @since 2.6
397    */

398   public void testCompareTo_2()
399       throws Exception JavaDoc {
400     Gene gene1 = new StringGene(conf, 2, 6);
401     gene1.setApplicationData(new Double JavaDoc(2.3d));
402     Gene gene2 = new StringGene(conf, 2, 6);
403     assertEquals(0, gene1.compareTo(gene2));
404     assertEquals(0, gene2.compareTo(gene1));
405     gene1.setCompareApplicationData(true);
406     assertEquals(1, gene1.compareTo(gene2));
407     assertEquals(0, gene2.compareTo(gene1));
408     gene2.setCompareApplicationData(true);
409     assertEquals(1, gene1.compareTo(gene2));
410     assertEquals( -1, gene2.compareTo(gene1));
411     gene2.setApplicationData(new Double JavaDoc(2.3d));
412     assertEquals(0, gene1.compareTo(gene2));
413     assertEquals(0, gene2.compareTo(gene1));
414     gene2.setApplicationData(null);
415     assertEquals(1, gene1.compareTo(gene2));
416   }
417
418   /**
419    * Set Allele to null, no exception should occur.
420    * @throws Exception
421    *
422    * @author Klaus Meffert
423    */

424   public void testSetAllele_0()
425       throws Exception JavaDoc {
426     Gene gene1 = new StringGene(conf, 0, 10000);
427     gene1.setAllele(null);
428   }
429
430   /**
431    * Allele too short.
432    * @throws Exception
433    *
434    * @author Klaus Meffert
435    */

436   public void testSetAllele_1()
437       throws Exception JavaDoc {
438     Gene gene1 = new StringGene(conf, 3, 4);
439     try {
440       gene1.setAllele("AB");
441       fail();
442     }
443     catch (IllegalArgumentException JavaDoc iex) {
444       ; //this is OK
445
}
446   }
447
448   /**
449    * Allele consists of illegal characters.
450    * @throws Exception
451    *
452    * @author Klaus Meffert
453    */

454   public void testSetAllele_2()
455       throws Exception JavaDoc {
456     Gene gene1 = new StringGene(conf, 3, 4, "ABCDEFHI");
457     try {
458       gene1.setAllele("ABDG");
459       fail();
460     }
461     catch (IllegalArgumentException JavaDoc iex) {
462       ; //this is OK
463
}
464   }
465
466   /**
467    * @throws Exception
468    *
469    * @author Klaus Meffert
470    */

471   public void testSetAllele_3()
472       throws Exception JavaDoc {
473     Gene gene1 = new StringGene(conf, 3, 4, "EGAL");
474     try {
475       // Length of allele to short.
476
// --------------------------
477
gene1.setAllele("");
478       fail();
479     }
480     catch (IllegalArgumentException JavaDoc iex) {
481       ; //this is OK
482
}
483   }
484
485   /**
486    * @throws Exception
487    *
488    * @author Klaus Meffert
489    */

490   public void testSetAllele_4()
491       throws Exception JavaDoc {
492     Gene gene1 = new StringGene(conf, 0, 4, "");
493     //following should be possible
494
gene1.setAllele("");
495   }
496
497   /**
498    * @throws Exception
499    *
500    * @author Klaus Meffert
501    * @since 2.2
502    */

503   public void testSetAllele_5()
504       throws Exception JavaDoc {
505     StringGene gene1 = new StringGene(conf, 0, 4, "ABC");
506     //following should be possible
507
gene1.setAllele("A");
508     gene1.setConstraintChecker(new IGeneConstraintChecker() {
509       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
510                             IChromosome a_chrom, int a_index) {
511         return false;
512       }
513     });
514     gene1.setAllele("B");
515     assertEquals("A", gene1.stringValue());
516   }
517
518   /**
519    * @throws Exception
520    *
521    * @author Klaus Meffert
522    * @since 2.2
523    */

524   public void testSetAllele_6()
525       throws Exception JavaDoc {
526     StringGene gene1 = new StringGene(conf, 0, 4, "ABC");
527     //following should be possible
528
gene1.setAllele("A");
529     gene1.setConstraintChecker(new IGeneConstraintChecker() {
530       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
531                             IChromosome a_chrom, int a_index) {
532         return true;
533       }
534     });
535     gene1.setAllele("B");
536     assertEquals("B", gene1.stringValue());
537   }
538
539   /**
540    *
541    * @throws Exception
542    *
543    * @author Klaus Meffert
544    */

545   public void testNewGene_0()
546       throws Exception JavaDoc {
547     StringGene gene1 = new StringGene(conf, 1, 4);
548     IGeneConstraintChecker checker = new GeneConstraintChecker();
549     gene1.setConstraintChecker(checker);
550     gene1.setAllele("XYZ");
551     int minLength1 = gene1.getMinLength();
552     int maxLength1 = gene1.getMaxLength();
553     StringGene gene2 = (StringGene) gene1.newGene();
554     int minLength2 = gene2.getMinLength();
555     int maxLength2 = gene2.getMaxLength();
556     assertEquals(minLength1, minLength2);
557     assertEquals(maxLength1, maxLength2);
558     assertEquals(checker, gene2.getConstraintChecker());
559   }
560
561   /**
562    *
563    * @throws Exception
564    *
565    * @author Klaus Meffert
566    */

567   public void testPersistentRepresentation_0()
568       throws Exception JavaDoc {
569     Gene gene1 = new StringGene(conf, 2, 10, "ABCDE");
570     gene1.setAllele("BABE");
571     String JavaDoc pres1 = gene1.getPersistentRepresentation();
572     Gene gene2 = new StringGene(conf);
573     gene2.setValueFromPersistentRepresentation(pres1);
574     String JavaDoc pres2 = gene2.getPersistentRepresentation();
575     assertEquals(pres1, pres2);
576   }
577
578   /**
579    * @throws Exception
580    *
581    * @author Klaus Meffert
582    */

583   public void testPersistentRepresentation_1()
584       throws Exception JavaDoc {
585     Gene gene1 = new StringGene(conf, 2, 10);
586     try {
587       gene1.setAllele("");
588       fail();
589     }
590     catch (IllegalArgumentException JavaDoc iex) {
591       ; //this is OK
592
}
593   }
594
595   /**
596    *
597    * @throws Exception
598    *
599    * @author Klaus Meffert
600    */

601   public void testPersistentRepresentation_2()
602       throws Exception JavaDoc {
603     Gene gene1 = new StringGene(conf, 0, 10);
604     gene1.setAllele("");
605     String JavaDoc pres1 = gene1.getPersistentRepresentation();
606     Gene gene2 = new StringGene(conf);
607     gene2.setValueFromPersistentRepresentation(pres1);
608     String JavaDoc pres2 = gene2.getPersistentRepresentation();
609     assertEquals(pres1, pres2);
610   }
611
612   /**
613    *
614    * @throws Exception
615    *
616    * @author Klaus Meffert
617    */

618   public void testPersistentRepresentation_3()
619       throws Exception JavaDoc {
620     Gene gene1 = new StringGene(conf);
621     gene1.setAllele("");
622     String JavaDoc pres1 = gene1.getPersistentRepresentation();
623     Gene gene2 = new StringGene(conf);
624     gene2.setValueFromPersistentRepresentation(pres1);
625     String JavaDoc pres2 = gene2.getPersistentRepresentation();
626     assertEquals(pres1, pres2);
627   }
628
629   /**
630    *
631    * @throws Exception
632    *
633    * @author Klaus Meffert
634    */

635   public void testPersistentRepresentation_4()
636       throws Exception JavaDoc {
637     Gene gene1 = new StringGene(conf);
638     gene1.setAllele(null);
639     String JavaDoc pres1 = gene1.getPersistentRepresentation();
640     Gene gene2 = new StringGene(conf);
641     gene2.setValueFromPersistentRepresentation(pres1);
642     String JavaDoc pres2 = gene2.getPersistentRepresentation();
643     assertEquals(pres1, pres2);
644   }
645
646   /**
647    *
648    * @throws Exception
649    *
650    * @author Klaus Meffert
651    */

652   public void testPersistentRepresentation_5()
653       throws Exception JavaDoc {
654     StringGene gene1 = new StringGene(conf, 2, 10,
655                                       "ABCDE" + CompositeGene.GENE_DELIMITER);
656     gene1.setAllele("BABE");
657     String JavaDoc pres1 = gene1.getPersistentRepresentation();
658     StringGene gene2 = new StringGene(conf);
659     gene2.setValueFromPersistentRepresentation(pres1);
660     String JavaDoc pres2 = gene2.getPersistentRepresentation();
661     assertEquals(pres1, pres2);
662     assertEquals(gene1, gene2);
663     assertEquals(gene1.getAlphabet(), gene2.getAlphabet());
664   }
665
666   /**
667    *
668    * @throws Exception
669    *
670    * @author Klaus Meffert
671    */

672   public void testPersistentRepresentation_6()
673       throws Exception JavaDoc {
674     Gene gene1 = new StringGene(conf, 2, 10, "ABCDE");
675     gene1.setAllele("BABE");
676     gene1.setValueFromPersistentRepresentation(null);
677     assertEquals("BABE", gene1.getAllele());
678   }
679
680   /**
681    *
682    * @throws Exception
683    *
684    * @author Klaus Meffert
685    */

686   public void testPersistentRepresentation_7()
687       throws Exception JavaDoc {
688     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
689     gene1.setAllele(null);
690     assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
691   }
692
693   /**
694    * @throws Exception
695    *
696    * @author Klaus Meffert
697    * @since 2.2
698    */

699   public void testPersistentRepresentation_8()
700       throws Exception JavaDoc {
701     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
702     gene1.setAllele(null);
703     try {
704       gene1.setValueFromPersistentRepresentation("null:2:ABCDE");
705       fail();
706     }
707     catch (UnsupportedRepresentationException uex) {
708       ; //this is OK
709
}
710   }
711
712   /**
713    * @throws Exception
714    *
715    * @author Klaus Meffert
716    * @since 2.2
717    */

718   public void testPersistentRepresentation_9()
719       throws Exception JavaDoc {
720     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
721     assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
722   }
723
724   /**
725    * Invalid number in second argument.
726    * @throws Exception
727    *
728    * @author Klaus Meffert
729    * @since 2.6
730    */

731   public void testPersistentRepresentation_10()
732       throws Exception JavaDoc {
733     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
734     gene1.setAllele(null);
735     try {
736       gene1.setValueFromPersistentRepresentation("null:a:10:ABCDE");
737       fail();
738     }
739     catch (UnsupportedRepresentationException uex) {
740       ; //this is OK
741
}
742   }
743
744   /**
745    * Invalid number in third argument.
746    * @throws Exception
747    *
748    * @author Klaus Meffert
749    * @since 2.6
750    */

751   public void testPersistentRepresentation_11()
752       throws Exception JavaDoc {
753     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
754     gene1.setAllele(null);
755     try {
756       gene1.setValueFromPersistentRepresentation("null:2:3b:ABCDE");
757       fail();
758     }
759     catch (UnsupportedRepresentationException uex) {
760       ; //this is OK
761
}
762   }
763
764   /**
765    * Minlen to great
766    * @throws Exception
767    * @author Klaus Meffert
768    * @since 2.6
769    */

770   public void testPersistentRepresentation_12()
771       throws Exception JavaDoc {
772     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
773     gene1.setAllele(null);
774     try {
775       gene1.setValueFromPersistentRepresentation("nada:7:6:ABCDE");
776       fail();
777     }
778     catch (UnsupportedRepresentationException uex) {
779       ; //this is OK
780
}
781   }
782
783   /**
784    * Maxlen to small
785    * @throws Exception
786    * @author Klaus Meffert
787    * @since 2.6
788    */

789   public void testPersistentRepresentation_14()
790       throws Exception JavaDoc {
791     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
792     gene1.setAllele(null);
793     try {
794       gene1.setValueFromPersistentRepresentation("nada:1:3:ABCDE");
795       fail();
796     }
797     catch (UnsupportedRepresentationException uex) {
798       ; //this is OK
799
}
800   }
801
802   /**
803    * Illegal character.
804    * @throws Exception
805    *
806    * @author Klaus Meffert
807    * @since 2.6
808    */

809   public void testPersistentRepresentation_15()
810       throws Exception JavaDoc {
811     StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
812     gene1.setAllele(null);
813     try {
814       gene1.setValueFromPersistentRepresentation("ABHJ:4:7:ABCDE");
815       fail();
816     }
817     catch (UnsupportedRepresentationException uex) {
818       ; //this is OK
819
}
820   }
821
822   /**
823    * Following should be possible without exception.
824    *
825    * @throws Exception
826    *
827    * @author Klaus Meffert
828    */

829   public void testApplyMutation_0()
830       throws Exception JavaDoc {
831     Configuration conf = new ConfigurationForTest();
832     Gene gene1 = new StringGene(conf, 5, 5);
833     gene1.setAllele("12345");
834     gene1.applyMutation(0, 0.99d);
835     gene1.applyMutation(4, -0.99d);
836   }
837
838   /**
839    * Following should be possible without exception.
840    *
841    * @throws Exception
842    *
843    * @author Klaus Meffert
844    */

845   public void testApplyMutation_1()
846       throws Exception JavaDoc {
847     Configuration conf = new ConfigurationForTest();
848     Gene gene1 = new StringGene(conf, 1, 1);
849     gene1.setAllele("1");
850     gene1.applyMutation(0, 0.99d);
851   }
852
853   /**
854    * Invalid index specified.
855    *
856    * @throws Exception
857    *
858    * @author Klaus Meffert
859    */

860   public void testApplyMutation_2()
861       throws Exception JavaDoc {
862     Configuration conf = new ConfigurationForTest();
863     Gene gene1 = new StringGene(conf, 1, 1);
864     gene1.setAllele("1");
865     try {
866       gene1.applyMutation(1, 0.99d);
867       fail();
868     }
869     catch (StringIndexOutOfBoundsException JavaDoc sex) {
870       ; //this is OK
871
}
872   }
873
874   /**
875    * No allele set.
876    *
877    * @throws Exception
878    *
879    * @author Klaus Meffert
880    */

881   public void testApplyMutation_3()
882       throws Exception JavaDoc {
883     Configuration conf = new ConfigurationForTest();
884     Gene gene1 = new StringGene(conf, 1, 1);
885     gene1.applyMutation(0, 0.99d);
886   }
887
888   /**
889    * @throws Exception
890    * @author Klaus Meffert
891    */

892   public void testApplyMutation_4()
893       throws Exception JavaDoc {
894     Configuration conf = new ConfigurationForTest();
895     Gene gene1 = new StringGene(conf, 6, 6,
896                                 StringGene.ALPHABET_CHARACTERS_LOWER);
897     gene1.setAllele("ijklmn");
898     gene1.applyMutation(0, 0.3d);
899     assertFalse(gene1.getAllele().equals("ijklmn"));
900     gene1.setAllele("ijklmn");
901     gene1.applyMutation(4, -0.3d);
902     assertFalse(gene1.getAllele().equals("ijklmn"));
903   }
904
905   /**
906    * Mutation 0.0 should not change anything.
907    *
908    * @throws Exception
909    * @author Klaus Meffert
910    */

911   public void testApplyMutation_5()
912       throws Exception JavaDoc {
913     Configuration conf = new ConfigurationForTest();
914     Gene gene1 = new StringGene(conf, 6, 6,
915                                 StringGene.ALPHABET_CHARACTERS_LOWER);
916     gene1.setAllele("ijklmn");
917     gene1.applyMutation(0, 0.0d);
918     assertEquals(gene1.getAllele(), "ijklmn");
919   }
920
921   /**
922    * applyMutation with empty alphabet.
923    *
924    * @throws Exception
925    *
926    * @author Klaus Meffert
927    */

928   public void testApplyMutation_6()
929       throws Exception JavaDoc {
930     Configuration conf = new ConfigurationForTest();
931     StringGene gene1 = new StringGene(conf, 6, 6);
932     gene1.setAllele("ijklmn");
933     gene1.setAlphabet("");
934     try {
935       gene1.applyMutation(0, 0.0d);
936     }
937     catch (IllegalArgumentException JavaDoc iex) {
938       ; //this is OK
939
}
940   }
941
942   /**
943    * null random generator should lead to NullPointerException.
944    *
945    * @throws Exception
946    *
947    * @author Klaus Meffert
948    */

949   public void testApplyMutation_7()
950       throws Exception JavaDoc {
951     Configuration conf = new ConfigurationForTest();
952     conf.setRandomGenerator(null);
953     Gene gene1 = new StringGene(conf, 5, 5);
954     gene1.setAllele("12345");
955     try {
956       gene1.applyMutation(0, 0.99d);
957       fail();
958     }
959     catch (NullPointerException JavaDoc nex) {
960       ; //this is OK
961
}
962   }
963
964   /**
965    * @throws Exception
966    *
967    * @author Klaus Meffert
968    */

969   public void testSetMinMaxLength_0()
970       throws Exception JavaDoc {
971     StringGene gene = new StringGene(conf);
972     gene.setMinLength(4);
973     gene.setMaxLength(3);
974     assertEquals(4, gene.getMinLength());
975     assertEquals(3, gene.getMaxLength());
976   }
977
978   /**
979    * @throws Exception
980    *
981    * @author Klaus Meffert
982    */

983   public void testSetToRandomValue_0()
984       throws Exception JavaDoc {
985     StringGene gene = new StringGene(conf, 1, 6,
986                                      StringGene.ALPHABET_CHARACTERS_UPPER);
987     gene.setToRandomValue(new RandomGeneratorForTest(2));
988     assertEquals("CCC", gene.getAllele());
989     gene.setToRandomValue(new RandomGeneratorForTest(1));
990     assertEquals("BB", gene.getAllele());
991     gene.setToRandomValue(new RandomGeneratorForTest(0));
992     assertEquals("A", gene.getAllele());
993   }
994
995   /**
996    * @throws Exception
997    *
998    * @author Klaus Meffert
999    */

1000  public void testSetToRandomValue_1()
1001      throws Exception JavaDoc {
1002    Gene gene = new StringGene(conf, 1, 6, StringGene.ALPHABET_CHARACTERS_UPPER);
1003    gene.setAllele("XYZA"); // should not matter here
1004
gene.setToRandomValue(new RandomGeneratorForTest(3));
1005    assertEquals("DDDD", gene.getAllele());
1006  }
1007
1008  public void testSetToRandomValue_2()
1009      throws Exception JavaDoc {
1010    Gene gene = new StringGene(conf, 1, 6, "ABC");
1011    gene.setToRandomValue(new RandomGeneratorForTest(3));
1012    assertEquals("AAAA", gene.getAllele());
1013  }
1014
1015  /**
1016   *
1017   * @throws Exception
1018   *
1019   * @author Klaus Meffert
1020   */

1021  public void testSetToRandomValue_3()
1022      throws Exception JavaDoc {
1023    StringGene gene = new StringGene(conf, 1, 7, "DEF");
1024    gene.setToRandomValue(new RandomGeneratorForTest(2));
1025    assertEquals("FFF", gene.getAllele());
1026  }
1027
1028  /**
1029   *
1030   * @throws Exception
1031   *
1032   * @author Klaus Meffert
1033   */

1034  public void testSetToRandomValue_4()
1035      throws Exception JavaDoc {
1036    StringGene gene = new StringGene(conf, 1, 7, "DEF");
1037    gene.setAllele("EEFD");
1038    RandomGeneratorForTest rn = new RandomGeneratorForTest();
1039    // set random generator to produce
1040
// 1) length of new allele (-1)
1041
// 2) first character out of alphabet ("DEF"), starting from 0
1042
// 3) second character out of alphabet
1043
// 4) third character out of alphabet
1044
// 5) fourth character out of alphabet
1045
rn.setNextIntSequence(new int[] {3, 2, 1, 0, 2});
1046    gene.setToRandomValue(rn);
1047    assertEquals("FEDF", gene.getAllele());
1048  }
1049
1050  /**
1051   * @throws Exception
1052   *
1053   * @author Klaus Meffert
1054   */

1055  public void testSetToRandomValue_5()
1056      throws Exception JavaDoc {
1057    StringGene gene = new StringGene(conf, 1, 8,
1058                                     StringGene.ALPHABET_CHARACTERS_LOWER);
1059    gene.setToRandomValue(new StockRandomGenerator());
1060    for (int i = 0; i < gene.size(); i++) {
1061      if ( ( (String JavaDoc) gene.getAllele()).charAt(i) < 'a'
1062          || ( (String JavaDoc) gene.getAllele()).charAt(i) > 'z') {
1063        fail();
1064      }
1065    }
1066  }
1067
1068  /**
1069   * @throws Exception
1070   *
1071   * @author Klaus Meffert
1072   */

1073  public void testSetToRandomValue_6()
1074      throws Exception JavaDoc {
1075    Gene gene = new StringGene(conf, 1, 6, "");
1076    try {
1077      gene.setToRandomValue(new StockRandomGenerator());
1078      fail();
1079    }
1080    catch (IllegalStateException JavaDoc iex) {
1081      ; //this is OK
1082
}
1083  }
1084
1085  /**
1086   * @throws Exception
1087   *
1088   * @author Klaus Meffert
1089   */

1090  public void testSetToRandomValue_7()
1091      throws Exception JavaDoc {
1092    Gene gene = new StringGene(conf, 1, 6, null);
1093    try {
1094      gene.setToRandomValue(new StockRandomGenerator());
1095      fail();
1096    }
1097    catch (IllegalStateException JavaDoc iex) {
1098      ; //this is OK
1099
}
1100  }
1101
1102  /**
1103   * @throws Exception
1104   *
1105   * @author Klaus Meffert
1106   * @since 2.2
1107   */

1108  public void testSetToRandomValue_8()
1109      throws Exception JavaDoc {
1110    StringGene gene = new StringGene(conf, 2, 6, "ABC");
1111    try {
1112      gene.setMaxLength(1);
1113      gene.setToRandomValue(new StockRandomGenerator());
1114      fail();
1115    }
1116    catch (IllegalStateException JavaDoc iex) {
1117      ; //this is OK
1118
}
1119  }
1120
1121  /**
1122   * @throws Exception
1123   *
1124   * @author Klaus Meffert
1125   */

1126  public void testSetConstraintChecker_0()
1127      throws Exception JavaDoc {
1128    StringGene gene = new StringGene(conf, 1, 6, "ABC");
1129    assertNull(gene.getConstraintChecker());
1130    gene.setConstraintChecker(new IGeneConstraintChecker() {
1131      public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
1132                            IChromosome a_chrom, int a_index) {
1133        return false;
1134      }
1135    });
1136    assertNotNull(gene.getConstraintChecker());
1137  }
1138
1139  /**
1140   * @throws Exception
1141   *
1142   * @author Klaus Meffert
1143   */

1144  public void testIsValidAlphabet_0()
1145      throws Exception JavaDoc {
1146    StringGene gene = new StringGene(conf, 1, 6, "");
1147    try {
1148      gene.setAllele("HALLO");
1149      fail();
1150    }
1151    catch (IllegalArgumentException JavaDoc ilex) {
1152      ; //this is OK
1153
}
1154  }
1155
1156  /**
1157   * @throws Exception
1158   *
1159   * @author Klaus Meffert
1160   * @since 2.4
1161   */

1162  public void testSetEnergy_0()
1163      throws Exception JavaDoc {
1164    BaseGene gene = new IntegerGene(conf);
1165    assertEquals(0.0, gene.getEnergy(), DELTA);
1166  }
1167
1168  /**
1169   * @throws Exception
1170   *
1171   * @author Klaus Meffert
1172   * @since 2.4
1173   */

1174  public void testSetEnergy_1()
1175      throws Exception JavaDoc {
1176    BaseGene gene = new IntegerGene(conf);
1177    gene.setEnergy(2.3);
1178    assertEquals(2.3, gene.getEnergy(), DELTA);
1179    gene.setEnergy( -55.8);
1180    assertEquals( -55.8, gene.getEnergy(), DELTA);
1181    gene.setEnergy(0.5);
1182    gene.setEnergy(0.8);
1183    assertEquals(0.8, gene.getEnergy(), DELTA);
1184  }
1185
1186  class GeneConstraintChecker
1187      implements IGeneConstraintChecker {
1188    public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
1189                          IChromosome a_chrom,
1190                          int a_index) {
1191      return true;
1192    }
1193  }
1194}
1195
Popular Tags