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

📄 compositegenetest.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 CompositeGene class.
 *
 * @author Klaus Meffert
 * @since 1.1
 */
public class CompositeGeneTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.48 $";

  private static int cleanedUp = 0;

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

  /**
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testCompareTo_4()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    CompositeGene gene1 = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 3, 5);
    newGene1.setAllele(new Integer(4));
    Gene newGene2 = new IntegerGene(conf, 3, 5);
    newGene2.setAllele(new Integer(2));
    gene1.addGene(newGene1, false);
    gene1.addGene(newGene2, false);
    CompositeGene gene2 = new CompositeGene(conf);
    Gene newGene3 = new IntegerGene(conf, 3, 5);
    newGene3.setAllele(new Integer(1));
    newGene3.setAllele(new Integer(3));
    gene2.addGene(newGene3, false);
    assertEquals(1, gene1.compareTo(gene2));
    assertEquals( -1, gene2.compareTo(gene1));
  }

  /**
   *
   * @author Klaus Meffert
   * @throws Exception
   */
  public void testConstruct_0()
      throws Exception {
    //following should be possible without exception
    CompositeGene gene = new CompositeGene(conf);
    assertEquals(null, gene.getGeneTypeAllowed());
  }

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

  /**
   * Ensures that a CompositeGene may be added to a CompositeGene
   *
   * @author Audrius Meskauskas
   * @author Klaus Meffert
   * @since 2.0
   * @throws Exception
   */
  public void testAddGene_0()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    gene.addGene(new CompositeGene(conf), false);
    assertEquals(1, gene.size());
  }

  public void testAddGene_1()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    Gene newGene = new DoubleGene(conf);
    gene.addGene(newGene, false);
    assertEquals(1, gene.size());
    gene.addGene(new DoubleGene(conf, 1.2d, 3.4d), false);
    assertEquals(2, gene.size());
    //try to remove a non existent gene
    gene.removeGeneByIdentity(new DoubleGene(conf));
    assertEquals(2, gene.size());
    //try to add an already existent gene
    try {
      gene.addGene(newGene, false);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testAddGene_2()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf, new DoubleGene(conf, 2, 3));
    assertEquals(new DoubleGene(conf), gene.getGeneTypeAllowed());
    gene.addGene(new DoubleGene(conf), false);
    assertEquals(1, gene.size());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testAddGene_3()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf, new DoubleGene(conf, 2, 3));
    try {
      gene.addGene(new CompositeGene(conf), false);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testAddGene_4()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    gene.addGene(new CompositeGene(conf), true);
    assertEquals(1, gene.size());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testAddGene_5()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    CompositeGene gene2 = new CompositeGene(conf);
    gene.addGene(gene2, true);
    try {
      gene.addGene(gene2, false);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * Adding two different initial genes with strict parameter set is not allowed.
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testAddGene_6()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    gene.addGene(new CompositeGene(conf), true);
    try {
      gene.addGene(new CompositeGene(conf), true);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * Adding an initial CompositeGene twice should be possible, if they are two
   * newly created instances.
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testAddGene_7()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    gene.addGene(new CompositeGene(conf), true);
    gene.addGene(new CompositeGene(conf), false);
  }

  /**
   *
   * @author Klaus Meffert
   * @throws Exception
   */
  public void testToString_0()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    Gene newGene1 = new DoubleGene(conf);
    newGene1.setAllele(new Double(47.123d));
    gene.addGene(newGene1, false);
    assertEquals("CompositeGene=(" + newGene1.toString() + ")", gene.toString());
    Gene newGene2 = new IntegerGene(conf);
    newGene2.setAllele(new Integer(23456));
    gene.addGene(newGene2, false);
    assertEquals("CompositeGene=(" + newGene1.toString() + gene.GENE_DELIMITER
                 + newGene2.toString() + ")", gene.toString());
  }

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

  public void testGetAllele_0()
      throws Exception {
    Gene gene = new CompositeGene(conf);
    //this should be possible without exception
    gene.setAllele(new Vector());
  }

  public void testGetAllele_1()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 1, 5);
    gene.addGene(newGene1, false);
    Vector v = new Vector();
    v.add(new Integer(4));
    gene.setAllele(v);
    v = (Vector) gene.getAllele();
    assertEquals(newGene1.getAllele(), v.elementAt(0));
    assertEquals(1, v.size());
  }

  public void testGetAllele_2()
      throws Exception {
    CompositeGene gene = new CompositeGene(conf);
    Gene newGene1 = new IntegerGene(conf, 1, 5);
    gene.addGene(newGene1, false);
    Gene newGene2 = new DoubleGene(conf, 77.2d, 999.0d);
    gene.addGene(newGene2, false);
    Vector v = new Vector();
    v.add(new Integer(4));
    v.add(new Double(333.5d));
    gene.setAllele(v);
    v = (Vector) gene.getAllele();

⌨️ 快捷键说明

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