1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class StringGeneTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.35 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(StringGeneTest.class); 29 return suite; 30 } 31 32 37 public void testConstruct_0() 38 throws Exception { 39 StringGene gene = new StringGene(conf, 1, 100); 40 gene.setAllele("ABC"); 42 assertEquals(null, gene.getAlphabet()); 43 assertEquals(1, gene.getMinLength()); 44 assertEquals(100, gene.getMaxLength()); 45 } 46 47 52 public void testConstruct_1() 53 throws Exception { 54 try { 55 new StringGene(conf, 2, 1); 56 fail(); 57 } 58 catch (IllegalArgumentException iex) { 59 ; } 61 } 62 63 68 public void testConstruct_2() 69 throws Exception { 70 try { 71 new StringGene(conf, -1, 3); 72 fail(); 73 } 74 catch (IllegalArgumentException iex) { 75 ; } 77 } 78 79 84 public void testConstruct_3() 85 throws Exception { 86 try { 87 Gene gene = new StringGene(conf, 1, 3); 88 gene.setAllele("ABCD"); 89 } 90 catch (IllegalArgumentException iex) { 91 ; } 93 } 94 95 100 public void testConstruct_4() 101 throws Exception { 102 try { 103 Gene gene = new StringGene(conf, 1, 3); 104 gene.setAllele(new Double (2.3d)); 105 } 106 catch (ClassCastException castex) { 107 ; } 109 } 110 111 117 public void testConstruct_5() 118 throws Exception { 119 Genotype.setStaticConfiguration(conf); 120 StringGene gene = new StringGene(); 121 assertSame(conf, gene.getConfiguration()); 122 } 123 124 129 public void testSetAlphabet_1() 130 throws Exception { 131 StringGene gene = new StringGene(conf, 3, 5); 132 final String alphabet_const = "1234"; 133 gene.setAlphabet(alphabet_const); 134 String alphabet = (String ) privateAccessor.getField(gene, "m_alphabet"); 135 assertEquals(alphabet, alphabet_const); 136 assertEquals(alphabet, gene.getAlphabet()); 137 } 138 139 144 public void testToString_0() 145 throws Exception { 146 Gene gene = new StringGene(conf, 3, 7); 147 gene.setAllele("ABC"); 148 assertEquals("StringGene=ABC", gene.toString()); 149 } 150 151 157 public void testToString_1() 158 throws Exception { 159 StringGene gene = new StringGene(conf, 3, 7); 160 assertEquals("StringGene=null", gene.toString()); 161 } 162 163 169 public void testToString_2() 170 throws Exception { 171 StringGene gene = new StringGene(conf, 0, 7); 172 gene.setAllele(""); 173 assertEquals("StringGene=\"\"", gene.toString()); 174 } 175 176 181 public void testGetAllele_0() 182 throws Exception { 183 Gene gene = new StringGene(conf, 3, 5); 184 gene.setAllele("BCD"); 185 assertEquals("BCD", gene.getAllele()); 186 } 187 188 193 public void testEquals_0() 194 throws Exception { 195 Gene gene1 = new StringGene(conf, 1, 100); 196 Gene gene2 = new StringGene(conf, 1, 100); 197 assertTrue(gene1.equals(gene2)); 198 assertTrue(gene2.equals(gene1)); 199 } 200 201 206 public void testEquals_1() 207 throws Exception { 208 Gene gene1 = new StringGene(conf, 3, 100); 209 assertFalse(gene1.equals(null)); 210 } 211 212 217 public void testEquals_2() 218 throws Exception { 219 Gene gene1 = new StringGene(conf, 11, 77); 220 assertTrue(gene1.equals(new StringGene(conf))); 221 } 222 223 228 public void testEquals_3() 229 throws Exception { 230 Gene gene1 = new StringGene(conf, 11, 17); 231 assertFalse(gene1.equals(new Vector())); 232 } 233 234 239 public void testEquals_4() 240 throws Exception { 241 Gene gene1 = new StringGene(conf, 12, 100); 242 Gene gene2 = new StringGene(conf, 12, 99); 243 assertTrue(gene1.equals(gene2)); 244 assertTrue(gene2.equals(gene1)); 245 } 246 247 252 public void testEquals_5() 253 throws Exception { 254 Gene gene1 = new StringGene(conf, 2, 5); 255 Gene gene2 = new StringGene(conf, 1, 5); 256 gene1.setAllele("ABC"); 257 gene2.setAllele("AB"); 258 assertFalse(gene1.equals(gene2)); 259 assertFalse(gene2.equals(gene1)); 260 } 261 262 267 public void testEquals_6() 268 throws Exception { 269 Gene gene1 = new StringGene(conf, 2, 5); 270 Gene gene2 = new DoubleGene(conf, 1, 5); 271 assertFalse(gene1.equals(gene2)); 272 assertFalse(gene2.equals(gene1)); 273 } 274 275 280 public void testEquals_7() 281 throws Exception { 282 Gene gene1 = new StringGene(conf, 2, 5); 283 Gene gene2 = new BooleanGene(conf); 284 assertFalse(gene1.equals(gene2)); 285 assertFalse(gene2.equals(gene1)); 286 } 287 288 294 public void testEquals_8() 295 throws Exception { 296 Gene gene1 = new StringGene(conf, 2, 6); 297 gene1.setAllele("hallo"); 298 Gene gene2 = new StringGene(conf, 2, 6); 299 gene2.setAllele("hello"); 300 assertFalse(gene1.equals(gene2)); 301 assertFalse(gene2.equals(gene1)); 302 gene1.setAllele("hello1"); 303 assertFalse(gene1.equals(gene2)); 304 assertFalse(gene2.equals(gene1)); 305 gene2.setAllele("HELLO1"); 306 assertFalse(gene1.equals(gene2)); 307 assertFalse(gene2.equals(gene1)); 308 } 309 310 318 public void testEquals_9() 319 throws Exception { 320 Gene gene1 = new StringGene(conf, 2, 6); 321 gene1.setAllele("hallo"); 322 gene1.setApplicationData(new Double (2.3d)); 323 Gene gene2 = new StringGene(conf, 2, 6); 324 gene2.setAllele("hallo"); 325 assertTrue(gene1.equals(gene2)); 326 assertTrue(gene2.equals(gene1)); 327 gene1.setCompareApplicationData(true); 328 assertFalse(gene1.equals(gene2)); 329 assertTrue(gene2.equals(gene1)); 330 gene2.setCompareApplicationData(true); 331 assertFalse(gene1.equals(gene2)); 332 assertFalse(gene2.equals(gene1)); 333 gene2.setApplicationData(new Double (2.3d)); 334 assertTrue(gene1.equals(gene2)); 335 assertTrue(gene2.equals(gene1)); 336 } 337 338 346 public void testCompareTo_0() 347 throws Exception { 348 Gene gene1 = new StringGene(conf, 2, 6); 349 gene1.setAllele("hallo"); 350 gene1.setApplicationData(new Double (2.3d)); 351 Gene gene2 = new StringGene(conf, 2, 6); 352 gene2.setAllele("hallo"); 353 assertEquals(0, gene1.compareTo(gene2)); 354 assertEquals(0, gene2.compareTo(gene1)); 355 gene1.setCompareApplicationData(true); 356 assertEquals(1, gene1.compareTo(gene2)); 357 assertEquals(0, gene2.compareTo(gene1)); 358 gene2.setCompareApplicationData(true); 359 assertEquals(1, gene1.compareTo(gene2)); 360 assertEquals( -1, gene2.compareTo(gene1)); 361 gene2.setApplicationData(new Double (2.3d)); 362 assertEquals(0, gene1.compareTo(gene2)); 363 assertEquals(0, gene2.compareTo(gene1)); 364 gene2.setAllele(null); 365 assertEquals(1, gene1.compareTo(gene2)); 366 gene2.setApplicationData(null); 367 assertEquals(1, gene1.compareTo(gene2)); 368 } 369 370 378 public void testCompareTo_1() 379 throws Exception { 380 Gene gene1 = new StringGene(conf, 2, 6); 381 try { 382 gene1.compareTo(new Chromosome(conf)); 383 fail(); 384 } 385 catch (ClassCastException cex) { 386 ; } 388 } 389 390 398 public void testCompareTo_2() 399 throws Exception { 400 Gene gene1 = new StringGene(conf, 2, 6); 401 gene1.setApplicationData(new Double (2.3d)); 402 Gene gene2 = new StringGene(conf, 2, 6); 403 assertEquals(0, gene1.compareTo(gene2)); 404 assertEquals(0, gene2.compareTo(gene1)); 405 gene1.setCompareApplicationData(true); 406 assertEquals(1, gene1.compareTo(gene2)); 407 assertEquals(0, gene2.compareTo(gene1)); 408 gene2.setCompareApplicationData(true); 409 assertEquals(1, gene1.compareTo(gene2)); 410 assertEquals( -1, gene2.compareTo(gene1)); 411 gene2.setApplicationData(new Double (2.3d)); 412 assertEquals(0, gene1.compareTo(gene2)); 413 assertEquals(0, gene2.compareTo(gene1)); 414 gene2.setApplicationData(null); 415 assertEquals(1, gene1.compareTo(gene2)); 416 } 417 418 424 public void testSetAllele_0() 425 throws Exception { 426 Gene gene1 = new StringGene(conf, 0, 10000); 427 gene1.setAllele(null); 428 } 429 430 436 public void testSetAllele_1() 437 throws Exception { 438 Gene gene1 = new StringGene(conf, 3, 4); 439 try { 440 gene1.setAllele("AB"); 441 fail(); 442 } 443 catch (IllegalArgumentException iex) { 444 ; } 446 } 447 448 454 public void testSetAllele_2() 455 throws Exception { 456 Gene gene1 = new StringGene(conf, 3, 4, "ABCDEFHI"); 457 try { 458 gene1.setAllele("ABDG"); 459 fail(); 460 } 461 catch (IllegalArgumentException iex) { 462 ; } 464 } 465 466 471 public void testSetAllele_3() 472 throws Exception { 473 Gene gene1 = new StringGene(conf, 3, 4, "EGAL"); 474 try { 475 gene1.setAllele(""); 478 fail(); 479 } 480 catch (IllegalArgumentException iex) { 481 ; } 483 } 484 485 490 public void testSetAllele_4() 491 throws Exception { 492 Gene gene1 = new StringGene(conf, 0, 4, ""); 493 gene1.setAllele(""); 495 } 496 497 503 public void testSetAllele_5() 504 throws Exception { 505 StringGene gene1 = new StringGene(conf, 0, 4, "ABC"); 506 gene1.setAllele("A"); 508 gene1.setConstraintChecker(new IGeneConstraintChecker() { 509 public boolean verify(Gene a_gene, Object a_alleleValue, 510 IChromosome a_chrom, int a_index) { 511 return false; 512 } 513 }); 514 gene1.setAllele("B"); 515 assertEquals("A", gene1.stringValue()); 516 } 517 518 524 public void testSetAllele_6() 525 throws Exception { 526 StringGene gene1 = new StringGene(conf, 0, 4, "ABC"); 527 gene1.setAllele("A"); 529 gene1.setConstraintChecker(new IGeneConstraintChecker() { 530 public boolean verify(Gene a_gene, Object a_alleleValue, 531 IChromosome a_chrom, int a_index) { 532 return true; 533 } 534 }); 535 gene1.setAllele("B"); 536 assertEquals("B", gene1.stringValue()); 537 } 538 539 545 public void testNewGene_0() 546 throws Exception { 547 StringGene gene1 = new StringGene(conf, 1, 4); 548 IGeneConstraintChecker checker = new GeneConstraintChecker(); 549 gene1.setConstraintChecker(checker); 550 gene1.setAllele("XYZ"); 551 int minLength1 = gene1.getMinLength(); 552 int maxLength1 = gene1.getMaxLength(); 553 StringGene gene2 = (StringGene) gene1.newGene(); 554 int minLength2 = gene2.getMinLength(); 555 int maxLength2 = gene2.getMaxLength(); 556 assertEquals(minLength1, minLength2); 557 assertEquals(maxLength1, maxLength2); 558 assertEquals(checker, gene2.getConstraintChecker()); 559 } 560 561 567 public void testPersistentRepresentation_0() 568 throws Exception { 569 Gene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 570 gene1.setAllele("BABE"); 571 String pres1 = gene1.getPersistentRepresentation(); 572 Gene gene2 = new StringGene(conf); 573 gene2.setValueFromPersistentRepresentation(pres1); 574 String pres2 = gene2.getPersistentRepresentation(); 575 assertEquals(pres1, pres2); 576 } 577 578 583 public void testPersistentRepresentation_1() 584 throws Exception { 585 Gene gene1 = new StringGene(conf, 2, 10); 586 try { 587 gene1.setAllele(""); 588 fail(); 589 } 590 catch (IllegalArgumentException iex) { 591 ; } 593 } 594 595 601 public void testPersistentRepresentation_2() 602 throws Exception { 603 Gene gene1 = new StringGene(conf, 0, 10); 604 gene1.setAllele(""); 605 String pres1 = gene1.getPersistentRepresentation(); 606 Gene gene2 = new StringGene(conf); 607 gene2.setValueFromPersistentRepresentation(pres1); 608 String pres2 = gene2.getPersistentRepresentation(); 609 assertEquals(pres1, pres2); 610 } 611 612 618 public void testPersistentRepresentation_3() 619 throws Exception { 620 Gene gene1 = new StringGene(conf); 621 gene1.setAllele(""); 622 String pres1 = gene1.getPersistentRepresentation(); 623 Gene gene2 = new StringGene(conf); 624 gene2.setValueFromPersistentRepresentation(pres1); 625 String pres2 = gene2.getPersistentRepresentation(); 626 assertEquals(pres1, pres2); 627 } 628 629 635 public void testPersistentRepresentation_4() 636 throws Exception { 637 Gene gene1 = new StringGene(conf); 638 gene1.setAllele(null); 639 String pres1 = gene1.getPersistentRepresentation(); 640 Gene gene2 = new StringGene(conf); 641 gene2.setValueFromPersistentRepresentation(pres1); 642 String pres2 = gene2.getPersistentRepresentation(); 643 assertEquals(pres1, pres2); 644 } 645 646 652 public void testPersistentRepresentation_5() 653 throws Exception { 654 StringGene gene1 = new StringGene(conf, 2, 10, 655 "ABCDE" + CompositeGene.GENE_DELIMITER); 656 gene1.setAllele("BABE"); 657 String pres1 = gene1.getPersistentRepresentation(); 658 StringGene gene2 = new StringGene(conf); 659 gene2.setValueFromPersistentRepresentation(pres1); 660 String pres2 = gene2.getPersistentRepresentation(); 661 assertEquals(pres1, pres2); 662 assertEquals(gene1, gene2); 663 assertEquals(gene1.getAlphabet(), gene2.getAlphabet()); 664 } 665 666 672 public void testPersistentRepresentation_6() 673 throws Exception { 674 Gene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 675 gene1.setAllele("BABE"); 676 gene1.setValueFromPersistentRepresentation(null); 677 assertEquals("BABE", gene1.getAllele()); 678 } 679 680 686 public void testPersistentRepresentation_7() 687 throws Exception { 688 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 689 gene1.setAllele(null); 690 assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation()); 691 } 692 693 699 public void testPersistentRepresentation_8() 700 throws Exception { 701 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 702 gene1.setAllele(null); 703 try { 704 gene1.setValueFromPersistentRepresentation("null:2:ABCDE"); 705 fail(); 706 } 707 catch (UnsupportedRepresentationException uex) { 708 ; } 710 } 711 712 718 public void testPersistentRepresentation_9() 719 throws Exception { 720 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 721 assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation()); 722 } 723 724 731 public void testPersistentRepresentation_10() 732 throws Exception { 733 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 734 gene1.setAllele(null); 735 try { 736 gene1.setValueFromPersistentRepresentation("null:a:10:ABCDE"); 737 fail(); 738 } 739 catch (UnsupportedRepresentationException uex) { 740 ; } 742 } 743 744 751 public void testPersistentRepresentation_11() 752 throws Exception { 753 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 754 gene1.setAllele(null); 755 try { 756 gene1.setValueFromPersistentRepresentation("null:2:3b:ABCDE"); 757 fail(); 758 } 759 catch (UnsupportedRepresentationException uex) { 760 ; } 762 } 763 764 770 public void testPersistentRepresentation_12() 771 throws Exception { 772 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 773 gene1.setAllele(null); 774 try { 775 gene1.setValueFromPersistentRepresentation("nada:7:6:ABCDE"); 776 fail(); 777 } 778 catch (UnsupportedRepresentationException uex) { 779 ; } 781 } 782 783 789 public void testPersistentRepresentation_14() 790 throws Exception { 791 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 792 gene1.setAllele(null); 793 try { 794 gene1.setValueFromPersistentRepresentation("nada:1:3:ABCDE"); 795 fail(); 796 } 797 catch (UnsupportedRepresentationException uex) { 798 ; } 800 } 801 802 809 public void testPersistentRepresentation_15() 810 throws Exception { 811 StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE"); 812 gene1.setAllele(null); 813 try { 814 gene1.setValueFromPersistentRepresentation("ABHJ:4:7:ABCDE"); 815 fail(); 816 } 817 catch (UnsupportedRepresentationException uex) { 818 ; } 820 } 821 822 829 public void testApplyMutation_0() 830 throws Exception { 831 Configuration conf = new ConfigurationForTest(); 832 Gene gene1 = new StringGene(conf, 5, 5); 833 gene1.setAllele("12345"); 834 gene1.applyMutation(0, 0.99d); 835 gene1.applyMutation(4, -0.99d); 836 } 837 838 845 public void testApplyMutation_1() 846 throws Exception { 847 Configuration conf = new ConfigurationForTest(); 848 Gene gene1 = new StringGene(conf, 1, 1); 849 gene1.setAllele("1"); 850 gene1.applyMutation(0, 0.99d); 851 } 852 853 860 public void testApplyMutation_2() 861 throws Exception { 862 Configuration conf = new ConfigurationForTest(); 863 Gene gene1 = new StringGene(conf, 1, 1); 864 gene1.setAllele("1"); 865 try { 866 gene1.applyMutation(1, 0.99d); 867 fail(); 868 } 869 catch (StringIndexOutOfBoundsException sex) { 870 ; } 872 } 873 874 881 public void testApplyMutation_3() 882 throws Exception { 883 Configuration conf = new ConfigurationForTest(); 884 Gene gene1 = new StringGene(conf, 1, 1); 885 gene1.applyMutation(0, 0.99d); 886 } 887 888 892 public void testApplyMutation_4() 893 throws Exception { 894 Configuration conf = new ConfigurationForTest(); 895 Gene gene1 = new StringGene(conf, 6, 6, 896 StringGene.ALPHABET_CHARACTERS_LOWER); 897 gene1.setAllele("ijklmn"); 898 gene1.applyMutation(0, 0.3d); 899 assertFalse(gene1.getAllele().equals("ijklmn")); 900 gene1.setAllele("ijklmn"); 901 gene1.applyMutation(4, -0.3d); 902 assertFalse(gene1.getAllele().equals("ijklmn")); 903 } 904 905 911 public void testApplyMutation_5() 912 throws Exception { 913 Configuration conf = new ConfigurationForTest(); 914 Gene gene1 = new StringGene(conf, 6, 6, 915 StringGene.ALPHABET_CHARACTERS_LOWER); 916 gene1.setAllele("ijklmn"); 917 gene1.applyMutation(0, 0.0d); 918 assertEquals(gene1.getAllele(), "ijklmn"); 919 } 920 921 928 public void testApplyMutation_6() 929 throws Exception { 930 Configuration conf = new ConfigurationForTest(); 931 StringGene gene1 = new StringGene(conf, 6, 6); 932 gene1.setAllele("ijklmn"); 933 gene1.setAlphabet(""); 934 try { 935 gene1.applyMutation(0, 0.0d); 936 } 937 catch (IllegalArgumentException iex) { 938 ; } 940 } 941 942 949 public void testApplyMutation_7() 950 throws Exception { 951 Configuration conf = new ConfigurationForTest(); 952 conf.setRandomGenerator(null); 953 Gene gene1 = new StringGene(conf, 5, 5); 954 gene1.setAllele("12345"); 955 try { 956 gene1.applyMutation(0, 0.99d); 957 fail(); 958 } 959 catch (NullPointerException nex) { 960 ; } 962 } 963 964 969 public void testSetMinMaxLength_0() 970 throws Exception { 971 StringGene gene = new StringGene(conf); 972 gene.setMinLength(4); 973 gene.setMaxLength(3); 974 assertEquals(4, gene.getMinLength()); 975 assertEquals(3, gene.getMaxLength()); 976 } 977 978 983 public void testSetToRandomValue_0() 984 throws Exception { 985 StringGene gene = new StringGene(conf, 1, 6, 986 StringGene.ALPHABET_CHARACTERS_UPPER); 987 gene.setToRandomValue(new RandomGeneratorForTest(2)); 988 assertEquals("CCC", gene.getAllele()); 989 gene.setToRandomValue(new RandomGeneratorForTest(1)); 990 assertEquals("BB", gene.getAllele()); 991 gene.setToRandomValue(new RandomGeneratorForTest(0)); 992 assertEquals("A", gene.getAllele()); 993 } 994 995 1000 public void testSetToRandomValue_1() 1001 throws Exception { 1002 Gene gene = new StringGene(conf, 1, 6, StringGene.ALPHABET_CHARACTERS_UPPER); 1003 gene.setAllele("XYZA"); gene.setToRandomValue(new RandomGeneratorForTest(3)); 1005 assertEquals("DDDD", gene.getAllele()); 1006 } 1007 1008 public void testSetToRandomValue_2() 1009 throws Exception { 1010 Gene gene = new StringGene(conf, 1, 6, "ABC"); 1011 gene.setToRandomValue(new RandomGeneratorForTest(3)); 1012 assertEquals("AAAA", gene.getAllele()); 1013 } 1014 1015 1021 public void testSetToRandomValue_3() 1022 throws Exception { 1023 StringGene gene = new StringGene(conf, 1, 7, "DEF"); 1024 gene.setToRandomValue(new RandomGeneratorForTest(2)); 1025 assertEquals("FFF", gene.getAllele()); 1026 } 1027 1028 1034 public void testSetToRandomValue_4() 1035 throws Exception { 1036 StringGene gene = new StringGene(conf, 1, 7, "DEF"); 1037 gene.setAllele("EEFD"); 1038 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 1039 rn.setNextIntSequence(new int[] {3, 2, 1, 0, 2}); 1046 gene.setToRandomValue(rn); 1047 assertEquals("FEDF", gene.getAllele()); 1048 } 1049 1050 1055 public void testSetToRandomValue_5() 1056 throws Exception { 1057 StringGene gene = new StringGene(conf, 1, 8, 1058 StringGene.ALPHABET_CHARACTERS_LOWER); 1059 gene.setToRandomValue(new StockRandomGenerator()); 1060 for (int i = 0; i < gene.size(); i++) { 1061 if ( ( (String ) gene.getAllele()).charAt(i) < 'a' 1062 || ( (String ) gene.getAllele()).charAt(i) > 'z') { 1063 fail(); 1064 } 1065 } 1066 } 1067 1068 1073 public void testSetToRandomValue_6() 1074 throws Exception { 1075 Gene gene = new StringGene(conf, 1, 6, ""); 1076 try { 1077 gene.setToRandomValue(new StockRandomGenerator()); 1078 fail(); 1079 } 1080 catch (IllegalStateException iex) { 1081 ; } 1083 } 1084 1085 1090 public void testSetToRandomValue_7() 1091 throws Exception { 1092 Gene gene = new StringGene(conf, 1, 6, null); 1093 try { 1094 gene.setToRandomValue(new StockRandomGenerator()); 1095 fail(); 1096 } 1097 catch (IllegalStateException iex) { 1098 ; } 1100 } 1101 1102 1108 public void testSetToRandomValue_8() 1109 throws Exception { 1110 StringGene gene = new StringGene(conf, 2, 6, "ABC"); 1111 try { 1112 gene.setMaxLength(1); 1113 gene.setToRandomValue(new StockRandomGenerator()); 1114 fail(); 1115 } 1116 catch (IllegalStateException iex) { 1117 ; } 1119 } 1120 1121 1126 public void testSetConstraintChecker_0() 1127 throws Exception { 1128 StringGene gene = new StringGene(conf, 1, 6, "ABC"); 1129 assertNull(gene.getConstraintChecker()); 1130 gene.setConstraintChecker(new IGeneConstraintChecker() { 1131 public boolean verify(Gene a_gene, Object a_alleleValue, 1132 IChromosome a_chrom, int a_index) { 1133 return false; 1134 } 1135 }); 1136 assertNotNull(gene.getConstraintChecker()); 1137 } 1138 1139 1144 public void testIsValidAlphabet_0() 1145 throws Exception { 1146 StringGene gene = new StringGene(conf, 1, 6, ""); 1147 try { 1148 gene.setAllele("HALLO"); 1149 fail(); 1150 } 1151 catch (IllegalArgumentException ilex) { 1152 ; } 1154 } 1155 1156 1162 public void testSetEnergy_0() 1163 throws Exception { 1164 BaseGene gene = new IntegerGene(conf); 1165 assertEquals(0.0, gene.getEnergy(), DELTA); 1166 } 1167 1168 1174 public void testSetEnergy_1() 1175 throws Exception { 1176 BaseGene gene = new IntegerGene(conf); 1177 gene.setEnergy(2.3); 1178 assertEquals(2.3, gene.getEnergy(), DELTA); 1179 gene.setEnergy( -55.8); 1180 assertEquals( -55.8, gene.getEnergy(), DELTA); 1181 gene.setEnergy(0.5); 1182 gene.setEnergy(0.8); 1183 assertEquals(0.8, gene.getEnergy(), DELTA); 1184 } 1185 1186 class GeneConstraintChecker 1187 implements IGeneConstraintChecker { 1188 public boolean verify(Gene a_gene, Object a_alleleValue, 1189 IChromosome a_chrom, 1190 int a_index) { 1191 return true; 1192 } 1193 } 1194} 1195 | Popular Tags |