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

📄 mapgenetest.java

📁 JGAP(发音"jay-gap")是一款用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 MapGene class
 *
 * @author Klaus Meffert
 * @since 2.4
 */
public class MapGeneTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.13 $";

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

  public void testConstruct_0()
      throws Exception {
    Gene gene = new MapGene(conf);
    assertNotNull(privateAccessor.getField(gene, "m_geneMap"));
  }

  public void testConstruct_1()
      throws Exception {
    Map map = new HashMap();
    map.put(new Integer(2), new Integer(3));
    MapGene gene = new MapGene(conf, map);
    Map geneMap = (Map) privateAccessor.getField(gene, "m_geneMap");
    assertNotNull(geneMap);
    assertEquals(new Integer(3), geneMap.get(new Integer(2)));
    assertEquals(1, geneMap.size());
  }

  public void testConstruct_2()
      throws Exception {
    try {
      new MapGene(conf, null);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  public void testToString_0()
      throws Exception {
    MapGene gene = new MapGene(conf);
    assertEquals("[null]", gene.toString());
  }

  public void testToString_1()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(new Integer(102));
    assertEquals("[(102,102)]", gene.toString());
  }

  public void testToString_2()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(new Integer(3), new Integer(102));
    gene.addAllele(new Integer(7), new Integer( -55));
    assertEquals("[(3,102),(7,-55)]", gene.toString());
  }

  public void testToString_3()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(new Double(3), new Integer(102));
    gene.addAllele(new Double(7), new Integer( -55));
    assertEquals("[(3.0,102),(7.0,-55)]", gene.toString());
  }

  /**
   * Using different types as keys. Order of adding alleles not important!
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testToString_4()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(new Integer(3), new Double(102));
    gene.addAllele(new Double(7), new Integer( -55));
    assertEquals("[(7.0,-55),(3,102.0)]", gene.toString());
  }

  /**
   * Using different types as keys. Order of adding alleles not important!
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testToString_5()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(new Double(7), new Integer( -55));
    gene.addAllele(new Integer(3), new Double(102));
    assertEquals("[(7.0,-55),(3,102.0)]", gene.toString());
  }

  public void testGetAllele_0()
      throws Exception {
    MapGene gene = new MapGene(conf);
    gene.addAllele(1);
    gene.addAllele(7);
    gene.addAllele( -5);
    gene.addAllele(3);
    gene.setAllele(new Integer(1));
    assertEquals(new Integer(1), gene.getAllele());
    gene.setAllele(new Integer(3));
    assertEquals(new Integer(3), gene.getAllele());
    gene.setAllele(new Integer( -5));
    assertEquals(new Integer( -5), gene.getAllele());
    gene.setAllele(new Integer(7));
    assertEquals(new Integer(7), gene.getAllele());
    try {
      gene.setAllele(new Integer(2));
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  public void testEquals_0()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    Gene gene2 = new MapGene(conf);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  public void testEquals_1()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    assertFalse(gene1.equals(null));
  }

  public void testEquals_2()
      throws Exception {
    Map alleles = new HashMap();
    alleles.put(new Integer(1), new Integer(1));
    Gene gene1 = new MapGene(conf, alleles);
    assertFalse(gene1.equals(new BooleanGene(conf)));
  }

  public void testEquals_3()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    assertFalse(gene1.equals(new Vector()));
  }

  public void testEquals_4()
      throws Exception {
    Map alleles = new HashMap();
    alleles.put(new Integer(1), new Integer(1));
    Gene gene1 = new MapGene(conf, alleles);
    Gene gene2 = new MapGene(conf, alleles);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  public void testEquals_4_2()
      throws Exception {
    Map alleles1 = new HashMap();
    alleles1.put(new Integer(1), new Integer(1));
    Gene gene1 = new MapGene(conf, alleles1);
    Map alleles2 = new HashMap();
    alleles1.put(new Integer(2), new Integer(3));
    Gene gene2 = new MapGene(conf, alleles2);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  public void testEquals_5()
      throws Exception {
    Gene gene1 = new MapGene(conf);
    Gene gene2 = new DoubleGene(conf, 1, 99);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * Set Allele to null, no exception should occur
   * @throws Exception
   */
  public void testSetAllele_0()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.setAllele(null);
  }

  public void testSetAllele_1()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.setAllele("22");
  }

  public void testSetAllele_2()
      throws Exception {
    Gene gene = new MapGene(conf);
    gene.setAllele(new Integer(101));
  }

  /**
   * @throws Exception
   * @since 2.5
   */
  public void testNewGene_0()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    gene1.setAllele(new Integer(4711));
    Object value1 = privateAccessor.getField(gene1, "m_value");
    MapGene gene2 = (MapGene) gene1.newGene();
    Object value2 = privateAccessor.getField(gene2, "m_value");
    Map geneMap = (Map) privateAccessor.getField(gene2, "m_geneMap");
    assertEquals(value1, value2);
    assertEquals(0, geneMap.size());
    assertEquals(gene1, gene2);
  }

  /**
   * @throws Exception
   * @since 2.5
   */
  public void testNewGene_1()
      throws Exception {
    MapGene gene1 = new MapGene(conf);
    MapGene gene2 = (MapGene) gene1.newGene();
    assertEquals(gene1, gene2);
  }

  /**
   * @throws Exception
   * @since 2.5
   */
  public void testNewGene_2()
      throws Exception {
    Map alleles = new Hashtable();
    for (int i = 0; i < 40; i++) {
      alleles.put(new Integer(i), new Integer(i));
    }
    MapGene gene1 = new MapGene(conf, alleles);
    MapGene gene2 = (MapGene) gene1.newGene();
    assertTrue(gene1.equals(gene2));
  }

  public void testCleanup()
      throws Exception {
    //cleanup should do nothing!
    Gene gene = new MapGene(conf);
    gene.setAllele("Hello");
    Gene copy = gene.newGene();
    gene.cleanup();
    assertEquals(copy, gene);
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert

⌨️ 快捷键说明

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