1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class CompositeGeneTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.47 $"; 26 27 private static int cleanedUp = 0; 28 29 public static Test suite() { 30 TestSuite suite = new TestSuite(CompositeGeneTest.class); 31 return suite; 32 } 33 34 39 public void testCompareTo_4() 40 throws Exception { 41 Configuration conf = new ConfigurationForTest(); 42 CompositeGene gene1 = new CompositeGene(conf); 43 Gene newGene1 = new IntegerGene(conf, 3, 5); 44 newGene1.setAllele(new Integer (4)); 45 Gene newGene2 = new IntegerGene(conf, 3, 5); 46 newGene2.setAllele(new Integer (2)); 47 gene1.addGene(newGene1, false); 48 gene1.addGene(newGene2, false); 49 CompositeGene gene2 = new CompositeGene(conf); 50 Gene newGene3 = new IntegerGene(conf, 3, 5); 51 newGene3.setAllele(new Integer (1)); 52 newGene3.setAllele(new Integer (3)); 53 gene2.addGene(newGene3, false); 54 assertEquals(1, gene1.compareTo(gene2)); 55 assertEquals( -1, gene2.compareTo(gene1)); 56 } 57 58 63 public void testConstruct_0() 64 throws Exception { 65 CompositeGene gene = new CompositeGene(conf); 67 assertEquals(null, gene.getGeneTypeAllowed()); 68 } 69 70 76 public void testConstruct_1() 77 throws Exception { 78 Genotype.setStaticConfiguration(conf); 79 CompositeGene gene = new CompositeGene(); 80 assertSame(conf, gene.getConfiguration()); 81 } 82 83 91 public void testAddGene_0() 92 throws Exception { 93 CompositeGene gene = new CompositeGene(conf); 94 gene.addGene(new CompositeGene(conf), false); 95 assertEquals(1, gene.size()); 96 } 97 98 public void testAddGene_1() 99 throws Exception { 100 CompositeGene gene = new CompositeGene(conf); 101 Gene newGene = new DoubleGene(conf); 102 gene.addGene(newGene, false); 103 assertEquals(1, gene.size()); 104 gene.addGene(new DoubleGene(conf, 1.2d, 3.4d), false); 105 assertEquals(2, gene.size()); 106 gene.removeGeneByIdentity(new DoubleGene(conf)); 108 assertEquals(2, gene.size()); 109 try { 111 gene.addGene(newGene, false); 112 fail(); 113 } 114 catch (IllegalArgumentException iex) { 115 ; } 117 } 118 119 125 public void testAddGene_2() 126 throws Exception { 127 CompositeGene gene = new CompositeGene(conf, new DoubleGene(conf, 2, 3)); 128 assertEquals(new DoubleGene(conf), gene.getGeneTypeAllowed()); 129 gene.addGene(new DoubleGene(conf), false); 130 assertEquals(1, gene.size()); 131 } 132 133 139 public void testAddGene_3() 140 throws Exception { 141 CompositeGene gene = new CompositeGene(conf, new DoubleGene(conf, 2, 3)); 142 try { 143 gene.addGene(new CompositeGene(conf), false); 144 fail(); 145 } 146 catch (IllegalArgumentException iex) { 147 ; } 149 } 150 151 157 public void testAddGene_4() 158 throws Exception { 159 CompositeGene gene = new CompositeGene(conf); 160 gene.addGene(new CompositeGene(conf), true); 161 assertEquals(1, gene.size()); 162 } 163 164 170 public void testAddGene_5() 171 throws Exception { 172 CompositeGene gene = new CompositeGene(conf); 173 CompositeGene gene2 = new CompositeGene(conf); 174 gene.addGene(gene2, true); 175 try { 176 gene.addGene(gene2, false); 177 fail(); 178 } 179 catch (IllegalArgumentException iex) { 180 ; } 182 } 183 184 191 public void testAddGene_6() 192 throws Exception { 193 CompositeGene gene = new CompositeGene(conf); 194 gene.addGene(new CompositeGene(conf), true); 195 try { 196 gene.addGene(new CompositeGene(conf), true); 197 fail(); 198 } 199 catch (IllegalArgumentException iex) { 200 ; } 202 } 203 204 212 public void testAddGene_7() 213 throws Exception { 214 CompositeGene gene = new CompositeGene(conf); 215 gene.addGene(new CompositeGene(conf), true); 216 gene.addGene(new CompositeGene(conf), false); 217 } 218 219 224 public void testToString_0() 225 throws Exception { 226 CompositeGene gene = new CompositeGene(conf); 227 Gene newGene1 = new DoubleGene(conf); 228 newGene1.setAllele(new Double (47.123d)); 229 gene.addGene(newGene1, false); 230 assertEquals("CompositeGene=(" + newGene1.toString() + ")", gene.toString()); 231 Gene newGene2 = new IntegerGene(conf); 232 newGene2.setAllele(new Integer (23456)); 233 gene.addGene(newGene2, false); 234 assertEquals("CompositeGene=(" + newGene1.toString() + gene.GENE_DELIMITER 235 + newGene2.toString() + ")", gene.toString()); 236 } 237 238 244 public void testToString_1() 245 throws Exception { 246 CompositeGene gene = new CompositeGene(conf); 247 assertEquals("CompositeGene=null", gene.toString()); 248 } 249 250 public void testGetAllele_0() 251 throws Exception { 252 Gene gene = new CompositeGene(conf); 253 gene.setAllele(new Vector()); 255 } 256 257 public void testGetAllele_1() 258 throws Exception { 259 CompositeGene gene = new CompositeGene(conf); 260 Gene newGene1 = new IntegerGene(conf, 1, 5); 261 gene.addGene(newGene1, false); 262 Vector v = new Vector(); 263 v.add(new Integer (4)); 264 gene.setAllele(v); 265 v = (Vector) gene.getAllele(); 266 assertEquals(newGene1.getAllele(), v.elementAt(0)); 267 assertEquals(1, v.size()); 268 } 269 270 public void testGetAllele_2() 271 throws Exception { 272 CompositeGene gene = new CompositeGene(conf); 273 Gene newGene1 = new IntegerGene(conf, 1, 5); 274 gene.addGene(newGene1, false); 275 Gene newGene2 = new DoubleGene(conf, 77.2d, 999.0d); 276 gene.addGene(newGene2, false); 277 Vector v = new Vector(); 278 v.add(new Integer (4)); 279 v.add(new Double (333.5d)); 280 gene.setAllele(v); 281 v = (Vector) gene.getAllele(); 282 assertEquals(newGene1.getAllele(), v.elementAt(0)); 283 assertEquals(newGene2.getAllele(), v.elementAt(1)); 284 assertEquals(2, v.size()); 285 } 286 287 public void testEquals_0() 288 throws Exception { 289 Gene gene1 = new CompositeGene(conf); 290 Gene gene2 = new CompositeGene(conf); 291 assertTrue(gene1.equals(gene2)); 292 assertTrue(gene2.equals(gene1)); 293 } 294 295 public void testEquals_1() 296 throws Exception { 297 Gene gene1 = new CompositeGene(conf); 298 assertFalse(gene1.equals(null)); 299 } 300 301 public void testEquals_2() 302 throws Exception { 303 Gene gene1 = new CompositeGene(conf); 304 assertFalse(gene1.equals(new BooleanGene(conf))); 305 } 306 307 public void testEquals_3() 308 throws Exception { 309 Gene gene1 = new CompositeGene(conf); 310 assertFalse(gene1.equals(new Vector())); 311 } 312 313 320 public void testEquals_4_2() 321 throws Exception { 322 CompositeGene gene1 = new CompositeGene(conf, new BooleanGene(conf)); 323 try { 324 gene1.addGene(null, false); 325 fail(); 326 } 327 catch (IllegalArgumentException iex) { 328 ; } 330 } 331 332 public void testEquals_5() 333 throws Exception { 334 CompositeGene gene1 = new CompositeGene(conf); 335 Gene newGene1 = new IntegerGene(conf, 3, 5); 336 gene1.addGene(newGene1, false); 337 CompositeGene gene2 = new CompositeGene(conf); 338 gene2.addGene(newGene1, false); 339 assertTrue(gene1.equals(gene2)); 340 assertTrue(gene2.equals(gene1)); 341 } 342 343 349 public void testEquals_5_2() 350 throws Exception { 351 CompositeGene gene1 = new CompositeGene(conf); 352 Gene newGene1 = new IntegerGene(conf, 3, 5); 353 Gene newGene2 = new IntegerGene(conf, 3, 7); 354 gene1.addGene(newGene1, false); 355 gene1.addGene(newGene2, false); 356 CompositeGene gene2 = new CompositeGene(conf); 357 gene2.addGene(newGene2, false); 358 assertFalse(gene1.equals(gene2)); 359 assertFalse(gene2.equals(gene1)); 360 } 361 362 public void testEquals_6() 363 throws Exception { 364 CompositeGene gene1 = new CompositeGene(conf); 365 Gene newGene1 = new IntegerGene(conf, 3, 5); 366 gene1.addGene(newGene1, false); 367 Gene newGene2 = new IntegerGene(conf, 7, 9); 368 CompositeGene gene2 = new CompositeGene(conf); 369 gene2.addGene(newGene2, false); 370 assertEquals(newGene1.equals(newGene2), gene1.equals(gene2)); 371 assertEquals(newGene2.equals(newGene1), gene2.equals(gene1)); 372 } 373 374 public void testEquals_7() 375 throws Exception { 376 CompositeGene gene1 = new CompositeGene(conf); 377 Gene newGene1 = new IntegerGene(conf, 3, 5); 378 gene1.addGene(newGene1, false); 379 Gene newGene2 = new DoubleGene(conf, 7.2d, 59.4d); 380 CompositeGene gene2 = new CompositeGene(conf); 381 gene2.addGene(newGene2, false); 382 assertEquals(newGene1.equals(newGene2), gene1.equals(gene2)); 383 assertEquals(newGene2.equals(newGene1), gene2.equals(gene1)); 384 } 385 386 392 public void testEquals_8() 393 throws Exception { 394 Gene newGene1 = new IntegerGene(conf, 3, 5); 395 newGene1.setAllele(new Integer (3)); 396 Gene newGene2 = new IntegerGene(conf, 3, 15); 397 newGene2.setAllele(new Integer (9)); 398 CompositeGene gene1 = new CompositeGene(conf); 399 gene1.addGene(newGene1, false); 400 CompositeGene gene2 = new CompositeGene(conf); 401 gene2.addGene(newGene2, false); 402 assertFalse(gene1.equals(gene2)); 403 assertFalse(gene2.equals(gene1)); 404 } 405 406 public void testSetAllele_0() 407 throws Exception { 408 Gene gene = new CompositeGene(conf); 409 try { 410 gene.setAllele(null); 411 fail(); 412 } 413 catch (IllegalArgumentException iex) { 414 ; } 416 } 417 418 public void testSetAllele_1() 419 throws Exception { 420 Gene gene = new CompositeGene(conf); 421 try { 422 gene.setAllele(new Double (2.3d)); 423 fail(); 424 } 425 catch (IllegalArgumentException iex) { 426 ; } 428 } 429 430 435 public void testSetAllele_2() 436 throws Exception { 437 Gene gene1 = new CompositeGene(conf); 438 gene1.setAllele(new Vector()); 439 } 440 441 public void testSetAllele_3() 442 throws Exception { 443 Gene gene1 = new CompositeGene(conf); 444 try { 445 gene1.setAllele("22"); 446 fail(); 447 } 448 catch (IllegalArgumentException classex) { 449 ; } 451 } 452 453 459 public void testSetAllele_4() 460 throws Exception { 461 CompositeGene gene = new CompositeGene(conf); 462 DoubleGene gene2 = new DoubleGene(conf, 1.0d, 3.0d); 463 gene2.setAllele(new Double (1.0d)); 464 gene.addGene(gene2); 465 gene.setConstraintChecker(new IGeneConstraintChecker() { 466 public boolean verify(Gene a_gene, Object a_alleleValue, 467 IChromosome a_chrom, int a_index) { 468 return false; 469 } 470 }); 471 List l = new Vector(); 472 l.add(new Double (2.0d)); 473 gene.setAllele(l); 474 assertEquals(1.0d, ( (Double ) gene2.getAllele()).doubleValue(), DELTA); 475 } 476 477 483 public void testSetAllele_5() 484 throws Exception { 485 CompositeGene gene = new CompositeGene(conf); 486 DoubleGene gene2 = new DoubleGene(conf, 1.0d, 3.0d); 487 gene2.setAllele(new Double (1.0d)); 488 gene.addGene(gene2); 489 gene.setConstraintChecker(new IGeneConstraintChecker() { 490 public boolean verify(Gene a_gene, Object a_alleleValue, 491 IChromosome a_chrom, int a_index) { 492 return true; 493 } 494 }); 495 List l = new Vector(); 496 l.add(new Double (2.0d)); 497 gene.setAllele(l); 498 assertEquals(2.0d, ( (Double ) gene2.getAllele()).doubleValue(), DELTA); 499 } 500 501 public void testNewGene_0() 502 throws Exception { 503 CompositeGene gene1 = new CompositeGene(conf); 504 IGeneConstraintChecker checker = new GeneConstraintChecker(); 505 gene1.setConstraintChecker(checker); 506 gene1.addGene(new DoubleGene(conf, 2.05d, 7.53d), false); 507 gene1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false); 508 gene1.addGene(new IntegerGene(conf, 3, 8), false); 509 gene1.addGene(new BooleanGene(conf), false); 510 gene1.addGene(new StringGene(conf), false); 511 gene1.addGene(new StringGene(conf, 2, 5), false); 512 gene1.addGene(new StringGene(conf, 6, 11, "ABC"), false); 513 CompositeGene gene2 = (CompositeGene) gene1.newGene(); 514 assertTrue(gene1.equals(gene2)); 515 assertEquals(checker, gene2.getConstraintChecker()); 516 for (int i = 0; i < gene1.size(); i++) { 520 gene2.removeGene(gene1.geneAt(i)); 521 } 522 assertEquals(0, gene2.size()); 523 } 524 525 public void testPersistentPresentation_0() 526 throws Exception { 527 CompositeGene gene1 = new CompositeGene(conf); 528 Gene gene0 = new DoubleGene(conf, 2.05d, 7.53d); 529 gene0.setAllele(new Double (7.52d)); 530 gene1.addGene(gene0, false); 531 gene1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false); 532 gene0 = new IntegerGene(conf, 3, 8); 533 gene0.setAllele(new Integer (5)); 534 gene1.addGene(gene0, false); 535 gene0 = new BooleanGene(conf); 536 gene0.setAllele(Boolean.valueOf(true)); 537 gene1.addGene(gene0, false); 538 gene1.addGene(new StringGene(conf), false); 539 gene1.addGene(new StringGene(conf, 2, 5), false); 540 gene0 = new StringGene(conf, 6, 11, ":ABC"); gene0.setAllele("B:BCBCCA"); 542 gene1.addGene(gene0, false); 543 String pres1 = gene1.getPersistentRepresentation(); 544 CompositeGene gene2 = new CompositeGene(conf); 545 gene2.setValueFromPersistentRepresentation(pres1); 546 String pres2 = gene2.getPersistentRepresentation(); 547 assertEquals(pres1, pres2); 548 } 549 550 557 public void testPersistentPresentation_1() 558 throws Exception { 559 CompositeGene composite1 = new CompositeGene(conf); 560 Gene strgene = new DoubleGene(conf, 2.05d, 7.53d); 561 strgene.setAllele(new Double (7.52d)); 562 composite1.addGene(strgene, false); 563 composite1.addGene(new DoubleGene(conf, 128.35d, 155.90d), false); 564 strgene = new IntegerGene(conf, 3, 8); 565 strgene.setAllele(new Integer (5)); 566 composite1.addGene(strgene, false); 567 strgene = new BooleanGene(conf); 568 strgene.setAllele(Boolean.valueOf(true)); 569 composite1.addGene(strgene, false); 570 composite1.addGene(new StringGene(conf), false); 571 composite1.addGene(new StringGene(conf, 2, 5), false); 572 String string = "<!-- many:various:chars &%$§/()=<>C:CA/ -->"; 573 strgene = new StringGene(conf, 6, 50, "CA! many:various:chars<>:&%$§/()-="); 574 strgene.setAllele(string); 575 int stringPosition = composite1.size(); 577 composite1.addGene(strgene, false); 578 CompositeGene compositeInside = new CompositeGene(conf); 579 Gene istrgene = new DoubleGene(conf, 2.05d, 17.53d); 580 istrgene.setAllele(new Double (3.33)); 581 compositeInside.addGene(istrgene, false); 582 compositeInside.addGene(new DoubleGene(conf, 128.35d, 155.90d), false); 583 istrgene = new IntegerGene(conf, 3, 8); 584 istrgene.setAllele(new Integer (5)); 585 compositeInside.addGene(istrgene, false); 586 istrgene = new BooleanGene(conf); 587 istrgene.setAllele(Boolean.valueOf(true)); 588 compositeInside.addGene(istrgene, false); 589 compositeInside.addGene(new StringGene(conf), false); 590 compositeInside.addGene(new StringGene(conf, 2, 5), false); 591 istrgene = new StringGene(conf, 6, 11, "&xyzab<:>"); 592 String string2 = "<:yzab:>"; 593 istrgene.setAllele(string2); 594 int position2 = compositeInside.size(); 595 compositeInside.addGene(istrgene, false); 596 int whereCompositeGene = composite1.size(); 597 composite1.addGene(compositeInside); 598 String pres1 = composite1.getPersistentRepresentation(); 599 CompositeGene gene2 = new CompositeGene(conf); 600 gene2.setValueFromPersistentRepresentation(pres1); 601 String pres2 = gene2.getPersistentRepresentation(); 602 assertEquals(pres1, pres2); 603 StringGene s = (StringGene) gene2.geneAt(stringPosition); 605 assertTrue(string.equals(s.getAllele())); 606 CompositeGene cg = (CompositeGene) gene2.geneAt(whereCompositeGene); 608 s = (StringGene) cg.geneAt(position2); 609 assertTrue(string2.equals(s.getAllele())); 610 } 611 612 618 public void testPersistentPresentation_2() 619 throws Exception { 620 CompositeGene gene1 = new CompositeGene(conf); 621 gene1.setValueFromPersistentRepresentation(null); 622 assertEquals(0, gene1.size()); 623 } 624 625 632 public void testPersistentPresentation_3() 633 throws Exception { 634 CompositeGene gene1 = new CompositeGene(conf); 635 try { 636 gene1.setValueFromPersistentRepresentation("1" 637 + CompositeGene.GENE_DELIMITER 638 + "2"); 639 fail(); 640 } 641 catch (UnsupportedRepresentationException uex) { 642 ; } 644 } 645 646 654 public void testPersistentPresentation_4() 655 throws Exception { 656 CompositeGene gene1 = new CompositeGene(conf); 657 try { 658 gene1.setValueFromPersistentRepresentation("<1" 659 + CompositeGene.GENE_DELIMITER 660 + "2>"); 661 fail(); 662 } 663 catch (UnsupportedRepresentationException uex) { 664 ; } 666 } 667 668 675 public void testPersistentPresentation_5() 676 throws Exception { 677 CompositeGene gene1 = new CompositeGene(conf); 678 try { 679 gene1.setValueFromPersistentRepresentation("<1>"); 680 fail(); 681 } 682 catch (UnsupportedRepresentationException uex) { 683 ; } 685 } 686 687 695 public void testPersistentPresentation_6() 696 throws Exception { 697 CompositeGene gene1 = new CompositeGene(conf); 698 try { 699 gene1.setValueFromPersistentRepresentation("<IntegerGene" 700 + CompositeGene.GENE_DELIMITER 701 + "2<"); 702 fail(); 703 } 704 catch (UnsupportedRepresentationException uex) { 705 ; } 707 } 708 709 717 public void testPersistentPresentation_7() 718 throws Exception { 719 CompositeGene gene1 = new CompositeGene(conf); 720 try { 721 gene1.setValueFromPersistentRepresentation("<org.jgap.impl.IntegerGene" 722 + CompositeGene.GENE_DELIMITER 723 + "2:4:4" 724 + CompositeGene.GENE_DELIMITER 725 + "><>"); 726 fail(); 727 } 728 catch (UnsupportedRepresentationException uex) { 729 assertEquals(1, gene1.size()); 730 ; } 732 } 733 734 742 public void testRemoveGeneByIdentity_0() 743 throws Exception { 744 CompositeGene gene1 = new CompositeGene(conf); 745 assertFalse(gene1.removeGeneByIdentity(gene1)); 746 assertFalse(gene1.removeGeneByIdentity(null)); 747 } 748 749 756 public void testRemoveGeneByIdentity_1() 757 throws Exception { 758 CompositeGene gene1 = new CompositeGene(conf); 759 DoubleGene gene2 = new DoubleGene(conf, 1.0d, 4.0d); 760 gene1.addGene(gene2); 761 DoubleGene gene3 = new DoubleGene(conf, 1.0d, 5.0d); 762 assertFalse(gene1.removeGeneByIdentity(gene3)); 763 assertTrue(gene1.removeGeneByIdentity(gene2)); 764 assertEquals(0, gene1.size()); 765 } 766 767 774 public void testCleanup_0() 775 throws Exception { 776 CompositeGene gene = new CompositeGene(conf); 777 CompositeGene gene2 = new CompositeGene(conf) { 778 public void cleanup() { 779 cleanedUp++; 780 } 781 }; 782 CompositeGene gene3 = new CompositeGene(conf) { 783 public void cleanup() { 784 cleanedUp++; 785 } 786 }; 787 gene.addGene(gene2, true); 788 gene.addGene(gene3, false); 789 gene.cleanup(); 790 assertEquals(2, gene.size()); 791 assertEquals(2, cleanedUp); 792 } 793 794 801 public void testCleanup_1() 802 throws Exception { 803 CompositeGene gene = new CompositeGene(conf); 804 gene.cleanup(); 805 assertEquals(0, gene.size()); 806 } 807 808 815 public void testSetConstraintChecker_0() 816 throws Exception { 817 CompositeGene gene = new CompositeGene(conf); 818 assertNull(gene.getConstraintChecker()); 819 gene.setConstraintChecker(new IGeneConstraintChecker() { 820 public boolean verify(Gene a_gene, Object a_alleleValue, 821 IChromosome a_chrom, int a_index) { 822 return false; 823 } 824 }); 825 assertNotNull(gene.getConstraintChecker()); 826 } 827 828 835 public void testSetToRandomValue_0() 836 throws Exception { 837 Gene gene1 = new CompositeGene(conf); 838 try { 839 gene1.setToRandomValue(null); 840 fail(); 841 } 842 catch (IllegalArgumentException iex) { 843 ; } 845 } 846 847 854 public void testSetToRandomValue_1() 855 throws Exception { 856 CompositeGene gene1 = new CompositeGene(conf); 857 DoubleGene d = new DoubleGene(conf, 0.0d, 1.0d); 858 gene1.addGene(d); 859 gene1.setToRandomValue(new RandomGeneratorForTest(0.23d)); 860 assertEquals(0.23d, d.doubleValue(), DELTA); 861 } 862 863 870 public void testSetToRandomValue_2() 871 throws Exception { 872 CompositeGene gene1 = new CompositeGene(conf); 873 DoubleGene d = new DoubleGene(conf, 0.5d, 1.8d); 874 gene1.addGene(d); 875 gene1.setToRandomValue(new RandomGeneratorForTest(0.23d)); 876 assertEquals( (1.8d - 0.5d) * 0.23d + 0.5d, d.doubleValue(), DELTA); 877 } 878 879 886 public void testSetEnergy_0() 887 throws Exception { 888 BaseGene gene = new CompositeGene(conf); 889 assertEquals(0.0, gene.getEnergy(), DELTA); 890 } 891 892 899 public void testSetEnergy_1() 900 throws Exception { 901 BaseGene gene = new CompositeGene(conf); 902 gene.setEnergy(2.3); 903 assertEquals(2.3, gene.getEnergy(), DELTA); 904 gene.setEnergy( -55.8); 905 assertEquals( -55.8, gene.getEnergy(), DELTA); 906 gene.setEnergy(0.5); 907 gene.setEnergy(0.8); 908 assertEquals(0.8, gene.getEnergy(), DELTA); 909 } 910 911 918 public void testCompareTo_0() 919 throws Exception { 920 CompositeGene gene1 = new CompositeGene(conf); 921 Gene newGene1 = new IntegerGene(conf, 3, 5); 922 Gene newGene2 = new IntegerGene(conf, 3, 7); 923 gene1.addGene(newGene1, false); 924 gene1.addGene(newGene2, false); 925 CompositeGene gene2 = new CompositeGene(conf); 926 gene2.addGene(newGene2, false); 927 assertEquals(1, gene1.compareTo(gene2)); 928 assertEquals( -1, gene2.compareTo(gene1)); 929 } 930 931 938 public void testCompareTo_1() 939 throws Exception { 940 CompositeGene gene1 = new CompositeGene(conf); 941 Gene newGene1 = new IntegerGene(conf, 3, 5); 942 gene1.addGene(newGene1, false); 943 CompositeGene gene2 = new CompositeGene(conf); 944 gene2.addGene(newGene1, false); 945 assertEquals(0, gene1.compareTo(gene2)); 946 assertEquals(0, gene2.compareTo(gene1)); 947 } 948 949 956 public void testCompareTo_2() 957 throws Exception { 958 CompositeGene gene1 = new CompositeGene(conf); 959 Gene newGene1 = new IntegerGene(conf, 3, 5); 960 gene1.addGene(newGene1, false); 961 CompositeGene gene2 = new CompositeGene(conf); 962 Gene newGene2 = new IntegerGene(conf, 3, 5); 963 gene2.addGene(newGene2, false); 964 assertEquals(0, gene1.compareTo(gene2)); 965 assertEquals(0, gene2.compareTo(gene1)); 966 } 967 968 975 public void testCompareTo_3() 976 throws Exception { 977 CompositeGene gene1 = new CompositeGene(conf); 978 Gene newGene1 = new IntegerGene(conf, 3, 5); 979 gene1.addGene(newGene1, false); 980 Gene newGene2 = new IntegerGene(conf, 3, 5); 981 newGene2.setAllele(new Integer (2)); 982 int i = ( (Integer ) newGene2.getAllele()).intValue(); 983 assertTrue(i >= 3 && i <= 5); 984 } 985 986 public void testCompareTo_3_2() 987 throws Exception { 988 Configuration conf = new ConfigurationForTest(); 989 CompositeGene gene1 = new CompositeGene(conf); 990 Gene newGene1 = new IntegerGene(conf, 3, 5); 991 gene1.addGene(newGene1, false); 992 CompositeGene gene2 = new CompositeGene(conf); 993 Gene newGene2 = new IntegerGene(conf, 3, 5); 994 newGene2.setAllele(new Integer (2)); 995 gene2.addGene(newGene2, false); 996 assertEquals( -1, gene1.compareTo(gene2)); 997 assertEquals(1, gene2.compareTo(gene1)); 998 } 999 1000 1008 public void testCompareTo_5() 1009 throws Exception { 1010 CompositeGene gene1 = new CompositeGene(conf); 1011 gene1.setCompareApplicationData(true); 1012 Gene newGene1 = new IntegerGene(conf, 3, 5); 1013 gene1.addGene(newGene1, false); 1014 CompositeGene gene2 = new CompositeGene(conf); 1015 gene2.setCompareApplicationData(true); 1016 Gene newGene2 = new IntegerGene(conf, 3, 5); 1017 gene2.addGene(newGene2, false); 1018 assertEquals(0, gene1.compareTo(gene2)); 1019 assertEquals(0, gene2.compareTo(gene1)); 1020 gene1.setApplicationData(new Integer (2)); 1021 assertEquals(1, gene1.compareTo(gene2)); 1022 assertEquals( -1, gene2.compareTo(gene1)); 1023 gene2.setApplicationData(new Integer (3)); 1024 assertEquals( -1, gene1.compareTo(gene2)); 1025 assertEquals(1, gene2.compareTo(gene1)); 1026 newGene1.setApplicationData(new Integer (5)); 1027 newGene2.setApplicationData(new Integer (4)); 1028 assertEquals( -1, gene1.compareTo(gene2)); 1029 assertEquals(1, gene2.compareTo(gene1)); 1030 newGene1.setCompareApplicationData(true); 1031 newGene2.setCompareApplicationData(true); 1032 assertEquals(1, gene1.compareTo(gene2)); 1033 assertEquals( -1, gene2.compareTo(gene1)); 1034 } 1035 1036 1042 public void testCompareTo_6() 1043 throws Exception { 1044 Configuration conf = new ConfigurationForTest(); 1045 CompositeGene gene1 = new CompositeGene(conf); 1046 Gene newGene1 = new IntegerGene(conf, 3, 5); 1047 gene1.addGene(newGene1, false); 1048 CompositeGene gene2 = new CompositeGene(conf); 1049 assertEquals(1, gene1.compareTo(gene2)); 1050 assertEquals( -1, gene2.compareTo(gene1)); 1051 } 1052 1053 1060 public void testApplyMutation_0() 1061 throws Exception { 1062 CompositeGene gene = new CompositeGene(conf); 1063 try { 1064 gene.applyMutation(0, 0.5d); 1065 fail(); 1066 } 1067 catch (RuntimeException rex) { 1068 ; } 1070 } 1071 1072 1079 public void testGetAllele_3() 1080 throws Exception { 1081 CompositeGene gene = new CompositeGene(conf); 1082 assertNull(gene.getInternalValue()); 1083 } 1084 1085 1091 public void testHashCode_0() 1092 throws Exception { 1093 Configuration conf = new ConfigurationForTest(); 1094 CompositeGene c1 = new CompositeGene(conf); 1095 CompositeGene c2 = new CompositeGene(conf); 1096 assertEquals(c1.hashCode(), c2.hashCode()); 1097 Gene newGene1 = new IntegerGene(conf, 3, 5); 1098 c1.addGene(newGene1, false); 1099 assertFalse(c1.hashCode() == c2.hashCode()); 1100 assertEquals(c1.hashCode(), c1.hashCode()); 1101 Gene newGene2 = new IntegerGene(conf, 2, 5); 1102 c2.addGene(newGene2, false); 1103 assertTrue(c1.hashCode() == c2.hashCode()); 1104 newGene1.setAllele(new Integer (2)); 1105 assertFalse(c1.hashCode() == c2.hashCode()); 1106 } 1107 1108 class GeneConstraintChecker 1109 implements IGeneConstraintChecker { 1110 public boolean verify(Gene a_gene, Object a_alleleValue, 1111 IChromosome a_chrom, int a_index) { 1112 return true; 1113 } 1114 } 1115} 1116 | Popular Tags |