KickJava   Java API By Example, From Geeks To Geeks.

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


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

21 public class SetGeneTest
22     extends JGAPTestCase {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
25
26   public static Test suite() {
27     TestSuite suite = new TestSuite(SetGeneTest.class);
28     return suite;
29   }
30
31   public void testConstruct_0()
32       throws Exception JavaDoc {
33     SetGene gene = new SetGene(conf);
34     assertSame(conf, gene.getConfiguration());
35   }
36
37   /**
38    * @throws Exception
39    *
40    * @author Klaus Meffert
41    * @since 3.0
42    */

43   public void testConstruct_1()
44       throws Exception JavaDoc {
45     SetGene gene = new SetGene(conf);
46     assertEquals(null, gene.getInternalValue());
47   }
48
49   /**
50    * @throws Exception
51    *
52    * @author Klaus Meffert
53    * @since 3.0
54    */

55   public void testToString_0()
56       throws Exception JavaDoc {
57     SetGene gene = new SetGene(conf);
58     gene.addAllele("testValue1");
59     gene.addAllele("testValue2");
60     gene.setAllele("testValue1");
61     assertEquals("testValue1, Application data:null", gene.toString());
62   }
63
64   /**
65    * @throws Exception
66    *
67    * @author Klaus Meffert
68    * @since 3.0
69    */

70   public void testToString_1()
71       throws Exception JavaDoc {
72     SetGene gene = new SetGene(conf);
73     gene.setApplicationData("hallo");
74     gene.addAllele("testValue1");
75     gene.addAllele("testValue2");
76     gene.setAllele("testValue1");
77     assertEquals("testValue1, Application data:hallo", gene.toString());
78   }
79
80   /**
81    * @throws Exception
82    *
83    * @author Klaus Meffert
84    * @since 3.0
85    */

86   public void testGetAllele_0()
87       throws Exception JavaDoc {
88     SetGene gene = new SetGene(conf);
89     gene.addAllele("testValue1");
90     gene.addAllele("testValue2");
91     gene.setAllele("testValue2");
92     assertEquals("testValue2", gene.getAllele());
93   }
94
95   public void testEquals_0()
96       throws Exception JavaDoc {
97     Gene gene1 = new SetGene(conf);
98     Gene gene2 = new SetGene(conf);
99     assertTrue(gene1.equals(gene2));
100   }
101
102   public void testEquals_1()
103       throws Exception JavaDoc {
104     Gene gene1 = new SetGene(conf);
105     assertFalse(gene1.equals(null));
106   }
107
108   public void testEquals_2()
109       throws Exception JavaDoc {
110     SetGene gene1 = new SetGene(conf);
111     gene1.addAllele(new Double JavaDoc(2.3d));
112     gene1.setAllele(new Double JavaDoc(2.3d));
113     SetGene gene2 = new SetGene(conf);
114     gene2.addAllele(new Double JavaDoc(2.4d));
115     gene2.setAllele(new Double JavaDoc(2.4d));
116     assertFalse(gene1.equals(gene2));
117     assertFalse(gene2.equals(gene1));
118   }
119
120   public void testEquals_3()
121       throws Exception JavaDoc {
122     Gene gene1 = new SetGene(conf);
123     assertFalse(gene1.equals(new IntegerGene(conf)));
124   }
125
126   public void testEquals_4()
127       throws Exception JavaDoc {
128     Gene gene1 = new SetGene(conf);
129     Gene gene2 = new IntegerGene(conf);
130     assertFalse(gene1.equals(gene2));
131     assertFalse(gene2.equals(gene1));
132   }
133
134   public void testEquals_5()
135       throws Exception JavaDoc {
136     Gene gene1 = new SetGene(conf);
137     Gene gene2 = new FixedBinaryGene(conf, 1);
138     assertFalse(gene1.equals(gene2));
139     assertFalse(gene2.equals(gene1));
140   }
141
142   /**
143    * @throws Exception
144    *
145    * @author Klaus Meffert
146    * @since 3.0
147    */

148   public void testSetAllele_0()
149       throws Exception JavaDoc {
150     SetGene gene1 = new SetGene(conf);
151     try {
152       gene1.setAllele(null);
153       fail();
154     }
155     catch (IllegalArgumentException JavaDoc iex) {
156       ; //this is OK
157
}
158   }
159
160   /**
161    * @throws Exception
162    *
163    * @author Klaus Meffert
164    * @since 3.0
165    */

166   public void testSetAllele_1()
167       throws Exception JavaDoc {
168     SetGene gene = new SetGene(conf);
169     gene.addAllele("testValue1");
170     gene.addAllele("testValue2");
171     try {
172       gene.setAllele("testValue3");
173       fail();
174     }
175     catch (IllegalArgumentException JavaDoc iex) {
176       ; //this is OK
177
}
178   }
179
180   /**
181    * @throws Exception
182    *
183    * @author Klaus Meffert
184    * @since 3.0
185    */

186   public void testSetAllele_2()
187       throws Exception JavaDoc {
188     SetGene gene = new SetGene(conf);
189     gene.addAllele("testValue1");
190     gene.addAllele("testValue2");
191     gene.addAllele("testValue3");
192     gene.setAllele("testValue2");
193   }
194
195   public void testCompareTo_0()
196       throws Exception JavaDoc {
197     Gene gene1 = new SetGene(conf);
198     assertEquals(1, gene1.compareTo(null));
199   }
200
201   public void testCompareTo_1()
202       throws Exception JavaDoc {
203     Gene gene1 = new SetGene(conf);
204     Gene gene2 = new SetGene(conf);
205     assertEquals(0, gene1.compareTo(gene2));
206     assertEquals(0, gene2.compareTo(gene1));
207   }
208
209   public void testCompareTo_2()
210       throws Exception JavaDoc {
211     SetGene gene1 = new SetGene(conf);
212     gene1.addAllele(Boolean.valueOf(true));
213     gene1.addAllele(Boolean.valueOf(false));
214     gene1.setAllele(Boolean.valueOf(true));
215     SetGene gene2 = new SetGene(conf);
216     gene2.addAllele(Boolean.valueOf(true));
217     gene2.setAllele(Boolean.valueOf(true));
218     assertEquals(0, gene1.compareTo(gene2));
219     assertEquals(0, gene2.compareTo(gene1));
220     gene1.setAllele(Boolean.valueOf(false));
221     gene2.addAllele(Boolean.valueOf(false));
222     gene2.setAllele(Boolean.valueOf(false));
223     assertEquals(0, gene1.compareTo(gene2));
224     assertEquals(0, gene2.compareTo(gene1));
225   }
226
227   public void testCompareTo_3()
228       throws Exception JavaDoc {
229     SetGene gene1 = new SetGene(conf);
230     gene1.addAllele(Boolean.valueOf(true));
231     gene1.setAllele(Boolean.valueOf(true));
232     SetGene gene2 = new SetGene(conf);
233     gene2.addAllele(Boolean.valueOf(false));
234     gene2.setAllele(Boolean.valueOf(false));
235     assertEquals(1, gene1.compareTo(gene2));
236     assertEquals( -1, gene2.compareTo(gene1));
237   }
238
239   /**
240    * @throws Exception
241    *
242    * @author Klaus Meffert
243    * @since 3.0
244    */

245   public void testApplyMutation_0()
246       throws Exception JavaDoc {
247     conf.setRandomGenerator(new RandomGeneratorForTest(0));
248     SetGene gene = new SetGene(conf);
249     gene.addAllele(Boolean.valueOf(true));
250     gene.addAllele(Boolean.valueOf(false));
251     gene.setAllele(Boolean.valueOf(true));
252     gene.applyMutation(0, 0.0d);
253     assertEquals(true, ((Boolean JavaDoc)gene.getAllele()).booleanValue());
254   }
255
256   /**
257    * @throws Exception
258    *
259    * @author Klaus Meffert
260    * @since 3.0
261    */

262   public void testApplyMutation_1()
263       throws Exception JavaDoc {
264     conf.setRandomGenerator(new RandomGeneratorForTest(0));
265     SetGene gene = new SetGene(conf);
266     gene.addAllele(Boolean.valueOf(true));
267     gene.addAllele(Boolean.valueOf(false));
268     gene.setAllele(Boolean.valueOf(true));
269     gene.applyMutation(1, 0.000001d); //index 1 should be ignored
270
assertEquals(true, ((Boolean JavaDoc)gene.getAllele()).booleanValue());
271   }
272
273   /**
274    * @throws Exception
275    *
276    * @author Klaus Meffert
277    * @since 3.0
278    */

279   public void testApplyMutation_2()
280       throws Exception JavaDoc {
281     conf.setRandomGenerator(new RandomGeneratorForTest(0));
282     SetGene gene = new SetGene(conf);
283     gene.addAllele(Boolean.valueOf(true));
284     gene.addAllele(Boolean.valueOf(false));
285     gene.setAllele(Boolean.valueOf(true));
286     gene.applyMutation(333, -0.000001d); //index 333 should be ignored
287
assertEquals(true, ((Boolean JavaDoc)gene.getAllele()).booleanValue());
288   }
289
290   /**@todo from here on: adapt tests to SetGene*/
291
292   /**
293    *
294    * @author Klaus Meffert
295    * @throws Exception
296    */

297   public void testApplyMutation_3()
298       throws Exception JavaDoc {
299     BooleanGene gene = new BooleanGene(conf);
300     gene.setAllele(Boolean.valueOf(true));
301     gene.applyMutation(0, -1.0d);
302     assertEquals(false, gene.booleanValue());
303   }
304
305   /**
306    *
307    * @author Klaus Meffert
308    * @throws Exception
309    */

310   public void testApplyMutation_4()
311       throws Exception JavaDoc {
312     BooleanGene gene = new BooleanGene(conf);
313     gene.setAllele(Boolean.valueOf(true));
314     gene.applyMutation(0, -2.0d);
315     assertEquals(false, gene.booleanValue());
316   }
317
318   /**
319    *
320    * @author Klaus Meffert
321    * @throws Exception
322    */

323   public void testApplyMutation_5()
324       throws Exception JavaDoc {
325     BooleanGene gene = new BooleanGene(conf);
326     gene.setAllele(Boolean.valueOf(true));
327     gene.applyMutation(0, 2.0d);
328     assertEquals(true, gene.booleanValue());
329   }
330
331   /**
332    *
333    * @author Klaus Meffert
334    * @throws Exception
335    */

336   public void testApplyMutation_6()
337       throws Exception JavaDoc {
338     BooleanGene gene = new BooleanGene(conf);
339     gene.setAllele(Boolean.valueOf(false));
340     gene.applyMutation(0, 2.0d);
341     assertEquals(true, gene.booleanValue());
342   }
343
344   /**
345    *
346    * @author Klaus Meffert
347    * @throws Exception
348    */

349   public void testApplyMutation_7()
350       throws Exception JavaDoc {
351     BooleanGene gene = new BooleanGene(conf);
352     gene.setAllele(Boolean.valueOf(false));
353     gene.applyMutation(0, -1.0d);
354     assertEquals(false, gene.booleanValue());
355   }
356
357   /**
358    *
359    * @author Klaus Meffert
360    * @throws Exception
361    */

362   public void testApplyMutation_8()
363       throws Exception JavaDoc {
364     BooleanGene gene = new BooleanGene(conf);
365     gene.setAllele(Boolean.valueOf(false));
366     gene.applyMutation(22, -0.5d); //22 should be ignored
367
assertEquals(false, gene.booleanValue());
368   }
369
370   /**
371    *
372    * @author Klaus Meffert
373    * @throws Exception
374    */

375   public void testApplyMutation_9()
376       throws Exception JavaDoc {
377     BooleanGene gene = new BooleanGene(conf);
378     gene.setAllele(Boolean.valueOf(false));
379     gene.applyMutation(22, 0.5d); //22 should be ignored
380
assertEquals(true, gene.booleanValue());
381   }
382
383   /**
384    *
385    * @author Klaus Meffert
386    * @since 2.2
387    * @throws Exception
388    */

389   public void testApplyMutation_10()
390       throws Exception JavaDoc {
391     BooleanGene gene = new BooleanGene(conf);
392     gene.applyMutation(0, 0.0d);
393     assertEquals(false, gene.booleanValue());
394   }
395
396   /**
397    * Should be possible without exception.
398    * @throws Exception
399    *
400    * @author Klaus Meffert
401    * @since 3.0
402    */

403   public void testSetValueFromPersistentRepresentation_0()
404       throws Exception JavaDoc {
405     SetGene gene = new SetGene(conf);
406     gene.setValueFromPersistentRepresentation(null);
407   }
408
409   /**
410    * @throws Exception
411    *
412    * @author Klaus Meffert
413    * @since 2.0
414    */

415   public void testSetValueFromPersistentRepresentation_1()
416       throws Exception JavaDoc {
417     BooleanGene gene = new BooleanGene(conf);
418     gene.setValueFromPersistentRepresentation("null");
419     assertEquals(null, gene.getAllele());
420   }
421
422   /**
423    * @throws Exception
424    *
425    * @author Klaus Meffert
426    * @since 2.0
427    */

428   public void testSetValueFromPersistentRepresentation_2()
429       throws Exception JavaDoc {
430     BooleanGene gene = new BooleanGene(conf);
431     gene.setValueFromPersistentRepresentation("true");
432     assertEquals(Boolean.TRUE, gene.getAllele());
433   }
434
435   /**
436    * @throws Exception
437    * @author Klaus Meffert
438    * @since 2.0
439    */

440   public void testSetValueFromPersistentRepresentation_3()
441       throws Exception JavaDoc {
442     BooleanGene gene = new BooleanGene(conf);
443     gene.setValueFromPersistentRepresentation("false");
444     assertEquals(Boolean.FALSE, gene.getAllele());
445   }
446
447   /**
448    *
449    * @author Klaus Meffert
450    * @since 2.0
451    * @throws Exception
452    */

453   public void testSetValueFromPersistentRepresentation_4()
454       throws Exception JavaDoc {
455     BooleanGene gene = new BooleanGene(conf);
456     try {
457       gene.setValueFromPersistentRepresentation("True");
458       fail();
459     }
460     catch (UnsupportedRepresentationException uex) {
461       ; //this is OK
462
}
463   }
464
465   /**
466    *
467    * @author Klaus Meffert
468    * @since 2.0
469    * @throws Exception
470    */

471   public void testSetValueFromPersistentRepresentation_5()
472       throws Exception JavaDoc {
473     BooleanGene gene = new BooleanGene(conf);
474     try {
475       gene.setValueFromPersistentRepresentation("False");
476       fail();
477     }
478     catch (UnsupportedRepresentationException uex) {
479       ; //this is OK
480
}
481   }
482
483   /**
484    *
485    * @author Klaus Meffert
486    * @since 2.0
487    * @throws Exception
488    */

489   public void testSetValueFromPersistentRepresentation_6()
490       throws Exception JavaDoc {
491     BooleanGene gene = new BooleanGene(conf);
492     try {
493       gene.setValueFromPersistentRepresentation("X");
494       fail();
495     }
496     catch (UnsupportedRepresentationException uex) {
497       ; //this is OK
498
}
499   }
500
501   /**
502    *
503    * @author Klaus Meffert
504    * @throws Exception
505    */

506   public void testGetPersistentRepresentation_0()
507       throws Exception JavaDoc {
508     BooleanGene gene = new BooleanGene(conf);
509     gene.setAllele(Boolean.valueOf(true));
510     String JavaDoc s = gene.getPersistentRepresentation();
511     assertEquals("true", s);
512   }
513
514   /**
515    *
516    * @author Klaus Meffert
517    * @throws Exception
518    */

519   public void testGetPersistentRepresentation_1()
520       throws Exception JavaDoc {
521     BooleanGene gene = new BooleanGene(conf);
522     gene.setAllele(Boolean.valueOf(false));
523     String JavaDoc s = gene.getPersistentRepresentation();
524     assertEquals("false", s);
525   }
526
527   /**
528    *
529    * @author Klaus Meffert
530    * @throws Exception
531    */

532   public void testGetPersistentRepresentation_2()
533       throws Exception JavaDoc {
534     BooleanGene gene = new BooleanGene(conf);
535     String JavaDoc s = gene.getPersistentRepresentation();
536     assertEquals("null", s);
537   }
538
539   /**
540    *
541    * @author Klaus Meffert
542    * @since 2.2
543    * @throws Exception
544    */

545   public void testHashCode_0()
546       throws Exception JavaDoc {
547     BooleanGene gene = new BooleanGene(conf);
548     assertEquals( -2, gene.hashCode());
549   }
550
551   /**
552    *
553    * @author Klaus Meffert
554    * @since 2.4
555    * @throws Exception
556    */

557   public void testSetEnergy_0()
558       throws Exception JavaDoc {
559     BaseGene gene = new BooleanGene(conf);
560     assertEquals(0.0, gene.getEnergy(), DELTA);
561   }
562
563   /**
564    *
565    * @author Klaus Meffert
566    * @since 2.4
567    * @throws Exception
568    */

569   public void testSetEnergy_1()
570       throws Exception JavaDoc {
571     BaseGene gene = new BooleanGene(conf);
572     gene.setEnergy(2.3);
573     assertEquals(2.3, gene.getEnergy(), DELTA);
574     gene.setEnergy( -55.8);
575     assertEquals( -55.8, gene.getEnergy(), DELTA);
576     gene.setEnergy(0.5);
577     gene.setEnergy(0.8);
578     assertEquals(0.8, gene.getEnergy(), DELTA);
579   }
580 }
581
Popular Tags