KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

38   public void testConstruct_0()
39       throws Exception JavaDoc {
40     // following should be possible without exception
41
new FixedBinaryGene(conf, 1);
42     new FixedBinaryGene(conf, 10);
43     new FixedBinaryGene(conf, 1000);
44     new FixedBinaryGene(conf, 100000);
45   }
46
47   /**
48    *
49    * @author Klaus Meffert
50    * @throws Exception
51    */

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

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

84   public void testConstruct_3()
85       throws Exception JavaDoc {
86     int i = 0;
87     FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
88     for (i = 0; i < 4; i++) {
89       //assert that we have
90
assertFalse(gene.getBit(i));
91     }
92     assertEquals("FixedBinaryGene[0,0,0,0,0]", gene.toString());
93   }
94
95   /**
96    *
97    * @author vamsi
98    * @throws Exception
99    */

100   public void testConstruct_4()
101       throws Exception JavaDoc {
102     FixedBinaryGene gene = new FixedBinaryGene(conf, 6);
103     assertEquals(1, gene.size());
104     assertEquals(1, (gene.getValue()).length);
105     assertEquals("FixedBinaryGene[0,0,0,0,0,0]", gene.toString());
106   }
107
108   /**
109    * Buffer allocation test case.
110    *
111    * @author vamsi
112    * @throws Exception
113    */

114   public void testConstruct_5()
115       throws Exception JavaDoc {
116     FixedBinaryGene gene;
117     gene = new FixedBinaryGene(conf, 32);
118     assertEquals(1, gene.size());
119     gene = new FixedBinaryGene(conf, 81);
120     assertEquals(3, gene.size());
121   }
122
123   /**
124    *
125    * @author Klaus Meffert
126    * @since 2.2
127    * @throws Exception
128    */

129   public void testConstruct_6()
130       throws Exception JavaDoc {
131     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1);
132     new FixedBinaryGene(conf, gene1);
133   }
134
135   /**
136    *
137    * @author vamsi
138    * @throws Exception
139    */

140   public void testToString_0()
141       throws Exception JavaDoc {
142     Gene gene = new FixedBinaryGene(conf, 1);
143     gene.setAllele(new int[] {1});
144     assertEquals("FixedBinaryGene[1]", gene.toString());
145   }
146
147   /**
148    *
149    * @author vamsi
150    * @throws Exception
151    */

152   public void testToString_1()
153       throws Exception JavaDoc {
154     Gene gene = new FixedBinaryGene(conf, 3);
155     gene.setAllele(new int[] {1, 0, 1});
156     assertEquals("FixedBinaryGene[1,0,1]", gene.toString());
157   }
158
159   /**
160    *
161    * @author Klaus Meffert
162    * @since 2.2
163    * @throws Exception
164    */

165   public void testToString_2()
166       throws Exception JavaDoc {
167     Gene gene = new FixedBinaryGene(conf, 3);
168     assertEquals("FixedBinaryGene[0,0,0]", gene.toString());
169   }
170
171   /**
172    *
173    * @author vamsi
174    * @throws Exception
175    */

176   public void testGetAllele_0()
177       throws Exception JavaDoc {
178     Gene gene = new FixedBinaryGene(conf, 1);
179     int[] value = new int[] {
180         0};
181     gene.setAllele(value);
182     assertEquals(value.length, ( (int[]) gene.getAllele()).length);
183     for (int i = 0; i < value.length; i++) {
184       assertEquals(value[i], ( (int[]) gene.getAllele())[i]);
185     }
186   }
187
188   /**
189    *
190    * @author vamsi
191    * @throws Exception
192    */

193   public void testGetAllele_1()
194       throws Exception JavaDoc {
195     Gene gene = new FixedBinaryGene(conf, 2);
196     try {
197       gene.setAllele(new Integer JavaDoc(100));
198       fail();
199     }
200     catch (ClassCastException JavaDoc classex) {
201       ; //this is OK
202
}
203   }
204
205   /**
206    *
207    * @author Klaus Meffert
208    * @since 2.2
209    * @throws Exception
210    */

211   public void testGetAllele_2()
212       throws Exception JavaDoc {
213     Gene gene = new FixedBinaryGene(conf, 1);
214     int[] value = new int[] {
215         1};
216     gene.setAllele(value);
217     assertEquals(value.length, ( (int[]) gene.getAllele()).length);
218     for (int i = 0; i < value.length; i++) {
219       assertEquals(value[i], ( (int[]) gene.getAllele())[i]);
220     }
221   }
222
223   /**
224    *
225    * @author vamsi
226    * @throws Exception
227    */

228   public void testEquals_0()
229       throws Exception JavaDoc {
230     Gene gene1 = new FixedBinaryGene(conf, 1);
231     Gene gene2 = new FixedBinaryGene(conf, 1);
232     assertTrue(gene1.equals(gene2));
233   }
234
235   /**
236    *
237    * @author vamsi
238    * @throws Exception
239    */

240   public void testEquals_1()
241       throws Exception JavaDoc {
242     Gene gene1 = new FixedBinaryGene(conf, 1);
243     assertFalse(gene1.equals(null));
244   }
245
246   /**
247    *
248    * @author vamsi
249    * @throws Exception
250    */

251   public void testEquals_2()
252       throws Exception JavaDoc {
253     Gene gene1 = new FixedBinaryGene(conf, 2);
254     gene1.setAllele(new int[] {1, 0});
255     Gene gene2 = new FixedBinaryGene(conf, 2);
256     gene2.setAllele(new int[] {0, 1});
257     assertFalse(gene1.equals(gene2));
258     assertFalse(gene2.equals(gene1));
259   }
260
261   /**
262    *
263    * @author vamsi
264    * @throws Exception
265    */

266   public void testEquals_3()
267       throws Exception JavaDoc {
268     Gene gene1 = new FixedBinaryGene(conf, 5);
269     assertFalse(gene1.equals(new IntegerGene(conf)));
270   }
271
272   public void testEquals_4()
273       throws Exception JavaDoc {
274     Gene gene1 = new FixedBinaryGene(conf, 1);
275     Gene gene2 = new IntegerGene(conf);
276     assertFalse(gene1.equals(gene2));
277     assertFalse(gene2.equals(gene1));
278   }
279
280   public void testEquals_5()
281       throws Exception JavaDoc {
282     Gene gene1 = new FixedBinaryGene(conf, 1);
283     Gene gene2 = new DoubleGene(conf);
284     assertFalse(gene1.equals(gene2));
285     assertFalse(gene2.equals(gene1));
286   }
287
288   public void testEquals_6()
289       throws Exception JavaDoc {
290     Gene gene1 = new FixedBinaryGene(conf, 1);
291     Gene gene2 = new BooleanGene(conf);
292     assertFalse(gene1.equals(gene2));
293     assertFalse(gene2.equals(gene1));
294   }
295
296   public void testEquals_7()
297       throws Exception JavaDoc {
298     Gene gene1 = new FixedBinaryGene(conf, 1);
299     Gene gene2 = new StringGene(conf);
300     assertFalse(gene1.equals(gene2));
301     assertFalse(gene2.equals(gene1));
302   }
303
304   /**
305    *
306    * @author Klaus Meffert
307    * @since 2.4
308    * @throws Exception
309    */

310   public void testEquals_8()
311       throws Exception JavaDoc {
312     Gene gene1 = new FixedBinaryGene(conf, 2);
313     gene1.setAllele(new int[] {0, 1});
314     Gene gene2 = new FixedBinaryGene(conf, 2);
315     gene2.setAllele(new int[] {0, 0});
316     assertFalse(gene1.equals(gene2));
317     assertFalse(gene2.equals(gene1));
318   }
319
320   /**
321    *
322    * @author vamsi
323    * @throws Exception
324    */

325   public void testIntValues_0()
326       throws Exception JavaDoc {
327     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
328     assertFalse(gene1.getIntValues() == null);
329   }
330
331   /**
332    *
333    * @author vamsi
334    * @throws Exception
335    */

336   public void testIntValues_1()
337       throws Exception JavaDoc {
338     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 2);
339     int[] values = gene1.getIntValues();
340     int i;
341     for (i = 0; i < values.length; i++) {
342       assertEquals(0, values[i]);
343     }
344   }
345
346   /**
347    *
348    * @author vamsi
349    * @throws Exception
350    */

351   public void testIntValues_2()
352       throws Exception JavaDoc {
353     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
354     gene1.setAllele(new int[] {0, 1, 0});
355     assertEquals(false, gene1.getBit(0));
356     assertEquals(true, gene1.getBit(1));
357     assertEquals(false, gene1.getBit(2));
358     assertEquals(3, gene1.getLength());
359   }
360
361   /**
362    * Allele is null.
363    *
364    * @author vamsi
365    * @throws Exception
366    */

367   public void testSetAllele_0()
368       throws Exception JavaDoc {
369     Gene gene1 = new FixedBinaryGene(conf, 1);
370     try {
371       gene1.setAllele(null);
372       fail();
373     }
374     catch (IllegalArgumentException JavaDoc iex) {
375       ; //this is OK
376
}
377   }
378
379   /**
380    * Allele is of wrong type.
381    *
382    * @author vamsi
383    * @throws Exception
384    */

385   public void testSetAllele_1()
386       throws Exception JavaDoc {
387     Gene gene1 = new FixedBinaryGene(conf, 1);
388     try {
389       gene1.setAllele("22");
390       fail();
391     }
392     catch (ClassCastException JavaDoc classex) {
393       ; //this is OK
394
}
395   }
396
397   /**
398    * Set Allele to int values, no exception should occur.
399    *
400    * @author vamsi
401    * @throws Exception
402    */

403   public void testSetAllele_2()
404       throws Exception JavaDoc {
405     Gene gene1 = new FixedBinaryGene(conf, 3);
406     gene1.setAllele(new int[] {0, 0, 1});
407   }
408
409   /**
410    * The implementation should throw an exception if the alle size is more than
411    * the size of the created gene.
412    *
413    * @author vamsi
414    * @throws Exception
415    */

416   public void testSetAllele_3()
417       throws Exception JavaDoc {
418     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
419     try {
420       gene1.setAllele(new int[] {0, 1, 1, 1, 1, 1});
421       fail();
422     }
423     catch (Exception JavaDoc e) {
424       ; //this is OK
425
}
426   }
427
428   /**
429    * Allele contains illegal characters.
430    *
431    * @author vamsi
432    * @throws Exception
433    */

434   public void testSetAllele_4()
435       throws Exception JavaDoc {
436     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
437     try {
438       gene1.setAllele(new int[] {0, 3, 1, 4});
439       fail();
440     }
441     catch (Exception JavaDoc e) {
442       ; //this is OK
443
}
444   }
445
446   /**
447    * Allele is of wrong length.
448    *
449    * @author vamsi
450    * @throws Exception
451    */

452   public void testSetAllele_5()
453       throws Exception JavaDoc {
454     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
455     try {
456       gene1.setAllele(new int[] {0, 0});
457       fail();
458     }
459     catch (Exception JavaDoc e) {
460       ; //this is OK
461
}
462   }
463
464   /**
465    *
466    * @author Klaus Meffert
467    * @since 2.2
468    * @throws Exception
469    */

470   public void testSetAllele_6()
471       throws Exception JavaDoc {
472     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
473     gene1.setConstraintChecker(new IGeneConstraintChecker() {
474       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
475                             IChromosome a_chrom, int a_index) {
476         return false;
477       }
478     });
479     gene1.setAllele(new int[] {0, 0, 1});
480     assertFalse(gene1.getBit(0));
481     assertFalse(gene1.getBit(1));
482     assertFalse(gene1.getBit(2));
483   }
484
485   /**
486    *
487    * @author Klaus Meffert
488    * @since 2.2
489    * @throws Exception
490    */

491   public void testSetAllele_7()
492       throws Exception JavaDoc {
493     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
494     gene1.setConstraintChecker(new IGeneConstraintChecker() {
495       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
496                             IChromosome a_chrom, int a_index) {
497         return true;
498       }
499     });
500     gene1.setAllele(new int[] {0, 0, 1});
501     assertFalse(gene1.getBit(0));
502     assertFalse(gene1.getBit(1));
503     assertTrue(gene1.getBit(2));
504   }
505
506   /**
507    *
508    * @author Klaus Meffert
509    * @since 2.2
510    * @throws Exception
511    */

512   public void testSetConstraintChecker_0()
513       throws Exception JavaDoc {
514     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
515     assertNull(gene1.getConstraintChecker());
516     gene1.setConstraintChecker(new IGeneConstraintChecker() {
517       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
518                             IChromosome a_chrom, int a_index) {
519         return false;
520       }
521     });
522     assertNotNull(gene1.getConstraintChecker());
523   }
524
525   /**
526    * Comparison should return 0 if same, -1 if less 1 if more.
527    *
528    * @author vamsi
529    * @throws Exception
530    */

531   public void testCompareTo_0()
532       throws Exception JavaDoc {
533     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
534     FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4);
535     gene1.setAllele(new int[] {1, 0, 1, 0});
536     gene2.setAllele(new int[] {1, 1, 0, 1});
537     assertEquals(1, gene1.compareTo(null));
538     assertEquals( -1, gene1.compareTo(gene2));
539     assertEquals(1, gene2.compareTo(gene1));
540   }
541
542   /**
543    *
544    * @author vamsi
545    * @throws Exception
546    */

547   public void testCompareTo_1()
548       throws Exception JavaDoc {
549     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
550     FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3);
551     assertEquals(0, gene1.compareTo(gene2));
552     assertEquals(0, gene2.compareTo(gene1));
553   }
554
555   /**
556    *
557    * @author vamsi
558    * @throws Exception
559    */

560   public void testCompareTo_2()
561       throws Exception JavaDoc {
562     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
563     FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3);
564     gene1.setAllele(new int[] {1, 1, 1});
565     gene2.setAllele(new int[] {1, 1, 1});
566     assertEquals(0, gene1.compareTo(gene2));
567     assertEquals(0, gene2.compareTo(gene1));
568     gene1.setAllele(new int[] {0, 0, 0});
569     gene2.setAllele(new int[] {0, 0, 0});
570     assertEquals(0, gene1.compareTo(gene2));
571     assertEquals(0, gene2.compareTo(gene1));
572   }
573
574   /**
575    *
576    * @author vamsi
577    * @throws Exception
578    */

579   public void testCompareTo_3_1()
580       throws Exception JavaDoc {
581     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
582     BooleanGene gene2 = new BooleanGene(conf);
583     try {
584       gene1.compareTo(gene2);
585       fail();
586     }
587     catch (Exception JavaDoc e) {
588       ; //this is OK (should compare only FixedBinaryGene's)
589
}
590   }
591
592   /**
593    *
594    * @author vamsi
595    * @throws Exception
596    */

597   public void testCompareTo_3_2()
598       throws Exception JavaDoc {
599     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
600     try {
601       gene1.compareTo(new Integer JavaDoc(3));
602       fail();
603     }
604     catch (Exception JavaDoc e) {
605       ; //this is OK (should compare only FixedBinaryGene's)
606
}
607   }
608
609   /**
610    *
611    * @author Klaus Meffert
612    * @since 2.2
613    * @throws Exception
614    */

615   public void testCompareTo_4()
616       throws Exception JavaDoc {
617     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
618     FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4);
619     assertEquals( -1, gene1.compareTo(gene2));
620     assertEquals(1, gene2.compareTo(gene1));
621   }
622
623   /**
624    * Apply Mutation (index,percentage). if >0 make 1(0) if <0 make 0(1)
625    *
626    * @author vamsi
627    * @throws Exception
628    */

629   public void testApplyMutation_0()
630       throws Exception JavaDoc {
631     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
632     gene.setAllele(new int[] {0, 0, 1, 1});
633     gene.applyMutation(0, 0.0d);
634     assertEquals("FixedBinaryGene[0,0,1,1]", gene.toString());
635   }
636
637   /**
638    *
639    * @author vamsi
640    * @throws Exception
641    */

642   public void testApplyMutation_1()
643       throws Exception JavaDoc {
644     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
645     gene.setAllele(new int[] {0, 0, 1, 0});
646     gene.applyMutation(1, 0.000001d);
647     assertEquals("FixedBinaryGene[0,1,1,0]", gene.toString());
648   }
649
650   /**
651    *
652    * @author vamsi
653    * @throws Exception
654    */

655   public void testApplyMutation_2()
656       throws Exception JavaDoc {
657     FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
658     gene.setAllele(new int[] {1, 0, 1, 0, 1});
659     try {
660       //index size is greater
661
gene.applyMutation(333, -0.000001d);
662       fail();
663     }
664     catch (Exception JavaDoc e) {
665       ; //this is OK
666
}
667   }
668
669   /**
670    *
671    * @author vamsi
672    * @throws Exception
673    */

674   public void testApplyMutation_3()
675       throws Exception JavaDoc {
676     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
677     gene.setAllele(new int[] {1, 1, 0, 1});
678     gene.applyMutation(0, -1.0d);
679     assertEquals("FixedBinaryGene[0,1,0,1]", gene.toString());
680   }
681
682   /**
683    *
684    * @author vamsi
685    * @throws Exception
686    */

687   public void testApplyMutation_4()
688       throws Exception JavaDoc {
689     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
690     gene.setAllele(new int[] {0, 1, 0, 1});
691     gene.applyMutation(0, -2.0d);
692     gene.applyMutation(3, 2.0d);
693     gene.applyMutation(1, -4.0d);
694     assertEquals("FixedBinaryGene[0,0,0,1]", gene.toString());
695   }
696
697   /**
698    *
699    * @author vamsi
700    * @throws Exception
701    */

702   public void testApplyMutation_5()
703       throws Exception JavaDoc {
704     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
705     gene.setAllele(new int[] {1, 1, 1, 1});
706     gene.applyMutation(0, 2.0d);
707     gene.applyMutation(1, 2.0d);
708     gene.applyMutation(2, 2.0d);
709     gene.applyMutation(3, 2.0d);
710     assertEquals("FixedBinaryGene[1,1,1,1]", gene.toString());
711   }
712
713   /**
714    *
715    * @author vamsi
716    * @since 2.0
717    * @throws Exception
718    */

719   public void testSetValueFromPersistentRepresentation_0()
720       throws Exception JavaDoc {
721     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
722     try {
723       gene.setValueFromPersistentRepresentation(null);
724       fail();
725     }
726     catch (UnsupportedRepresentationException uex) {
727       ; //this is OK
728
}
729   }
730
731   /**
732    * @since 2.0
733    * @throws Exception
734    */

735   public void testSetValueFromPersistentRepresentation_1()
736       throws Exception JavaDoc {
737     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
738     try {
739       gene.setValueFromPersistentRepresentation("null");
740       fail();
741     }
742     catch (UnsupportedRepresentationException uex) {
743       ; //this is OK
744
}
745   }
746
747   /**
748    * @throws Exception
749    *
750    * @author vamsi
751    * @since 2.0
752    */

753   public void testSetValueFromPersistentRepresentation_2()
754       throws Exception JavaDoc {
755     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
756     gene.setValueFromPersistentRepresentation("[1,1,1,1]");
757     assertTrue(gene.getBit(0));
758     assertTrue(gene.getBit(1));
759     assertTrue(gene.getBit(2));
760     assertTrue(gene.getBit(3));
761   }
762
763   /**
764    * @throws Exception
765    *
766    * @author vamsi
767    * @since 2.0
768    */

769   public void testSetValueFromPersistentRepresentation_3()
770       throws Exception JavaDoc {
771     FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
772     gene.setValueFromPersistentRepresentation("[0,0,0,0]");
773     assertFalse(gene.getBit(0));
774     assertFalse(gene.getBit(1));
775     assertFalse(gene.getBit(2));
776     assertFalse(gene.getBit(3));
777   }
778
779   /**
780    * @throws Exception
781    *
782    * @author vamsi
783    * @since 2.0
784    */

785   public void testSetValueFromPersistentRepresentation_4()
786       throws Exception JavaDoc {
787     FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
788     gene.setValueFromPersistentRepresentation("[0,1,1,0,0]");
789     assertFalse(gene.getBit(0));
790     assertTrue(gene.getBit(1));
791     assertTrue(gene.getBit(2));
792     assertFalse(gene.getBit(3));
793     assertFalse(gene.getBit(4));
794   }
795
796   /**
797    * @throws Exception
798    *
799    * @since 2.0
800    * @author Klaus Meffert
801    */

802   public void testSetValueFromPersistentRepresentation_5()
803       throws Exception JavaDoc {
804     FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
805     try {
806       gene.setValueFromPersistentRepresentation("[0,1,1,0]");
807       fail();
808     }
809     catch (UnsupportedRepresentationException uex) {
810       ; //this is OK
811
}
812   }
813
814   /**
815    *
816    * @author vamsi
817    * @since 2.0
818    * @throws Exception
819    */

820   public void testSetValueFromPersistentRepresentation_6()
821       throws Exception JavaDoc {
822     FixedBinaryGene gene = new FixedBinaryGene(conf, 1);
823     try {
824       gene.setValueFromPersistentRepresentation("X");
825       fail();
826     }
827     catch (UnsupportedRepresentationException uex) {
828       ; //this is OK
829
}
830   }
831
832   /**
833    *
834    * @author vamsi
835    * @throws Exception
836    */

837   public void testGetPersistentRepresentation_0()
838       throws Exception JavaDoc {
839     FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
840     gene.setAllele(new int[] {1, 0, 1});
841     String JavaDoc s = gene.getPersistentRepresentation();
842     assertEquals("FixedBinaryGene[1,0,1]", s);
843   }
844
845   /**
846    * @throws Exception
847    *
848    * @author vamsi
849    * @since 2.0
850    */

851   public void testGetPersistentRepresentation_1()
852       throws Exception JavaDoc {
853     FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
854     try {
855       gene.setValueFromPersistentRepresentation(null);
856       fail();
857     }
858     catch (UnsupportedRepresentationException uex) {
859       ; //this is OK
860
}
861   }
862
863   /**
864    * @throws Exception
865    *
866    * @author Klaus Meffert
867    * @since 2.0
868    */

869   public void testGetPersistentRepresentation_2()
870       throws Exception JavaDoc {
871     FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
872     String JavaDoc s = gene.getPersistentRepresentation();
873     assertEquals("FixedBinaryGene[0,0,0]", s);
874   }
875
876   /**
877    *
878    * @author Klaus Meffert
879    * @since 2.2
880    * @throws Exception
881    */

882   public void testClone_0()
883       throws Exception JavaDoc {
884     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1);
885     FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
886     assertEquals(gene1, gene2);
887   }
888
889   /**
890    *
891    * @author Klaus Meffert
892    * @since 2.2
893    * @throws Exception
894    */

895   public void testClone_1()
896       throws Exception JavaDoc {
897     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
898     FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
899     assertEquals(gene1, gene2);
900   }
901
902   /**
903    *
904    * @author Klaus Meffert
905    * @since 2.2
906    * @throws Exception
907    */

908   public void testSetBit_0()
909       throws Exception JavaDoc {
910     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
911     gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
912     assertTrue(gene1.getBit(0));
913     gene1.setBit(0, false);
914     assertFalse(gene1.getBit(0));
915     gene1.setBit(1, true);
916     assertTrue(gene1.getBit(1));
917     gene1.setBit(4, false);
918     assertFalse(gene1.getBit(0));
919   }
920
921   /**
922    *
923    * @author Klaus Meffert
924    * @since 2.2
925    * @throws Exception
926    */

927   public void testSetBit_1()
928       throws Exception JavaDoc {
929     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
930     gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
931     gene1.setBit(2, 4, true);
932     assertTrue(gene1.getBit(0));
933     assertTrue(gene1.getBit(1));
934     assertTrue(gene1.getBit(2));
935     assertTrue(gene1.getBit(3));
936   }
937
938   /**
939    *
940    * @author Klaus Meffert
941    * @since 2.2
942    * @throws Exception
943    */

944   public void testSetBit_2()
945       throws Exception JavaDoc {
946     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
947     gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
948     try {
949       gene1.setBit(2, 2);
950       fail();
951     }
952     catch (IllegalArgumentException JavaDoc iex) {
953       ; //this is OK
954
}
955   }
956
957   /**
958    *
959    * @author Klaus Meffert
960    * @since 2.2
961    * @throws Exception
962    */

963   public void testSetBit_3()
964       throws Exception JavaDoc {
965     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
966     gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
967     try {
968       gene1.setBit(2, -1);
969       fail();
970     }
971     catch (IllegalArgumentException JavaDoc iex) {
972       ; //this is OK
973
}
974   }
975
976   /**
977    *
978    * @author Klaus Meffert
979    * @since 2.2
980    * @throws Exception
981    */

982   public void testSetBit_4()
983       throws Exception JavaDoc {
984     FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
985     gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
986     try {
987       gene1.setBit(2, 1, false);
988       fail();
989     }
990     catch (IllegalArgumentException JavaDoc iex) {
991       ; //this is OK
992
}
993   }
994
995   /**
996    *
997    * @author Klaus Meffert
998    * @since 2.6
999    * @throws Exception
1000   */

1001  public void testSetBit_5()
1002      throws Exception JavaDoc {
1003    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1004    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
1005    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 9);
1006    gene2.setBit(2, 6, gene1);
1007    assertTrue(gene2.getBit(2));
1008    assertTrue(gene2.getBit(3));
1009    assertTrue(gene2.getBit(6));
1010    assertFalse(gene2.getBit(0));
1011    assertFalse(gene2.getBit(1));
1012    assertFalse(gene2.getBit(4));
1013    assertFalse(gene2.getBit(5));
1014    assertFalse(gene2.getBit(7));
1015    assertFalse(gene2.getBit(8));
1016  }
1017
1018  /**
1019   *
1020   * @author Klaus Meffert
1021   * @since 2.6
1022   * @throws Exception
1023   */

1024  public void testSetBit_6()
1025      throws Exception JavaDoc {
1026    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
1027    gene1.setAllele(new int[] {1, 1, 0, 0});
1028    FixedBinaryGene gene2 = new FixedBinaryGene(conf, 7);
1029    gene2.setBit(2, 6, gene1);
1030    assertTrue(gene2.getBit(2));
1031    assertTrue(gene2.getBit(3));
1032    assertTrue(gene2.getBit(6));
1033    assertFalse(gene2.getBit(0));
1034    assertFalse(gene2.getBit(1));
1035    assertFalse(gene2.getBit(4));
1036    assertFalse(gene2.getBit(5));
1037  }
1038
1039  /**
1040   *
1041   * @author Klaus Meffert
1042   * @since 2.2
1043   * @throws Exception
1044   */

1045  public void testSubbString_0()
1046      throws Exception JavaDoc {
1047    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1048    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
1049    FixedBinaryGene gene2 = gene1.substring(0, 4);
1050    assertEquals(5, gene2.getLength());
1051    assertEquals(1, gene2.size());
1052    assertTrue(gene2.getBit(0));
1053    assertTrue(gene2.getBit(1));
1054    assertFalse(gene2.getBit(2));
1055    assertFalse(gene2.getBit(3));
1056    assertTrue(gene2.getBit(4));
1057  }
1058
1059  /**
1060   *
1061   * @author Klaus Meffert
1062   * @since 2.2
1063   * @throws Exception
1064   */

1065  public void testSubString_1()
1066      throws Exception JavaDoc {
1067    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1068    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
1069    try {
1070      gene1.substring(0, 7);
1071      fail();
1072    }
1073    catch (IndexOutOfBoundsException JavaDoc iex) {
1074      ; //this is OK
1075
}
1076  }
1077
1078  /**
1079   *
1080   * @author Klaus Meffert
1081   * @since 2.2
1082   * @throws Exception
1083   */

1084  public void testFlip_0()
1085      throws Exception JavaDoc {
1086    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1087    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
1088    gene1.flip(0);
1089    assertFalse(gene1.getBit(0));
1090    gene1.flip(6);
1091    assertFalse(gene1.getBit(6));
1092    gene1.flip(2);
1093    assertTrue(gene1.getBit(2));
1094  }
1095
1096  /**
1097   *
1098   * @author Klaus Meffert
1099   * @since 2.2
1100   * @throws Exception
1101   */

1102  public void testFlip_1()
1103      throws Exception JavaDoc {
1104    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1105    gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
1106    try {
1107      gene1.flip(7);
1108      fail();
1109    }
1110    catch (IndexOutOfBoundsException JavaDoc iex) {
1111      ; //this is OK
1112
}
1113  }
1114
1115  /**
1116   *
1117   * @author Klaus Meffert
1118   * @since 2.2
1119   * @throws Exception
1120   */

1121  public void testSetToRandomValue_0()
1122      throws Exception JavaDoc {
1123    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1124    try {
1125      gene1.setToRandomValue(null);
1126      fail();
1127    }
1128    catch (IllegalArgumentException JavaDoc iex) {
1129      ; //this is OK
1130
}
1131  }
1132
1133  /**
1134   *
1135   * @author Klaus Meffert
1136   * @since 2.2
1137   * @throws Exception
1138   */

1139  public void testSetToRandomValue_1()
1140      throws Exception JavaDoc {
1141    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1142    gene1.setToRandomValue(new StockRandomGenerator());
1143  }
1144
1145  /**
1146   *
1147   * @author Klaus Meffert
1148   * @since 2.2
1149   * @throws Exception
1150   */

1151  public void testSetToRandomValue_2()
1152      throws Exception JavaDoc {
1153    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1154    gene1.setToRandomValue(new RandomGeneratorForTest(false));
1155    for (int i = 0; i < 7; i++) {
1156      assertFalse(gene1.getBit(i));
1157    }
1158  }
1159
1160  /**
1161   *
1162   * @author Klaus Meffert
1163   * @since 2.2
1164   * @throws Exception
1165   */

1166  public void testSetToRandomValue_3()
1167      throws Exception JavaDoc {
1168    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1169    gene1.setToRandomValue(new RandomGeneratorForTest(true));
1170    for (int i = 0; i < 7; i++) {
1171      assertTrue(gene1.getBit(i));
1172    }
1173  }
1174
1175  /**
1176   *
1177   * @author Klaus Meffert
1178   * @throws Exception
1179   */

1180  public void testNewGene_0()
1181      throws Exception JavaDoc {
1182    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1183    IGeneConstraintChecker checker = new GeneConstraintChecker();
1184    gene1.setConstraintChecker(checker);
1185    FixedBinaryGene gene2 = (FixedBinaryGene) gene1.newGene();
1186    assertTrue(gene1.equals(gene2));
1187    assertTrue(gene2.equals(gene1));
1188    assertEquals(checker, gene2.getConstraintChecker());
1189  }
1190
1191  /**
1192   *
1193   * @author Klaus Meffert
1194   * @since 2.2
1195   * @throws Exception
1196   */

1197  public void testHashCode_0()
1198      throws Exception JavaDoc {
1199    FixedBinaryGene gene = new FixedBinaryGene(conf, 6);
1200    gene.hashCode();
1201    gene.setBit(0, 5, false);
1202    gene.hashCode();
1203    gene.setBit(0, 5, true);
1204    gene.hashCode();
1205    /**@todo implement checks for uniqueness*/
1206  }
1207
1208  /**
1209   *
1210   * @author Klaus Meffert
1211   * @since 2.5
1212   * @throws Exception
1213   */

1214  public void testHashCode_1()
1215      throws Exception JavaDoc {
1216    Gene gene1 = new FixedBinaryGene(conf, 2);
1217    Gene gene2 = new FixedBinaryGene(conf, 2);
1218    int[] v1 = (int[]) gene1.getAllele();
1219    int[] v2 = (int[]) gene1.getAllele();
1220    int h1 = v1.hashCode();
1221    int h2 = v2.hashCode();
1222    assertEquals(gene1.hashCode(), gene2.hashCode());
1223  }
1224
1225  /**
1226   *
1227   * @author Klaus Meffert
1228   * @since 2.5
1229   * @throws Exception
1230   */

1231  public void testHashCode_2()
1232      throws Exception JavaDoc {
1233    Gene gene1 = new FixedBinaryGene(conf, 3);
1234    gene1.setAllele(new int[] {0, 1, 0});
1235    Gene gene2 = new FixedBinaryGene(conf, 3);
1236    gene2.setAllele(new int[] {0, 1, 0});
1237    assertEquals(gene1.hashCode(), gene2.hashCode());
1238  }
1239
1240  /**
1241   *
1242   * @author Klaus Meffert
1243   * @since 2.5
1244   * @throws Exception
1245   */

1246  public void testHashCode_3()
1247      throws Exception JavaDoc {
1248    Gene gene1 = new FixedBinaryGene(conf, 2);
1249    gene1.setAllele(new int[] {1, 1});
1250    Gene gene2 = new FixedBinaryGene(conf, 2);
1251    gene1.setAllele(new int[] {1, 0});
1252    assertFalse(gene1.hashCode() == gene2.hashCode());
1253    assertTrue(gene1.hashCode() == gene1.hashCode());
1254    assertTrue(gene2.hashCode() == gene2.hashCode());
1255  }
1256
1257  /**
1258   *
1259   * @author Klaus Meffert
1260   * @since 2.4
1261   * @throws Exception
1262   */

1263  public void testSetEnergy_0()
1264      throws Exception JavaDoc {
1265    BaseGene gene = new FixedBinaryGene(conf, 2);
1266    assertEquals(0.0, gene.getEnergy(), DELTA);
1267  }
1268
1269  /**
1270   *
1271   * @author Klaus Meffert
1272   * @since 2.4
1273   * @throws Exception
1274   */

1275  public void testSetEnergy_1()
1276      throws Exception JavaDoc {
1277    BaseGene gene = new FixedBinaryGene(conf, 3);
1278    gene.setEnergy(2.3);
1279    assertEquals(2.3, gene.getEnergy(), DELTA);
1280    gene.setEnergy( -55.8);
1281    assertEquals( -55.8, gene.getEnergy(), DELTA);
1282    gene.setEnergy(0.5);
1283    gene.setEnergy(0.8);
1284    assertEquals(0.8, gene.getEnergy(), DELTA);
1285  }
1286
1287  class GeneConstraintChecker
1288      implements IGeneConstraintChecker {
1289    public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
1290                          IChromosome a_chrom, int a_index) {
1291      return true;
1292    }
1293  }
1294
1295  /**@todo test compareTo with applicationcata*/
1296}
1297
Popular Tags