commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang RandomStringUtils.java
Date Wed, 14 May 2003 02:50:43 GMT
bayard      2003/05/13 19:50:43

  Modified:    lang/src/test/org/apache/commons/lang
                        RandomStringUtilsTest.java
               lang/src/java/org/apache/commons/lang RandomStringUtils.java
  Log:
  Bug #19880 fixed. RandomStringUtils.random(int count, char[] set)
  doesn't include the last character in set.
  
  Submitted by:	Phil Steitz
  
  Revision  Changes    Path
  1.6       +47 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/RandomStringUtilsTest.java
  
  Index: RandomStringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/RandomStringUtilsTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RandomStringUtilsTest.java	9 Apr 2003 14:13:03 -0000	1.5
  +++ RandomStringUtilsTest.java	14 May 2003 02:50:43 -0000	1.6
  @@ -166,6 +166,52 @@
           }
           assertNotNull("random(<0) throws exception", e);
       }
  +    
  +    /** 
  +     * Test homogeneity of random strings generated --
  +     * i.e., test that characters show up with expected frequencies
  +     * in generated strings.  Will fail randomly about 1 in 1000 times.
  +     * Repeated failures indicate a problem.
  +     */
  +    public void testRandomStringUtilsHomog() {
  +        String set = "abc";
  +        char[] chars = set.toCharArray();
  +        String gen = "";
  +        int[] counts = {0,0,0};
  +        int[] expected = {200,200,200};
  +        for (int i = 0; i< 100; i++) {
  +           gen = RandomStringUtils.random(6,chars);
  +           for (int j = 0; j < 6; j++) {
  +               switch (gen.charAt(j)) {
  +                   case 'a': {counts[0]++; break;}
  +                   case 'b': {counts[1]++; break;}
  +                   case 'c': {counts[2]++; break;}
  +                   default: {fail("generated character not in set");}
  +               }
  +           }
  +        } 
  +        // Perform chi-square test with df = 3-1 = 2, testing at .001 level
  +        assertTrue("test homogeneity -- will fail about 1 in 1000 times",
  +            chiSquare(expected,counts) < 13.82);  
  +    }
  +    
  +    //FIXME: add similar tests for other functions
  +    
  +    /**
  +     * Computes Chi-Square statistic given observed and expected counts
  +     * @param observed array of observed frequency counts
  +     * @param expected array of exptected 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;
  +    }           
  +        
   
       public static void main(String args[]) {
           TestRunner.run(suite());
  
  
  
  1.11      +2 -2      jakarta-commons/lang/src/java/org/apache/commons/lang/RandomStringUtils.java
  
  Index: RandomStringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/RandomStringUtils.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- RandomStringUtils.java	9 Apr 2003 14:13:03 -0000	1.10
  +++ RandomStringUtils.java	14 May 2003 02:50:43 -0000	1.11
  @@ -313,6 +313,6 @@
        * @return the random string
        */
       public static String random(int count, char[] set) {
  -        return random(count, 0, set.length - 1, false, false, set);
  +        return random(count, 0, set.length, false, false, set);
       }
   }
  
  
  

---------------------------------------------------------------------
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