⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringgenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    gene1.setCompareApplicationData(true);
    assertEquals(1, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));
    gene2.setCompareApplicationData(true);
    assertEquals(1, gene1.compareTo(gene2));
    assertEquals( -1, gene2.compareTo(gene1));
    gene2.setApplicationData(new Double(2.3d));
    assertEquals(0, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));
    gene2.setApplicationData(null);
    assertEquals(1, gene1.compareTo(gene2));
  }

  /**
   * Set Allele to null, no exception should occur.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_0()
      throws Exception {
    Gene gene1 = new StringGene(conf, 0, 10000);
    gene1.setAllele(null);
  }

  /**
   * Allele too short.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_1()
      throws Exception {
    Gene gene1 = new StringGene(conf, 3, 4);
    try {
      gene1.setAllele("AB");
      fail();
    } catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * Allele consists of illegal characters.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_2()
      throws Exception {
    Gene gene1 = new StringGene(conf, 3, 4, "ABCDEFHI");
    try {
      gene1.setAllele("ABDG");
      fail();
    } catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_3()
      throws Exception {
    Gene gene1 = new StringGene(conf, 3, 4, "EGAL");
    try {
      // Length of allele to short.
      // --------------------------
      gene1.setAllele("");
      fail();
    } catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_4()
      throws Exception {
    Gene gene1 = new StringGene(conf, 0, 4, "");
    //following should be possible
    gene1.setAllele("");
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetAllele_5()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 0, 4, "ABC");
    //following should be possible
    gene1.setAllele("A");
    gene1.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return false;
      }
    });
    gene1.setAllele("B");
    assertEquals("A", gene1.stringValue());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testSetAllele_6()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 0, 4, "ABC");
    //following should be possible
    gene1.setAllele("A");
    gene1.setConstraintChecker(new IGeneConstraintChecker() {
      public boolean verify(Gene a_gene, Object a_alleleValue,
                            IChromosome a_chrom, int a_index) {
        return true;
      }
    });
    gene1.setAllele("B");
    assertEquals("B", gene1.stringValue());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNewGene_0()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 1, 4);
    IGeneConstraintChecker checker = new GeneConstraintChecker();
    gene1.setConstraintChecker(checker);
    gene1.setAllele("XYZ");
    int minLength1 = gene1.getMinLength();
    int maxLength1 = gene1.getMaxLength();
    StringGene gene2 = (StringGene) gene1.newGene();
    int minLength2 = gene2.getMinLength();
    int maxLength2 = gene2.getMaxLength();
    assertEquals(minLength1, minLength2);
    assertEquals(maxLength1, maxLength2);
    assertEquals(checker, gene2.getConstraintChecker());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_0()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele("BABE");
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_1()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 10);
    try {
      gene1.setAllele("");
      fail();
    } catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_2()
      throws Exception {
    Gene gene1 = new StringGene(conf, 0, 10);
    gene1.setAllele("");
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_3()
      throws Exception {
    Gene gene1 = new StringGene(conf);
    gene1.setAllele("");
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_4()
      throws Exception {
    Gene gene1 = new StringGene(conf);
    gene1.setAllele(null);
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_5()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10,
                                      "ABCDE" + CompositeGene.GENE_DELIMITER);
    gene1.setAllele("BABE");
    String pres1 = gene1.getPersistentRepresentation();
    StringGene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
    assertEquals(gene1, gene2);
    assertEquals(gene1.getAlphabet(), gene2.getAlphabet());
  }

  /**
   * Use special characters in alphabet
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testPersistentRepresentation_5_2()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10,
                                      "ABCDE"
                                      + Gene.PERSISTENT_FIELD_DELIMITER
                                      + "+*~'碻");
    gene1.setAllele("BABE");
    String pres1 = gene1.getPersistentRepresentation();
    StringGene gene2 = new StringGene(conf);
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
    assertEquals(gene1, gene2);
    assertEquals(gene1.getAlphabet(), gene2.getAlphabet());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_6()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele("BABE");
    gene1.setValueFromPersistentRepresentation(null);
    assertEquals("BABE", gene1.getAllele());
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_7()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_8()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("null:2:ABCDE");
      fail();
    } catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testPersistentRepresentation_9()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    assertEquals("null:2:10:ABCDE", gene1.getPersistentRepresentation());
  }

  /**
   * Invalid number in second argument.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_10()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("null:a:10:ABCDE");
      fail();
    } catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Invalid number in third argument.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_11()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("null:2:3b:ABCDE");
      fail();
    } catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Minlen to great
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testPersistentRepresentation_12()
      throws Exception {
    StringGene gene1 = new StringGene(conf, 2, 10, "ABCDE");
    gene1.setAllele(null);
    try {
      gene1.setValueFromPersistentRepresentation("nada:7:6:ABCDE");
      fail();
    } catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   * Maxlen to small
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.6

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -