chainofselectorstest.java

来自「jgap3.2 遗传算法工具包,嘿嘿,笨鸟先飞哦」· Java 代码 · 共 175 行

JAVA
175
字号
/*
 * 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 ChainOfSelectors class.
 *
 * @since 1.1
 * @author Klaus Meffert
 */
public class ChainOfSelectorsTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.10 $";

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

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testConstruct_0() {
    new ChainOfSelectors();
  }

  /**
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testClear_0() {
    ChainOfSelectors c = new ChainOfSelectors();
    assertTrue(c.isEmpty());
    c.clear();
    assertTrue(c.isEmpty());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testClear_1()
      throws Exception {
    ChainOfSelectors c = new ChainOfSelectors();
    assertEquals(0, c.size());
    c.addNaturalSelector(new BestChromosomesSelector(conf));
    assertEquals(1, c.size());
    assertFalse(c.isEmpty());
    c.clear();
    assertTrue(c.isEmpty());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testClear_2()
      throws Exception {
    ChainOfSelectors c = new ChainOfSelectors();
    Collection l = new Vector();
    l.add(new BestChromosomesSelector(conf));
    l.add(new WeightedRouletteSelector(conf));
    c.addAll(l);
    assertEquals(2, c.size());
    c.clear();
    assertTrue(c.isEmpty());
    assertEquals(0, c.size());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testIterator_0()
      throws Exception {
    ChainOfSelectors c = new ChainOfSelectors();
    Collection l = new Vector();
    l.add(new BestChromosomesSelector(conf));
    l.add(new WeightedRouletteSelector(conf));
    c.addAll(l);
    Iterator it = c.iterator();
    assertTrue(it.hasNext());
    assertNotNull(it.next());
    assertTrue(it.hasNext());
    assertNotNull(it.next());
    assertFalse(it.hasNext());
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public void testAddNaturalSelector_0()
      throws Exception {
    ChainOfSelectors c = new ChainOfSelectors();
    try {
      c.addNaturalSelector(null);
      fail();
    }
    catch (InvalidConfigurationException inex) {
      ; //this is OK
    }
  }

  public void testEquals_0()
      throws Exception {
    ChainOfSelectors c1 = new ChainOfSelectors();
    ChainOfSelectors c2 = new ChainOfSelectors();
    assertFalse(c1.equals(null));
    assertTrue(c1.equals(c2));
    TournamentSelector t1 = new TournamentSelector();
    c1.addNaturalSelector(t1);
    assertFalse(c1.equals(c2));
    c2.addNaturalSelector(new TournamentSelector());
    /**@todo improve: 2 unsame selectors of same class with same params should
     * make the chain equal!
     */
    assertFalse(c1.equals(c2));
    c2.clear();
    c2.addNaturalSelector(t1);
    assertTrue(c1.equals(c2));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testEquals_1()
      throws Exception {
    ChainOfSelectors c1 = new ChainOfSelectors();
    assertFalse(c1.equals(new BooleanGene(conf)));
  }

  /**
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.6
   */
  public void testHashCode_0()
      throws Exception {
    ChainOfSelectors c1 = new ChainOfSelectors();
    ChainOfSelectors c2 = new ChainOfSelectors();
    assertEquals(c1.hashCode(), c2.hashCode());
    c1.addNaturalSelector(new BestChromosomesSelector(conf));
    assertFalse(c1.hashCode() == c2.hashCode());
    assertEquals(c1.hashCode(), c1.hashCode());
    c2.addNaturalSelector(new BestChromosomesSelector(conf));
    assertFalse(c1.hashCode() == c2.hashCode());
  }
}

⌨️ 快捷键说明

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