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

📄 stringgenetest.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.impl;

import java.util.*;

import org.jgap.*;

import junit.framework.*;

/**
 * Tests the StringGene class
 *
 * @author Klaus Meffert
 * @since 1.1
 */
public class StringGeneTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.24 $";

  public static Test suite() {
    TestSuite suite = new TestSuite(StringGeneTest.class);
    return suite;
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_0() {
    StringGene gene = new StringGene(1, 100);
    //following should be possible without exception
    gene.setAllele("ABC");
    assertEquals(null, gene.getAlphabet());
    assertEquals(1, gene.getMinLength());
    assertEquals(100, gene.getMaxLength());
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_1() {
    try {
      new StringGene(2, 1);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_2() {
    try {
      new StringGene( -1, 3);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }


  /**
   * @author Klaus Meffert
   */
  public void testConstruct_3() {
    try {
      Gene gene = new StringGene(1, 3);
      gene.setAllele("ABCD");
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_4() {
    try {
      Gene gene = new StringGene(1, 3);
      gene.setAllele(new Double(2.3d));
    }
    catch (ClassCastException castex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAlphabet_1()
      throws Exception {
    StringGene gene = new StringGene(3, 5);
    final String ALPHABET = "1234";
    gene.setAlphabet(ALPHABET);
    String alphabet = (String) privateAccessor.getField(gene, "m_alphabet");
    assertEquals(ALPHABET, alphabet);
    assertEquals(alphabet, gene.getAlphabet());
  }

  /**
   * @author Klaus Meffert
   */
  public void testToString_0() {
    Gene gene = new StringGene(3, 7);
    gene.setAllele("ABC");
    assertEquals("StringGene=ABC", gene.toString());
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testToString_1() {
    StringGene gene = new StringGene(3, 7);
    assertEquals("StringGene=null", gene.toString());
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testToString_2() {
    StringGene gene = new StringGene(0, 7);
    gene.setAllele("");
    assertEquals("StringGene=\"\"", gene.toString());
  }

  /**
   * @author Klaus Meffert
   */
  public void testGetAllele_0() {
    Gene gene = new StringGene(3, 5);
    gene.setAllele("BCD");
    assertEquals("BCD", gene.getAllele());
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_0() {
    Gene gene1 = new StringGene(1, 100);
    Gene gene2 = new StringGene(1, 100);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_1() {
    Gene gene1 = new StringGene(3, 100);
    assertFalse(gene1.equals(null));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_2() {
    Gene gene1 = new StringGene(11, 77);
    assertTrue(gene1.equals(new StringGene()));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_3() {
    Gene gene1 = new StringGene(11, 17);
    assertFalse(gene1.equals(new Vector()));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_4() {
    Gene gene1 = new StringGene(12, 100);
    Gene gene2 = new StringGene(12, 99);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_5() {
    Gene gene1 = new StringGene(2, 5);
    Gene gene2 = new StringGene(1, 5);
    gene1.setAllele("ABC");
    gene2.setAllele("AB");
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_6() {
    Gene gene1 = new StringGene(2, 5);
    Gene gene2 = new DoubleGene(1, 5);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_7() {
    Gene gene1 = new StringGene(2, 5);
    Gene gene2 = new BooleanGene();
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testEquals_8() {
    Gene gene1 = new StringGene(2, 6);
    gene1.setAllele("hallo");
    Gene gene2 = new StringGene(2,6);
    gene2.setAllele("hello");
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
    gene1.setAllele("hello1");
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
    gene2.setAllele("HELLO1");
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * Using application data
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testEquals_9() {
    Gene gene1 = new StringGene(2, 6);
    gene1.setAllele("hallo");
    gene1.setApplicationData(new Double(2.3d));
    Gene gene2 = new StringGene(2,6);
    gene2.setAllele("hallo");
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
    gene1.setCompareApplicationData(true);
    assertFalse(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
    gene2.setCompareApplicationData(true);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
    gene2.setApplicationData(new Double(2.3d));
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   * Using application data
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testCompareTo_0() {
    Gene gene1 = new StringGene(2, 6);
    gene1.setAllele("hallo");
    gene1.setApplicationData(new Double(2.3d));
    Gene gene2 = new StringGene(2,6);
    gene2.setAllele("hallo");
    assertEquals(0,gene1.compareTo(gene2));
    assertEquals(0,gene2.compareTo(gene1));
    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));
  }

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

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

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

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

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

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

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

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNewGene_0()
      throws Exception {
    StringGene gene1 = new StringGene(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()

⌨️ 快捷键说明

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