1 10 package org.jgap.impl; 11 12 import org.jgap.*; 13 import junit.framework.*; 14 15 22 public class FixedBinaryGeneTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.31 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(FixedBinaryGeneTest.class); 29 return suite; 30 } 31 32 38 public void testConstruct_0() 39 throws Exception { 40 new FixedBinaryGene(conf, 1); 42 new FixedBinaryGene(conf, 10); 43 new FixedBinaryGene(conf, 1000); 44 new FixedBinaryGene(conf, 100000); 45 } 46 47 52 public void testConstruct_1() 53 throws Exception { 54 try { 55 new FixedBinaryGene(conf, 0); 56 fail(); 57 } 58 catch (IllegalArgumentException iex) { 59 ; } 61 } 62 63 68 public void testConstruct_2() 69 throws Exception { 70 try { 71 new FixedBinaryGene(conf, -5); 72 fail(); 73 } 74 catch (IllegalArgumentException iex) { 75 ; } 77 } 78 79 84 public void testConstruct_3() 85 throws Exception { 86 int i = 0; 87 FixedBinaryGene gene = new FixedBinaryGene(conf, 5); 88 for (i = 0; i < 4; i++) { 89 assertFalse(gene.getBit(i)); 91 } 92 assertEquals("FixedBinaryGene[0,0,0,0,0]", gene.toString()); 93 } 94 95 100 public void testConstruct_4() 101 throws Exception { 102 FixedBinaryGene gene = new FixedBinaryGene(conf, 6); 103 assertEquals(1, gene.size()); 104 assertEquals(1, (gene.getValue()).length); 105 assertEquals("FixedBinaryGene[0,0,0,0,0,0]", gene.toString()); 106 } 107 108 114 public void testConstruct_5() 115 throws Exception { 116 FixedBinaryGene gene; 117 gene = new FixedBinaryGene(conf, 32); 118 assertEquals(1, gene.size()); 119 gene = new FixedBinaryGene(conf, 81); 120 assertEquals(3, gene.size()); 121 } 122 123 129 public void testConstruct_6() 130 throws Exception { 131 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1); 132 new FixedBinaryGene(conf, gene1); 133 } 134 135 140 public void testToString_0() 141 throws Exception { 142 Gene gene = new FixedBinaryGene(conf, 1); 143 gene.setAllele(new int[] {1}); 144 assertEquals("FixedBinaryGene[1]", gene.toString()); 145 } 146 147 152 public void testToString_1() 153 throws Exception { 154 Gene gene = new FixedBinaryGene(conf, 3); 155 gene.setAllele(new int[] {1, 0, 1}); 156 assertEquals("FixedBinaryGene[1,0,1]", gene.toString()); 157 } 158 159 165 public void testToString_2() 166 throws Exception { 167 Gene gene = new FixedBinaryGene(conf, 3); 168 assertEquals("FixedBinaryGene[0,0,0]", gene.toString()); 169 } 170 171 176 public void testGetAllele_0() 177 throws Exception { 178 Gene gene = new FixedBinaryGene(conf, 1); 179 int[] value = new int[] { 180 0}; 181 gene.setAllele(value); 182 assertEquals(value.length, ( (int[]) gene.getAllele()).length); 183 for (int i = 0; i < value.length; i++) { 184 assertEquals(value[i], ( (int[]) gene.getAllele())[i]); 185 } 186 } 187 188 193 public void testGetAllele_1() 194 throws Exception { 195 Gene gene = new FixedBinaryGene(conf, 2); 196 try { 197 gene.setAllele(new Integer (100)); 198 fail(); 199 } 200 catch (ClassCastException classex) { 201 ; } 203 } 204 205 211 public void testGetAllele_2() 212 throws Exception { 213 Gene gene = new FixedBinaryGene(conf, 1); 214 int[] value = new int[] { 215 1}; 216 gene.setAllele(value); 217 assertEquals(value.length, ( (int[]) gene.getAllele()).length); 218 for (int i = 0; i < value.length; i++) { 219 assertEquals(value[i], ( (int[]) gene.getAllele())[i]); 220 } 221 } 222 223 228 public void testEquals_0() 229 throws Exception { 230 Gene gene1 = new FixedBinaryGene(conf, 1); 231 Gene gene2 = new FixedBinaryGene(conf, 1); 232 assertTrue(gene1.equals(gene2)); 233 } 234 235 240 public void testEquals_1() 241 throws Exception { 242 Gene gene1 = new FixedBinaryGene(conf, 1); 243 assertFalse(gene1.equals(null)); 244 } 245 246 251 public void testEquals_2() 252 throws Exception { 253 Gene gene1 = new FixedBinaryGene(conf, 2); 254 gene1.setAllele(new int[] {1, 0}); 255 Gene gene2 = new FixedBinaryGene(conf, 2); 256 gene2.setAllele(new int[] {0, 1}); 257 assertFalse(gene1.equals(gene2)); 258 assertFalse(gene2.equals(gene1)); 259 } 260 261 266 public void testEquals_3() 267 throws Exception { 268 Gene gene1 = new FixedBinaryGene(conf, 5); 269 assertFalse(gene1.equals(new IntegerGene(conf))); 270 } 271 272 public void testEquals_4() 273 throws Exception { 274 Gene gene1 = new FixedBinaryGene(conf, 1); 275 Gene gene2 = new IntegerGene(conf); 276 assertFalse(gene1.equals(gene2)); 277 assertFalse(gene2.equals(gene1)); 278 } 279 280 public void testEquals_5() 281 throws Exception { 282 Gene gene1 = new FixedBinaryGene(conf, 1); 283 Gene gene2 = new DoubleGene(conf); 284 assertFalse(gene1.equals(gene2)); 285 assertFalse(gene2.equals(gene1)); 286 } 287 288 public void testEquals_6() 289 throws Exception { 290 Gene gene1 = new FixedBinaryGene(conf, 1); 291 Gene gene2 = new BooleanGene(conf); 292 assertFalse(gene1.equals(gene2)); 293 assertFalse(gene2.equals(gene1)); 294 } 295 296 public void testEquals_7() 297 throws Exception { 298 Gene gene1 = new FixedBinaryGene(conf, 1); 299 Gene gene2 = new StringGene(conf); 300 assertFalse(gene1.equals(gene2)); 301 assertFalse(gene2.equals(gene1)); 302 } 303 304 310 public void testEquals_8() 311 throws Exception { 312 Gene gene1 = new FixedBinaryGene(conf, 2); 313 gene1.setAllele(new int[] {0, 1}); 314 Gene gene2 = new FixedBinaryGene(conf, 2); 315 gene2.setAllele(new int[] {0, 0}); 316 assertFalse(gene1.equals(gene2)); 317 assertFalse(gene2.equals(gene1)); 318 } 319 320 325 public void testIntValues_0() 326 throws Exception { 327 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4); 328 assertFalse(gene1.getIntValues() == null); 329 } 330 331 336 public void testIntValues_1() 337 throws Exception { 338 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 2); 339 int[] values = gene1.getIntValues(); 340 int i; 341 for (i = 0; i < values.length; i++) { 342 assertEquals(0, values[i]); 343 } 344 } 345 346 351 public void testIntValues_2() 352 throws Exception { 353 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 354 gene1.setAllele(new int[] {0, 1, 0}); 355 assertEquals(false, gene1.getBit(0)); 356 assertEquals(true, gene1.getBit(1)); 357 assertEquals(false, gene1.getBit(2)); 358 assertEquals(3, gene1.getLength()); 359 } 360 361 367 public void testSetAllele_0() 368 throws Exception { 369 Gene gene1 = new FixedBinaryGene(conf, 1); 370 try { 371 gene1.setAllele(null); 372 fail(); 373 } 374 catch (IllegalArgumentException iex) { 375 ; } 377 } 378 379 385 public void testSetAllele_1() 386 throws Exception { 387 Gene gene1 = new FixedBinaryGene(conf, 1); 388 try { 389 gene1.setAllele("22"); 390 fail(); 391 } 392 catch (ClassCastException classex) { 393 ; } 395 } 396 397 403 public void testSetAllele_2() 404 throws Exception { 405 Gene gene1 = new FixedBinaryGene(conf, 3); 406 gene1.setAllele(new int[] {0, 0, 1}); 407 } 408 409 416 public void testSetAllele_3() 417 throws Exception { 418 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 419 try { 420 gene1.setAllele(new int[] {0, 1, 1, 1, 1, 1}); 421 fail(); 422 } 423 catch (Exception e) { 424 ; } 426 } 427 428 434 public void testSetAllele_4() 435 throws Exception { 436 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4); 437 try { 438 gene1.setAllele(new int[] {0, 3, 1, 4}); 439 fail(); 440 } 441 catch (Exception e) { 442 ; } 444 } 445 446 452 public void testSetAllele_5() 453 throws Exception { 454 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4); 455 try { 456 gene1.setAllele(new int[] {0, 0}); 457 fail(); 458 } 459 catch (Exception e) { 460 ; } 462 } 463 464 470 public void testSetAllele_6() 471 throws Exception { 472 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 473 gene1.setConstraintChecker(new IGeneConstraintChecker() { 474 public boolean verify(Gene a_gene, Object a_alleleValue, 475 IChromosome a_chrom, int a_index) { 476 return false; 477 } 478 }); 479 gene1.setAllele(new int[] {0, 0, 1}); 480 assertFalse(gene1.getBit(0)); 481 assertFalse(gene1.getBit(1)); 482 assertFalse(gene1.getBit(2)); 483 } 484 485 491 public void testSetAllele_7() 492 throws Exception { 493 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 494 gene1.setConstraintChecker(new IGeneConstraintChecker() { 495 public boolean verify(Gene a_gene, Object a_alleleValue, 496 IChromosome a_chrom, int a_index) { 497 return true; 498 } 499 }); 500 gene1.setAllele(new int[] {0, 0, 1}); 501 assertFalse(gene1.getBit(0)); 502 assertFalse(gene1.getBit(1)); 503 assertTrue(gene1.getBit(2)); 504 } 505 506 512 public void testSetConstraintChecker_0() 513 throws Exception { 514 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 515 assertNull(gene1.getConstraintChecker()); 516 gene1.setConstraintChecker(new IGeneConstraintChecker() { 517 public boolean verify(Gene a_gene, Object a_alleleValue, 518 IChromosome a_chrom, int a_index) { 519 return false; 520 } 521 }); 522 assertNotNull(gene1.getConstraintChecker()); 523 } 524 525 531 public void testCompareTo_0() 532 throws Exception { 533 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4); 534 FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4); 535 gene1.setAllele(new int[] {1, 0, 1, 0}); 536 gene2.setAllele(new int[] {1, 1, 0, 1}); 537 assertEquals(1, gene1.compareTo(null)); 538 assertEquals( -1, gene1.compareTo(gene2)); 539 assertEquals(1, gene2.compareTo(gene1)); 540 } 541 542 547 public void testCompareTo_1() 548 throws Exception { 549 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 550 FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3); 551 assertEquals(0, gene1.compareTo(gene2)); 552 assertEquals(0, gene2.compareTo(gene1)); 553 } 554 555 560 public void testCompareTo_2() 561 throws Exception { 562 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 563 FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3); 564 gene1.setAllele(new int[] {1, 1, 1}); 565 gene2.setAllele(new int[] {1, 1, 1}); 566 assertEquals(0, gene1.compareTo(gene2)); 567 assertEquals(0, gene2.compareTo(gene1)); 568 gene1.setAllele(new int[] {0, 0, 0}); 569 gene2.setAllele(new int[] {0, 0, 0}); 570 assertEquals(0, gene1.compareTo(gene2)); 571 assertEquals(0, gene2.compareTo(gene1)); 572 } 573 574 579 public void testCompareTo_3_1() 580 throws Exception { 581 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 582 BooleanGene gene2 = new BooleanGene(conf); 583 try { 584 gene1.compareTo(gene2); 585 fail(); 586 } 587 catch (Exception e) { 588 ; } 590 } 591 592 597 public void testCompareTo_3_2() 598 throws Exception { 599 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 600 try { 601 gene1.compareTo(new Integer (3)); 602 fail(); 603 } 604 catch (Exception e) { 605 ; } 607 } 608 609 615 public void testCompareTo_4() 616 throws Exception { 617 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 618 FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4); 619 assertEquals( -1, gene1.compareTo(gene2)); 620 assertEquals(1, gene2.compareTo(gene1)); 621 } 622 623 629 public void testApplyMutation_0() 630 throws Exception { 631 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 632 gene.setAllele(new int[] {0, 0, 1, 1}); 633 gene.applyMutation(0, 0.0d); 634 assertEquals("FixedBinaryGene[0,0,1,1]", gene.toString()); 635 } 636 637 642 public void testApplyMutation_1() 643 throws Exception { 644 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 645 gene.setAllele(new int[] {0, 0, 1, 0}); 646 gene.applyMutation(1, 0.000001d); 647 assertEquals("FixedBinaryGene[0,1,1,0]", gene.toString()); 648 } 649 650 655 public void testApplyMutation_2() 656 throws Exception { 657 FixedBinaryGene gene = new FixedBinaryGene(conf, 5); 658 gene.setAllele(new int[] {1, 0, 1, 0, 1}); 659 try { 660 gene.applyMutation(333, -0.000001d); 662 fail(); 663 } 664 catch (Exception e) { 665 ; } 667 } 668 669 674 public void testApplyMutation_3() 675 throws Exception { 676 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 677 gene.setAllele(new int[] {1, 1, 0, 1}); 678 gene.applyMutation(0, -1.0d); 679 assertEquals("FixedBinaryGene[0,1,0,1]", gene.toString()); 680 } 681 682 687 public void testApplyMutation_4() 688 throws Exception { 689 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 690 gene.setAllele(new int[] {0, 1, 0, 1}); 691 gene.applyMutation(0, -2.0d); 692 gene.applyMutation(3, 2.0d); 693 gene.applyMutation(1, -4.0d); 694 assertEquals("FixedBinaryGene[0,0,0,1]", gene.toString()); 695 } 696 697 702 public void testApplyMutation_5() 703 throws Exception { 704 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 705 gene.setAllele(new int[] {1, 1, 1, 1}); 706 gene.applyMutation(0, 2.0d); 707 gene.applyMutation(1, 2.0d); 708 gene.applyMutation(2, 2.0d); 709 gene.applyMutation(3, 2.0d); 710 assertEquals("FixedBinaryGene[1,1,1,1]", gene.toString()); 711 } 712 713 719 public void testSetValueFromPersistentRepresentation_0() 720 throws Exception { 721 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 722 try { 723 gene.setValueFromPersistentRepresentation(null); 724 fail(); 725 } 726 catch (UnsupportedRepresentationException uex) { 727 ; } 729 } 730 731 735 public void testSetValueFromPersistentRepresentation_1() 736 throws Exception { 737 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 738 try { 739 gene.setValueFromPersistentRepresentation("null"); 740 fail(); 741 } 742 catch (UnsupportedRepresentationException uex) { 743 ; } 745 } 746 747 753 public void testSetValueFromPersistentRepresentation_2() 754 throws Exception { 755 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 756 gene.setValueFromPersistentRepresentation("[1,1,1,1]"); 757 assertTrue(gene.getBit(0)); 758 assertTrue(gene.getBit(1)); 759 assertTrue(gene.getBit(2)); 760 assertTrue(gene.getBit(3)); 761 } 762 763 769 public void testSetValueFromPersistentRepresentation_3() 770 throws Exception { 771 FixedBinaryGene gene = new FixedBinaryGene(conf, 4); 772 gene.setValueFromPersistentRepresentation("[0,0,0,0]"); 773 assertFalse(gene.getBit(0)); 774 assertFalse(gene.getBit(1)); 775 assertFalse(gene.getBit(2)); 776 assertFalse(gene.getBit(3)); 777 } 778 779 785 public void testSetValueFromPersistentRepresentation_4() 786 throws Exception { 787 FixedBinaryGene gene = new FixedBinaryGene(conf, 5); 788 gene.setValueFromPersistentRepresentation("[0,1,1,0,0]"); 789 assertFalse(gene.getBit(0)); 790 assertTrue(gene.getBit(1)); 791 assertTrue(gene.getBit(2)); 792 assertFalse(gene.getBit(3)); 793 assertFalse(gene.getBit(4)); 794 } 795 796 802 public void testSetValueFromPersistentRepresentation_5() 803 throws Exception { 804 FixedBinaryGene gene = new FixedBinaryGene(conf, 5); 805 try { 806 gene.setValueFromPersistentRepresentation("[0,1,1,0]"); 807 fail(); 808 } 809 catch (UnsupportedRepresentationException uex) { 810 ; } 812 } 813 814 820 public void testSetValueFromPersistentRepresentation_6() 821 throws Exception { 822 FixedBinaryGene gene = new FixedBinaryGene(conf, 1); 823 try { 824 gene.setValueFromPersistentRepresentation("X"); 825 fail(); 826 } 827 catch (UnsupportedRepresentationException uex) { 828 ; } 830 } 831 832 837 public void testGetPersistentRepresentation_0() 838 throws Exception { 839 FixedBinaryGene gene = new FixedBinaryGene(conf, 3); 840 gene.setAllele(new int[] {1, 0, 1}); 841 String s = gene.getPersistentRepresentation(); 842 assertEquals("FixedBinaryGene[1,0,1]", s); 843 } 844 845 851 public void testGetPersistentRepresentation_1() 852 throws Exception { 853 FixedBinaryGene gene = new FixedBinaryGene(conf, 3); 854 try { 855 gene.setValueFromPersistentRepresentation(null); 856 fail(); 857 } 858 catch (UnsupportedRepresentationException uex) { 859 ; } 861 } 862 863 869 public void testGetPersistentRepresentation_2() 870 throws Exception { 871 FixedBinaryGene gene = new FixedBinaryGene(conf, 3); 872 String s = gene.getPersistentRepresentation(); 873 assertEquals("FixedBinaryGene[0,0,0]", s); 874 } 875 876 882 public void testClone_0() 883 throws Exception { 884 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1); 885 FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone(); 886 assertEquals(gene1, gene2); 887 } 888 889 895 public void testClone_1() 896 throws Exception { 897 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3); 898 FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone(); 899 assertEquals(gene1, gene2); 900 } 901 902 908 public void testSetBit_0() 909 throws Exception { 910 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 911 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 912 assertTrue(gene1.getBit(0)); 913 gene1.setBit(0, false); 914 assertFalse(gene1.getBit(0)); 915 gene1.setBit(1, true); 916 assertTrue(gene1.getBit(1)); 917 gene1.setBit(4, false); 918 assertFalse(gene1.getBit(0)); 919 } 920 921 927 public void testSetBit_1() 928 throws Exception { 929 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 930 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 931 gene1.setBit(2, 4, true); 932 assertTrue(gene1.getBit(0)); 933 assertTrue(gene1.getBit(1)); 934 assertTrue(gene1.getBit(2)); 935 assertTrue(gene1.getBit(3)); 936 } 937 938 944 public void testSetBit_2() 945 throws Exception { 946 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 947 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 948 try { 949 gene1.setBit(2, 2); 950 fail(); 951 } 952 catch (IllegalArgumentException iex) { 953 ; } 955 } 956 957 963 public void testSetBit_3() 964 throws Exception { 965 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 966 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 967 try { 968 gene1.setBit(2, -1); 969 fail(); 970 } 971 catch (IllegalArgumentException iex) { 972 ; } 974 } 975 976 982 public void testSetBit_4() 983 throws Exception { 984 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 985 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 986 try { 987 gene1.setBit(2, 1, false); 988 fail(); 989 } 990 catch (IllegalArgumentException iex) { 991 ; } 993 } 994 995 1001 public void testSetBit_5() 1002 throws Exception { 1003 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1004 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 1005 FixedBinaryGene gene2 = new FixedBinaryGene(conf, 9); 1006 gene2.setBit(2, 6, gene1); 1007 assertTrue(gene2.getBit(2)); 1008 assertTrue(gene2.getBit(3)); 1009 assertTrue(gene2.getBit(6)); 1010 assertFalse(gene2.getBit(0)); 1011 assertFalse(gene2.getBit(1)); 1012 assertFalse(gene2.getBit(4)); 1013 assertFalse(gene2.getBit(5)); 1014 assertFalse(gene2.getBit(7)); 1015 assertFalse(gene2.getBit(8)); 1016 } 1017 1018 1024 public void testSetBit_6() 1025 throws Exception { 1026 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4); 1027 gene1.setAllele(new int[] {1, 1, 0, 0}); 1028 FixedBinaryGene gene2 = new FixedBinaryGene(conf, 7); 1029 gene2.setBit(2, 6, gene1); 1030 assertTrue(gene2.getBit(2)); 1031 assertTrue(gene2.getBit(3)); 1032 assertTrue(gene2.getBit(6)); 1033 assertFalse(gene2.getBit(0)); 1034 assertFalse(gene2.getBit(1)); 1035 assertFalse(gene2.getBit(4)); 1036 assertFalse(gene2.getBit(5)); 1037 } 1038 1039 1045 public void testSubbString_0() 1046 throws Exception { 1047 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1048 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 1049 FixedBinaryGene gene2 = gene1.substring(0, 4); 1050 assertEquals(5, gene2.getLength()); 1051 assertEquals(1, gene2.size()); 1052 assertTrue(gene2.getBit(0)); 1053 assertTrue(gene2.getBit(1)); 1054 assertFalse(gene2.getBit(2)); 1055 assertFalse(gene2.getBit(3)); 1056 assertTrue(gene2.getBit(4)); 1057 } 1058 1059 1065 public void testSubString_1() 1066 throws Exception { 1067 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1068 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 1069 try { 1070 gene1.substring(0, 7); 1071 fail(); 1072 } 1073 catch (IndexOutOfBoundsException iex) { 1074 ; } 1076 } 1077 1078 1084 public void testFlip_0() 1085 throws Exception { 1086 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1087 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 1088 gene1.flip(0); 1089 assertFalse(gene1.getBit(0)); 1090 gene1.flip(6); 1091 assertFalse(gene1.getBit(6)); 1092 gene1.flip(2); 1093 assertTrue(gene1.getBit(2)); 1094 } 1095 1096 1102 public void testFlip_1() 1103 throws Exception { 1104 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1105 gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1}); 1106 try { 1107 gene1.flip(7); 1108 fail(); 1109 } 1110 catch (IndexOutOfBoundsException iex) { 1111 ; } 1113 } 1114 1115 1121 public void testSetToRandomValue_0() 1122 throws Exception { 1123 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1124 try { 1125 gene1.setToRandomValue(null); 1126 fail(); 1127 } 1128 catch (IllegalArgumentException iex) { 1129 ; } 1131 } 1132 1133 1139 public void testSetToRandomValue_1() 1140 throws Exception { 1141 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1142 gene1.setToRandomValue(new StockRandomGenerator()); 1143 } 1144 1145 1151 public void testSetToRandomValue_2() 1152 throws Exception { 1153 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1154 gene1.setToRandomValue(new RandomGeneratorForTest(false)); 1155 for (int i = 0; i < 7; i++) { 1156 assertFalse(gene1.getBit(i)); 1157 } 1158 } 1159 1160 1166 public void testSetToRandomValue_3() 1167 throws Exception { 1168 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1169 gene1.setToRandomValue(new RandomGeneratorForTest(true)); 1170 for (int i = 0; i < 7; i++) { 1171 assertTrue(gene1.getBit(i)); 1172 } 1173 } 1174 1175 1180 public void testNewGene_0() 1181 throws Exception { 1182 FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7); 1183 IGeneConstraintChecker checker = new GeneConstraintChecker(); 1184 gene1.setConstraintChecker(checker); 1185 FixedBinaryGene gene2 = (FixedBinaryGene) gene1.newGene(); 1186 assertTrue(gene1.equals(gene2)); 1187 assertTrue(gene2.equals(gene1)); 1188 assertEquals(checker, gene2.getConstraintChecker()); 1189 } 1190 1191 1197 public void testHashCode_0() 1198 throws Exception { 1199 FixedBinaryGene gene = new FixedBinaryGene(conf, 6); 1200 gene.hashCode(); 1201 gene.setBit(0, 5, false); 1202 gene.hashCode(); 1203 gene.setBit(0, 5, true); 1204 gene.hashCode(); 1205 1206 } 1207 1208 1214 public void testHashCode_1() 1215 throws Exception { 1216 Gene gene1 = new FixedBinaryGene(conf, 2); 1217 Gene gene2 = new FixedBinaryGene(conf, 2); 1218 int[] v1 = (int[]) gene1.getAllele(); 1219 int[] v2 = (int[]) gene1.getAllele(); 1220 int h1 = v1.hashCode(); 1221 int h2 = v2.hashCode(); 1222 assertEquals(gene1.hashCode(), gene2.hashCode()); 1223 } 1224 1225 1231 public void testHashCode_2() 1232 throws Exception { 1233 Gene gene1 = new FixedBinaryGene(conf, 3); 1234 gene1.setAllele(new int[] {0, 1, 0}); 1235 Gene gene2 = new FixedBinaryGene(conf, 3); 1236 gene2.setAllele(new int[] {0, 1, 0}); 1237 assertEquals(gene1.hashCode(), gene2.hashCode()); 1238 } 1239 1240 1246 public void testHashCode_3() 1247 throws Exception { 1248 Gene gene1 = new FixedBinaryGene(conf, 2); 1249 gene1.setAllele(new int[] {1, 1}); 1250 Gene gene2 = new FixedBinaryGene(conf, 2); 1251 gene1.setAllele(new int[] {1, 0}); 1252 assertFalse(gene1.hashCode() == gene2.hashCode()); 1253 assertTrue(gene1.hashCode() == gene1.hashCode()); 1254 assertTrue(gene2.hashCode() == gene2.hashCode()); 1255 } 1256 1257 1263 public void testSetEnergy_0() 1264 throws Exception { 1265 BaseGene gene = new FixedBinaryGene(conf, 2); 1266 assertEquals(0.0, gene.getEnergy(), DELTA); 1267 } 1268 1269 1275 public void testSetEnergy_1() 1276 throws Exception { 1277 BaseGene gene = new FixedBinaryGene(conf, 3); 1278 gene.setEnergy(2.3); 1279 assertEquals(2.3, gene.getEnergy(), DELTA); 1280 gene.setEnergy( -55.8); 1281 assertEquals( -55.8, gene.getEnergy(), DELTA); 1282 gene.setEnergy(0.5); 1283 gene.setEnergy(0.8); 1284 assertEquals(0.8, gene.getEnergy(), DELTA); 1285 } 1286 1287 class GeneConstraintChecker 1288 implements IGeneConstraintChecker { 1289 public boolean verify(Gene a_gene, Object a_alleleValue, 1290 IChromosome a_chrom, int a_index) { 1291 return true; 1292 } 1293 } 1294 1295 1296} 1297 | Popular Tags |