commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r264672 - in /jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random: AbstractRandomGeneratorTest.java RandomDataTest.java
Date Tue, 30 Aug 2005 02:21:46 GMT
Author: psteitz
Date: Mon Aug 29 19:21:44 2005
New Revision: 264672

URL: http://svn.apache.org/viewcvs?rev=264672&view=rev
Log:
Added tests for default method implementations not covered in RandomData.

Modified:
    jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
    jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/RandomDataTest.java

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/AbstractRandomGeneratorTest.java?rev=264672&r1=264671&r2=264672&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
(original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/AbstractRandomGeneratorTest.java
Mon Aug 29 19:21:44 2005
@@ -17,6 +17,9 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import org.apache.commons.math.stat.Frequency;
+ 
+
 /**
  * Test cases for the AbstractRandomGenerator class
  *
@@ -24,14 +27,123 @@
  */
 
 public class AbstractRandomGeneratorTest extends RandomDataTest {
+    
+    protected TestRandomGenerator testGenerator = new TestRandomGenerator();
+    
     public AbstractRandomGeneratorTest(String name) {
         super(name);
-        randomData = new RandomDataImpl(new TestRandomGenerator());
+        randomData = new RandomDataImpl(testGenerator);
     } 
     
     public static Test suite() {
         TestSuite suite = new TestSuite(AbstractRandomGeneratorTest.class);
         suite.setName("AbstractRandomGenerator Tests");
         return suite;
+    }
+    
+    public void testNextInt() {
+        try {
+            int x = testGenerator.nextInt(-1);
+            fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException ex) {
+            ;
+        }
+        Frequency freq = new Frequency();
+        int value = 0;
+        for (int i=0; i<smallSampleSize; i++) {
+            value = testGenerator.nextInt(4);
+            assertTrue("nextInt range",(value >= 0) && (value <= 3));
+            freq.addValue(value);  
+        }
+        long[] observed = new long[4];
+        for (int i=0; i<4; i++) {
+            observed[i] = freq.getCount(i);
+        } 
+        
+        /* 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",
+                testStatistic.chiSquare(expected,observed) < 16.27);    
+    }
+    
+    public void testNextLong() {
+        long q1 = Long.MAX_VALUE/4;
+        long q2 = 2 *  q1;
+        long q3 = 3 * q1;
+        
+        Frequency freq = new Frequency();
+        long val = 0;
+        int value = 0;
+        for (int i=0; i<smallSampleSize; i++) {
+            val = testGenerator.nextLong();
+            if (val < q1) {
+                value = 0;
+            } else if (val < q2) {
+                value = 1;
+            } else if (val < q3) {
+                value = 2;
+            } else {
+                value = 3;
+            }
+            freq.addValue(value);  
+        }
+        long[] observed = new long[4];
+        for (int i=0; i<4; i++) {
+            observed[i] = freq.getCount(i);
+        } 
+        
+        /* 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",
+                testStatistic.chiSquare(expected,observed) < 16.27);    
+    }
+    
+    public void testNextBoolean() {
+        long halfSampleSize = smallSampleSize / 2; 
+        double[] expected = {halfSampleSize, halfSampleSize};
+        long[] observed = new long[2];
+        for (int i=0; i<smallSampleSize; i++) {
+            if (testGenerator.nextBoolean()) {
+                observed[0]++;
+            } else {
+                observed[1]++;
+            }
+        }
+        /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
+         * Change to 6.635 for alpha = .01
+         */
+        assertTrue("chi-square test -- will fail about 1 in 1000 times",
+                testStatistic.chiSquare(expected,observed) < 10.828);    
+    }
+    
+    public void testNextFloat() {
+        Frequency freq = new Frequency();
+        float val = 0;
+        int value = 0;
+        for (int i=0; i<smallSampleSize; i++) {
+            val = testGenerator.nextFloat();
+            if (val < 0.25) {
+                value = 0;
+            } else if (val < 0.5) {
+                value = 1;
+            } else if (val < 0.75) {
+                value = 2;
+            } else {
+                value = 3;
+            }
+            freq.addValue(value);  
+        }
+        long[] observed = new long[4];
+        for (int i=0; i<4; i++) {
+            observed[i] = freq.getCount(i);
+        } 
+        
+        /* 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",
+                testStatistic.chiSquare(expected,observed) < 16.27);    
     }
 }

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/RandomDataTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/RandomDataTest.java?rev=264672&r1=264671&r2=264672&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/RandomDataTest.java
(original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/RandomDataTest.java
Mon Aug 29 19:21:44 2005
@@ -39,14 +39,14 @@
         randomData = new RandomDataImpl();
     }
 
-    private long smallSampleSize = 1000;
-    private double[] expected = {250,250,250,250};
-    private int largeSampleSize = 10000;
+    protected long smallSampleSize = 1000;
+    protected double[] expected = {250,250,250,250};
+    protected int largeSampleSize = 10000;
     private int tolerance = 50;
     private String[] hex = 
         {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; 
     protected RandomDataImpl randomData = null; 
-    private ChiSquareTestImpl testStatistic = new ChiSquareTestImpl();
+    protected ChiSquareTestImpl testStatistic = new ChiSquareTestImpl();
     
     public void setUp() { 
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message