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

📄 stringgenetest.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * 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.36 $";

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

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

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

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

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public void testConstruct_5()
      throws Exception {
    Genotype.setStaticConfiguration(conf);
    StringGene gene = new StringGene();
    assertSame(conf, gene.getConfiguration());
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testEquals_8()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 6);
    gene1.setAllele("hallo");
    Gene gene2 = new StringGene(conf, 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.
   * @throws Exception
   *
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testEquals_9()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 6);
    gene1.setAllele("hallo");
    gene1.setApplicationData(new Double(2.3d));
    Gene gene2 = new StringGene(conf, 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));
  }

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

  /**
   * class cast exception.
   * @throws Exception
   *
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testCompareTo_1()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 6);
    try {
      gene1.compareTo(new Chromosome(conf));
      fail();
    } catch (ClassCastException cex) {
      ; // this is OK
    }
  }

  /**
   * Comparation using application data and null allele.
   * @throws Exception
   *
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testCompareTo_2()
      throws Exception {
    Gene gene1 = new StringGene(conf, 2, 6);
    gene1.setApplicationData(new Double(2.3d));
    Gene gene2 = new StringGene(conf, 2, 6);
    assertEquals(0, gene1.compareTo(gene2));
    assertEquals(0, gene2.compareTo(gene1));

⌨️ 快捷键说明

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