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

📄 fixedbinarygenetest.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 org.jgap.*;
import junit.framework.*;

/**
 * Tests the FixedBinaryGene class.
 *
 * @author Klaus Meffert
 * @author vamsi
 * @since 2.0
 */
public class FixedBinaryGeneTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.31 $";

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

  /**
   *
   * @author Klaus Meffert
   * @since 2.0
   * @throws Exception
   */
  public void testConstruct_0()
      throws Exception {
    // following should be possible without exception
    new FixedBinaryGene(conf, 1);
    new FixedBinaryGene(conf, 10);
    new FixedBinaryGene(conf, 1000);
    new FixedBinaryGene(conf, 100000);
  }

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

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

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testConstruct_3()
      throws Exception {
    int i = 0;
    FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
    for (i = 0; i < 4; i++) {
      //assert that we have
      assertFalse(gene.getBit(i));
    }
    assertEquals("FixedBinaryGene[0,0,0,0,0]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testConstruct_4()
      throws Exception {
    FixedBinaryGene gene = new FixedBinaryGene(conf, 6);
    assertEquals(1, gene.size());
    assertEquals(1, (gene.getValue()).length);
    assertEquals("FixedBinaryGene[0,0,0,0,0,0]", gene.toString());
  }

  /**
   * Buffer allocation test case.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testConstruct_5()
      throws Exception {
    FixedBinaryGene gene;
    gene = new FixedBinaryGene(conf, 32);
    assertEquals(1, gene.size());
    gene = new FixedBinaryGene(conf, 81);
    assertEquals(3, gene.size());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testConstruct_6()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1);
    new FixedBinaryGene(conf, gene1);
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testToString_0()
      throws Exception {
    Gene gene = new FixedBinaryGene(conf, 1);
    gene.setAllele(new int[] {1});
    assertEquals("FixedBinaryGene[1]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testToString_1()
      throws Exception {
    Gene gene = new FixedBinaryGene(conf, 3);
    gene.setAllele(new int[] {1, 0, 1});
    assertEquals("FixedBinaryGene[1,0,1]", gene.toString());
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testToString_2()
      throws Exception {
    Gene gene = new FixedBinaryGene(conf, 3);
    assertEquals("FixedBinaryGene[0,0,0]", gene.toString());
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testGetAllele_0()
      throws Exception {
    Gene gene = new FixedBinaryGene(conf, 1);
    int[] value = new int[] {
        0};
    gene.setAllele(value);
    assertEquals(value.length, ( (int[]) gene.getAllele()).length);
    for (int i = 0; i < value.length; i++) {
      assertEquals(value[i], ( (int[]) gene.getAllele())[i]);
    }
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testGetAllele_1()
      throws Exception {
    Gene gene = new FixedBinaryGene(conf, 2);
    try {
      gene.setAllele(new Integer(100));
      fail();
    }
    catch (ClassCastException classex) {
      ; //this is OK
    }
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.2
   * @throws Exception
   */
  public void testGetAllele_2()
      throws Exception {
    Gene gene = new FixedBinaryGene(conf, 1);
    int[] value = new int[] {
        1};
    gene.setAllele(value);
    assertEquals(value.length, ( (int[]) gene.getAllele()).length);
    for (int i = 0; i < value.length; i++) {
      assertEquals(value[i], ( (int[]) gene.getAllele())[i]);
    }
  }

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

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testEquals_1()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 1);
    assertFalse(gene1.equals(null));
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testEquals_2()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 2);
    gene1.setAllele(new int[] {1, 0});
    Gene gene2 = new FixedBinaryGene(conf, 2);
    gene2.setAllele(new int[] {0, 1});
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testEquals_3()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 5);
    assertFalse(gene1.equals(new IntegerGene(conf)));
  }

  public void testEquals_4()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 1);
    Gene gene2 = new IntegerGene(conf);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

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

  public void testEquals_6()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 1);
    Gene gene2 = new BooleanGene(conf);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  public void testEquals_7()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 1);
    Gene gene2 = new StringGene(conf);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   *
   * @author Klaus Meffert
   * @since 2.4
   * @throws Exception
   */
  public void testEquals_8()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 2);
    gene1.setAllele(new int[] {0, 1});
    Gene gene2 = new FixedBinaryGene(conf, 2);
    gene2.setAllele(new int[] {0, 0});
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testIntValues_0()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
    assertFalse(gene1.getIntValues() == null);
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testIntValues_1()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 2);
    int[] values = gene1.getIntValues();
    int i;
    for (i = 0; i < values.length; i++) {
      assertEquals(0, values[i]);
    }
  }

  /**
   *
   * @author vamsi
   * @throws Exception
   */
  public void testIntValues_2()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    gene1.setAllele(new int[] {0, 1, 0});
    assertEquals(false, gene1.getBit(0));
    assertEquals(true, gene1.getBit(1));
    assertEquals(false, gene1.getBit(2));
    assertEquals(3, gene1.getLength());
  }

  /**
   * Allele is null.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testSetAllele_0()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 1);
    try {
      gene1.setAllele(null);
      fail();
    }
    catch (IllegalArgumentException iex) {
      ; //this is OK
    }
  }

  /**
   * Allele is of wrong type.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testSetAllele_1()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 1);
    try {
      gene1.setAllele("22");
      fail();
    }
    catch (ClassCastException classex) {
      ; //this is OK
    }
  }

  /**
   * Set Allele to int values, no exception should occur.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testSetAllele_2()
      throws Exception {
    Gene gene1 = new FixedBinaryGene(conf, 3);
    gene1.setAllele(new int[] {0, 0, 1});
  }

  /**
   * The implementation should throw an exception if the alle size is more than
   * the size of the created gene.
   *
   * @author vamsi
   * @throws Exception
   */
  public void testSetAllele_3()
      throws Exception {
    FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
    try {
      gene1.setAllele(new int[] {0, 1, 1, 1, 1, 1});
      fail();
    }
    catch (Exception e) {
      ; //this is OK
    }
  }

  /**
   * Allele contains illegal characters.
   *
   * @author vamsi
   * @throws Exception
   */

⌨️ 快捷键说明

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