KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

36   public void testConstruct_0() {
37     new ChainOfSelectors();
38   }
39
40   /**
41    * @author Klaus Meffert
42    * @since 2.2
43    */

44   public void testClear_0() {
45     ChainOfSelectors c = new ChainOfSelectors();
46     assertTrue(c.isEmpty());
47     c.clear();
48     assertTrue(c.isEmpty());
49   }
50
51   /**
52    * @throws Exception
53    *
54    * @author Klaus Meffert
55    * @since 2.2
56    */

57   public void testClear_1()
58       throws Exception JavaDoc {
59     ChainOfSelectors c = new ChainOfSelectors();
60     assertEquals(0, c.size());
61     c.addNaturalSelector(new BestChromosomesSelector(conf));
62     assertEquals(1, c.size());
63     assertFalse(c.isEmpty());
64     c.clear();
65     assertTrue(c.isEmpty());
66   }
67
68   /**
69    * @throws Exception
70    *
71    * @author Klaus Meffert
72    * @since 2.2
73    */

74   public void testClear_2()
75       throws Exception JavaDoc {
76     ChainOfSelectors c = new ChainOfSelectors();
77     Collection l = new Vector();
78     l.add(new BestChromosomesSelector(conf));
79     l.add(new WeightedRouletteSelector(conf));
80     c.addAll(l);
81     assertEquals(2, c.size());
82     c.clear();
83     assertTrue(c.isEmpty());
84     assertEquals(0, c.size());
85   }
86
87   /**
88    * @throws Exception
89    *
90    * @author Klaus Meffert
91    * @since 2.2
92    */

93   public void testIterator_0()
94       throws Exception JavaDoc {
95     ChainOfSelectors c = new ChainOfSelectors();
96     Collection l = new Vector();
97     l.add(new BestChromosomesSelector(conf));
98     l.add(new WeightedRouletteSelector(conf));
99     c.addAll(l);
100     Iterator it = c.iterator();
101     assertTrue(it.hasNext());
102     assertNotNull(it.next());
103     assertTrue(it.hasNext());
104     assertNotNull(it.next());
105     assertFalse(it.hasNext());
106   }
107
108   /**
109    * @throws Exception
110    *
111    * @author Klaus Meffert
112    * @since 2.2
113    */

114   public void testAddNaturalSelector_0()
115       throws Exception JavaDoc {
116     ChainOfSelectors c = new ChainOfSelectors();
117     try {
118       c.addNaturalSelector(null);
119       fail();
120     }
121     catch (InvalidConfigurationException inex) {
122       ; //this is OK
123
}
124   }
125
126   public void testEquals_0()
127       throws Exception JavaDoc {
128     ChainOfSelectors c1 = new ChainOfSelectors();
129     ChainOfSelectors c2 = new ChainOfSelectors();
130     assertFalse(c1.equals(null));
131     assertTrue(c1.equals(c2));
132     TournamentSelector t1 = new TournamentSelector();
133     c1.addNaturalSelector(t1);
134     assertFalse(c1.equals(c2));
135     c2.addNaturalSelector(new TournamentSelector());
136     /**@todo improve: 2 unsame selectors of same class with same params should
137      * make the chain equal!
138      */

139     assertFalse(c1.equals(c2));
140     c2.clear();
141     c2.addNaturalSelector(t1);
142     assertTrue(c1.equals(c2));
143   }
144
145   /**
146    * @throws Exception
147    *
148    * @author Klaus Meffert
149    * @since 2.6
150    */

151   public void testEquals_1()
152       throws Exception JavaDoc {
153     ChainOfSelectors c1 = new ChainOfSelectors();
154     assertFalse(c1.equals(new BooleanGene(conf)));
155   }
156
157   /**
158    * @throws Exception
159    *
160    * @author Klaus Meffert
161    * @since 2.6
162    */

163   public void testHashCode_0()
164       throws Exception JavaDoc {
165     ChainOfSelectors c1 = new ChainOfSelectors();
166     ChainOfSelectors c2 = new ChainOfSelectors();
167     assertEquals(c1.hashCode(), c2.hashCode());
168     c1.addNaturalSelector(new BestChromosomesSelector(conf));
169     assertFalse(c1.hashCode() == c2.hashCode());
170     assertEquals(c1.hashCode(), c1.hashCode());
171     c2.addNaturalSelector(new BestChromosomesSelector(conf));
172     assertFalse(c1.hashCode() == c2.hashCode());
173   }
174 }
175
Popular Tags