KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > eval > PopulationHistoryTest


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.eval;
11
12 import java.util.*;
13 import org.jgap.*;
14 import org.jgap.impl.*;
15 import junit.framework.*;
16
17 /**
18  * Tests for PopulationHistory class
19  *
20  * @author Klaus Meffert
21  * @since 1.1
22  */

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

39   public void testConstruct_0() {
40     new PopulationHistory(0);
41     new PopulationHistory(1);
42     new PopulationHistory(100);
43     new PopulationHistory(1000);
44   }
45
46   /**
47    * Test if construction fails correctly.
48    *
49    * @author Klaus Meffert
50    * @since 2.1
51    */

52   public void testConstruct_1() {
53     try {
54       new PopulationHistory( -1);
55       fail();
56     }
57     catch (IllegalArgumentException JavaDoc iex) {
58       ; //this is OK
59
}
60   }
61
62   /**
63    * @author Klaus Meffert
64    * @since 2.1
65    */

66   public void testGetPopulation_0() {
67     Population pop = new PopulationHistory(0).getPopulation(0);
68     assertEquals(null, pop);
69   }
70
71   /**
72    * @author Klaus Meffert
73    * @since 2.1
74    */

75   public void testGetPopulation_1()
76       throws Exception JavaDoc {
77     Population pop = new Population(conf);
78     Gene gene = new BooleanGene(conf);
79     Chromosome chrom = new Chromosome(conf, gene, 4);
80     chrom.setFitnessValue(17);
81     pop.addChromosome(chrom);
82     Population pop2 = new Population(conf);
83     gene = new IntegerGene(conf);
84     chrom = new Chromosome(conf, gene, 3);
85     chrom.setFitnessValue(3);
86     pop2.addChromosome(chrom);
87     //ensure we have not two populations seen as equal.
88
assertFalse(pop.equals(pop2));
89     PopulationHistory ph = new PopulationHistory(0);
90     ph.addPopulation(pop);
91     ph.addPopulation(pop2);
92     assertEquals(2, ph.size());
93     //lastly added population has index 0!
94
Population result = ph.getPopulation(1);
95     assertEquals(pop, result);
96     result = ph.getPopulation(0);
97     assertEquals(pop2, result);
98   }
99
100   /**
101    * @author Klaus Meffert
102    * @since 2.1
103    */

104   public void testRemoveAllPopulations_0()
105       throws Exception JavaDoc {
106     PopulationHistory ph = new PopulationHistory(0);
107     Population pop = new Population(conf);
108     Population pop2 = new Population(conf);
109     ph.addPopulation(pop);
110     ph.addPopulation(pop2);
111     ph.removeAllPopulations();
112     assertEquals(0, ph.size());
113   }
114
115   /**
116    * Limited capacity of 2 Population objects.
117    *
118    * @author Klaus Meffert
119    * @since 2.1
120    */

121   public void testAddPopulation_0()
122       throws Exception JavaDoc {
123     PopulationHistory ph = new PopulationHistory(2);
124     Population pop = new Population(conf);
125     Population pop2 = new Population(conf);
126     Population pop3 = new Population(conf);
127     ph.addPopulation(pop);
128     assertEquals(1, ph.size());
129     ph.addPopulation(pop2);
130     assertEquals(2, ph.size());
131     ph.addPopulation(pop3);
132     assertEquals(2, ph.size());
133   }
134
135   /**
136    * Unlimited capacity of Population objects.
137    *
138    * @author Klaus Meffert
139    * @since 2.1
140    */

141   public void testAddPopulation_1()
142       throws Exception JavaDoc {
143     PopulationHistory ph = new PopulationHistory(0);
144     Population pop = new Population(conf);
145     Population pop2 = new Population(conf);
146     Population pop3 = new Population(conf);
147     ph.addPopulation(pop);
148     assertEquals(1, ph.size());
149     ph.addPopulation(pop2);
150     assertEquals(2, ph.size());
151     ph.addPopulation(pop3);
152     assertEquals(3, ph.size());
153   }
154
155   /**
156    * Unlimited capacity of Population objects.
157    *
158    * @author Klaus Meffert
159    * @since 2.1
160    */

161   public void testSetPopulations_0()
162       throws Exception JavaDoc {
163     Population pop = new Population(conf);
164     Population pop2 = new Population(conf);
165     Population pop3 = new Population(conf);
166     List pops = new Vector();
167     pops.add(pop);
168     pops.add(pop2);
169     pops.add(pop3);
170     PopulationHistory ph = new PopulationHistory(0);
171     ph.setPopulations(pops);
172     assertEquals(3, ph.size());
173     assertEquals(pop, ph.getPopulation(0));
174     assertEquals(pop2, ph.getPopulation(1));
175     assertEquals(pop3, ph.getPopulation(2));
176   }
177
178   /**
179    * Limited capacity of Population objects.
180    *
181    * @author Klaus Meffert
182    * @since 2.1
183    */

184   public void testSetPopulations_1()
185       throws Exception JavaDoc {
186     Population pop = new Population(conf);
187     Population pop2 = new Population(conf);
188     Population pop3 = new Population(conf);
189     List pops = new Vector();
190     pops.add(pop);
191     pops.add(pop2);
192     pops.add(pop3);
193     PopulationHistory ph = new PopulationHistory(2);
194     ph.setPopulations(pops);
195     assertEquals(2, ph.size());
196     assertEquals(pop, ph.getPopulation(0));
197     assertEquals(pop2, ph.getPopulation(1));
198   }
199
200   /**
201    * Limited capacity of Population objects.
202    *
203    * @author Klaus Meffert
204    * @since 2.1
205    */

206   public void testSize_0()
207       throws Exception JavaDoc {
208     Population pop = new Population(conf);
209     Population pop2 = new Population(conf);
210     PopulationHistory ph = new PopulationHistory(2);
211     ph.addPopulation(pop);
212     assertEquals(1, ph.size());
213     ph.addPopulation(pop2);
214     assertEquals(2, ph.size());
215     ph.addPopulation(pop2);
216     assertEquals(2, ph.size());
217     ph.removeAllPopulations();
218     assertEquals(0, ph.size());
219     ph.addPopulation(pop);
220     assertEquals(1, ph.size());
221     ph.addPopulation(pop);
222     assertEquals(2, ph.size());
223   }
224
225   /**
226    * @author Klaus Meffert
227    */

228   public void testGetPopulations_0()
229       throws Exception JavaDoc {
230     PopulationHistory ph = new PopulationHistory(3);
231     List l = ph.getPopulations();
232     assertEquals(0, l.size());
233     Population pop = new Population(conf);
234     ph.addPopulation(pop);
235     assertEquals(1, l.size());
236     Population pop2 = new Population(conf);
237     ph.addPopulation(pop2);
238     assertEquals(2, l.size());
239     ph.addPopulation(pop2);
240     assertEquals(3, l.size());
241     l = ph.getPopulations();
242     assertEquals(pop2, l.get(0));
243     assertEquals(pop2, l.get(1));
244     assertEquals(pop, l.get(2));
245   }
246 }
247
Popular Tags