KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

41   public void testConstruct_0()
42       throws Exception JavaDoc {
43     Configuration conf = new ConfigurationForTest();
44     Gene gene = new DoubleGene(conf, 1.1d, 100.0d);
45     //following should be possible without exception
46
gene.setAllele(new Double JavaDoc(101.1d));
47   }
48
49   /**
50    * @throws Exception
51    *
52    * @author Klaus Meffert
53    */

54   public void testConstruct_1()
55       throws Exception JavaDoc {
56     Gene gene = new DoubleGene(conf);
57     gene.setAllele(new Double JavaDoc(Double.MAX_VALUE));
58   }
59
60   /**
61    * @throws Exception
62    *
63    * @author Klaus Meffert
64    */

65   public void testConstruct_2()
66       throws Exception JavaDoc {
67     Gene gene = new DoubleGene(conf);
68     gene.setAllele(new Double JavaDoc( - (Double.MAX_VALUE / 2)));
69   }
70
71   /**
72    * @throws Exception
73    *
74    * @author Klaus Meffert
75    * @since 3.1
76    */

77   public void testConstruct_3()
78       throws Exception JavaDoc {
79     Genotype.setStaticConfiguration(conf);
80     Gene gene = new DoubleGene();
81     assertSame(conf, gene.getConfiguration());
82   }
83
84   /**
85    * @throws Exception
86    *
87    * @author Klaus Meffert
88    */

89   public void testToString_0()
90       throws Exception JavaDoc {
91     Gene gene = new DoubleGene(conf, 1.2d, 99.7d);
92     gene.setAllele(new Double JavaDoc(47.3d));
93     assertEquals("DoubleGene(1.2,99.7)=47.3", gene.toString());
94   }
95
96   /**
97    * @throws Exception
98    *
99    * @author Klaus Meffert
100    */

101   public void testToString_1()
102       throws Exception JavaDoc {
103     Gene gene = new DoubleGene(conf, -100.0d, 100.0d);
104     gene.setAllele(new Double JavaDoc( -88.75286d));
105     assertEquals("DoubleGene(-100.0,100.0)=-88.75286", gene.toString());
106   }
107
108   /**
109    * @throws Exception
110    *
111    * @author Klaus Meffert
112    * @since 2.6
113    */

114   public void testToString_2()
115       throws Exception JavaDoc {
116     Gene gene = new DoubleGene(conf, 1.2d, 99.7d);
117     assertEquals("DoubleGene(1.2,99.7)=null", gene.toString());
118   }
119
120   /**
121    * @throws Exception
122    *
123    * @author Klaus Meffert
124    */

125   public void testGetAllele_0()
126       throws Exception JavaDoc {
127     Gene gene = new DoubleGene(conf, 1.9d, 100.4d);
128     gene.setAllele(new Double JavaDoc(33.0d));
129     assertEquals(new Double JavaDoc(33.0d), gene.getAllele());
130   }
131
132   /**
133    * @throws Exception
134    *
135    * @author Klaus Meffert
136    */

137   public void testGetAllele_1()
138       throws Exception JavaDoc {
139     Gene gene = new DoubleGene(conf, 1.8d, 100.1d);
140     gene.setAllele(new Double JavaDoc(1.9d));
141     assertEquals(new Double JavaDoc(1.9d), gene.getAllele());
142   }
143
144   /**
145    * @throws Exception
146    *
147    * @author Klaus Meffert
148    */

149   public void testGetAllele_2()
150       throws Exception JavaDoc {
151     Gene gene = new DoubleGene(conf, 1.0d, 100.0d);
152     gene.setAllele(new Double JavaDoc(100.0d));
153     assertEquals(new Double JavaDoc(100.0d), gene.getAllele());
154   }
155
156   /**
157    * @throws Exception
158    *
159    * @author Klaus Meffert
160    */

161   public void testEquals_0()
162       throws Exception JavaDoc {
163     Gene gene1 = new DoubleGene(conf, 1.1d, 100.2d);
164     Gene gene2 = new DoubleGene(conf, 1.1d, 100.2d);
165     assertTrue(gene1.equals(gene2));
166     assertTrue(gene2.equals(gene1));
167   }
168
169   /**
170    * @throws Exception
171    *
172    * @author Klaus Meffert
173    */

174   public void testEquals_1()
175       throws Exception JavaDoc {
176     Gene gene1 = new DoubleGene(conf, 1.9d, 100.4d);
177     assertFalse(gene1.equals(null));
178   }
179
180   /**
181    * @throws Exception
182    *
183    * @author Klaus Meffert
184    */

185   public void testEquals_2()
186       throws Exception JavaDoc {
187     Gene gene1 = new DoubleGene(conf, 11.2d, 100.7d);
188     assertFalse(gene1.equals(new BooleanGene(conf)));
189   }
190
191   /**
192    * @throws Exception
193    *
194    * @author Klaus Meffert
195    */

196   public void testEquals_3()
197       throws Exception JavaDoc {
198     Gene gene1 = new DoubleGene(conf, 1.0d, 100.7d);
199     assertFalse(gene1.equals(new Vector()));
200   }
201
202   /**
203    * @throws Exception
204    *
205    * @author Klaus Meffert
206    */

207   public void testEquals_4()
208       throws Exception JavaDoc {
209     Gene gene1 = new DoubleGene(conf, 1.2d, 100.3d);
210     Gene gene2 = new DoubleGene(conf, 1.2d, 99.5d);
211     assertTrue(gene1.equals(gene2));
212     assertTrue(gene2.equals(gene1));
213   }
214
215   /**
216    * @throws Exception
217    *
218    * @author Klaus Meffert
219    */

220   public void testEquals_5()
221       throws Exception JavaDoc {
222     Gene gene1 = new FixedBinaryGene(conf, 5);
223     Gene gene2 = new DoubleGene(conf, 1, 99);
224     assertFalse(gene1.equals(gene2));
225     assertFalse(gene2.equals(gene1));
226   }
227
228   /**
229    * @throws Exception
230    *
231    * @author Klaus Meffert
232    */

233   public void testEquals_6()
234       throws Exception JavaDoc {
235     Gene gene1 = new DoubleGene(conf, 1, 99);
236     Gene gene2 = new BooleanGene(conf);
237     assertFalse(gene1.equals(gene2));
238     assertFalse(gene2.equals(gene1));
239   }
240
241   /**
242    * @throws Exception
243    *
244    * @author Klaus Meffert
245    */

246   public void testEquals_7()
247       throws Exception JavaDoc {
248     Gene gene1 = new DoubleGene(conf, 1, 99);
249     Gene gene2 = new IntegerGene(conf);
250     assertFalse(gene1.equals(gene2));
251     assertFalse(gene2.equals(gene1));
252   }
253
254   /**
255    * @throws Exception
256    *
257    * @author Klaus Meffert
258    * @since 2.4
259    */

260   public void testEquals_8()
261       throws Exception JavaDoc {
262     Configuration conf = new ConfigurationForTest();
263     Gene gene1 = new DoubleGene(conf, 1.2d, 100.3d);
264     gene1.setAllele(new Double JavaDoc(1));
265     Gene gene2 = new DoubleGene(conf, 1.2d, 99.5d);
266     gene2.setAllele(new Double JavaDoc( -1));
267     assertFalse(gene1.equals(gene2));
268     assertFalse(gene2.equals(gene1));
269   }
270
271   /**
272    * @throws Exception
273    *
274    * @author Klaus Meffert
275    */

276   public void testDoubleValue_0()
277       throws Exception JavaDoc {
278     DoubleGene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
279     gene1.setAllele(new Double JavaDoc(4711.0d));
280     assertEquals(4711.0d, gene1.doubleValue(), DELTA);
281   }
282
283   /**
284    * @throws Exception
285    *
286    * @author Klaus Meffert
287    */

288   public void testDoubleValue_1()
289       throws Exception JavaDoc {
290     DoubleGene gene1 = new DoubleGene(conf, 1.765d, 10000.0d);
291     gene1.setAllele(null);
292     try {
293       assertEquals(0.0d, gene1.doubleValue(), DELTA);
294       fail();
295     }
296     catch (NullPointerException JavaDoc nullex) {
297       ; //this is OK
298
}
299   }
300
301   /**
302    * Set allele to null, no exception should occur.
303    * @throws Exception
304    *
305    * @author Klaus Meffert
306    */

307   public void testSetAllele_0()
308       throws Exception JavaDoc {
309     Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
310     gene1.setAllele(null);
311   }
312
313   /**
314    * @throws Exception
315    *
316    * @author Klaus Meffert
317    */

318   public void testSetAllele_1()
319       throws Exception JavaDoc {
320     Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
321     try {
322       gene1.setAllele("22");
323       fail();
324     }
325     catch (ClassCastException JavaDoc classex) {
326       ; //this is OK
327
}
328   }
329
330   /**
331    * @throws Exception
332    *
333    * @author Klaus Meffert
334    */

335   public void testSetAllele_2()
336       throws Exception JavaDoc {
337     Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
338     try {
339       gene1.setAllele(new Integer JavaDoc(22));
340       fail();
341     }
342     catch (ClassCastException JavaDoc classex) {
343       ; //this is OK
344
}
345   }
346
347   /**
348    * Call setAllele with need of mapping to bounds.
349    * @throws Exception
350    *
351    * @author Klaus Meffert
352    * @since 2.6
353    */

354   public void testSetAllele_3()
355       throws Exception JavaDoc {
356     Gene gene1 = new DoubleGene(conf, 1.0d, 1000.0d);
357     gene1.setAllele(new Double JavaDoc(2000.0d));
358   }
359
360   /**
361    * @throws Exception
362    *
363    * @author Klaus Meffert
364    */

365   public void testNewGene_0()
366       throws Exception JavaDoc {
367     DoubleGene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
368     IGeneConstraintChecker checker = new GeneConstraintChecker();
369     gene1.setConstraintChecker(checker);
370     gene1.setAllele(new Double JavaDoc(4711.0d));
371     Double JavaDoc lower1 = (Double JavaDoc) privateAccessor.getField(gene1,
372         "m_lowerBound");
373     Double JavaDoc upper1 = (Double JavaDoc) privateAccessor.getField(gene1,
374         "m_upperBound");
375     DoubleGene gene2 = (DoubleGene) gene1.newGene();
376     Double JavaDoc lower2 = (Double JavaDoc) privateAccessor.getField(gene2,
377         "m_lowerBound");
378     Double JavaDoc upper2 = (Double JavaDoc) privateAccessor.getField(gene2,
379         "m_upperBound");
380     assertEquals(lower1, lower2);
381     assertEquals(upper1, upper2);
382     assertEquals(checker, gene2.getConstraintChecker());
383   }
384
385   /**
386    *
387    * @throws Exception
388    * @author Klaus Meffert
389    */

390   public void testPersistentRepresentation_0()
391       throws Exception JavaDoc {
392     Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
393     gene1.setAllele(new Double JavaDoc(4.5d));
394     String JavaDoc pres1 = gene1.getPersistentRepresentation();
395     Gene gene2 = new DoubleGene(conf);
396     gene2.setValueFromPersistentRepresentation(pres1);
397     String JavaDoc pres2 = gene2.getPersistentRepresentation();
398     assertEquals(pres1, pres2);
399   }
400
401   /**
402    *
403    * @throws Exception
404    * @author Klaus Meffert
405    */

406   public void testPersistentRepresentation_1()
407       throws Exception JavaDoc {
408     Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
409     gene1.setValueFromPersistentRepresentation(null);
410   }
411
412   /**
413    *
414    * @throws Exception
415    * @author Klaus Meffert
416    */

417   public void testPersistentRepresentation_2()
418       throws Exception JavaDoc {
419     Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
420     try {
421       gene1.setValueFromPersistentRepresentation("2.3");
422       fail();
423     }
424     catch (UnsupportedRepresentationException uex) {
425       ; //this is OK
426
}
427   }
428
429   /**
430    *
431    * @throws Exception
432    * @author Klaus Meffert
433    */

434   public void testPersistentRepresentation_3()
435       throws Exception JavaDoc {
436     Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
437     try {
438       gene1.setValueFromPersistentRepresentation("2.3"
439                                                  + DoubleGene.
440                                                  PERSISTENT_FIELD_DELIMITER
441                                                  + "4.6"
442                                                  + DoubleGene.
443                                                  PERSISTENT_FIELD_DELIMITER
444                                                  + "6,5");
445       fail();
446     }
447     catch (UnsupportedRepresentationException uex) {
448       ; //this is OK
449
}
450   }
451
452   /**
453    *
454    * @throws Exception
455    * @author Klaus Meffert
456    */

457   public void testPersistentRepresentation_4()
458       throws Exception JavaDoc {
459     Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
460     try {
461       gene1.setValueFromPersistentRepresentation("2.3"
462                                                  + DoubleGene.
463                                                  PERSISTENT_FIELD_DELIMITER
464                                                  + "b"
465                                                  + DoubleGene.
466                                                  PERSISTENT_FIELD_DELIMITER
467                                                  + "a");
468       fail();
469     }
470     catch (UnsupportedRepresentationException uex) {
471       ; //this is OK
472
}
473   }
474
475   /**
476    *
477    * @throws Exception
478    * @author Klaus Meffert
479    */

480   public void testPersistentRepresentation_5()
481       throws Exception JavaDoc {
482     Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
483     try {
484       gene1.setValueFromPersistentRepresentation("a"
485                                                  + DoubleGene.
486                                                  PERSISTENT_FIELD_DELIMITER
487                                                  + "b"
488                                                  + DoubleGene.
489                                                  PERSISTENT_FIELD_DELIMITER
490                                                  + "a");
491       fail();
492     }
493     catch (UnsupportedRepresentationException uex) {
494       ; //this is OK
495
}
496   }
497
498   /**
499    * @throws Exception
500    *
501    * @author Klaus Meffert
502    */

503   public void testCleanup_0()
504       throws Exception JavaDoc {
505     //cleanup should do nothing!
506
Gene gene = new DoubleGene(conf, 1.3d, 6.5d);
507     Gene copy = gene.newGene();
508     gene.cleanup();
509     assertEquals(copy, gene);
510   }
511
512   /**
513    * @throws Exception
514    *
515    * @author Klaus Meffert
516    */

517   public void testSetToRandomValue_0()
518       throws Exception JavaDoc {
519     Gene gene = new DoubleGene(conf, 1.3d, 6.5d);
520     gene.setAllele(new Double JavaDoc(5.8d));
521     gene.setToRandomValue(new RandomGeneratorForTest(0.789d));
522     assertEquals(new Double JavaDoc(0.789d * (6.5d - 1.3d) + 1.3d), gene.getAllele());
523   }
524
525   /**
526    * @throws Exception
527    *
528    * @author Klaus Meffert
529    */

530   public void testSetToRandomValue_1()
531       throws Exception JavaDoc {
532     Gene gene = new DoubleGene(conf, -1.3d, 6.5d);
533     gene.setAllele(new Double JavaDoc(5.8d));
534     conf.setRandomGenerator(new RandomGeneratorForTest(0.258d));
535     gene.setToRandomValue(new RandomGeneratorForTest(0.014));
536     assertEquals(new Double JavaDoc(0.014d * (6.5d + 1.3d) - 1.3d), gene.getAllele());
537   }
538
539   /**
540    *
541    * @throws Exception
542    * @author Klaus Meffert
543    */

544   public void testSetToRandomValue_2()
545       throws Exception JavaDoc {
546     /**@todo test needed any longer?*/
547     Configuration conf = new ConfigurationForTest();
548     Gene gene = new DoubleGene(conf, -1.3d, -0.5d);
549     gene.setAllele(new Double JavaDoc(5.8d));
550     conf.setRandomGenerator(new RandomGeneratorForTest(0.258d));
551     gene.setToRandomValue(new RandomGeneratorForTest(0.83d));
552     assertEquals(new Double JavaDoc(0.83d * ( -0.5d + 1.3d) - 1.3d), gene.getAllele());
553   }
554
555   /**
556    * @throws Exception
557    *
558    * @author Klaus Meffert
559    */

560   public void testSetToRandomValue_3()
561       throws Exception JavaDoc {
562     DoubleGene gene = new DoubleGene(conf, 1.3d, 6.5d);
563     gene.setAllele(new Double JavaDoc(5.8d));
564     gene.setToRandomValue(new RandomGeneratorForTest(0.478d));
565     if (gene.doubleValue() < 1.3d
566         || gene.doubleValue() > 6.5d) {
567       fail();
568     }
569   }
570
571   /**
572    * @throws Exception
573    *
574    * @author Klaus Meffert
575    */

576   public void testSetToRandomValue_4()
577       throws Exception JavaDoc {
578     DoubleGene gene = new DoubleGene(conf, 1.3d, 6.5d);
579     gene.setAllele(new Double JavaDoc(5.8d));
580     gene.setToRandomValue(new RandomGeneratorForTest(8.584d));
581     if (gene.doubleValue() < 1.3d
582         || gene.doubleValue() > 6.5d) {
583       fail();
584     }
585   }
586
587   /**
588    * @throws Exception
589    *
590    * @author Klaus Meffert
591    */

592   public void testCompareToNative_0()
593       throws Exception JavaDoc {
594     Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d);
595     gene1.setAllele(new Double JavaDoc(5.8d));
596     Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d);
597     gene2.setAllele(new Double JavaDoc(5.9d));
598     assertEquals( ( (Double JavaDoc) gene1.getAllele()).compareTo( (Double JavaDoc) gene2.
599         getAllele()), gene1.compareTo(gene2));
600   }
601
602   /**
603    * @throws Exception
604    *
605    * @author Klaus Meffert
606    */

607   public void testCompareToNative_1()
608       throws Exception JavaDoc {
609     Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d);
610     gene1.setAllele(new Double JavaDoc(5.8d));
611     Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d);
612     gene2.setAllele(new Double JavaDoc(5.8d));
613     assertEquals( ( (Double JavaDoc) gene1.getAllele()).compareTo( (Double JavaDoc) gene2.
614         getAllele()), gene1.compareTo(gene2));
615   }
616
617   /**
618    * @throws Exception
619    *
620    * @author Klaus Meffert
621    */

622   public void testCompareToNative_2()
623       throws Exception JavaDoc {
624     Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d);
625     gene1.setAllele(new Double JavaDoc(5.9d));
626     Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d);
627     gene2.setAllele(new Double JavaDoc(5.8d));
628     assertEquals( ( (Double JavaDoc) gene1.getAllele()).compareTo( (Double JavaDoc) gene2.
629         getAllele()), gene1.compareTo(gene2));
630   }
631
632   /**
633    * @throws Exception
634    *
635    * @author Klaus Meffert
636    */

637   public void testCompareToNative_3()
638       throws Exception JavaDoc {
639     Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d);
640     gene1.setAllele(new Double JavaDoc(5.9d));
641     Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d);
642     gene2.setAllele(new Double JavaDoc(5.4d));
643     assertEquals( ( (Double JavaDoc) gene1.getAllele()).compareTo( (Double JavaDoc) gene2.
644         getAllele()), gene1.compareTo(gene2));
645   }
646
647   /**
648    * @throws Exception
649    *
650    * @author Klaus Meffert
651    */

652   public void testCompareToNative_4()
653       throws Exception JavaDoc {
654     Gene gene1 = new DoubleGene(conf, -1.3d, 6.5d);
655     gene1.setAllele(new Double JavaDoc(0.0d));
656     Gene gene2 = new DoubleGene(conf, -5.3d, 6.7d);
657     gene2.setAllele(new Double JavaDoc( -0.0d));
658     assertEquals( ( (Double JavaDoc) gene1.getAllele()).compareTo( (Double JavaDoc) gene2.
659         getAllele()), gene1.compareTo(gene2));
660   }
661
662   /**
663    * @throws Exception
664    *
665    * @author Klaus Meffert
666    */

667   public void testApplyMutation_0()
668       throws Exception JavaDoc {
669     DoubleGene gene = new DoubleGene(conf, 0, 100);
670     gene.setAllele(new Double JavaDoc(50));
671     gene.applyMutation(0, 0.0d);
672     assertEquals(50.0d, gene.doubleValue(), DELTA);
673   }
674
675   /**
676    * @throws Exception
677    *
678    * @author Klaus Meffert
679    */

680   public void testApplyMutation_1()
681       throws Exception JavaDoc {
682     conf.setRandomGenerator(new RandomGeneratorForTest(15.0d));
683     DoubleGene gene = new DoubleGene(conf, 0, 100);
684     gene.setAllele(new Double JavaDoc(50));
685     gene.applyMutation(0, 0.5d);
686     assertEquals(50 + (100 - 0) * 0.5d, gene.doubleValue(), DELTA);
687   }
688
689   /**
690    * @throws Exception
691    *
692    * @author Klaus Meffert
693    */

694   public void testApplyMutation_2()
695       throws Exception JavaDoc {
696     conf.setRandomGenerator(new RandomGeneratorForTest(15.0d));
697     DoubleGene gene = new DoubleGene(conf, 44, 100);
698     gene.setAllele(new Double JavaDoc(50));
699     gene.applyMutation(0, 0.3d);
700     assertEquals(50 + (100 - 44) * 0.3d, gene.doubleValue(), DELTA);
701   }
702
703   /**
704    * @throws Exception
705    *
706    * @author Klaus Meffert
707    */

708   public void testApplyMutation_3()
709       throws Exception JavaDoc {
710     conf.setRandomGenerator(new RandomGeneratorForTest(0.5d));
711     DoubleGene gene = new DoubleGene(conf, 33, 100);
712     gene.setAllele(new Double JavaDoc(50));
713     gene.applyMutation(0, 1.9d);
714     assertEquals(33 + 0.5d * (100 - 33), gene.doubleValue(), DELTA);
715   }
716
717   /**
718    * @throws Exception
719    *
720    * @author Klaus Meffert
721    */

722   public void testApplyMutation_4()
723       throws Exception JavaDoc {
724     conf.setRandomGenerator(new RandomGeneratorForTest(0.4d));
725     DoubleGene gene = new DoubleGene(conf, 2, 100);
726     gene.setAllele(new Double JavaDoc(60));
727     gene.applyMutation(0, 1.9d);
728     assertEquals(2 + 0.4d * (100 - 2), gene.doubleValue(), DELTA);
729   }
730
731   /**
732    * Exceed bounds for applyMutation to force randomized setting of allele.
733    * @throws Exception
734    *
735    * @author Klaus Meffert
736    */

737   public void testApplyMutation_5()
738       throws Exception JavaDoc {
739     conf.setRandomGenerator(new RandomGeneratorForTest(0.8d));
740     DoubleGene gene = new DoubleGene(conf, 0, 100);
741     gene.setAllele(new Double JavaDoc(60));
742     gene.applyMutation(1, -1.0d);
743     assertEquals(0 + 0.8d * (100 - 0), gene.doubleValue(), DELTA);
744   }
745
746   /**
747    * @throws Exception
748    *
749    * @author Klaus Meffert
750    */

751   public void testApplyMutation_6()
752       throws Exception JavaDoc {
753     DoubleGene gene = new DoubleGene(conf, 0, 100);
754     gene.setAllele(new Double JavaDoc(60));
755     gene.applyMutation(77, -0.4d);
756     assertEquals(60 + (100 * ( -0.4d)), gene.doubleValue(), DELTA);
757   }
758
759   /**
760    * @throws Exception
761    *
762    * @author Klaus Meffert
763    */

764   public void testApplyMutation_7()
765       throws Exception JavaDoc {
766     DoubleGene gene = new DoubleGene(conf, 0, 100);
767     try {
768       gene.applyMutation(0, -0.4d);
769       fail();
770     }
771     catch (NullPointerException JavaDoc nex) {
772       ; //this is OK
773
}
774   }
775
776   /**
777    * @throws Exception
778    *
779    * @author Klaus Meffert
780    * @since 2.2
781    */

782   public void testSetConstraintChecker_0()
783       throws Exception JavaDoc {
784     DoubleGene gene = new DoubleGene(conf, 0, 100);
785     assertNull(gene.getConstraintChecker());
786     gene.setConstraintChecker(new IGeneConstraintChecker() {
787       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
788                             IChromosome a_chrom, int a_index) {
789         return false;
790       }
791     });
792     assertNotNull(gene.getConstraintChecker());
793   }
794
795   /**
796    * @throws Exception
797    *
798    * @author Klaus Meffert
799    * @since 2.2
800    */

801   public void testHashCode_0()
802       throws Exception JavaDoc {
803     DoubleGene gene = new DoubleGene(conf, 0, 100);
804     assertEquals( -3, gene.hashCode());
805   }
806
807   /**
808    * @throws Exception
809    *
810    * @author Klaus Meffert
811    * @since 2.6
812    */

813   public void testHashCode_1()
814       throws Exception JavaDoc {
815     Configuration conf = new ConfigurationForTest();
816     DoubleGene c1 = new DoubleGene(conf);
817     DoubleGene c2 = new DoubleGene(conf);
818     assertEquals(c1.hashCode(), c2.hashCode());
819     c1.setAllele(new Double JavaDoc(2));
820     assertFalse(c1.hashCode() == c2.hashCode());
821     assertEquals(c1.hashCode(), c1.hashCode());
822     c2.setAllele(new Double JavaDoc(2));
823     assertTrue(c1.hashCode() == c2.hashCode());
824   }
825
826   /**
827    * @throws Exception
828    *
829    * @author Klaus Meffert
830    * @since 2.4
831    */

832   public void testSetEnergy_0()
833       throws Exception JavaDoc {
834     BaseGene gene = new DoubleGene(conf);
835     assertEquals(0.0, gene.getEnergy(), DELTA);
836   }
837
838   /**
839    * @throws Exception
840    *
841    * @author Klaus Meffert
842    * @since 2.4
843    */

844   public void testSetEnergy_1()
845       throws Exception JavaDoc {
846     BaseGene gene = new DoubleGene(conf);
847     gene.setEnergy(2.3);
848     assertEquals(2.3, gene.getEnergy(), DELTA);
849     gene.setEnergy( -55.8);
850     assertEquals( -55.8, gene.getEnergy(), DELTA);
851     gene.setEnergy(0.5);
852     gene.setEnergy(0.8);
853     assertEquals(0.8, gene.getEnergy(), DELTA);
854   }
855
856
857   /**
858    * @throws Exception
859    *
860    * @author Klaus Meffert
861    * @since 3.0
862    */

863   public void testBounds_0()
864       throws Exception JavaDoc {
865     DoubleGene gene = new DoubleGene(conf, -3.4d, +5.7d);
866     assertEquals( -3.4d, gene.getLowerBound(), DELTA);
867     assertEquals( 5.7d, gene.getUpperBound(), DELTA);
868   }
869
870   /**
871    * @throws Exception
872    *
873    * @author Klaus Meffert
874    * @since 3.0
875    */

876   public void testBounds_1()
877       throws Exception JavaDoc {
878     DoubleGene gene = new DoubleGene(conf);
879     assertEquals( - (Double.MAX_VALUE / 2), gene.getLowerBound(), DELTA);
880     assertEquals(Double.MAX_VALUE / 2, gene.getUpperBound(), DELTA);
881   }
882
883   class GeneConstraintChecker
884       implements IGeneConstraintChecker {
885     public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
886                           IChromosome a_chrom, int a_index) {
887       return true;
888     }
889   }
890 }
891
Popular Tags