1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class IntegerGeneTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.41 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(IntegerGeneTest.class); 29 return suite; 30 } 31 32 public void testConstruct_0() 33 throws Exception { 34 Gene gene = new IntegerGene(conf, 1, 100); 35 gene.setAllele(new Integer (101)); 37 } 38 39 45 public void testConstruct_1() 46 throws Exception { 47 Genotype.setStaticConfiguration(conf); 48 Gene gene = new IntegerGene(); 49 assertSame(conf, gene.getConfiguration()); 50 } 51 52 public void testToString_0() 53 throws Exception { 54 Gene gene = new IntegerGene(conf, 1, 100); 55 gene.setAllele(new Integer (47)); 56 assertEquals("IntegerGene(1,100)=47", gene.toString()); 57 } 58 59 public void testToString_1() 60 throws Exception { 61 Gene gene = new IntegerGene(conf, -2, 100); 62 gene.setAllele(new Integer (99)); 63 assertEquals("IntegerGene(-2,100)=99", gene.toString()); 64 } 65 66 public void testGetAllele_0() 67 throws Exception { 68 Gene gene = new IntegerGene(conf, 1, 100); 69 gene.setAllele(new Integer (33)); 70 assertEquals(new Integer (33), gene.getAllele()); 71 } 72 73 public void testGetAllele_1() 74 throws Exception { 75 Gene gene = new IntegerGene(conf, 1, 100); 76 gene.setAllele(new Integer (1)); 77 assertEquals(new Integer (1), gene.getAllele()); 78 } 79 80 public void testGetAllele_2() 81 throws Exception { 82 Gene gene = new IntegerGene(conf, 1, 100); 83 gene.setAllele(new Integer (100)); 84 assertEquals(new Integer (100), gene.getAllele()); 85 } 86 87 public void testEquals_0() 88 throws Exception { 89 Gene gene1 = new IntegerGene(conf, 1, 100); 90 Gene gene2 = new IntegerGene(conf, 1, 100); 91 assertTrue(gene1.equals(gene2)); 92 assertTrue(gene2.equals(gene1)); 93 } 94 95 public void testEquals_1() 96 throws Exception { 97 Gene gene1 = new IntegerGene(conf, 1, 100); 98 assertFalse(gene1.equals(null)); 99 } 100 101 public void testEquals_2() 102 throws Exception { 103 Gene gene1 = new IntegerGene(conf, 1, 100); 104 assertFalse(gene1.equals(new BooleanGene(conf))); 105 } 106 107 public void testEquals_3() 108 throws Exception { 109 Gene gene1 = new IntegerGene(conf, 1, 100); 110 assertFalse(gene1.equals(new Vector())); 111 } 112 113 public void testEquals_4() 114 throws Exception { 115 Gene gene1 = new IntegerGene(conf, 1, 100); 116 Gene gene2 = new IntegerGene(conf, 1, 99); 117 assertTrue(gene1.equals(gene2)); 118 assertTrue(gene2.equals(gene1)); 119 } 120 121 public void testEquals_5() 122 throws Exception { 123 Gene gene1 = new IntegerGene(conf, 1, 100); 124 Gene gene2 = new DoubleGene(conf, 1, 99); 125 assertFalse(gene1.equals(gene2)); 126 assertFalse(gene2.equals(gene1)); 127 } 128 129 136 public void testEquals_6() 137 throws Exception { 138 Gene gene1 = new IntegerGene(conf, 1, 100); 139 Gene gene2 = new IntegerGene(conf, 1, 100); 140 gene1.setAllele(new Integer (45)); 141 gene2.setAllele(new Integer (46)); 142 assertFalse(gene1.equals(gene2)); 143 assertFalse(gene2.equals(gene1)); 144 } 145 146 153 public void testEquals_7() 154 throws Exception { 155 Gene gene1 = new IntegerGene(conf, 1, 100); 156 gene1.setAllele(new Integer (7)); 157 Gene gene2 = new IntegerGene2(conf, 1, 100); 158 gene2.setAllele(new Integer (7)); 159 assertFalse(gene1.equals(gene2)); 160 assertFalse(gene2.equals(gene1)); 161 } 162 163 170 public void testEquals_8() 171 throws Exception { 172 BaseGene gene1 = new IntegerGene(conf, 1, 100); 173 gene1.setAllele(new Integer (7)); 174 gene1.setApplicationData(new Integer (7)); 175 gene1.setCompareApplicationData(true); 176 Gene gene2 = new IntegerGene(conf, 1, 100); 177 gene2.setApplicationData(new Integer (7)); 178 gene2.setAllele(new Integer (7)); 179 gene2.setCompareApplicationData(true); 180 assertTrue(gene1.equals(gene2)); 181 assertTrue(gene2.equals(gene1)); 182 } 183 184 191 public void testEquals_9() 192 throws Exception { 193 BaseGene gene1 = new IntegerGene(conf, 1, 100); 194 gene1.setAllele(new Integer (7)); 195 gene1.setApplicationData(new Integer (7)); 196 gene1.setCompareApplicationData(true); 197 Gene gene2 = new IntegerGene(conf, 1, 100); 198 gene2.setCompareApplicationData(true); 199 gene2.setApplicationData(new Integer (7)); 200 gene2.setAllele(new Integer (7)); 201 assertTrue(gene1.equals(gene2)); 202 assertTrue(gene2.equals(gene1)); 203 } 204 205 212 public void testEquals_9_2() 213 throws Exception { 214 BaseGene gene1 = new IntegerGene(conf, 1, 100); 215 gene1.setAllele(new Integer (8)); 216 gene1.setApplicationData(new Integer (5)); 217 gene1.setCompareApplicationData(true); 218 Gene gene2 = new IntegerGene(conf, 1, 100); 219 gene2.setCompareApplicationData(true); 220 gene2.setApplicationData(new Integer (7)); 221 gene2.setAllele(new Integer (8)); 222 assertFalse(gene1.equals(gene2)); 223 assertFalse(gene2.equals(gene1)); 224 } 225 226 232 public void testEquals_10() 233 throws Exception { 234 Configuration conf = new ConfigurationForTest(); 235 conf.setRandomGenerator(new RandomGeneratorForTest(5)); 236 Gene gene1 = new IntegerGene(conf, 1, 100); 237 gene1.setAllele(new Integer (8)); 238 Gene gene2 = new IntegerGene(conf, 1, 99); 239 gene2.setAllele(new Integer ( -8)); 240 assertFalse(gene1.equals(gene2)); 241 assertFalse(gene2.equals(gene1)); 242 } 243 244 251 public void testEquals_9_3() 252 throws Exception { 253 BaseGene gene1 = new IntegerGene(conf, 1, 100); 254 gene1.setAllele(new Integer (8)); 255 gene1.setApplicationData(new Integer (5)); 256 gene1.setCompareApplicationData(false); 257 Gene gene2 = new IntegerGene(conf, 1, 100); 258 gene2.setCompareApplicationData(false); 259 gene2.setApplicationData(new Integer (7)); 260 gene2.setAllele(new Integer (8)); 261 assertTrue(gene1.equals(gene2)); 262 assertTrue(gene2.equals(gene1)); 263 } 264 265 public void testIntValue_0() 266 throws Exception { 267 IntegerGene gene1 = new IntegerGene(conf, 1, 10000); 268 gene1.setAllele(new Integer (4711)); 269 assertEquals(4711, gene1.intValue()); 270 } 271 272 public void testIntValue_1() 273 throws Exception { 274 IntegerGene gene1 = new IntegerGene(conf, 1, 10000); 275 gene1.setAllele(null); 276 try { 277 assertEquals(0, gene1.intValue()); 278 fail(); 279 } catch (NullPointerException nullex) { 280 ; } 282 } 283 284 288 public void testSetAllele_0() 289 throws Exception { 290 Gene gene1 = new IntegerGene(conf, 1, 10000); 291 gene1.setAllele(null); 292 } 293 294 public void testSetAllele_1() 295 throws Exception { 296 Gene gene1 = new IntegerGene(conf, 1, 10000); 297 try { 298 gene1.setAllele("22"); 299 fail(); 300 } catch (ClassCastException classex) { 301 ; } 303 } 304 305 public void testNewGene_0() 306 throws Exception { 307 IntegerGene gene1 = new IntegerGene(conf, 1, 10000); 308 IGeneConstraintChecker checker = new GeneConstraintChecker(); 309 gene1.setConstraintChecker(checker); 310 gene1.setAllele(new Integer (4711)); 311 int lower1 = gene1.getLowerBounds(); 312 int upper1 = gene1.getUpperBounds(); 313 IntegerGene gene2 = (IntegerGene) gene1.newGene(); 314 int lower2 = gene2.getLowerBounds(); 315 int upper2 = gene2.getUpperBounds(); 316 assertEquals(lower1, lower2); 317 assertEquals(upper1, upper2); 318 assertEquals(checker, gene2.getConstraintChecker()); 319 } 320 321 public void testCleanup() 322 throws Exception { 323 Gene gene = new IntegerGene(conf, 1, 6); 325 Gene copy = gene.newGene(); 326 gene.cleanup(); 327 assertEquals(copy, gene); 328 } 329 330 public void testPersistentRepresentation_0() 331 throws Exception { 332 Gene gene1 = new IntegerGene(conf, 2, 753); 333 gene1.setAllele(new Integer (45)); 334 String pres1 = gene1.getPersistentRepresentation(); 335 Gene gene2 = new IntegerGene(conf); 336 gene2.setValueFromPersistentRepresentation(pres1); 337 String pres2 = gene2.getPersistentRepresentation(); 338 assertEquals(pres1, pres2); 339 } 340 341 348 public void testPersistentRepresentation_1() 349 throws Exception { 350 Gene gene1 = new IntegerGene(conf, 2, 753); 351 gene1.setAllele(new Integer (45)); 352 gene1.setValueFromPersistentRepresentation(null); 353 } 354 355 361 public void testPersistentRepresentation_2() 362 throws Exception { 363 IntegerGene gene1 = new IntegerGene(conf, 2, 753); 364 gene1.setAllele(new Integer (45)); 365 gene1.setValueFromPersistentRepresentation("2" 366 + IntegerGene. 367 PERSISTENT_FIELD_DELIMITER 368 + "3" 369 + IntegerGene. 370 PERSISTENT_FIELD_DELIMITER 371 + "4"); 372 assertEquals(2, ( (Integer ) gene1.getAllele()).intValue()); 373 assertEquals(3, gene1.getLowerBounds()); 374 assertEquals(4, gene1.getUpperBounds()); 375 } 376 377 383 public void testPersistentRepresentation_3() 384 throws Exception { 385 IntegerGene gene1 = new IntegerGene(conf, 2, 753); 386 gene1.setAllele(new Integer (45)); 387 gene1.setValueFromPersistentRepresentation("null" 388 + IntegerGene. 389 PERSISTENT_FIELD_DELIMITER 390 + "-3" 391 + IntegerGene. 392 PERSISTENT_FIELD_DELIMITER 393 + "4"); 394 assertNull(gene1.getAllele()); 395 assertEquals( -3, gene1.getLowerBounds()); 396 assertEquals(4, gene1.getUpperBounds()); 397 } 398 399 405 public void testPersistentRepresentation_4() 406 throws Exception { 407 Gene gene1 = new IntegerGene(conf, 2, 753); 408 gene1.setAllele(new Integer (45)); 409 try { 410 gene1.setValueFromPersistentRepresentation("null" 411 + IntegerGene. 412 PERSISTENT_FIELD_DELIMITER 413 + "3.5" 414 + IntegerGene. 415 PERSISTENT_FIELD_DELIMITER 416 + "4"); 417 fail(); 418 } catch (UnsupportedRepresentationException uex) { 419 ; } 421 } 422 423 429 public void testPersistentRepresentation_5() 430 throws Exception { 431 Gene gene1 = new IntegerGene(conf, 2, 753); 432 gene1.setAllele(new Integer (45)); 433 try { 434 gene1.setValueFromPersistentRepresentation("null" 435 + IntegerGene. 436 PERSISTENT_FIELD_DELIMITER 437 + "3" 438 + IntegerGene. 439 PERSISTENT_FIELD_DELIMITER 440 + "a"); 441 fail(); 442 } catch (UnsupportedRepresentationException uex) { 443 ; } 445 } 446 447 454 public void testPersistentRepresentation_6() 455 throws Exception { 456 IntegerGene gene1 = new IntegerGene(conf); 457 assertEquals("null" + IntegerGene.PERSISTENT_FIELD_DELIMITER 458 + Integer.MIN_VALUE + IntegerGene.PERSISTENT_FIELD_DELIMITER 459 + Integer.MAX_VALUE, 460 gene1.getPersistentRepresentation()); 461 } 462 463 469 public void testCompareToNative_0() 470 throws Exception { 471 Gene gene1 = new IntegerGene(conf, 13, 65); 472 gene1.setAllele(new Integer (58)); 473 Gene gene2 = new IntegerGene(conf, 53, 67); 474 gene2.setAllele(new Integer (59)); 475 assertEquals( ( (Integer ) gene1.getAllele()).compareTo( (Integer ) gene2. 476 getAllele()), gene1.compareTo(gene2)); 477 } 478 479 485 public void testCompareToNative_1() 486 throws Exception { 487 Gene gene1 = new IntegerGene(conf, 13, 65); 488 gene1.setAllele(new Integer (58)); 489 Gene gene2 = new IntegerGene(conf, 53, 67); 490 gene2.setAllele(new Integer (58)); 491 assertEquals( ( (Integer ) gene1.getAllele()).compareTo( (Integer ) gene2. 492 getAllele()), gene1.compareTo(gene2)); 493 } 494 495 501 public void testCompareToNative_2() 502 throws Exception { 503 Gene gene1 = new IntegerGene(conf, 13, 65); 504 gene1.setAllele(new Integer (59)); 505 Gene gene2 = new IntegerGene(conf, 53, 67); 506 gene2.setAllele(new Integer (58)); 507 assertEquals( ( (Integer ) gene1.getAllele()).compareTo( (Integer ) gene2. 508 getAllele()), gene1.compareTo(gene2)); 509 } 510 511 517 public void testCompareToNative_3() 518 throws Exception { 519 Configuration conf = new ConfigurationForTest(); 520 Gene gene1 = new IntegerGene(conf, 13, 65); 521 gene1.setAllele(new Integer (59)); 522 Gene gene2 = new IntegerGene(conf, 53, 67); 523 gene2.setAllele(new Integer ( -59)); 524 assertEquals( ( (Integer ) gene1.getAllele()).compareTo( (Integer ) gene2. 525 getAllele()), gene1.compareTo(gene2)); 526 } 527 528 534 public void testCompareToNative_4() 535 throws Exception { 536 Configuration conf = new ConfigurationForTest(); 537 Gene gene1 = new IntegerGene(conf, 13, 65); 538 gene1.setAllele(new Integer (0)); 539 Gene gene2 = new IntegerGene(conf, 53, 67); 540 gene2.setAllele(new Integer ( -0)); 541 assertEquals( ( (Integer ) gene1.getAllele()).compareTo( (Integer ) gene2. 542 getAllele()), gene1.compareTo(gene2)); 543 } 544 545 551 public void testCompareTo_0() 552 throws Exception { 553 Configuration conf = new ConfigurationForTest(); 554 Gene gene1 = new IntegerGene(conf, 13, 65); 555 gene1.setAllele(new Integer (0)); 556 Gene gene2 = new IntegerGene2(conf, 53, 67); 557 gene2.setAllele(new Integer ( -0)); 558 try { 559 gene1.compareTo(gene2); 560 fail(); 561 } catch (ClassCastException cex) { 562 ; } 564 } 565 566 572 public void testCompareTo_1() 573 throws Exception { 574 Gene gene1 = new IntegerGene(conf, 13, 65); 575 gene1.setAllele(new Integer (58)); 576 Gene gene2 = new IntegerGene(conf, 53, 67); 577 gene2.setAllele(new Integer (59)); 578 assertEquals( -1, gene1.compareTo(gene2)); 579 } 580 581 587 public void testCompareTo_2() 588 throws Exception { 589 Gene gene1 = new IntegerGene(conf, 13, 65); 590 gene1.setAllele(new Integer (58)); 591 Gene gene2 = new IntegerGene(conf, 53, 67); 592 gene2.setAllele(new Integer (58)); 593 assertEquals(0, gene1.compareTo(gene2)); 594 } 595 596 602 public void testCompareTo_3() 603 throws Exception { 604 Gene gene1 = new IntegerGene(conf, 13, 65); 605 gene1.setAllele(new Integer (59)); 606 Gene gene2 = new IntegerGene(conf, 53, 67); 607 gene2.setAllele(new Integer (58)); 608 assertEquals(1, gene1.compareTo(gene2)); 609 assertEquals( -1, gene2.compareTo(gene1)); 610 } 611 612 618 public void testCompareTo_4() 619 throws Exception { 620 Gene gene1 = new IntegerGene(conf, 13, 65); 621 Gene gene2 = new IntegerGene(conf, 53, 67); 622 assertEquals(0, gene1.compareTo(gene2)); 623 assertEquals(0, gene2.compareTo(gene1)); 624 } 625 626 632 public void testApplyMutation_0() 633 throws Exception { 634 IntegerGene gene = new IntegerGene(conf, 0, 100); 635 gene.setAllele(new Integer (50)); 636 gene.applyMutation(0, 0.0d); 637 assertEquals(50, gene.intValue()); 638 } 639 640 646 public void testApplyMutation_1() 647 throws Exception { 648 conf.setRandomGenerator(new RandomGeneratorForTest(15)); 649 IntegerGene gene = new IntegerGene(conf, 0, 100); 650 gene.setAllele(new Integer (50)); 651 gene.applyMutation(0, 0.5d); 652 assertEquals(Math.round(50 + (100 - 0) * 0.5d), gene.intValue()); 653 } 654 655 661 public void testApplyMutation_2() 662 throws Exception { 663 conf.setRandomGenerator(new RandomGeneratorForTest(15)); 664 IntegerGene gene = new IntegerGene(conf, 44, 100); 665 gene.setAllele(new Integer (50)); 666 gene.applyMutation(0, 0.3d); 667 assertEquals(Math.round(50 + (100 - 44) * 0.3d), gene.intValue()); 668 } 669 670 676 public void testApplyMutation_3() 677 throws Exception { 678 conf.setRandomGenerator(new RandomGeneratorForTest(15)); 679 IntegerGene gene = new IntegerGene(conf, 33, 100); 680 gene.setAllele(new Integer (50)); 681 gene.applyMutation(0, 1.9d); 682 assertEquals(Math.round(33 + 15), gene.intValue()); 683 } 684 685 691 public void testApplyMutation_4() 692 throws Exception { 693 conf.setRandomGenerator(new RandomGeneratorForTest(15)); 694 IntegerGene gene = new IntegerGene(conf, 2, 100); 695 gene.setAllele(new Integer (60)); 696 gene.applyMutation(0, 1.9d); 697 assertEquals(Math.round(2 + 15), gene.intValue()); 698 } 699 700 706 public void testApplyMutation_5() 707 throws Exception { 708 conf.setRandomGenerator(new RandomGeneratorForTest(15)); 709 IntegerGene gene = new IntegerGene(conf, 0, 100); 710 gene.setAllele(new Integer (60)); 711 gene.applyMutation(0, -1.0d); 712 assertEquals(Math.round(0 + 15), gene.intValue()); 713 } 714 715 721 public void testApplyMutation_6() 722 throws Exception { 723 IntegerGene gene = new IntegerGene(conf, 0, 100); 724 gene.setAllele(new Integer (60)); 725 gene.applyMutation(0, -0.4d); 726 assertEquals(Math.round(60 + (100 * ( -0.4d))), gene.intValue()); 727 } 728 729 735 public void testApplyMutation_7() 736 throws Exception { 737 IntegerGene gene = new IntegerGene(conf, 0, 100); 738 gene.setAllele(null); 739 gene.applyMutation(0, 0.4d); 740 assertEquals(Math.round( (100 * (0.4d))), gene.intValue()); 741 } 742 743 749 public void testApplyMutation_8() 750 throws Exception { 751 IntegerGene gene = new IntegerGene(conf, 5, 100); 752 gene.setAllele(null); 753 conf.setRandomGenerator(new RandomGeneratorForTest(10)); 754 gene.applyMutation(0, -0.4d); 755 assertEquals(Math.round(10 + 5), gene.intValue()); 756 } 757 758 764 public void testSetToRandomValue_0() 765 throws Exception { 766 Gene gene = new IntegerGene(conf, 1, 6); 767 gene.setAllele(new Integer (5)); 768 gene.setToRandomValue(new RandomGeneratorForTest(0.2d)); 769 assertEquals(new Integer ( (int) (0.2d * (6 - 1) + 1)), gene.getAllele()); 770 } 771 772 778 public void testSetToRandomValue_1() 779 throws Exception { 780 Gene gene = new IntegerGene(conf, -1, 7); 781 gene.setAllele(new Integer (4)); 782 gene.setToRandomValue(new RandomGeneratorForTest(0.3d)); 783 assertEquals(new Integer ( (int) (0.3d * (7 + 1) - 1)), gene.getAllele()); 784 } 785 786 792 public void testSetToRandomValue_2() 793 throws Exception { 794 Configuration conf = new ConfigurationForTest(); 795 Gene gene = new IntegerGene(conf, -2, -1); 796 gene.setAllele(new Integer (4)); 797 gene.setToRandomValue(new RandomGeneratorForTest(0.8d)); 798 assertEquals(new Integer ( (int) (0.8d * ( -1 + 2) - 2)), gene.getAllele()); 799 } 800 801 807 public void testSetToRandomValue_3() 808 throws Exception { 809 IntegerGene gene = new IntegerGene(conf, 0, 8); 810 gene.setAllele(new Integer (5)); 811 gene.setToRandomValue(new RandomGeneratorForTest(4)); 812 if (gene.intValue() < 0 813 || gene.intValue() > 8) { 814 fail(); 815 } 816 } 817 818 824 public void testSetToRandomValue_4() 825 throws Exception { 826 IntegerGene gene = new IntegerGene(conf, 1, 6); 827 gene.setAllele(new Integer (2)); 828 gene.setToRandomValue(new RandomGeneratorForTest(3)); 829 if (gene.intValue() < 1 830 || gene.intValue() > 6) { 831 fail(); 832 } 833 } 834 835 842 public void testSetToRandomValue_5() 843 throws Exception { 844 Configuration conf = new ConfigurationForTest(); 845 Gene gene = new IntegerGene(conf, 1, 3); 846 gene.setAllele(new Integer (4)); 847 gene.setToRandomValue(new RandomGeneratorForTest(0.95d)); 848 assertEquals(new Integer (3), gene.getAllele()); 849 gene.setToRandomValue(new RandomGeneratorForTest(0.05d)); 850 assertEquals(new Integer (1), gene.getAllele()); 851 } 852 853 859 public void testSetEnergy_0() 860 throws Exception { 861 BaseGene gene = new IntegerGene(conf); 862 assertEquals(0.0, gene.getEnergy(), DELTA); 863 } 864 865 871 public void testSetEnergy_1() 872 throws Exception { 873 BaseGene gene = new IntegerGene(conf); 874 gene.setEnergy(2.3); 875 assertEquals(2.3, gene.getEnergy(), DELTA); 876 gene.setEnergy( -55.8); 877 assertEquals( -55.8, gene.getEnergy(), DELTA); 878 gene.setEnergy(0.5); 879 gene.setEnergy(0.8); 880 assertEquals(0.8, gene.getEnergy(), DELTA); 881 } 882 883 890 class IntegerGene2 891 extends IntegerGene { 892 public IntegerGene2() 893 throws InvalidConfigurationException { 894 super(Genotype.getStaticConfiguration()); 895 } 896 897 public IntegerGene2(final Configuration a_conf, int a_lowerBounds, 898 int a_upperBounds) 899 throws InvalidConfigurationException { 900 super(a_conf, a_lowerBounds, a_upperBounds); 901 } 902 } 903 class GeneConstraintChecker 904 implements IGeneConstraintChecker { 905 public boolean verify(Gene a_gene, Object a_alleleValue, 906 IChromosome a_chrom, int a_index) { 907 return true; 908 } 909 } 910 916 public void testGetBounds_0() 917 throws Exception { 918 IntegerGene gene = new IntegerGene(conf, 2, 5); 919 assertEquals(2, gene.getLowerBounds()); 920 assertEquals(5, gene.getUpperBounds()); 921 } 922 } 923 | Popular Tags |