KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

45   public void testConstruct_1()
46       throws Exception JavaDoc {
47     Genotype.setStaticConfiguration(conf);
48     Gene gene = new IntegerGene();
49     assertSame(conf, gene.getConfiguration());
50   }
51
52   public void testToString_0()
53       throws Exception JavaDoc {
54     Gene gene = new IntegerGene(conf, 1, 100);
55     gene.setAllele(new Integer JavaDoc(47));
56     assertEquals("IntegerGene(1,100)=47", gene.toString());
57   }
58
59   public void testToString_1()
60       throws Exception JavaDoc {
61     Gene gene = new IntegerGene(conf, -2, 100);
62     gene.setAllele(new Integer JavaDoc(99));
63     assertEquals("IntegerGene(-2,100)=99", gene.toString());
64   }
65
66   public void testGetAllele_0()
67       throws Exception JavaDoc {
68     Gene gene = new IntegerGene(conf, 1, 100);
69     gene.setAllele(new Integer JavaDoc(33));
70     assertEquals(new Integer JavaDoc(33), gene.getAllele());
71   }
72
73   public void testGetAllele_1()
74       throws Exception JavaDoc {
75     Gene gene = new IntegerGene(conf, 1, 100);
76     gene.setAllele(new Integer JavaDoc(1));
77     assertEquals(new Integer JavaDoc(1), gene.getAllele());
78   }
79
80   public void testGetAllele_2()
81       throws Exception JavaDoc {
82     Gene gene = new IntegerGene(conf, 1, 100);
83     gene.setAllele(new Integer JavaDoc(100));
84     assertEquals(new Integer JavaDoc(100), gene.getAllele());
85   }
86
87   public void testEquals_0()
88       throws Exception JavaDoc {
89     Gene gene1 = new IntegerGene(conf, 1, 100);
90     Gene gene2 = new IntegerGene(conf, 1, 100);
91     assertTrue(gene1.equals(gene2));
92     assertTrue(gene2.equals(gene1));
93   }
94
95   public void testEquals_1()
96       throws Exception JavaDoc {
97     Gene gene1 = new IntegerGene(conf, 1, 100);
98     assertFalse(gene1.equals(null));
99   }
100
101   public void testEquals_2()
102       throws Exception JavaDoc {
103     Gene gene1 = new IntegerGene(conf, 1, 100);
104     assertFalse(gene1.equals(new BooleanGene(conf)));
105   }
106
107   public void testEquals_3()
108       throws Exception JavaDoc {
109     Gene gene1 = new IntegerGene(conf, 1, 100);
110     assertFalse(gene1.equals(new Vector()));
111   }
112
113   public void testEquals_4()
114       throws Exception JavaDoc {
115     Gene gene1 = new IntegerGene(conf, 1, 100);
116     Gene gene2 = new IntegerGene(conf, 1, 99);
117     assertTrue(gene1.equals(gene2));
118     assertTrue(gene2.equals(gene1));
119   }
120
121   public void testEquals_5()
122       throws Exception JavaDoc {
123     Gene gene1 = new IntegerGene(conf, 1, 100);
124     Gene gene2 = new DoubleGene(conf, 1, 99);
125     assertFalse(gene1.equals(gene2));
126     assertFalse(gene2.equals(gene1));
127   }
128
129   /**
130    * Uses subclass of IntegerGene.
131    * @throws Exception
132    *
133    * @author Klaus Meffert
134    * @since 2.4
135    */

136   public void testEquals_6()
137       throws Exception JavaDoc {
138     Gene gene1 = new IntegerGene(conf, 1, 100);
139     Gene gene2 = new IntegerGene(conf, 1, 100);
140     gene1.setAllele(new Integer JavaDoc(45));
141     gene2.setAllele(new Integer JavaDoc(46));
142     assertFalse(gene1.equals(gene2));
143     assertFalse(gene2.equals(gene1));
144   }
145
146   /**
147    * Uses subclass of IntegerGene.
148    * @throws Exception
149    *
150    * @author Klaus Meffert
151    * @since 2.4
152    */

153   public void testEquals_7()
154       throws Exception JavaDoc {
155     Gene gene1 = new IntegerGene(conf, 1, 100);
156     gene1.setAllele(new Integer JavaDoc(7));
157     Gene gene2 = new IntegerGene2(conf, 1, 100);
158     gene2.setAllele(new Integer JavaDoc(7));
159     assertFalse(gene1.equals(gene2));
160     assertFalse(gene2.equals(gene1));
161   }
162
163   /**
164    * Compare with application data set and option for comparation activated.
165    * @throws Exception
166    *
167    * @author Klaus Meffert
168    * @since 2.4
169    */

170   public void testEquals_8()
171       throws Exception JavaDoc {
172     BaseGene gene1 = new IntegerGene(conf, 1, 100);
173     gene1.setAllele(new Integer JavaDoc(7));
174     gene1.setApplicationData(new Integer JavaDoc(7));
175     gene1.setCompareApplicationData(true);
176     Gene gene2 = new IntegerGene(conf, 1, 100);
177     gene2.setApplicationData(new Integer JavaDoc(7));
178     gene2.setAllele(new Integer JavaDoc(7));
179     gene2.setCompareApplicationData(true);
180     assertTrue(gene1.equals(gene2));
181     assertTrue(gene2.equals(gene1));
182   }
183
184   /**
185    * Compare with application data set and option for comparation activated.
186    * @throws Exception
187    *
188    * @author Klaus Meffert
189    * @since 2.4
190    */

191   public void testEquals_9()
192       throws Exception JavaDoc {
193     BaseGene gene1 = new IntegerGene(conf, 1, 100);
194     gene1.setAllele(new Integer JavaDoc(7));
195     gene1.setApplicationData(new Integer JavaDoc(7));
196     gene1.setCompareApplicationData(true);
197     Gene gene2 = new IntegerGene(conf, 1, 100);
198     gene2.setCompareApplicationData(true);
199     gene2.setApplicationData(new Integer JavaDoc(7));
200     gene2.setAllele(new Integer JavaDoc(7));
201     assertTrue(gene1.equals(gene2));
202     assertTrue(gene2.equals(gene1));
203   }
204
205   /**
206    * Compare with application data set and option for comparation activated.
207    * @throws Exception
208    *
209    * @author Klaus Meffert
210    * @since 2.4
211    */

212   public void testEquals_9_2()
213       throws Exception JavaDoc {
214     BaseGene gene1 = new IntegerGene(conf, 1, 100);
215     gene1.setAllele(new Integer JavaDoc(8));
216     gene1.setApplicationData(new Integer JavaDoc(5));
217     gene1.setCompareApplicationData(true);
218     Gene gene2 = new IntegerGene(conf, 1, 100);
219     gene2.setCompareApplicationData(true);
220     gene2.setApplicationData(new Integer JavaDoc(7));
221     gene2.setAllele(new Integer JavaDoc(8));
222     assertFalse(gene1.equals(gene2));
223     assertFalse(gene2.equals(gene1));
224   }
225
226   /*
227    * @throws Exception
228
229    * @author Klaus Meffert
230    * @since 2.4
231    */

232   public void testEquals_10()
233       throws Exception JavaDoc {
234     Configuration conf = new ConfigurationForTest();
235     conf.setRandomGenerator(new RandomGeneratorForTest(5));
236     Gene gene1 = new IntegerGene(conf, 1, 100);
237     gene1.setAllele(new Integer JavaDoc(8));
238     Gene gene2 = new IntegerGene(conf, 1, 99);
239     gene2.setAllele(new Integer JavaDoc( -8));
240     assertFalse(gene1.equals(gene2));
241     assertFalse(gene2.equals(gene1));
242   }
243
244   /**
245    * Compare with application data set but option for comparation deactivated.
246    * @throws Exception
247    *
248    * @author Klaus Meffert
249    * @since 2.4
250    */

251   public void testEquals_9_3()
252       throws Exception JavaDoc {
253     BaseGene gene1 = new IntegerGene(conf, 1, 100);
254     gene1.setAllele(new Integer JavaDoc(8));
255     gene1.setApplicationData(new Integer JavaDoc(5));
256     gene1.setCompareApplicationData(false);
257     Gene gene2 = new IntegerGene(conf, 1, 100);
258     gene2.setCompareApplicationData(false);
259     gene2.setApplicationData(new Integer JavaDoc(7));
260     gene2.setAllele(new Integer JavaDoc(8));
261     assertTrue(gene1.equals(gene2));
262     assertTrue(gene2.equals(gene1));
263   }
264
265   public void testIntValue_0()
266       throws Exception JavaDoc {
267     IntegerGene gene1 = new IntegerGene(conf, 1, 10000);
268     gene1.setAllele(new Integer JavaDoc(4711));
269     assertEquals(4711, gene1.intValue());
270   }
271
272   public void testIntValue_1()
273       throws Exception JavaDoc {
274     IntegerGene gene1 = new IntegerGene(conf, 1, 10000);
275     gene1.setAllele(null);
276     try {
277       assertEquals(0, gene1.intValue());
278       fail();
279     } catch (NullPointerException JavaDoc nullex) {
280       ; //this is OK
281
}
282   }
283
284   /**
285    * Set Allele to null, no exception should occur.
286    * @throws Exception
287    */

288   public void testSetAllele_0()
289       throws Exception JavaDoc {
290     Gene gene1 = new IntegerGene(conf, 1, 10000);
291     gene1.setAllele(null);
292   }
293
294   public void testSetAllele_1()
295       throws Exception JavaDoc {
296     Gene gene1 = new IntegerGene(conf, 1, 10000);
297     try {
298       gene1.setAllele("22");
299       fail();
300     } catch (ClassCastException JavaDoc classex) {
301       ; //this is OK
302
}
303   }
304
305   public void testNewGene_0()
306       throws Exception JavaDoc {
307     IntegerGene gene1 = new IntegerGene(conf, 1, 10000);
308     IGeneConstraintChecker checker = new GeneConstraintChecker();
309     gene1.setConstraintChecker(checker);
310     gene1.setAllele(new Integer JavaDoc(4711));
311     int lower1 = gene1.getLowerBounds();
312     int upper1 = gene1.getUpperBounds();
313     IntegerGene gene2 = (IntegerGene) gene1.newGene();
314     int lower2 = gene2.getLowerBounds();
315     int upper2 = gene2.getUpperBounds();
316     assertEquals(lower1, lower2);
317     assertEquals(upper1, upper2);
318     assertEquals(checker, gene2.getConstraintChecker());
319   }
320
321   public void testCleanup()
322       throws Exception JavaDoc {
323     //cleanup should do nothing!
324
Gene gene = new IntegerGene(conf, 1, 6);
325     Gene copy = gene.newGene();
326     gene.cleanup();
327     assertEquals(copy, gene);
328   }
329
330   public void testPersistentRepresentation_0()
331       throws Exception JavaDoc {
332     Gene gene1 = new IntegerGene(conf, 2, 753);
333     gene1.setAllele(new Integer JavaDoc(45));
334     String JavaDoc pres1 = gene1.getPersistentRepresentation();
335     Gene gene2 = new IntegerGene(conf);
336     gene2.setValueFromPersistentRepresentation(pres1);
337     String JavaDoc pres2 = gene2.getPersistentRepresentation();
338     assertEquals(pres1, pres2);
339   }
340
341   /**
342    * Should be possible without exception.
343    * @throws Exception
344    *
345    * @author Klaus Meffert
346    * @since 2.2
347    */

348   public void testPersistentRepresentation_1()
349       throws Exception JavaDoc {
350     Gene gene1 = new IntegerGene(conf, 2, 753);
351     gene1.setAllele(new Integer JavaDoc(45));
352     gene1.setValueFromPersistentRepresentation(null);
353   }
354
355   /**
356    * @throws Exception
357    *
358    * @author Klaus Meffert
359    * @since 2.2
360    */

361   public void testPersistentRepresentation_2()
362       throws Exception JavaDoc {
363     IntegerGene gene1 = new IntegerGene(conf, 2, 753);
364     gene1.setAllele(new Integer JavaDoc(45));
365     gene1.setValueFromPersistentRepresentation("2"
366         + IntegerGene.
367         PERSISTENT_FIELD_DELIMITER
368         + "3"
369         + IntegerGene.
370         PERSISTENT_FIELD_DELIMITER
371         + "4");
372     assertEquals(2, ( (Integer JavaDoc) gene1.getAllele()).intValue());
373     assertEquals(3, gene1.getLowerBounds());
374     assertEquals(4, gene1.getUpperBounds());
375   }
376
377   /**
378    * @throws Exception
379    *
380    * @author Klaus Meffert
381    * @since 2.2
382    */

383   public void testPersistentRepresentation_3()
384       throws Exception JavaDoc {
385     IntegerGene gene1 = new IntegerGene(conf, 2, 753);
386     gene1.setAllele(new Integer JavaDoc(45));
387     gene1.setValueFromPersistentRepresentation("null"
388         + IntegerGene.
389         PERSISTENT_FIELD_DELIMITER
390         + "-3"
391         + IntegerGene.
392         PERSISTENT_FIELD_DELIMITER
393         + "4");
394     assertNull(gene1.getAllele());
395     assertEquals( -3, gene1.getLowerBounds());
396     assertEquals(4, gene1.getUpperBounds());
397   }
398
399   /**
400    * @throws Exception
401    *
402    * @author Klaus Meffert
403    * @since 2.2
404    */

405   public void testPersistentRepresentation_4()
406       throws Exception JavaDoc {
407     Gene gene1 = new IntegerGene(conf, 2, 753);
408     gene1.setAllele(new Integer JavaDoc(45));
409     try {
410       gene1.setValueFromPersistentRepresentation("null"
411           + IntegerGene.
412           PERSISTENT_FIELD_DELIMITER
413           + "3.5"
414           + IntegerGene.
415           PERSISTENT_FIELD_DELIMITER
416           + "4");
417       fail();
418     } catch (UnsupportedRepresentationException uex) {
419       ; //this is OK
420
}
421   }
422
423   /**
424    * @throws Exception
425    *
426    * @author Klaus Meffert
427    * @since 2.2
428    */

429   public void testPersistentRepresentation_5()
430       throws Exception JavaDoc {
431     Gene gene1 = new IntegerGene(conf, 2, 753);
432     gene1.setAllele(new Integer JavaDoc(45));
433     try {
434       gene1.setValueFromPersistentRepresentation("null"
435           + IntegerGene.
436           PERSISTENT_FIELD_DELIMITER
437           + "3"
438           + IntegerGene.
439           PERSISTENT_FIELD_DELIMITER
440           + "a");
441       fail();
442     } catch (UnsupportedRepresentationException uex) {
443       ; //this is OK
444
}
445   }
446
447   /**
448    * Constructed IntegerGene without user initialization.
449    * @throws Exception
450    *
451    * @author Klaus Meffert
452    * @since 2.6
453    */

454   public void testPersistentRepresentation_6()
455       throws Exception JavaDoc {
456     IntegerGene gene1 = new IntegerGene(conf);
457     assertEquals("null" + IntegerGene.PERSISTENT_FIELD_DELIMITER
458                  + Integer.MIN_VALUE + IntegerGene.PERSISTENT_FIELD_DELIMITER
459                  + Integer.MAX_VALUE,
460                  gene1.getPersistentRepresentation());
461   }
462
463   /**
464    * @throws Exception
465    *
466    * @author Klaus Meffert
467    * @since 2.2
468    */

469   public void testCompareToNative_0()
470       throws Exception JavaDoc {
471     Gene gene1 = new IntegerGene(conf, 13, 65);
472     gene1.setAllele(new Integer JavaDoc(58));
473     Gene gene2 = new IntegerGene(conf, 53, 67);
474     gene2.setAllele(new Integer JavaDoc(59));
475     assertEquals( ( (Integer JavaDoc) gene1.getAllele()).compareTo( (Integer JavaDoc) gene2.
476         getAllele()), gene1.compareTo(gene2));
477   }
478
479   /**
480    * @throws Exception
481    *
482    * @author Klaus Meffert
483    * @since 2.2
484    */

485   public void testCompareToNative_1()
486       throws Exception JavaDoc {
487     Gene gene1 = new IntegerGene(conf, 13, 65);
488     gene1.setAllele(new Integer JavaDoc(58));
489     Gene gene2 = new IntegerGene(conf, 53, 67);
490     gene2.setAllele(new Integer JavaDoc(58));
491     assertEquals( ( (Integer JavaDoc) gene1.getAllele()).compareTo( (Integer JavaDoc) gene2.
492         getAllele()), gene1.compareTo(gene2));
493   }
494
495   /**
496    * @throws Exception
497    *
498    * @author Klaus Meffert
499    * @since 2.2
500    */

501   public void testCompareToNative_2()
502       throws Exception JavaDoc {
503     Gene gene1 = new IntegerGene(conf, 13, 65);
504     gene1.setAllele(new Integer JavaDoc(59));
505     Gene gene2 = new IntegerGene(conf, 53, 67);
506     gene2.setAllele(new Integer JavaDoc(58));
507     assertEquals( ( (Integer JavaDoc) gene1.getAllele()).compareTo( (Integer JavaDoc) gene2.
508         getAllele()), gene1.compareTo(gene2));
509   }
510
511   /**
512    * @throws Exception
513    *
514    * @author Klaus Meffert
515    * @since 2.2
516    */

517   public void testCompareToNative_3()
518       throws Exception JavaDoc {
519     Configuration conf = new ConfigurationForTest();
520     Gene gene1 = new IntegerGene(conf, 13, 65);
521     gene1.setAllele(new Integer JavaDoc(59));
522     Gene gene2 = new IntegerGene(conf, 53, 67);
523     gene2.setAllele(new Integer JavaDoc( -59));
524     assertEquals( ( (Integer JavaDoc) gene1.getAllele()).compareTo( (Integer JavaDoc) gene2.
525         getAllele()), gene1.compareTo(gene2));
526   }
527
528   /**
529    * @throws Exception
530    *
531    * @author Klaus Meffert
532    * @since 2.2
533    */

534   public void testCompareToNative_4()
535       throws Exception JavaDoc {
536     Configuration conf = new ConfigurationForTest();
537     Gene gene1 = new IntegerGene(conf, 13, 65);
538     gene1.setAllele(new Integer JavaDoc(0));
539     Gene gene2 = new IntegerGene(conf, 53, 67);
540     gene2.setAllele(new Integer JavaDoc( -0));
541     assertEquals( ( (Integer JavaDoc) gene1.getAllele()).compareTo( (Integer JavaDoc) gene2.
542         getAllele()), gene1.compareTo(gene2));
543   }
544
545   /**
546    * @throws Exception
547    *
548    * @author Klaus Meffert
549    * @since 2.4
550    */

551   public void testCompareTo_0()
552       throws Exception JavaDoc {
553     Configuration conf = new ConfigurationForTest();
554     Gene gene1 = new IntegerGene(conf, 13, 65);
555     gene1.setAllele(new Integer JavaDoc(0));
556     Gene gene2 = new IntegerGene2(conf, 53, 67);
557     gene2.setAllele(new Integer JavaDoc( -0));
558     try {
559       gene1.compareTo(gene2);
560       fail();
561     } catch (ClassCastException JavaDoc cex) {
562       ; //this is OK
563
}
564   }
565
566   /**
567    * @throws Exception
568    *
569    * @author Klaus Meffert
570    * @since 2.2
571    */

572   public void testCompareTo_1()
573       throws Exception JavaDoc {
574     Gene gene1 = new IntegerGene(conf, 13, 65);
575     gene1.setAllele(new Integer JavaDoc(58));
576     Gene gene2 = new IntegerGene(conf, 53, 67);
577     gene2.setAllele(new Integer JavaDoc(59));
578     assertEquals( -1, gene1.compareTo(gene2));
579   }
580
581   /**
582    * @throws Exception
583    *
584    * @author Klaus Meffert
585    * @since 2.2
586    */

587   public void testCompareTo_2()
588       throws Exception JavaDoc {
589     Gene gene1 = new IntegerGene(conf, 13, 65);
590     gene1.setAllele(new Integer JavaDoc(58));
591     Gene gene2 = new IntegerGene(conf, 53, 67);
592     gene2.setAllele(new Integer JavaDoc(58));
593     assertEquals(0, gene1.compareTo(gene2));
594   }
595
596   /**
597    * @throws Exception
598    *
599    * @author Klaus Meffert
600    * @since 2.2
601    */

602   public void testCompareTo_3()
603       throws Exception JavaDoc {
604     Gene gene1 = new IntegerGene(conf, 13, 65);
605     gene1.setAllele(new Integer JavaDoc(59));
606     Gene gene2 = new IntegerGene(conf, 53, 67);
607     gene2.setAllele(new Integer JavaDoc(58));
608     assertEquals(1, gene1.compareTo(gene2));
609     assertEquals( -1, gene2.compareTo(gene1));
610   }
611
612   /**
613    * @throws Exception
614    *
615    * @author Klaus Meffert
616    * @since 2.2
617    */

618   public void testCompareTo_4()
619       throws Exception JavaDoc {
620     Gene gene1 = new IntegerGene(conf, 13, 65);
621     Gene gene2 = new IntegerGene(conf, 53, 67);
622     assertEquals(0, gene1.compareTo(gene2));
623     assertEquals(0, gene2.compareTo(gene1));
624   }
625
626   /**
627    * @throws Exception
628    *
629    * @author Klaus Meffert
630    * @since 2.2
631    */

632   public void testApplyMutation_0()
633       throws Exception JavaDoc {
634     IntegerGene gene = new IntegerGene(conf, 0, 100);
635     gene.setAllele(new Integer JavaDoc(50));
636     gene.applyMutation(0, 0.0d);
637     assertEquals(50, gene.intValue());
638   }
639
640   /**
641    * @throws Exception
642    *
643    * @author Klaus Meffert
644    * @since 2.2
645    */

646   public void testApplyMutation_1()
647       throws Exception JavaDoc {
648     conf.setRandomGenerator(new RandomGeneratorForTest(15));
649     IntegerGene gene = new IntegerGene(conf, 0, 100);
650     gene.setAllele(new Integer JavaDoc(50));
651     gene.applyMutation(0, 0.5d);
652     assertEquals(Math.round(50 + (100 - 0) * 0.5d), gene.intValue());
653   }
654
655   /**
656    * @throws Exception
657    *
658    * @author Klaus Meffert
659    * @since 2.2
660    */

661   public void testApplyMutation_2()
662       throws Exception JavaDoc {
663     conf.setRandomGenerator(new RandomGeneratorForTest(15));
664     IntegerGene gene = new IntegerGene(conf, 44, 100);
665     gene.setAllele(new Integer JavaDoc(50));
666     gene.applyMutation(0, 0.3d);
667     assertEquals(Math.round(50 + (100 - 44) * 0.3d), gene.intValue());
668   }
669
670   /**
671    * @throws Exception
672    *
673    * @author Klaus Meffert
674    * @since 2.2
675    */

676   public void testApplyMutation_3()
677       throws Exception JavaDoc {
678     conf.setRandomGenerator(new RandomGeneratorForTest(15));
679     IntegerGene gene = new IntegerGene(conf, 33, 100);
680     gene.setAllele(new Integer JavaDoc(50));
681     gene.applyMutation(0, 1.9d);
682     assertEquals(Math.round(33 + 15), gene.intValue());
683   }
684
685   /**
686    * @throws Exception
687    *
688    * @author Klaus Meffert
689    * @since 2.2
690    */

691   public void testApplyMutation_4()
692       throws Exception JavaDoc {
693     conf.setRandomGenerator(new RandomGeneratorForTest(15));
694     IntegerGene gene = new IntegerGene(conf, 2, 100);
695     gene.setAllele(new Integer JavaDoc(60));
696     gene.applyMutation(0, 1.9d);
697     assertEquals(Math.round(2 + 15), gene.intValue());
698   }
699
700   /**
701    * @throws Exception
702    *
703    * @author Klaus Meffert
704    * @since 2.2
705    */

706   public void testApplyMutation_5()
707       throws Exception JavaDoc {
708     conf.setRandomGenerator(new RandomGeneratorForTest(15));
709     IntegerGene gene = new IntegerGene(conf, 0, 100);
710     gene.setAllele(new Integer JavaDoc(60));
711     gene.applyMutation(0, -1.0d);
712     assertEquals(Math.round(0 + 15), gene.intValue());
713   }
714
715   /**
716    * @throws Exception
717    *
718    * @author Klaus Meffert
719    * @since 2.2
720    */

721   public void testApplyMutation_6()
722       throws Exception JavaDoc {
723     IntegerGene gene = new IntegerGene(conf, 0, 100);
724     gene.setAllele(new Integer JavaDoc(60));
725     gene.applyMutation(0, -0.4d);
726     assertEquals(Math.round(60 + (100 * ( -0.4d))), gene.intValue());
727   }
728
729   /**
730    * @throws Exception
731    *
732    * @author Klaus Meffert
733    * @since 2.6
734    */

735   public void testApplyMutation_7()
736       throws Exception JavaDoc {
737     IntegerGene gene = new IntegerGene(conf, 0, 100);
738     gene.setAllele(null);
739     gene.applyMutation(0, 0.4d);
740     assertEquals(Math.round( (100 * (0.4d))), gene.intValue());
741   }
742
743   /**
744    * @throws Exception
745    *
746    * @author Klaus Meffert
747    * @since 2.6
748    */

749   public void testApplyMutation_8()
750       throws Exception JavaDoc {
751     IntegerGene gene = new IntegerGene(conf, 5, 100);
752     gene.setAllele(null);
753     conf.setRandomGenerator(new RandomGeneratorForTest(10));
754     gene.applyMutation(0, -0.4d);
755     assertEquals(Math.round(10 + 5), gene.intValue());
756   }
757
758   /**
759    * @throws Exception
760    *
761    * @author Klaus Meffert
762    * @since 2.2
763    */

764   public void testSetToRandomValue_0()
765       throws Exception JavaDoc {
766     Gene gene = new IntegerGene(conf, 1, 6);
767     gene.setAllele(new Integer JavaDoc(5));
768     gene.setToRandomValue(new RandomGeneratorForTest(0.2d));
769     assertEquals(new Integer JavaDoc( (int) (0.2d * (6 - 1) + 1)), gene.getAllele());
770   }
771
772   /**
773    * @throws Exception
774    *
775    * @author Klaus Meffert
776    * @since 2.2
777    */

778   public void testSetToRandomValue_1()
779       throws Exception JavaDoc {
780     Gene gene = new IntegerGene(conf, -1, 7);
781     gene.setAllele(new Integer JavaDoc(4));
782     gene.setToRandomValue(new RandomGeneratorForTest(0.3d));
783     assertEquals(new Integer JavaDoc( (int) (0.3d * (7 + 1) - 1)), gene.getAllele());
784   }
785
786   /**
787    * @throws Exception
788    *
789    * @author Klaus Meffert
790    * @since 2.2
791    */

792   public void testSetToRandomValue_2()
793       throws Exception JavaDoc {
794     Configuration conf = new ConfigurationForTest();
795     Gene gene = new IntegerGene(conf, -2, -1);
796     gene.setAllele(new Integer JavaDoc(4));
797     gene.setToRandomValue(new RandomGeneratorForTest(0.8d));
798     assertEquals(new Integer JavaDoc( (int) (0.8d * ( -1 + 2) - 2)), gene.getAllele());
799   }
800
801   /**
802    * @throws Exception
803    *
804    * @author Klaus Meffert
805    * @since 2.2
806    */

807   public void testSetToRandomValue_3()
808       throws Exception JavaDoc {
809     IntegerGene gene = new IntegerGene(conf, 0, 8);
810     gene.setAllele(new Integer JavaDoc(5));
811     gene.setToRandomValue(new RandomGeneratorForTest(4));
812     if (gene.intValue() < 0
813         || gene.intValue() > 8) {
814       fail();
815     }
816   }
817
818   /**
819    * @throws Exception
820    *
821    * @author Klaus Meffert
822    * @since 2.2
823    */

824   public void testSetToRandomValue_4()
825       throws Exception JavaDoc {
826     IntegerGene gene = new IntegerGene(conf, 1, 6);
827     gene.setAllele(new Integer JavaDoc(2));
828     gene.setToRandomValue(new RandomGeneratorForTest(3));
829     if (gene.intValue() < 1
830         || gene.intValue() > 6) {
831       fail();
832     }
833   }
834
835   /**
836    * Exposing bug #1357474.
837    * @throws Exception
838    *
839    * @author Klaus Meffert
840    * @since 2.5
841    */

842   public void testSetToRandomValue_5()
843       throws Exception JavaDoc {
844     Configuration conf = new ConfigurationForTest();
845     Gene gene = new IntegerGene(conf, 1, 3);
846     gene.setAllele(new Integer JavaDoc(4));
847     gene.setToRandomValue(new RandomGeneratorForTest(0.95d));
848     assertEquals(new Integer JavaDoc(3), gene.getAllele());
849     gene.setToRandomValue(new RandomGeneratorForTest(0.05d));
850     assertEquals(new Integer JavaDoc(1), gene.getAllele());
851   }
852
853   /**
854    * @throws Exception
855    *
856    * @author Klaus Meffert
857    * @since 2.4
858    */

859   public void testSetEnergy_0()
860       throws Exception JavaDoc {
861     BaseGene gene = new IntegerGene(conf);
862     assertEquals(0.0, gene.getEnergy(), DELTA);
863   }
864
865   /**
866    * @throws Exception
867    *
868    * @author Klaus Meffert
869    * @since 2.4
870    */

871   public void testSetEnergy_1()
872       throws Exception JavaDoc {
873     BaseGene gene = new IntegerGene(conf);
874     gene.setEnergy(2.3);
875     assertEquals(2.3, gene.getEnergy(), DELTA);
876     gene.setEnergy( -55.8);
877     assertEquals( -55.8, gene.getEnergy(), DELTA);
878     gene.setEnergy(0.5);
879     gene.setEnergy(0.8);
880     assertEquals(0.8, gene.getEnergy(), DELTA);
881   }
882
883   /**
884    * Descendent of IntegerGene being virtually the same but of a different
885    * class so that equals and compareTo should signal a difference.
886    *
887    * @author Klaus Meffert
888    * @since 2.4
889    */

890   class IntegerGene2
891       extends IntegerGene {
892     public IntegerGene2()
893         throws InvalidConfigurationException {
894       super(Genotype.getStaticConfiguration());
895     }
896
897     public IntegerGene2(final Configuration a_conf, int a_lowerBounds,
898                         int a_upperBounds)
899         throws InvalidConfigurationException {
900       super(a_conf, a_lowerBounds, a_upperBounds);
901     }
902   }
903   class GeneConstraintChecker
904       implements IGeneConstraintChecker {
905     public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
906                           IChromosome a_chrom, int a_index) {
907       return true;
908     }
909   }
910   /**
911    * @throws Exception
912    *
913    * @author Klaus Meffert
914    * @since 2.6
915    */

916   public void testGetBounds_0()
917       throws Exception JavaDoc {
918     IntegerGene gene = new IntegerGene(conf, 2, 5);
919     assertEquals(2, gene.getLowerBounds());
920     assertEquals(5, gene.getUpperBounds());
921   }
922 }
923
Popular Tags