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

📄 stockrandomgeneratortest.java

📁 java实现的遗传算法
💻 JAVA
字号:
/*
 * 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 for StockRandomGenerator class
 *
 * @author Klaus Meffert
 * @since 2.2
 */
public class StockRandomGeneratorTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private static final String CVS_REVISION = "$Revision: 1.4 $";

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

  /**
   * Check if construction and calculation in general possible
   */
  public void testGeneral() {
    StockRandomGenerator calc = new StockRandomGenerator();
    calc.nextInt();
    calc.nextBoolean();
    calc.nextDouble();
    calc.nextFloat();
    calc.nextInt();
    calc.nextLong();
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_0() {
    StockRandomGenerator calc = new StockRandomGenerator();
    int i = calc.nextInt();
    if (Math.abs(i) < DELTA) {
      i = calc.nextInt();
      if (Math.abs(i) < DELTA) {
        i = calc.nextInt();
        if (Math.abs(i) < DELTA) {
          fail();
        }
      }
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNextInt_0()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    int res;
    for (int i = 0; i < 100; i++) {
      res = calc.nextInt(5);
      assertTrue(res < 5);
      assertTrue(res >= 0);
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNextInt_1()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    int res;
    int resOli = 0;
    for (int i = 0; i < 100; i++) {
      res = calc.nextInt();
      if (i > 0) {
        if (resOli == res) {
          fail("Two consecutive calls produced same value: " + res);
        }
        else {
          resOli = res;
        }
      }
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNextInt_2()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    int res;
    int resOli = 0;
    for (int i = 0; i < 100; i++) {
      res = calc.nextInt(100000);
      if (i > 0) {
        if (resOli == res) {
          fail("Two consecutive calls produced same value: " + res);
        }
        else {
          resOli = res;
        }
      }
      assertTrue(res >= 0.000d);
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNextLong_0()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    long res;
    long resOll = 0;
    for (int i = 0; i < 100; i++) {
      res = calc.nextLong();
      if (i > 0) {
        if (resOll == res) {
          fail("Two consecutive calls produced same value: " + res);
        }
        else {
          resOll = res;
        }
      }
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNextDouble_0()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    double res;
    double resOld = 0.0000d;
    for (int i = 0; i < 100; i++) {
      res = calc.nextDouble();
      if (i > 0) {
        if (Math.abs(resOld - res) < DELTA) {
          fail("Two consecutive calls produced same value: " + res);
        }
        else {
          resOld = res;
        }
      }
      assertTrue(res >= 0.000d);
    }
  }

  /**
   *
   * @throws Exception
   *
   * @author Klaus Meffert
   */
  public void testNextFloat_0()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    float res;
    float resOlf = 0.0000f;
    for (int i = 0; i < 100; i++) {
      res = calc.nextFloat();
      if (i > 0) {
        if (Math.abs(resOlf - res) < DELTA) {
          fail("Two consecutive calls produced same value: " + res);
        }
        else {
          resOlf = res;
        }
      }
      assertTrue(res >= 0.000d);
    }
  }

  /**
   * Tests distribution of boolean random values. We should have more than 10%
   * of each true and false when running a significant number of tests
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.3
   */
  public void testNextBoolean_0()
      throws Exception {
    StockRandomGenerator calc = new StockRandomGenerator();
    boolean res;
    int trueCounter = 0;
    int falseCounter = 0;
    int total = 100;
    for (int i = 0; i < total; i++) {
      res = calc.nextBoolean();
      if (res) {
        trueCounter++;
      }
      else {
        falseCounter++;
      }
    }
    assertTrue(trueCounter > total * 0.1);
    assertTrue(falseCounter > total * 0.1);
  }
}

⌨️ 快捷键说明

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