KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 public class BooleanGeneTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.21 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(BooleanGeneTest.class);
29     return suite;
30   }
31
32   public void testConstruct_0()
33       throws Exception JavaDoc {
34     Gene gene = new BooleanGene(conf);
35     //following should be possible without exception
36
gene.setAllele(Boolean.valueOf(true));
37     gene.setAllele(Boolean.valueOf(false));
38   }
39
40   /**
41    * @throws Exception
42    *
43    * @author Klaus Meffert
44    * @since 2.4
45    */

46   public void testConstruct_1()
47       throws Exception JavaDoc {
48     BooleanGene gene = new BooleanGene(conf, true);
49     assertEquals(true, gene.booleanValue());
50     gene = new BooleanGene(conf, false);
51     assertEquals(false, gene.booleanValue());
52   }
53
54   /**
55    * @throws Exception
56    *
57    * @author Klaus Meffert
58    * @since 2.4
59    */

60   public void testConstruct_2()
61       throws Exception JavaDoc {
62     BooleanGene gene = new BooleanGene(conf, Boolean.valueOf(true));
63     assertEquals(true, gene.booleanValue());
64     gene = new BooleanGene(conf, Boolean.valueOf(false));
65     assertEquals(false, gene.booleanValue());
66   }
67
68   /**
69    * @throws Exception
70    *
71    * @author Klaus Meffert
72    * @since 2.4
73    */

74   public void testConstruct_3()
75       throws Exception JavaDoc {
76     try {
77       new BooleanGene(conf, null);
78       fail();
79     } catch (IllegalArgumentException JavaDoc iex) {
80       ; //this is OK
81
}
82   }
83
84   /**
85    * @throws Exception
86    *
87    * @author Klaus Meffert
88    * @since 3.1
89    */

90   public void testConstruct_4()
91       throws Exception JavaDoc {
92     Genotype.setStaticConfiguration(conf);
93     Gene gene = new BooleanGene();
94     assertSame(conf, gene.getConfiguration());
95   }
96
97   public void testToString_0()
98       throws Exception JavaDoc {
99     Gene gene = new BooleanGene(conf);
100     gene.setAllele(Boolean.valueOf(true));
101     assertEquals("BooleanGene=true", gene.toString());
102   }
103
104   public void testToString_1()
105       throws Exception JavaDoc {
106     Gene gene = new BooleanGene(conf);
107     gene.setAllele(Boolean.valueOf(false));
108     assertEquals("BooleanGene=false", gene.toString());
109   }
110
111   /**
112    * @throws Exception
113    *
114    * @author Klaus Meffert
115    * @since 2.4
116    */

117   public void testToString_2()
118       throws Exception JavaDoc {
119     Gene gene = new BooleanGene(conf, true);
120     assertEquals("BooleanGene=true", gene.toString());
121   }
122
123   public void testGetAllele_0()
124       throws Exception JavaDoc {
125     Gene gene = new BooleanGene(conf);
126     gene.setAllele(Boolean.valueOf(true));
127     assertEquals(Boolean.valueOf(true), gene.getAllele());
128   }
129
130   public void testGetAllele_1()
131       throws Exception JavaDoc {
132     Gene gene = new BooleanGene(conf);
133     gene.setAllele(Boolean.valueOf(false));
134     assertEquals(Boolean.valueOf(false), gene.getAllele());
135   }
136
137   public void testGetAllele_2()
138       throws Exception JavaDoc {
139     Gene gene = new BooleanGene(conf);
140     try {
141       gene.setAllele(new Integer JavaDoc(100));
142       fail();
143     } catch (ClassCastException JavaDoc classex) {
144       ; //this is OK
145
}
146   }
147
148   public void testEquals_0()
149       throws Exception JavaDoc {
150     Gene gene1 = new BooleanGene(conf);
151     Gene gene2 = new BooleanGene(conf);
152     assertTrue(gene1.equals(gene2));
153   }
154
155   public void testEquals_1()
156       throws Exception JavaDoc {
157     Gene gene1 = new BooleanGene(conf);
158     assertFalse(gene1.equals(null));
159   }
160
161   public void testEquals_2()
162       throws Exception JavaDoc {
163     Gene gene1 = new BooleanGene(conf);
164     gene1.setAllele(Boolean.valueOf(true));
165     Gene gene2 = new BooleanGene(conf);
166     gene2.setAllele(Boolean.valueOf(false));
167     assertFalse(gene1.equals(gene2));
168     assertFalse(gene2.equals(gene1));
169   }
170
171   public void testEquals_3()
172       throws Exception JavaDoc {
173     Gene gene1 = new BooleanGene(conf);
174     assertFalse(gene1.equals(new IntegerGene(conf)));
175   }
176
177   public void testEquals_4()
178       throws Exception JavaDoc {
179     Gene gene1 = new BooleanGene(conf);
180     Gene gene2 = new IntegerGene(conf);
181     assertFalse(gene1.equals(gene2));
182     assertFalse(gene2.equals(gene1));
183   }
184
185   public void testEquals_5()
186       throws Exception JavaDoc {
187     Gene gene1 = new BooleanGene(conf);
188     Gene gene2 = new FixedBinaryGene(conf, 1);
189     assertFalse(gene1.equals(gene2));
190     assertFalse(gene2.equals(gene1));
191   }
192
193   public void testBooleanValue_0()
194       throws Exception JavaDoc {
195     BooleanGene gene1 = new BooleanGene(conf);
196     gene1.setAllele(Boolean.valueOf(true));
197     assertEquals(true, gene1.booleanValue());
198   }
199
200   public void testBooleanValue_1()
201       throws Exception JavaDoc {
202     BooleanGene gene1 = new BooleanGene(conf);
203     gene1.setAllele(Boolean.valueOf(false));
204     assertEquals(false, gene1.booleanValue());
205   }
206
207   public void testBooleanValue_2()
208       throws Exception JavaDoc {
209     BooleanGene gene1 = new BooleanGene(conf);
210     gene1.setAllele(null);
211     try {
212       assertEquals(true, gene1.booleanValue());
213       fail();
214     } catch (NullPointerException JavaDoc nullex) {
215       ; //this is OK
216
}
217   }
218
219   /**
220    * Set Allele to null, no exception should occur.
221    *
222    * @throws Exception
223    */

224   public void testSetAllele_0()
225       throws Exception JavaDoc {
226     Gene gene1 = new BooleanGene(conf);
227     gene1.setAllele(null);
228   }
229
230   public void testSetAllele_1()
231       throws Exception JavaDoc {
232     Gene gene1 = new BooleanGene(conf);
233     try {
234       gene1.setAllele("22");
235       fail();
236     } catch (ClassCastException JavaDoc classex) {
237       ; //this is OK
238
}
239   }
240
241   /**
242    * Set Allele to boolean value, no exception should occur.
243    *
244    * @throws Exception
245    */

246   public void testSetAllele_2()
247       throws Exception JavaDoc {
248     Gene gene1 = new BooleanGene(conf);
249     gene1.setAllele(Boolean.valueOf(true));
250   }
251
252   /**
253    * Set Allele to boolean value, no exception should occur.
254    *
255    * @throws Exception
256    */

257   public void testSetAllele_3()
258       throws Exception JavaDoc {
259     Gene gene1 = new BooleanGene(conf);
260     gene1.setAllele(Boolean.valueOf(false));
261   }
262
263   public void testCompareTo_0()
264       throws Exception JavaDoc {
265     Gene gene1 = new BooleanGene(conf);
266     assertEquals(1, gene1.compareTo(null));
267   }
268
269   public void testCompareTo_1()
270       throws Exception JavaDoc {
271     Gene gene1 = new BooleanGene(conf);
272     Gene gene2 = new BooleanGene(conf);
273     assertEquals(0, gene1.compareTo(gene2));
274     assertEquals(0, gene2.compareTo(gene1));
275   }
276
277   public void testCompareTo_2()
278       throws Exception JavaDoc {
279     Gene gene1 = new BooleanGene(conf);
280     gene1.setAllele(Boolean.valueOf(true));
281     Gene gene2 = new BooleanGene(conf);
282     gene2.setAllele(Boolean.valueOf(true));
283     assertEquals(0, gene1.compareTo(gene2));
284     assertEquals(0, gene2.compareTo(gene1));
285     gene1.setAllele(Boolean.valueOf(false));
286     gene2.setAllele(Boolean.valueOf(false));
287     assertEquals(0, gene1.compareTo(gene2));
288     assertEquals(0, gene2.compareTo(gene1));
289   }
290
291   public void testCompareTo_3()
292       throws Exception JavaDoc {
293     Gene gene1 = new BooleanGene(conf);
294     gene1.setAllele(Boolean.valueOf(true));
295     Gene gene2 = new BooleanGene(conf);
296     gene2.setAllele(Boolean.valueOf(false));
297     assertEquals(1, gene1.compareTo(gene2));
298     assertEquals( -1, gene2.compareTo(gene1));
299   }
300
301   /**
302    * @throws Exception
303    *
304    * @author Klaus Meffert
305    * @since 2.2
306    */

307   public void testCompareTo_4()
308       throws Exception JavaDoc {
309     Gene gene1 = new BooleanGene(conf);
310     gene1.setAllele(Boolean.valueOf(true));
311     Gene gene2 = new BooleanGene(conf);
312     assertEquals(1, gene1.compareTo(gene2));
313     assertEquals( -1, gene2.compareTo(gene1));
314   }
315
316   /**
317    * @throws Exception
318    *
319    * @author Klaus Meffert
320    * @since 2.2
321    */

322   public void testCompareTo_5()
323       throws Exception JavaDoc {
324     Gene gene1 = new BooleanGene(conf);
325     Gene gene2 = new BooleanGene(conf);
326     assertEquals(0, gene1.compareTo(gene2));
327     assertEquals(0, gene2.compareTo(gene1));
328   }
329
330   /**
331    * @throws Exception
332    *
333    * @author Klaus Meffert
334    * @since 3.1
335    */

336   public void testCompareTo_6()
337       throws Exception JavaDoc {
338     Gene gene1 = new BooleanGene(conf);
339     Gene gene2 = new BooleanGene(conf);
340     gene1.setCompareApplicationData(true);
341     gene2.setCompareApplicationData(false);
342     List app1 = new Vector();
343     gene1.setApplicationData(app1);
344     assertEquals(1, gene1.compareTo(gene2));
345     assertEquals(0, gene2.compareTo(gene1));
346   }
347
348   /**
349    * @throws Exception
350    *
351    * @author Klaus Meffert
352    * @since 3.1
353    */

354   public void testCompareTo_6_2()
355       throws Exception JavaDoc {
356     Gene gene1 = new BooleanGene(conf);
357     gene1.setAllele(Boolean.TRUE);
358     Gene gene2 = new BooleanGene(conf);
359     gene2.setAllele(Boolean.FALSE);
360     gene1.setCompareApplicationData(true);
361     gene2.setCompareApplicationData(true);
362     List app1 = new Vector();
363     gene1.setApplicationData(app1);
364     assertEquals(1, gene1.compareTo(gene2));
365     assertEquals(-1, gene2.compareTo(gene1));
366   }
367
368   /**
369    * @throws Exception
370    *
371    * @author Klaus Meffert
372    * @since 3.1
373    */

374   public void testCompareTo_6_3()
375       throws Exception JavaDoc {
376     Gene gene1 = new BooleanGene(conf);
377     gene1.setAllele(Boolean.FALSE);
378     Gene gene2 = new BooleanGene(conf);
379     gene2.setAllele(Boolean.FALSE);
380     gene1.setCompareApplicationData(true);
381     gene2.setCompareApplicationData(true);
382     List app1 = new Vector();
383     gene1.setApplicationData(app1);
384     assertEquals(1, gene1.compareTo(gene2));
385     assertEquals(-1, gene2.compareTo(gene1));
386   }
387
388   /**
389    * @throws Exception
390    *
391    * @author Klaus Meffert
392    * @since 3.1
393    */

394   public void testCompareTo_7()
395       throws Exception JavaDoc {
396     Gene gene1 = new BooleanGene(conf);
397     Gene gene2 = new BooleanGene(conf);
398     gene2.setAllele(Boolean.TRUE);
399     assertEquals(-1, gene1.compareTo(gene2));
400     assertEquals(1, gene2.compareTo(gene1));
401   }
402
403   /**
404    * @throws Exception
405    *
406    * @author Klaus Meffert
407    */

408   public void testApplyMutation_0()
409       throws Exception JavaDoc {
410     BooleanGene gene = new BooleanGene(conf);
411     gene.setAllele(Boolean.valueOf(true));
412     gene.applyMutation(0, 0.0d);
413     assertEquals(true, gene.booleanValue());
414   }
415
416   /**
417    * @throws Exception
418    *
419    * @author Klaus Meffert
420    */

421   public void testApplyMutation_1()
422       throws Exception JavaDoc {
423     BooleanGene gene = new BooleanGene(conf);
424     gene.setAllele(Boolean.valueOf(true));
425     gene.applyMutation(1, 0.000001d); //index 1 should be ignored
426
assertEquals(true, gene.booleanValue());
427   }
428
429   /**
430    * @throws Exception
431    *
432    * @author Klaus Meffert
433    */

434   public void testApplyMutation_2()
435       throws Exception JavaDoc {
436     BooleanGene gene = new BooleanGene(conf);
437     gene.setAllele(Boolean.valueOf(true));
438     gene.applyMutation(333, -0.000001d); //index 333 should be ignored
439
assertEquals(false, gene.booleanValue());
440   }
441
442   /**
443    * @throws Exception
444    *
445    * @author Klaus Meffert
446    */

447   public void testApplyMutation_3()
448       throws Exception JavaDoc {
449     BooleanGene gene = new BooleanGene(conf);
450     gene.setAllele(Boolean.valueOf(true));
451     gene.applyMutation(0, -1.0d);
452     assertEquals(false, gene.booleanValue());
453   }
454
455   /**
456    * @throws Exception
457    *
458    * @author Klaus Meffert
459    */

460   public void testApplyMutation_4()
461       throws Exception JavaDoc {
462     BooleanGene gene = new BooleanGene(conf);
463     gene.setAllele(Boolean.valueOf(true));
464     gene.applyMutation(0, -2.0d);
465     assertEquals(false, gene.booleanValue());
466   }
467
468   /**
469    * @throws Exception
470    *
471    * @author Klaus Meffert
472    */

473   public void testApplyMutation_5()
474       throws Exception JavaDoc {
475     BooleanGene gene = new BooleanGene(conf);
476     gene.setAllele(Boolean.valueOf(true));
477     gene.applyMutation(0, 2.0d);
478     assertEquals(true, gene.booleanValue());
479   }
480
481   /**
482    * @throws Exception
483    *
484    * @author Klaus Meffert
485    */

486   public void testApplyMutation_6()
487       throws Exception JavaDoc {
488     BooleanGene gene = new BooleanGene(conf);
489     gene.setAllele(Boolean.valueOf(false));
490     gene.applyMutation(0, 2.0d);
491     assertEquals(true, gene.booleanValue());
492   }
493
494   /**
495    * @throws Exception
496    *
497    * @author Klaus Meffert
498    */

499   public void testApplyMutation_7()
500       throws Exception JavaDoc {
501     BooleanGene gene = new BooleanGene(conf);
502     gene.setAllele(Boolean.valueOf(false));
503     gene.applyMutation(0, -1.0d);
504     assertEquals(false, gene.booleanValue());
505   }
506
507   /**
508    * @throws Exception
509    *
510    * @author Klaus Meffert
511    */

512   public void testApplyMutation_8()
513       throws Exception JavaDoc {
514     BooleanGene gene = new BooleanGene(conf);
515     gene.setAllele(Boolean.valueOf(false));
516     gene.applyMutation(22, -0.5d); //22 should be ignored
517
assertEquals(false, gene.booleanValue());
518   }
519
520   /**
521    * @throws Exception
522    *
523    * @author Klaus Meffert
524    */

525   public void testApplyMutation_9()
526       throws Exception JavaDoc {
527     BooleanGene gene = new BooleanGene(conf);
528     gene.setAllele(Boolean.valueOf(false));
529     gene.applyMutation(22, 0.5d); //22 should be ignored
530
assertEquals(true, gene.booleanValue());
531   }
532
533   /**
534    * @throws Exception
535    *
536    * @author Klaus Meffert
537    * @since 2.2
538    */

539   public void testApplyMutation_10()
540       throws Exception JavaDoc {
541     BooleanGene gene = new BooleanGene(conf);
542     gene.applyMutation(0, 0.0d);
543     assertEquals(false, gene.booleanValue());
544   }
545
546   /**
547    * @throws Exception
548    *
549    * @author Klaus Meffert
550    * @since 2.0
551    */

552   public void testSetValueFromPersistentRepresentation_0()
553       throws Exception JavaDoc {
554     BooleanGene gene = new BooleanGene(conf);
555     try {
556       gene.setValueFromPersistentRepresentation(null);
557       fail();
558     } catch (UnsupportedRepresentationException uex) {
559       ; //this is OK
560
}
561   }
562
563   /**
564    * @throws Exception
565    *
566    * @author Klaus Meffert
567    * @since 2.0
568    */

569   public void testSetValueFromPersistentRepresentation_1()
570       throws Exception JavaDoc {
571     BooleanGene gene = new BooleanGene(conf);
572     gene.setValueFromPersistentRepresentation("null");
573     assertEquals(null, gene.getAllele());
574   }
575
576   /**
577    * @throws Exception
578    *
579    * @author Klaus Meffert
580    * @since 2.0
581    */

582   public void testSetValueFromPersistentRepresentation_2()
583       throws Exception JavaDoc {
584     BooleanGene gene = new BooleanGene(conf);
585     gene.setValueFromPersistentRepresentation("true");
586     assertEquals(Boolean.TRUE, gene.getAllele());
587   }
588
589   /**
590    * @throws Exception
591    *
592    * @author Klaus Meffert
593    * @since 2.0
594    */

595   public void testSetValueFromPersistentRepresentation_3()
596       throws Exception JavaDoc {
597     BooleanGene gene = new BooleanGene(conf);
598     gene.setValueFromPersistentRepresentation("false");
599     assertEquals(Boolean.FALSE, gene.getAllele());
600   }
601
602   /**
603    * @throws Exception
604    *
605    * @author Klaus Meffert
606    * @since 2.0
607    */

608   public void testSetValueFromPersistentRepresentation_4()
609       throws Exception JavaDoc {
610     BooleanGene gene = new BooleanGene(conf);
611     try {
612       gene.setValueFromPersistentRepresentation("True");
613       fail();
614     } catch (UnsupportedRepresentationException uex) {
615       ; //this is OK
616
}
617   }
618
619   /**
620    * @throws Exception
621    *
622    * @author Klaus Meffert
623    * @since 2.0
624    */

625   public void testSetValueFromPersistentRepresentation_5()
626       throws Exception JavaDoc {
627     BooleanGene gene = new BooleanGene(conf);
628     try {
629       gene.setValueFromPersistentRepresentation("False");
630       fail();
631     } catch (UnsupportedRepresentationException uex) {
632       ; //this is OK
633
}
634   }
635
636   /**
637    * @throws Exception
638    *
639    * @author Klaus Meffert
640    * @since 2.0
641    */

642   public void testSetValueFromPersistentRepresentation_6()
643       throws Exception JavaDoc {
644     BooleanGene gene = new BooleanGene(conf);
645     try {
646       gene.setValueFromPersistentRepresentation("X");
647       fail();
648     } catch (UnsupportedRepresentationException uex) {
649       ; //this is OK
650
}
651   }
652
653   /**
654    * @throws Exception
655    *
656    * @author Klaus Meffert
657    */

658   public void testGetPersistentRepresentation_0()
659       throws Exception JavaDoc {
660     BooleanGene gene = new BooleanGene(conf);
661     gene.setAllele(Boolean.valueOf(true));
662     String JavaDoc s = gene.getPersistentRepresentation();
663     assertEquals("true", s);
664   }
665
666   /**
667    * @throws Exception
668    *
669    * @author Klaus Meffert
670    */

671   public void testGetPersistentRepresentation_1()
672       throws Exception JavaDoc {
673     BooleanGene gene = new BooleanGene(conf);
674     gene.setAllele(Boolean.valueOf(false));
675     String JavaDoc s = gene.getPersistentRepresentation();
676     assertEquals("false", s);
677   }
678
679   /**
680    * @throws Exception
681    *
682    * @author Klaus Meffert
683    */

684   public void testGetPersistentRepresentation_2()
685       throws Exception JavaDoc {
686     BooleanGene gene = new BooleanGene(conf);
687     String JavaDoc s = gene.getPersistentRepresentation();
688     assertEquals("null", s);
689   }
690
691   /**
692    * @throws Exception
693    *
694    * @author Klaus Meffert
695    * @since 2.2
696    */

697   public void testHashCode_0()
698       throws Exception JavaDoc {
699     BooleanGene gene = new BooleanGene(conf);
700     assertEquals( -2, gene.hashCode());
701   }
702
703   /**
704    * @throws Exception
705    *
706    * @author Klaus Meffert
707    * @since 2.4
708    */

709   public void testSetEnergy_0()
710       throws Exception JavaDoc {
711     BaseGene gene = new BooleanGene(conf);
712     assertEquals(0.0, gene.getEnergy(), DELTA);
713   }
714
715   /**
716    * @throws Exception
717    *
718    * @author Klaus Meffert
719    * @since 2.4
720    */

721   public void testSetEnergy_1()
722       throws Exception JavaDoc {
723     BaseGene gene = new BooleanGene(conf);
724     gene.setEnergy(2.3);
725     assertEquals(2.3, gene.getEnergy(), DELTA);
726     gene.setEnergy( -55.8);
727     assertEquals( -55.8, gene.getEnergy(), DELTA);
728     gene.setEnergy(0.5);
729     gene.setEnergy(0.8);
730     assertEquals(0.8, gene.getEnergy(), DELTA);
731   }
732 }
733
Popular Tags