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

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

  public void setUp() {
    super.setUp();
  }

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_0()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene = new DoubleGene(conf, 1.1d, 100.0d);
    //following should be possible without exception
    gene.setAllele(new Double(101.1d));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_1()
      throws Exception {
    Gene gene = new DoubleGene(conf);
    gene.setAllele(new Double(Double.MAX_VALUE));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testConstruct_2()
      throws Exception {
    Gene gene = new DoubleGene(conf);
    gene.setAllele(new Double( - (Double.MAX_VALUE / 2)));
  }

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testToString_0()
      throws Exception {
    Gene gene = new DoubleGene(conf, 1.2d, 99.7d);
    gene.setAllele(new Double(47.3d));
    assertEquals("DoubleGene(1.2,99.7)=47.3", gene.toString());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testToString_1()
      throws Exception {
    Gene gene = new DoubleGene(conf, -100.0d, 100.0d);
    gene.setAllele(new Double( -88.75286d));
    assertEquals("DoubleGene(-100.0,100.0)=-88.75286", gene.toString());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testToString_2()
      throws Exception {
    Gene gene = new DoubleGene(conf, 1.2d, 99.7d);
    assertEquals("DoubleGene(1.2,99.7)=null", gene.toString());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testGetAllele_0()
      throws Exception {
    Gene gene = new DoubleGene(conf, 1.9d, 100.4d);
    gene.setAllele(new Double(33.0d));
    assertEquals(new Double(33.0d), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testGetAllele_1()
      throws Exception {
    Gene gene = new DoubleGene(conf, 1.8d, 100.1d);
    gene.setAllele(new Double(1.9d));
    assertEquals(new Double(1.9d), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testGetAllele_2()
      throws Exception {
    Gene gene = new DoubleGene(conf, 1.0d, 100.0d);
    gene.setAllele(new Double(100.0d));
    assertEquals(new Double(100.0d), gene.getAllele());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEquals_0()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.1d, 100.2d);
    Gene gene2 = new DoubleGene(conf, 1.1d, 100.2d);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEquals_1()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.9d, 100.4d);
    assertFalse(gene1.equals(null));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEquals_2()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 11.2d, 100.7d);
    assertFalse(gene1.equals(new BooleanGene(conf)));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEquals_3()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.0d, 100.7d);
    assertFalse(gene1.equals(new Vector()));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEquals_4()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.2d, 100.3d);
    Gene gene2 = new DoubleGene(conf, 1.2d, 99.5d);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

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

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

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testEquals_7()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1, 99);
    Gene gene2 = new IntegerGene(conf);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testEquals_8()
      throws Exception {
    Configuration conf = new ConfigurationForTest();
    Gene gene1 = new DoubleGene(conf, 1.2d, 100.3d);
    gene1.setAllele(new Double(1));
    Gene gene2 = new DoubleGene(conf, 1.2d, 99.5d);
    gene2.setAllele(new Double( -1));
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testDoubleValue_0()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
    gene1.setAllele(new Double(4711.0d));
    assertEquals(4711.0d, gene1.doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testDoubleValue_1()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(conf, 1.765d, 10000.0d);
    gene1.setAllele(null);
    try {
      assertEquals(0.0d, gene1.doubleValue(), DELTA);
      fail();
    }
    catch (NullPointerException nullex) {
      ; //this is OK
    }
  }

  /**
   * Set allele to null, no exception should occur.
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_0()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
    gene1.setAllele(null);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_1()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
    try {
      gene1.setAllele("22");
      fail();
    }
    catch (ClassCastException classex) {
      ; //this is OK
    }
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testSetAllele_2()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
    try {
      gene1.setAllele(new Integer(22));
      fail();
    }
    catch (ClassCastException classex) {
      ; //this is OK
    }
  }

  /**
   * Call setAllele with need of mapping to bounds.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testSetAllele_3()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 1.0d, 1000.0d);
    gene1.setAllele(new Double(2000.0d));
  }

  /**
   * Call setAllele with need of mapping to bounds.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testSetAllele_4()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(conf, 1.0d, 1.0d);
    gene1.setAllele(new Double(5.7d));
    assertEquals(1.0d, gene1.doubleValue(), DELTA);
  }

  /**
   * Call setAllele with need of mapping to bounds.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testSetAllele_5()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(conf, 0.0d, 0.0d);
    gene1.setAllele(new Double(5.7d));
    assertEquals(0.0d, gene1.doubleValue(), DELTA);
  }

  /**
   * Call setAllele with need of mapping to bounds.
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void testSetAllele_6()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(conf, -12.5d, -12.5d);
    gene1.setAllele(new Double(5.7d));
    assertEquals(-12.5d, gene1.doubleValue(), DELTA);
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNewGene_0()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(conf, 1.0d, 10000.0d);
    IGeneConstraintChecker checker = new GeneConstraintChecker();
    gene1.setConstraintChecker(checker);
    gene1.setAllele(new Double(4711.0d));
    Double lower1 = (Double) privateAccessor.getField(gene1,
        "m_lowerBound");
    Double upper1 = (Double) privateAccessor.getField(gene1,
        "m_upperBound");
    DoubleGene gene2 = (DoubleGene) gene1.newGene();
    Double lower2 = (Double) privateAccessor.getField(gene2,
        "m_lowerBound");
    Double upper2 = (Double) privateAccessor.getField(gene2,
        "m_upperBound");
    assertEquals(lower1, lower2);
    assertEquals(upper1, upper2);
    assertEquals(checker, gene2.getConstraintChecker());
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_0()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
    gene1.setAllele(new Double(4.5d));
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new DoubleGene(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 DoubleGene(conf, 2.05d, 7.53d);
    gene1.setValueFromPersistentRepresentation(null);
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_2()
      throws Exception {
    Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d);
    try {
      gene1.setValueFromPersistentRepresentation("2.3");
      fail();
    }

⌨️ 快捷键说明

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