571 lines
19 KiB
Diff
571 lines
19 KiB
Diff
diff -Nur commons-lang-2.6-src-bak/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java commons-lang-2.6-src/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java
|
|
--- commons-lang-2.6-src-bak/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java 2019-12-04 17:52:01.857276428 +0800
|
|
+++ commons-lang-2.6-src/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java 1970-01-01 08:00:00.000000000 +0800
|
|
@@ -1,342 +0,0 @@
|
|
-/*
|
|
- * Licensed to the Apache Software Foundation (ASF) under one or more
|
|
- * contributor license agreements. See the NOTICE file distributed with
|
|
- * this work for additional information regarding copyright ownership.
|
|
- * The ASF licenses this file to You under the Apache License, Version 2.0
|
|
- * (the "License"); you may not use this file except in compliance with
|
|
- * the License. You may obtain a copy of the License at
|
|
- *
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
- *
|
|
- * Unless required by applicable law or agreed to in writing, software
|
|
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
- * See the License for the specific language governing permissions and
|
|
- * limitations under the License.
|
|
- */
|
|
-package org.apache.commons.lang.math;
|
|
-
|
|
-import java.util.Random;
|
|
-
|
|
-import junit.framework.TestCase;
|
|
-
|
|
-/**
|
|
- * Test cases for the {@link RandomUtils} class.
|
|
- *
|
|
- * @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
|
|
- * @version $Revision: 912374 $ $Date: 2010-02-21 17:20:57 +0000 (Sun, 21 Feb 2010) $
|
|
- */
|
|
-
|
|
-public final class RandomUtilsTest extends TestCase {
|
|
-
|
|
- public RandomUtilsTest(String name) {
|
|
- super(name);
|
|
- }
|
|
-
|
|
- /** test distribution of nextInt() */
|
|
- public void testNextInt() {
|
|
- tstNextInt(null);
|
|
-
|
|
- assertTrue (RandomUtils.nextInt() >= 0);
|
|
- }
|
|
-
|
|
- /** test distribution of nextInt(Random) */
|
|
- public void testNextInt2() {
|
|
- Random rnd = new Random();
|
|
- rnd.setSeed(System.currentTimeMillis());
|
|
- tstNextInt(rnd);
|
|
- }
|
|
-
|
|
- /** test distribution of JVMRandom.nextInt() */
|
|
- public void testJvmRandomNextInt() {
|
|
- tstNextInt(RandomUtils.JVM_RANDOM);
|
|
- }
|
|
-
|
|
-
|
|
- /**
|
|
- * Generate 1000 values for nextInt(bound) and compare
|
|
- * the observed frequency counts to expected counts using
|
|
- * a chi-square test.
|
|
- * @param rnd Random to use if not null
|
|
- */
|
|
- private void tstNextInt(Random rnd) {
|
|
- int bound = 0;
|
|
- int result = 0;
|
|
- // test boundary condition: n = Integer.MAX_VALUE;
|
|
- bound = Integer.MAX_VALUE;
|
|
- if (rnd == null) {
|
|
- result = RandomUtils.nextInt(bound);
|
|
- } else {
|
|
- result = RandomUtils.nextInt(rnd,bound);
|
|
- }
|
|
- assertTrue("result less than bound",result < bound);
|
|
- assertTrue("result non-negative",result >= 0);
|
|
-
|
|
- // test uniformity -- use Chi-Square test at .01 level
|
|
- bound = 4;
|
|
- int[] expected = new int[] {250,250,250,250};
|
|
- int[] observed = new int[] {0,0,0,0};
|
|
- for (int i = 0; i < 1000; i ++) {
|
|
- if (rnd == null) {
|
|
- result = RandomUtils.nextInt(bound);
|
|
- } else {
|
|
- result = RandomUtils.nextInt(rnd,bound);
|
|
- }
|
|
- assertTrue(result < bound);
|
|
- assertTrue(result >= 0);
|
|
- observed[result]++;
|
|
- }
|
|
- /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
|
|
- * Change to 11.34 for alpha = .01
|
|
- */
|
|
- assertTrue(
|
|
- "chi-square test -- will fail about 1 in 1000 times",
|
|
- chiSquare(expected,observed) < 16.27);
|
|
- }
|
|
-
|
|
- /** test distribution of nextLong() */
|
|
- public void testNextLong() {
|
|
- tstNextLong(null);
|
|
- }
|
|
-
|
|
- /** test distribution of nextLong(Random) BROKEN
|
|
- * contract of nextLong(Random) is different from
|
|
- * nextLong() */
|
|
- public void testNextLong2() {
|
|
- Random rnd = new Random();
|
|
- rnd.setSeed(System.currentTimeMillis());
|
|
- tstNextLong(rnd);
|
|
- }
|
|
-
|
|
- /**
|
|
- * Generate 1000 values for nextLong() and check that
|
|
- * p(value < long.MAXVALUE/2) ~ 0.5. Use chi-square test
|
|
- * with df = 2-1 = 1
|
|
- * @param rnd Random to use if not null
|
|
- */
|
|
- private void tstNextLong(Random rnd) {
|
|
- // Distribution
|
|
- int[] expected = new int[] {500,500};
|
|
- int[] observed = new int[] {0,0};
|
|
- // Even/Odd
|
|
- int[] expected2 = new int[] {500,500};
|
|
- int[] observed2 = new int[] {0,0};
|
|
- long result = 0;
|
|
- long midPoint = Long.MAX_VALUE/2;
|
|
- for (int i = 0; i < 1000; i ++) {
|
|
- if (rnd == null) {
|
|
- result = Math.abs(RandomUtils.nextLong());
|
|
- } else {
|
|
- result = Math.abs(RandomUtils.nextLong(rnd));
|
|
- }
|
|
- if (result < midPoint) {
|
|
- observed[0]++;
|
|
- } else {
|
|
- observed[1]++;
|
|
- }
|
|
- if (result % 2 == 0) {
|
|
- observed2[0]++;
|
|
- } else {
|
|
- observed2[1]++;
|
|
- }
|
|
- }
|
|
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
|
|
- * Change to 6.64 for alpha = .01
|
|
- */
|
|
- assertTrue(
|
|
- "mid point chi-square test -- will fail about 1 in 1000 times",
|
|
- chiSquare(expected,observed) < 10.83);
|
|
- assertTrue(
|
|
- "odd/even chi-square test -- will fail about 1 in 1000 times",
|
|
- chiSquare(expected2,observed2) < 10.83);
|
|
- }
|
|
-
|
|
-
|
|
- /** test distribution of nextBoolean() */
|
|
- public void testNextBoolean() {
|
|
- tstNextBoolean(null);
|
|
- }
|
|
-
|
|
- /** test distribution of nextBoolean(Random) */
|
|
- public void testNextBoolean2() {
|
|
- Random rnd = new Random();
|
|
- rnd.setSeed(System.currentTimeMillis());
|
|
- tstNextBoolean(rnd);
|
|
- }
|
|
-
|
|
- /**
|
|
- * Generate 1000 values for nextBoolean and check that
|
|
- * p(value = false) ~ 0.5. Use chi-square test
|
|
- * with df = 2-1 = 1
|
|
- * @param rnd Random to use if not null
|
|
- */
|
|
- private void tstNextBoolean(Random rnd) {
|
|
- int[] expected = new int[] {500,500};
|
|
- int[] observed = new int[] {0,0};
|
|
- boolean result = false;
|
|
- for (int i = 0; i < 1000; i ++) {
|
|
- if (rnd == null) {
|
|
- result = RandomUtils.nextBoolean();
|
|
- } else {
|
|
- result = RandomUtils.nextBoolean(rnd);
|
|
- }
|
|
- if (result) {
|
|
- observed[0]++;
|
|
- } else {
|
|
- observed[1]++;
|
|
- }
|
|
- }
|
|
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
|
|
- * Change to 6.64 for alpha = .01
|
|
- */
|
|
- assertTrue(
|
|
- "chi-square test -- will fail about 1 in 1000 times",
|
|
- chiSquare(expected,observed) < 10.83 );
|
|
- }
|
|
-
|
|
- /** test distribution of nextFloat() */
|
|
- public void testNextFloat() {
|
|
- tstNextFloat(null);
|
|
- }
|
|
-
|
|
- /** test distribution of nextFloat(Random) */
|
|
- public void testNextFloat2() {
|
|
- Random rnd = new Random();
|
|
- rnd.setSeed(System.currentTimeMillis());
|
|
- tstNextFloat(rnd);
|
|
- }
|
|
-
|
|
- /**
|
|
- * Generate 1000 values for nextFloat and check that
|
|
- * p(value < 0.5) ~ 0.5. Use chi-square test
|
|
- * with df = 2-1 = 1
|
|
- * @param rnd Random to use if not null
|
|
- */
|
|
- private void tstNextFloat(Random rnd) {
|
|
- int[] expected = new int[] {500,500};
|
|
- int[] observed = new int[] {0,0};
|
|
- float result = 0;
|
|
- for (int i = 0; i < 1000; i ++) {
|
|
- if (rnd == null) {
|
|
- result = RandomUtils.nextFloat();
|
|
- } else {
|
|
- result = RandomUtils.nextFloat(rnd);
|
|
- }
|
|
- if (result < 0.5) {
|
|
- observed[0]++;
|
|
- } else {
|
|
- observed[1]++;
|
|
- }
|
|
- }
|
|
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
|
|
- * Change to 6.64 for alpha = .01
|
|
- */
|
|
- assertTrue(
|
|
- "chi-square test -- will fail about 1 in 1000 times",
|
|
- chiSquare(expected,observed) < 10.83);
|
|
- }
|
|
-
|
|
- /** test distribution of nextDouble() */
|
|
- public void testNextDouble() {
|
|
- tstNextDouble(null);
|
|
- }
|
|
-
|
|
- /** test distribution of nextDouble(Random) */
|
|
- public void testNextDouble2() {
|
|
- Random rnd = new Random();
|
|
- rnd.setSeed(System.currentTimeMillis());
|
|
- tstNextDouble(rnd);
|
|
- }
|
|
-
|
|
- /**
|
|
- * Generate 1000 values for nextFloat and check that
|
|
- * p(value < 0.5) ~ 0.5. Use chi-square test
|
|
- * with df = 2-1 = 1
|
|
- * @param rnd Random to use if not null
|
|
- */
|
|
- private void tstNextDouble(Random rnd) {
|
|
- int[] expected = new int[] {500,500};
|
|
- int[] observed = new int[] {0,0};
|
|
- double result = 0;
|
|
- for (int i = 0; i < 1000; i ++) {
|
|
- if (rnd == null) {
|
|
- result = RandomUtils.nextDouble();
|
|
- } else {
|
|
- result = RandomUtils.nextDouble(rnd);
|
|
- }
|
|
- if (result < 0.5) {
|
|
- observed[0]++;
|
|
- } else {
|
|
- observed[1]++;
|
|
- }
|
|
- }
|
|
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
|
|
- * Change to 6.64 for alpha = .01
|
|
- */
|
|
- assertTrue(
|
|
- "chi-square test -- will fail about 1 in 1000 times",
|
|
- chiSquare(expected,observed) < 10.83);
|
|
- }
|
|
-
|
|
- /** make sure that unimplemented methods fail */
|
|
- public void testUnimplementedMethods() {
|
|
-
|
|
- try {
|
|
- RandomUtils.JVM_RANDOM.setSeed(1000);
|
|
- fail("expecting UnsupportedOperationException");
|
|
- } catch (UnsupportedOperationException ex) {
|
|
- // empty
|
|
- }
|
|
-
|
|
- try {
|
|
- RandomUtils.JVM_RANDOM.nextGaussian();
|
|
- fail("expecting UnsupportedOperationException");
|
|
- } catch (UnsupportedOperationException ex) {
|
|
- // empty
|
|
- }
|
|
-
|
|
- try {
|
|
- RandomUtils.JVM_RANDOM.nextBytes(null);
|
|
- fail("expecting UnsupportedOperationException");
|
|
- } catch (UnsupportedOperationException ex) {
|
|
- // empty
|
|
- }
|
|
-
|
|
- }
|
|
-
|
|
- /** make sure that illegal arguments fail */
|
|
- public void testIllegalArguments() {
|
|
-
|
|
- try {
|
|
- RandomUtils.JVM_RANDOM.nextInt(-1);
|
|
- fail("expecting IllegalArgumentException");
|
|
- } catch (IllegalArgumentException ex) {
|
|
- // empty
|
|
- }
|
|
-
|
|
- try {
|
|
- JVMRandom.nextLong( -1L );
|
|
- fail("expecting IllegalArgumentException");
|
|
- } catch (IllegalArgumentException ex) {
|
|
- // empty
|
|
- }
|
|
-
|
|
- }
|
|
-
|
|
- /**
|
|
- * Computes Chi-Square statistic given observed and expected counts
|
|
- * @param observed array of observed frequency counts
|
|
- * @param expected array of expected frequency counts
|
|
- */
|
|
- private double chiSquare(int[] expected, int[] observed) {
|
|
- double sumSq = 0.0d;
|
|
- double dev = 0.0d;
|
|
- for (int i = 0; i< observed.length; i++) {
|
|
- dev = (double)(observed[i] - expected[i]);
|
|
- sumSq += dev*dev/(double)expected[i];
|
|
- }
|
|
- return sumSq;
|
|
- }
|
|
-
|
|
-}
|
|
-
|
|
diff -Nur commons-lang-2.6-src-bak/src/test/java/org/apache/commons/lang/time/StopWatchTest.java commons-lang-2.6-src/src/test/java/org/apache/commons/lang/time/StopWatchTest.java
|
|
--- commons-lang-2.6-src-bak/src/test/java/org/apache/commons/lang/time/StopWatchTest.java 2019-12-04 17:52:01.847276247 +0800
|
|
+++ commons-lang-2.6-src/src/test/java/org/apache/commons/lang/time/StopWatchTest.java 1970-01-01 08:00:00.000000000 +0800
|
|
@@ -1,221 +0,0 @@
|
|
-/*
|
|
- * Licensed to the Apache Software Foundation (ASF) under one or more
|
|
- * contributor license agreements. See the NOTICE file distributed with
|
|
- * this work for additional information regarding copyright ownership.
|
|
- * The ASF licenses this file to You under the Apache License, Version 2.0
|
|
- * (the "License"); you may not use this file except in compliance with
|
|
- * the License. You may obtain a copy of the License at
|
|
- *
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
- *
|
|
- * Unless required by applicable law or agreed to in writing, software
|
|
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
- * See the License for the specific language governing permissions and
|
|
- * limitations under the License.
|
|
- */
|
|
-package org.apache.commons.lang.time;
|
|
-
|
|
-import junit.framework.Assert;
|
|
-import junit.framework.TestCase;
|
|
-
|
|
-/**
|
|
- * TestCase for StopWatch.
|
|
- *
|
|
- * @author Apache Software Foundation
|
|
- * @version $Id: StopWatchTest.java 905636 2010-02-02 14:03:32Z niallp $
|
|
- */
|
|
-public class StopWatchTest extends TestCase {
|
|
-
|
|
- public StopWatchTest(String s) {
|
|
- super(s);
|
|
- }
|
|
-
|
|
- //-----------------------------------------------------------------------
|
|
- public void testStopWatchSimple(){
|
|
- StopWatch watch = new StopWatch();
|
|
- watch.start();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.stop();
|
|
- long time = watch.getTime();
|
|
- assertEquals(time, watch.getTime());
|
|
-
|
|
- assertTrue(time >= 500);
|
|
- assertTrue(time < 700);
|
|
-
|
|
- watch.reset();
|
|
- assertEquals(0, watch.getTime());
|
|
- }
|
|
-
|
|
- public void testStopWatchSimpleGet(){
|
|
- StopWatch watch = new StopWatch();
|
|
- assertEquals(0, watch.getTime());
|
|
- assertEquals("0:00:00.000", watch.toString());
|
|
-
|
|
- watch.start();
|
|
- try {Thread.sleep(500);} catch (InterruptedException ex) {}
|
|
- assertTrue(watch.getTime() < 2000);
|
|
- }
|
|
-
|
|
- public void testStopWatchSplit(){
|
|
- StopWatch watch = new StopWatch();
|
|
- watch.start();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.split();
|
|
- long splitTime = watch.getSplitTime();
|
|
- String splitStr = watch.toSplitString();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.unsplit();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.stop();
|
|
- long totalTime = watch.getTime();
|
|
-
|
|
- assertEquals("Formatted split string not the correct length",
|
|
- splitStr.length(), 11);
|
|
- assertTrue(splitTime >= 500);
|
|
- assertTrue(splitTime < 700);
|
|
- assertTrue(totalTime >= 1500);
|
|
- assertTrue(totalTime < 1900);
|
|
- }
|
|
-
|
|
- public void testStopWatchSuspend(){
|
|
- StopWatch watch = new StopWatch();
|
|
- watch.start();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.suspend();
|
|
- long suspendTime = watch.getTime();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.resume();
|
|
- try {Thread.sleep(550);} catch (InterruptedException ex) {}
|
|
- watch.stop();
|
|
- long totalTime = watch.getTime();
|
|
-
|
|
- assertTrue(suspendTime >= 500);
|
|
- assertTrue(suspendTime < 700);
|
|
- assertTrue(totalTime >= 1000);
|
|
- assertTrue(totalTime < 1300);
|
|
- }
|
|
-
|
|
- public void testLang315() {
|
|
- StopWatch watch = new StopWatch();
|
|
- watch.start();
|
|
- try {Thread.sleep(200);} catch (InterruptedException ex) {}
|
|
- watch.suspend();
|
|
- long suspendTime = watch.getTime();
|
|
- try {Thread.sleep(200);} catch (InterruptedException ex) {}
|
|
- watch.stop();
|
|
- long totalTime = watch.getTime();
|
|
- assertTrue( suspendTime == totalTime );
|
|
- }
|
|
-
|
|
- // test bad states
|
|
- public void testBadStates() {
|
|
- StopWatch watch = new StopWatch();
|
|
- try {
|
|
- watch.stop();
|
|
- fail("Calling stop on an unstarted StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.stop();
|
|
- fail("Calling stop on an unstarted StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.suspend();
|
|
- fail("Calling suspend on an unstarted StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.split();
|
|
- fail("Calling split on a non-running StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.unsplit();
|
|
- fail("Calling unsplit on an unsplit StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.resume();
|
|
- fail("Calling resume on an unsuspended StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- watch.start();
|
|
-
|
|
- try {
|
|
- watch.start();
|
|
- fail("Calling start on a started StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.unsplit();
|
|
- fail("Calling unsplit on an unsplit StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.getSplitTime();
|
|
- fail("Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- try {
|
|
- watch.resume();
|
|
- fail("Calling resume on an unsuspended StopWatch should throw an exception. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
-
|
|
- watch.stop();
|
|
-
|
|
- try {
|
|
- watch.start();
|
|
- fail("Calling start on a stopped StopWatch should throw an exception as it needs to be reset. ");
|
|
- } catch(IllegalStateException ise) {
|
|
- // expected
|
|
- }
|
|
- }
|
|
-
|
|
- public void testGetStartTime() {
|
|
- long beforeStopWatch = System.currentTimeMillis();
|
|
- StopWatch watch = new StopWatch();
|
|
- try {
|
|
- watch.getStartTime();
|
|
- fail("Calling getStartTime on an unstarted StopWatch should throw an exception");
|
|
- } catch (IllegalStateException expected) {
|
|
- // expected
|
|
- }
|
|
- watch.start();
|
|
- try {
|
|
- watch.getStartTime();
|
|
- Assert.assertTrue(watch.getStartTime() >= beforeStopWatch);
|
|
- } catch (IllegalStateException ex) {
|
|
- fail("Start time should be available: " + ex.getMessage());
|
|
- }
|
|
- watch.reset();
|
|
- try {
|
|
- watch.getStartTime();
|
|
- fail("Calling getStartTime on a reset, but unstarted StopWatch should throw an exception");
|
|
- } catch (IllegalStateException expected) {
|
|
- // expected
|
|
- }
|
|
- }
|
|
-
|
|
-}
|