1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class NumberGeneTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.25 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(NumberGeneTest.class); 29 return suite; 30 } 31 32 38 public void testConstruct_0() 39 throws Exception { 40 Gene gene = new NumberGeneImpl(conf, 1, 100); 41 gene.setAllele(new Integer (101)); 43 } 44 45 51 public void testToString_0() 52 throws Exception { 53 Gene gene = new NumberGeneImpl(conf, 1, 100); 54 gene.setAllele(new Integer (47)); 55 assertEquals("47, " + BaseGene.S_APPLICATION_DATA + ":null", 56 gene.toString()); 57 } 58 59 65 public void testToString_1() 66 throws Exception { 67 Gene gene = new NumberGeneImpl(conf, 1, 100); 68 gene.setAllele(new Integer (102)); 69 int indexComma = gene.toString().indexOf(','); 70 int toString = Integer.parseInt(gene.toString().substring(0, indexComma)); 71 assertTrue(toString >= 1 && toString <= 100); 72 } 73 74 80 public void testGetAllele_0() 81 throws Exception { 82 Gene gene = new NumberGeneImpl(conf, 1, 100); 83 gene.setAllele(new Integer (33)); 84 assertEquals(new Integer (33), gene.getAllele()); 85 } 86 87 93 public void testGetAllele_1() 94 throws Exception { 95 Gene gene = new NumberGeneImpl(conf, 1, 100); 96 gene.setAllele(new Integer (1)); 97 assertEquals(new Integer (1), gene.getAllele()); 98 } 99 100 106 public void testGetAllele_2() 107 throws Exception { 108 Gene gene = new NumberGeneImpl(conf, 1, 100); 109 gene.setAllele(new Integer (100)); 110 assertEquals(new Integer (100), gene.getAllele()); 111 } 112 113 119 public void testHashCode_0() 120 throws Exception { 121 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 122 Gene gene2 = new NumberGeneImpl(conf, 1, 100); 123 assertEquals(gene1.hashCode(), gene2.hashCode()); 124 } 125 126 132 public void testHashCode_1() 133 throws Exception { 134 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 135 gene1.setAllele(new Integer (43)); 136 Gene gene2 = new NumberGeneImpl(conf, 1, 100); 137 gene2.setAllele(new Integer (43)); 138 assertEquals(gene1.hashCode(), gene2.hashCode()); 139 } 140 141 147 public void testHashCode_2() 148 throws Exception { 149 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 150 gene1.setAllele(new Integer (55)); 151 Gene gene2 = new NumberGeneImpl(conf, 1, 100); 152 gene1.setAllele(new Integer (43)); 153 assertFalse(gene1.hashCode() == gene2.hashCode()); 154 } 155 156 162 public void testEquals_0() 163 throws Exception { 164 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 165 Gene gene2 = new NumberGeneImpl(conf, 1, 100); 166 assertEquals(gene1, gene2); 167 assertEquals(gene2, gene1); 168 } 169 170 176 public void testEquals_1() 177 throws Exception { 178 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 179 assertFalse(gene1.equals(null)); 180 } 181 182 188 public void testEquals_2() 189 throws Exception { 190 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 191 assertFalse(gene1.equals(new BooleanGene(conf))); 192 } 193 194 200 public void testEquals_3() 201 throws Exception { 202 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 203 assertFalse(gene1.equals(new Vector())); 204 } 205 206 212 public void testEquals_4() 213 throws Exception { 214 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 215 Gene gene2 = new NumberGeneImpl(conf, 1, 99); 216 assertEquals(gene1, gene2); 217 assertEquals(gene2, gene1); 218 } 219 220 226 public void testEquals_5() 227 throws Exception { 228 Gene gene1 = new NumberGeneImpl(conf, 1, 100); 229 gene1.setAllele(new Integer (2)); 230 Gene gene2 = new NumberGeneImpl(conf, 1, 99); 231 gene1.setAllele(new Integer (3)); 232 assertFalse(gene1.equals(gene2)); 233 assertFalse(gene2.equals(gene1)); 234 } 235 236 243 public void testSetAllele_0() 244 throws Exception { 245 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 246 gene1.setAllele(null); 247 } 248 249 255 public void testSetAllele_1() 256 throws Exception { 257 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 258 try { 259 gene1.setAllele("22"); 260 fail(); 261 } 262 catch (ClassCastException classex) { 263 ; } 265 } 266 267 273 public void testSetAllele_2() 274 throws Exception { 275 NumberGene gene1 = new NumberGeneImpl(conf, 1, 10000); 276 gene1.setConstraintChecker(new IGeneConstraintChecker() { 277 public boolean verify(Gene a_gene, Object a_alleleValue, 278 IChromosome a_chrom, int a_index) { 279 return false; 280 } 281 } 282 ); 283 gene1.setAllele("22"); 284 } 285 286 292 public void testSetAllele_3() 293 throws Exception { 294 NumberGene gene1 = new NumberGeneImpl(conf, 1, 10000); 295 gene1.setConstraintChecker(new IGeneConstraintChecker() { 296 public boolean verify(Gene a_gene, Object a_alleleValue, 297 IChromosome a_chrom, int a_index) { 298 return true; 299 } 300 } 301 ); 302 try { 303 gene1.setAllele("22"); 304 fail(); 305 } 306 catch (ClassCastException classex) { 307 ; } 309 } 310 311 317 public void testNewGene_0() 318 throws Exception { 319 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 320 gene1.setAllele(new Integer (4711)); 321 Integer lower1 = (Integer ) privateAccessor.getField(gene1, 322 "m_lowerBounds"); 323 Integer upper1 = (Integer ) privateAccessor.getField(gene1, 324 "m_upperBounds"); 325 Gene gene2 = gene1.newGene(); 326 Integer lower2 = (Integer ) privateAccessor.getField(gene2, 327 "m_lowerBounds"); 328 Integer upper2 = (Integer ) privateAccessor.getField(gene2, 329 "m_upperBounds"); 330 assertEquals(lower1, lower2); 331 assertEquals(upper1, upper2); 332 } 333 334 340 public void testCompareTo_0() 341 throws Exception { 342 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 343 gene1.setAllele(new Integer (4711)); 344 Gene gene2 = gene1.newGene(); 345 assertEquals(0, gene1.compareTo(gene1)); 346 assertEquals(1, gene1.compareTo(gene2)); 347 gene2.setAllele(new Integer (4711)); 348 assertEquals(0, gene1.compareTo(gene2)); 349 assertEquals(0, gene2.compareTo(gene1)); 350 } 351 352 358 public void testCompareTo_1() 359 throws Exception { 360 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 361 gene1.setAllele(new Integer (4711)); 362 Gene gene2 = new DoubleGene(conf); 363 try { 364 assertEquals(0, gene1.compareTo(gene2)); 365 fail(); 366 } 367 catch (ClassCastException cex) { 368 ; } 370 } 371 372 378 public void testCompareTo_2() 379 throws Exception { 380 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 381 gene1.setAllele(new Integer (4711)); 382 Gene gene2 = new DoubleGene(conf); 383 gene2.setAllele(new Double (4711.0d)); 384 try { 385 assertEquals(0, gene1.compareTo(gene2)); 386 fail(); 387 } 388 catch (ClassCastException cex) { 389 ; } 391 } 392 393 400 public void testCompareTo_3() 401 throws Exception { 402 Gene gene1 = new NumberGeneImpl(conf, 1, 10000); 403 gene1.setAllele(new Integer (4711)); 404 gene1.setApplicationData(new Integer (2)); 405 gene1.setCompareApplicationData(true); 406 Gene gene2 = gene1.newGene(); 407 gene2.setAllele(new Integer (4711)); 408 assertEquals(1, gene1.compareTo(gene2)); 409 assertEquals(0, gene2.compareTo(gene1)); 410 gene2.setCompareApplicationData(true); 411 assertEquals( -1, gene2.compareTo(gene1)); 412 gene2.setApplicationData(new Integer (3)); 413 assertEquals(1, gene2.compareTo(gene1)); 414 assertEquals( -1, gene1.compareTo(gene2)); 415 } 416 417 public void testSetConstraintChecker_0() 418 throws Exception { 419 NumberGene gene1 = new NumberGeneImpl(conf, 1, 3); 420 gene1.setConstraintChecker(new IGeneConstraintChecker() { 421 public boolean verify(Gene a_gene, Object a_alleleValue, 422 IChromosome a_chrom, int a_index) { 423 return false; 424 } 425 } 426 ); 427 assertNotNull(gene1.getConstraintChecker()); 428 } 429 430 436 public void testSetEnergy_0() 437 throws Exception { 438 BaseGene gene = new NumberGeneImpl(conf, 0, 1); 439 assertEquals(0.0, gene.getEnergy(), DELTA); 440 } 441 442 448 public void testSetEnergy_1() 449 throws Exception { 450 BaseGene gene = new NumberGeneImpl(conf, 2, 4); 451 gene.setEnergy(2.3); 452 assertEquals(2.3, gene.getEnergy(), DELTA); 453 gene.setEnergy( -55.8); 454 assertEquals( -55.8, gene.getEnergy(), DELTA); 455 gene.setEnergy(0.5); 456 gene.setEnergy(0.8); 457 assertEquals(0.8, gene.getEnergy(), DELTA); 458 } 459 460 466 private class NumberGeneImpl 467 extends NumberGene { 468 protected final static long INTEGER_RANGE = (long) Integer.MAX_VALUE 469 - (long) Integer.MIN_VALUE; 470 471 private int m_upperBounds; 472 473 private int m_lowerBounds; 474 475 private long m_boundsUnitsToIntegerUnits; 476 477 public NumberGeneImpl(final Configuration a_conf, int a_lowerBounds, 478 int a_upperBounds) 479 throws InvalidConfigurationException { 480 super(a_conf); 481 m_lowerBounds = a_lowerBounds; 482 m_upperBounds = a_upperBounds; 483 calculateBoundsUnitsToIntegerUnitsRatio(); 484 } 485 486 protected Gene newGeneInternal() { 487 try { 488 return new NumberGeneImpl(getConfiguration(), m_lowerBounds, 489 m_upperBounds); 490 } 491 catch (InvalidConfigurationException iex) { 492 throw new IllegalStateException (iex.getMessage()); 493 } 494 } 495 496 public String getPersistentRepresentation() { 497 return toString() + PERSISTENT_FIELD_DELIMITER + m_lowerBounds 498 + PERSISTENT_FIELD_DELIMITER + m_upperBounds; 499 } 500 501 public void setValueFromPersistentRepresentation(String a_representation) { 502 } 505 506 public void setToRandomValue(RandomGenerator a_numberGenerator) { 507 } 510 511 public void applyMutation(int a_index, double a_percentage) { 512 } 515 516 protected void calculateBoundsUnitsToIntegerUnitsRatio() { 517 int divisor = m_upperBounds - m_lowerBounds + 1; 518 if (divisor == 0) { 519 m_boundsUnitsToIntegerUnits = INTEGER_RANGE; 520 } 521 else { 522 m_boundsUnitsToIntegerUnits = INTEGER_RANGE / divisor; 523 } 524 } 525 526 protected void mapValueToWithinBounds() { 527 if (getAllele() != null) { 528 Integer i_value = ( (Integer ) getAllele()); 529 if (i_value.intValue() > m_upperBounds 536 || i_value.intValue() < m_lowerBounds) { 537 long differenceFromIntMin = (long) Integer.MIN_VALUE 538 + (long) i_value.intValue(); 539 int differenceFromBoundsMin = (int) (differenceFromIntMin 540 / m_boundsUnitsToIntegerUnits); 541 setAllele(new Integer (m_upperBounds + differenceFromBoundsMin)); 542 } 543 } 544 } 545 546 protected int compareToNative(Object a_o1, Object a_o2) { 547 return ( (Integer ) a_o1).compareTo( (Integer ) a_o2); 548 } 549 550 public boolean equals(Object a_other) { 551 try { 552 return compareTo(a_other) == 0; 553 } 554 catch (ClassCastException e) { 555 return false; 556 } 557 } 558 } 559 } 560 | Popular Tags |