This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 7fce4c843 Improve RandomStringUtils boundary test coverage
7fce4c843 is described below
commit 7fce4c843fcd758da9b3408aaf9435c2471ab218
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Oct 2 08:50:02 2026 -0400
Improve RandomStringUtils boundary test coverage
Assert exact generated lengths and check character boundaries for
length-range
overloads and deprecated static graph and print methods. Reuse the
boundary
helper for ASCII tests, fixing the missed check at index zero and
removing
inaccurate Javadoc.
---
.../commons/lang3/RandomStringUtilsTest.java | 111 ++++++++++++---------
1 file changed, 66 insertions(+), 45 deletions(-)
diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
index 52507302f..190faffc8 100644
--- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
@@ -72,6 +72,7 @@ private static void assertInclusiveRange(final
IntFunction<String> generator, fi
boolean maxFound = false;
for (int i = 0; i < LOOP_COUNT; i++) {
final String randString = generator.apply(BOUNDARY_SAMPLE_LENGTH);
+ assertEquals(BOUNDARY_SAMPLE_LENGTH, randString.length(),
"generated length");
for (int j = 0; j < randString.length(); j++) {
final char ch = randString.charAt(j);
assertTrue(ch >= minInclusive && ch <= maxInclusive, () ->
"character out of range: " + (int) ch);
@@ -755,54 +756,10 @@ void testRandomApis(final RandomStringUtils rsu) {
assertEquals("", r1, "random(0).equals(\"\")");
}
- /**
- * Make sure 32 and 127 are generated by randomNumeric This test will fail
randomly with probability = 2*(95/96)**1000 ~ 5.7E-5
- *
- * @param rsu The instance to test
- */
@ParameterizedTest
@MethodSource("randomProvider")
void testRandomAscii(final RandomStringUtils rsu) {
- final char[] testChars = { (char) 32, (char) 126 };
- final boolean[] found = { false, false };
- // Test failures have been observed on GitHub builds with a 100 limit.
- for (int i = 0; i < LOOP_COUNT; i++) {
- final String randString = rsu.nextAscii(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++) {
- assertTrue(found[i], "ascii character not generated in 1000
attempts: " + (int) testChars[i] + " -- repeated failures indicate a problem");
- }
- }
-
- /**
- * Verifies that {@link RandomStringUtils#nextGraph(int)} generates both
ends of the POSIX {@code [:graph:]} class,
- * {@code '!'} (0x21) and {@code '~'} (0x7E), and nothing outside it.
- *
- * @param rsu The instance to test
- * @see #assertInclusiveRange(IntFunction, char, char)
- */
- @ParameterizedTest
- @MethodSource("randomProvider")
- void testRandomGraphIncludesTilde(final RandomStringUtils rsu) {
- assertInclusiveRange(rsu::nextGraph, '!', '~');
- }
-
- /**
- * Verifies that {@link RandomStringUtils#nextPrint(int)} generates both
ends of the POSIX {@code [:print:]} class,
- * space (0x20) and {@code '~'} (0x7E), and nothing outside it.
- *
- * @param rsu The instance to test
- * @see #assertInclusiveRange(IntFunction, char, char)
- */
- @ParameterizedTest
- @MethodSource("randomProvider")
- void testRandomPrintIncludesTilde(final RandomStringUtils rsu) {
- assertInclusiveRange(rsu::nextPrint, ' ', '~');
+ assertInclusiveRange(rsu::nextAscii, ' ', '~');
}
@ParameterizedTest
@@ -832,6 +789,25 @@ void testRandomAsciiRange(final RandomStringUtils rsu) {
assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max
generated, may fail randomly rarely");
}
+ @ParameterizedTest
+ @MethodSource("randomProvider")
+ void testRandomAsciiRangeBoundaries(final RandomStringUtils rsu) {
+ assertInclusiveRange(count -> rsu.nextAscii(count, count + 1), ' ',
'~');
+ }
+
+ /**
+ * Verifies that {@link RandomStringUtils#nextGraph(int)} generates both
ends of the POSIX {@code [:graph:]} class,
+ * {@code '!'} (0x21) and {@code '~'} (0x7E), and nothing outside it.
+ *
+ * @param rsu The instance to test
+ * @see #assertInclusiveRange(IntFunction, char, char)
+ */
+ @ParameterizedTest
+ @MethodSource("randomProvider")
+ void testRandomGraphIncludesTilde(final RandomStringUtils rsu) {
+ assertInclusiveRange(rsu::nextGraph, '!', '~');
+ }
+
@ParameterizedTest
@MethodSource("randomProvider")
void testRandomGraphRange(final RandomStringUtils rsu) {
@@ -859,6 +835,22 @@ void testRandomGraphRange(final RandomStringUtils rsu) {
assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max
generated, may fail randomly rarely");
}
+ @ParameterizedTest
+ @MethodSource("randomProvider")
+ void testRandomGraphRangeBoundaries(final RandomStringUtils rsu) {
+ assertInclusiveRange(count -> rsu.nextGraph(count, count + 1), '!',
'~');
+ }
+
+ @Test
+ void testRandomGraphStaticBoundaries() {
+ assertInclusiveRange(RandomStringUtils::randomGraph, '!', '~');
+ }
+
+ @Test
+ void testRandomGraphStaticRangeBoundaries() {
+ assertInclusiveRange(count -> RandomStringUtils.randomGraph(count,
count + 1), '!', '~');
+ }
+
/**
* Make sure '0' and '9' are generated by randomNumeric This test will
fail randomly with probability = 2 * (9/10)**1000 ~ 3.5E-46
*
@@ -917,6 +909,19 @@ void testRandomParameter() {
assertEquals(r1, r2, "r1.equals(r2)");
}
+ /**
+ * Verifies that {@link RandomStringUtils#nextPrint(int)} generates both
ends of the POSIX {@code [:print:]} class,
+ * space (0x20) and {@code '~'} (0x7E), and nothing outside it.
+ *
+ * @param rsu The instance to test
+ * @see #assertInclusiveRange(IntFunction, char, char)
+ */
+ @ParameterizedTest
+ @MethodSource("randomProvider")
+ void testRandomPrintIncludesTilde(final RandomStringUtils rsu) {
+ assertInclusiveRange(rsu::nextPrint, ' ', '~');
+ }
+
@ParameterizedTest
@MethodSource("randomProvider")
void testRandomPrintRange(final RandomStringUtils rsu) {
@@ -944,6 +949,22 @@ void testRandomPrintRange(final RandomStringUtils rsu) {
assertEquals(expectedMaxLengthExclusive - 1, maxCreatedLength, "max
generated, may fail randomly rarely");
}
+ @ParameterizedTest
+ @MethodSource("randomProvider")
+ void testRandomPrintRangeBoundaries(final RandomStringUtils rsu) {
+ assertInclusiveRange(count -> rsu.nextPrint(count, count + 1), ' ',
'~');
+ }
+
+ @Test
+ void testRandomPrintStaticBoundaries() {
+ assertInclusiveRange(RandomStringUtils::randomPrint, ' ', '~');
+ }
+
+ @Test
+ void testRandomPrintStaticRangeBoundaries() {
+ assertInclusiveRange(count -> RandomStringUtils.randomPrint(count,
count + 1), ' ', '~');
+ }
+
/**
* Test {@code RandomStringUtils.random} works appropriately when chars
specified.
*