1 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 23 public class PopulationHistoryTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.11 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(PopulationHistoryTest.class); 30 return suite; 31 } 32 33 39 public void testConstruct_0() { 40 new PopulationHistory(0); 41 new PopulationHistory(1); 42 new PopulationHistory(100); 43 new PopulationHistory(1000); 44 } 45 46 52 public void testConstruct_1() { 53 try { 54 new PopulationHistory( -1); 55 fail(); 56 } 57 catch (IllegalArgumentException iex) { 58 ; } 60 } 61 62 66 public void testGetPopulation_0() { 67 Population pop = new PopulationHistory(0).getPopulation(0); 68 assertEquals(null, pop); 69 } 70 71 75 public void testGetPopulation_1() 76 throws Exception { 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 assertFalse(pop.equals(pop2)); 89 PopulationHistory ph = new PopulationHistory(0); 90 ph.addPopulation(pop); 91 ph.addPopulation(pop2); 92 assertEquals(2, ph.size()); 93 Population result = ph.getPopulation(1); 95 assertEquals(pop, result); 96 result = ph.getPopulation(0); 97 assertEquals(pop2, result); 98 } 99 100 104 public void testRemoveAllPopulations_0() 105 throws Exception { 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 121 public void testAddPopulation_0() 122 throws Exception { 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 141 public void testAddPopulation_1() 142 throws Exception { 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 161 public void testSetPopulations_0() 162 throws Exception { 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 184 public void testSetPopulations_1() 185 throws Exception { 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 206 public void testSize_0() 207 throws Exception { 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 228 public void testGetPopulations_0() 229 throws Exception { 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 |