1 10 package org.jgap; 11 12 import junit.framework.*; 13 14 20 public class BaseGeneTest 21 extends JGAPTestCase { 22 23 private final static String CVS_REVISION = "$Revision: 1.20 $"; 24 25 public static Test suite() { 26 TestSuite suite = new TestSuite(BaseGeneTest.class); 27 return suite; 28 } 29 30 37 public void testConstruct_0() 38 throws Exception { 39 assertNotNull(new BaseGeneImpl(conf)); 40 } 41 42 48 public void testToString_0() 49 throws Exception { 50 Gene gene = new BaseGeneImpl(conf); 51 assertEquals("null, " + BaseGene.S_APPLICATION_DATA + ":null", 52 gene.toString()); 53 } 54 55 61 public void testToString_1() 62 throws Exception { 63 Gene gene = new BaseGeneImpl(conf); 64 gene.setAllele(new Integer (98)); 65 assertEquals("98, " + BaseGene.S_APPLICATION_DATA + ":null", 66 gene.toString()); 67 } 68 69 75 public void testToString_2() 76 throws Exception { 77 Gene gene = new BaseGeneImpl(conf); 78 gene.setAllele(new Integer (98)); 79 gene.setApplicationData("myAppData"); 80 assertEquals("98, " + BaseGene.S_APPLICATION_DATA + ":myAppData", 81 gene.toString()); 82 } 83 84 90 public void testGetAllele_0() 91 throws Exception { 92 Gene gene = new BaseGeneImpl(conf); 93 gene.setAllele(new Double (75)); 94 assertEquals(new Double (75), gene.getAllele()); 95 } 96 97 103 public void testSize_0() 104 throws Exception { 105 Gene gene = new BaseGeneImpl(conf); 106 assertEquals(1, gene.size()); 107 } 108 109 115 public void testEquals_0() 116 throws Exception { 117 BaseGeneImpl gene = new BaseGeneImpl(conf); 118 gene.m_compareTo_result = 0; 119 assertTrue(gene.equals(null)); 120 assertTrue(gene.equals(gene)); 121 assertTrue(gene.equals(new Integer (2))); 122 } 123 124 130 public void testEquals_1() 131 throws Exception { 132 BaseGeneImpl gene = new BaseGeneImpl(conf); 133 gene.m_compareTo_result = -1; 134 assertFalse(gene.equals(null)); 135 assertFalse(gene.equals(gene)); 136 assertFalse(gene.equals(new Integer (2))); 137 } 138 139 145 public void testEquals_2() 146 throws Exception { 147 BaseGeneImpl gene = new BaseGeneImpl(conf); 148 gene.m_compareTo_result = 1; 149 assertFalse(gene.equals(null)); 150 assertFalse(gene.equals(gene)); 151 assertFalse(gene.equals(new Integer (2))); 152 } 153 154 161 public void testEquals_3() 162 throws Exception { 163 Configuration conf = new ConfigurationForTest(); 164 BaseGeneImpl gene = new BaseGeneImpl(conf); 167 gene.m_compareTo_result = 0; 168 gene.setApplicationData(new AppDataForTest()); 169 BaseGeneImpl gene2 = new BaseGeneImpl(conf); 170 gene2.m_compareTo_result = 0; 171 gene2.setApplicationData(new AppDataForTest()); 172 gene.setCompareApplicationData(true); 173 assertTrue(gene.equals(gene2)); 174 177 } 178 179 187 public void testCleanup_0() 188 throws Exception { 189 Gene gene = new BaseGeneImpl(conf); 190 gene.setAllele(new Double (75)); 191 gene.cleanup(); 192 } 193 194 200 public void testHashCode_0() 201 throws Exception { 202 BaseGeneImpl gene = new BaseGeneImpl(conf); 203 assertEquals( -79, gene.hashCode()); 204 } 205 206 212 public void testHashCode_1() 213 throws Exception { 214 BaseGeneImpl gene = new BaseGeneImpl(conf); 215 gene.setAllele(new Double (1.5d)); 216 assertEquals(new Double (1.5d).hashCode(), gene.hashCode()); 217 } 218 219 225 public void testSetEnergy_0() 226 throws Exception { 227 BaseGeneImpl gene = new BaseGeneImpl(conf); 228 assertEquals(0.0, gene.getEnergy(), DELTA); 229 gene.setEnergy(2.3); 230 assertEquals(2.3, gene.getEnergy(), DELTA); 231 } 232 233 239 public void testSetEnergy_1() 240 throws Exception { 241 BaseGeneImpl gene = new BaseGeneImpl(conf); 242 gene.setEnergy(2.3); 243 assertEquals(2.3, gene.getEnergy(), DELTA); 244 gene.setEnergy( -55.8); 245 assertEquals( -55.8, gene.getEnergy(), DELTA); 246 gene.setEnergy(0.5); 247 gene.setEnergy(0.8); 248 assertEquals(0.8, gene.getEnergy(), DELTA); 249 } 250 251 257 public void testSetApplicationData_0() 258 throws Exception { 259 BaseGeneImpl gene = new BaseGeneImpl(conf); 260 assertNull(gene.getApplicationData()); 261 Integer i = new Integer (23); 262 gene.setApplicationData(i); 263 assertSame(i, gene.getApplicationData()); 264 String s = "Hallo"; 265 gene.setApplicationData(s); 266 assertSame(s, gene.getApplicationData()); 267 } 268 269 275 public void testIsCompareApplicationData_0() 276 throws Exception { 277 BaseGeneImpl gene = new BaseGeneImpl(conf); 278 assertFalse(gene.isCompareApplicationData()); 279 gene.setCompareApplicationData(false); 280 assertFalse(gene.isCompareApplicationData()); 281 gene.setCompareApplicationData(true); 282 assertTrue(gene.isCompareApplicationData()); 283 } 284 285 291 class BaseGeneImpl 292 extends BaseGene { 293 private Object m_allele; 294 295 private int m_compareTo_result; 296 297 public int compareTo(Object a_o) { 298 return m_compareTo_result; 299 } 300 301 public BaseGeneImpl(final Configuration a_config) 302 throws InvalidConfigurationException { 303 super(a_config); 304 } 305 306 protected Gene newGeneInternal() { 307 return null; 308 } 309 310 public void setAllele(Object a_newValue) { 311 m_allele = a_newValue; 312 } 313 314 public String getPersistentRepresentation() { 315 return null; 316 } 317 318 public void setValueFromPersistentRepresentation(String a_representation) { 319 } 320 321 public void setToRandomValue(RandomGenerator a_numberGenerator) { 322 } 323 324 public void applyMutation(int a_index, double a_percentage) { 325 } 326 327 protected Object getInternalValue() { 328 return m_allele; 329 } 330 } 331 class AppDataForTest 332 implements IApplicationData { 333 public int compareTo(Object o2) { 334 return 0; 335 } 336 337 public Object clone() 338 throws CloneNotSupportedException { 339 return null; 340 } 341 } 342 343 } 344 | Popular Tags |