You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/06/09 23:36:03 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang RandomStringUtilsTest.java
scolebourne 2003/06/09 14:36:03
Modified: lang/src/java/org/apache/commons/lang RandomStringUtils.java
lang/src/test/org/apache/commons/lang/math
RandomUtilsTest.java
lang/src/test/org/apache/commons/lang
RandomStringUtilsTest.java
Log:
Ensure that RandomStringUtils returns all expected characters
bug 20592, reported/patched by Phil Steitz
Revision Changes Path
1.13 +6 -6 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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- RandomStringUtils.java 20 May 2003 21:15:19 -0000 1.12
+++ RandomStringUtils.java 9 Jun 2003 21:36:02 -0000 1.13
@@ -63,6 +63,7 @@
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author Stephen Colebourne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @author Phil Steitz
* @since 1.0
* @version $Id$
*/
@@ -104,7 +105,7 @@
* specified.</p>
*
* <p>Characters will be chosen from the set of characters whose
- * ASCII value is between <code>32</code> and <code>127</code>.</p>
+ * ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p>
*
* @param count length of random string to create
* @return the random string
@@ -199,9 +200,8 @@
*
* This method has exactly the same semantics as {@link
* #random(int,int,int,boolean,boolean,char[],Random)}, but
- * instead of depending on internal source of randomness ({@link
- * #RANDOM}) it uses externally supplied instance of {@link
- * Random} class.
+ * instead of using an externally supplied source of randomness, it uses
+ * the internal static {@link Random} instance ({@link #RANDOM}).
*
* @param count length of random string to create
* @param start position in set of chars to start at
@@ -256,7 +256,7 @@
throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
}
if( (start == 0) && (end == 0) ) {
- end = (int)'z';
+ end = (int)'z' + 1;
start = (int)' ';
if(!letters && !numbers) {
start = 0;
1.3 +21 -21 jakarta-commons/lang/src/test/org/apache/commons/lang/math/RandomUtilsTest.java
Index: RandomUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/RandomUtilsTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RandomUtilsTest.java 8 Jun 2003 14:19:43 -0000 1.2
+++ RandomUtilsTest.java 9 Jun 2003 21:36:03 -0000 1.3
@@ -126,12 +126,12 @@
assertTrue(result >= 0);
observed[result]++;
}
- /* Use ChiSquare dist with df = 4-1 = 3, alpha = .01
- * Change to 16.27 for alpha = .001
+ /* 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 100 times",
- chiSquare(expected,observed) < 11.34);
+ "chi-square test -- will fail about 1 in 1000 times",
+ chiSquare(expected,observed) < 16.27);
}
/** test distribution of nextLong() */
@@ -171,12 +171,12 @@
observed[1]++;
}
}
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .01
- * Change to 10.83 for alpha = .001
+ /* 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 100 times",
- chiSquare(expected,observed) < 6.64);
+ "chi-square test -- will fail about 1 in 1000 times",
+ chiSquare(expected,observed) < 10.83);
}
@@ -214,12 +214,12 @@
observed[1]++;
}
}
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .01
- * Change to 10.83 for alpha = .001
+ /* 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 100 times",
- chiSquare(expected,observed) < 6.64);
+ "chi-square test -- will fail about 1 in 1000 times",
+ chiSquare(expected,observed) < 10.83 );
}
/** test distribution of nextFloat() */
@@ -256,12 +256,12 @@
observed[1]++;
}
}
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .01
- * Change to 10.83 for alpha = .001
+ /* 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 100 times",
- chiSquare(expected,observed) < 6.64);
+ "chi-square test -- will fail about 1 in 1000 times",
+ chiSquare(expected,observed) < 10.83);
}
/** test distribution of nextDouble() */
@@ -298,12 +298,12 @@
observed[1]++;
}
}
- /* Use ChiSquare dist with df = 2-1 = 1, alpha = .01
- * Change to 10.83 for alpha = .001
+ /* 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 100 times",
- chiSquare(expected,observed) < 6.64);
+ "chi-square test -- will fail about 1 in 1000 times",
+ chiSquare(expected,observed) < 10.83);
}
/** make sure that setSeed fails */
1.7 +96 -3 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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- RandomStringUtilsTest.java 14 May 2003 02:50:43 -0000 1.6
+++ RandomStringUtilsTest.java 9 Jun 2003 21:36:03 -0000 1.7
@@ -57,11 +57,13 @@
import junit.framework.*;
import junit.textui.TestRunner;
+
/**
* Unit tests {@link org.apache.commons.lang.RandomStringUtils}.
*
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
+ * @author Phil Steitz
* @version $Id$
*/
public class RandomStringUtilsTest extends junit.framework.TestCase {
@@ -167,6 +169,99 @@
assertNotNull("random(<0) throws exception", e);
}
+ /**
+ * Make sure boundary alphanumeric characters are generated by randomAlphaNumeric
+ * This test will fail randomly with probability = 6 * (61/62)**1000 ~ 5.2E-7
+ */
+ public void testRandomAlphaNumeric() {
+ char[] testChars = {'a', 'z', 'A', 'Z', '0', '9'};
+ boolean[] found = {false, false, false, false, false, false};
+ for (int i = 0; i < 100; i++) {
+ String randString = RandomStringUtils.randomAlphanumeric(10);
+ for (int j = 0; j < testChars.length; j++) {
+ if (randString.indexOf(testChars[j]) > 0) {
+ found[j] = true;
+ }
+ }
+ }
+ for (int i = 0; i < testChars.length; i++) {
+ if (!found[i]) {
+ fail("alphanumeric character not generated in 1000 attempts: "
+ + testChars[i] +" -- repeated failures indicate a problem ");
+ }
+ }
+ }
+
+ /**
+ * Make sure '0' and '9' are generated by randomNumeric
+ * This test will fail randomly with probability = 2 * (9/10)**1000 ~ 3.5E-46
+ */
+ public void testRandomNumeric() {
+ char[] testChars = {'0','9'};
+ boolean[] found = {false, false};
+ for (int i = 0; i < 100; i++) {
+ String randString = RandomStringUtils.randomNumeric(10);
+ for (int j = 0; j < testChars.length; j++) {
+ if (randString.indexOf(testChars[j]) > 0) {
+ found[j] = true;
+ }
+ }
+ }
+ for (int i = 0; i < testChars.length; i++) {
+ if (!found[i]) {
+ fail("digit not generated in 1000 attempts: "
+ + testChars[i] +" -- repeated failures indicate a problem ");
+ }
+ }
+ }
+
+ /**
+ * Make sure boundary alpha characters are generated by randomAlphabetic
+ * This test will fail randomly with probability = 4 * (51/52)**1000 ~ 1.58E-8
+ */
+ public void testRandomAlphabetic() {
+ char[] testChars = {'a', 'z', 'A', 'Z'};
+ boolean[] found = {false, false, false, false};
+ for (int i = 0; i < 100; i++) {
+ String randString = RandomStringUtils.randomAlphabetic(10);
+ for (int j = 0; j < testChars.length; j++) {
+ if (randString.indexOf(testChars[j]) > 0) {
+ found[j] = true;
+ }
+ }
+ }
+ for (int i = 0; i < testChars.length; i++) {
+ if (!found[i]) {
+ fail("alphanumeric character not generated in 1000 attempts: "
+ + testChars[i] +" -- repeated failures indicate a problem ");
+ }
+ }
+ }
+
+ /**
+ * Make sure 32 and 127 are generated by randomNumeric
+ * This test will fail randomly with probability = 2*(95/96)**1000 ~ 5.7E-5
+ */
+ public void testRandomAscii() {
+ char[] testChars = {(char) 32, (char) 126};
+ boolean[] found = {false, false};
+ for (int i = 0; i < 100; i++) {
+ String randString = RandomStringUtils.randomAscii(10);
+ for (int j = 0; j < testChars.length; j++) {
+ if (randString.indexOf(testChars[j]) > 0) {
+ found[j] = true;
+ }
+ }
+ }
+ for (int i = 0; i < testChars.length; i++) {
+ if (!found[i]) {
+ fail("ascii character not generated in 1000 attempts: "
+ + (int) testChars[i] +
+ " -- repeated failures indicate a problem");
+ }
+ }
+ }
+
/**
* Test homogeneity of random strings generated --
* i.e., test that characters show up with expected frequencies
@@ -194,8 +289,6 @@
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
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org