This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 13706c851aada724a7763ab93d7f7e6407be5c50 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Sep 22 09:24:13 2024 -0400 Sort members --- .../commons/lang3/RandomStringUtilsTest.java | 140 ++++++++++----------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java index 2fb68e299..3c0e0f5cc 100644 --- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java @@ -289,6 +289,76 @@ public class RandomStringUtilsTest extends AbstractLangTest { assertTrue(msg.contains("end"), "Message (" + msg + ") must contain 'end'"); } + /** + * Test {@code RandomStringUtils.random} works appropriately when letters=true + * and the range does not only include ASCII letters. + * Fails with probability less than 2^-40 (in practice this never happens). + */ + @ParameterizedTest + @MethodSource("randomProvider") + void testNonASCIILetters(final RandomStringUtils rsu) { + // Check that the following create a string with 10 characters 0x4e00 (a non-ASCII letter) + String r1 = rsu.next(10, 0x4e00, 0x4e01, true, false); + assertEquals(10, r1.length(), "wrong length"); + for (int i = 0; i < r1.length(); i++) { + assertEquals(0x4e00, r1.charAt(i), "characters not all equal to 0x4e00"); + } + + // Same with both letters=true and numbers=true + r1 = rsu.next(10, 0x4e00, 0x4e01, true, true); + assertEquals(10, r1.length(), "wrong length"); + for (int i = 0; i < r1.length(); i++) { + assertEquals(0x4e00, r1.charAt(i), "characters not all equal to 0x4e00"); + } + + // Check that at least one letter is not ASCII + boolean found = false; + r1 = rsu.next(40, 'F', 0x3000, true, false); + assertEquals(40, r1.length(), "wrong length"); + for (int i = 0; i < r1.length(); i++) { + assertTrue(Character.isLetter(r1.charAt(i)), "characters not all letters"); + if (r1.charAt(i) > 0x7f) { + found = true; + } + } + assertTrue(found, "no non-ASCII letter generated"); + } + + /** + * Test {@code RandomStringUtils.random} works appropriately when numbers=true + * and the range does not only include ASCII numbers/digits. + * Fails with probability less than 2^-40 (in practice this never happens). + */ + @ParameterizedTest + @MethodSource("randomProvider") + void testNonASCIINumbers(final RandomStringUtils rsu) { + // Check that the following create a string with 10 characters 0x0660 (a non-ASCII digit) + String r1 = rsu.next(10, 0x0660, 0x0661, false, true); + assertEquals(10, r1.length(), "wrong length"); + for (int i = 0; i < r1.length(); i++) { + assertEquals(0x0660, r1.charAt(i), "characters not all equal to 0x0660"); + } + + // Same with both letters=true and numbers=true + r1 = rsu.next(10, 0x0660, 0x0661, true, true); + assertEquals(10, r1.length(), "wrong length"); + for (int i = 0; i < r1.length(); i++) { + assertEquals(0x0660, r1.charAt(i), "characters not all equal to 0x0660"); + } + + // Check that at least one letter is not ASCII + boolean found = false; + r1 = rsu.next(40, 'F', 0x3000, false, true); + assertEquals(40, r1.length(), "wrong length"); + for (int i = 0; i < r1.length(); i++) { + assertTrue(Character.isDigit(r1.charAt(i)), "characters not all numbers"); + if (r1.charAt(i) > 0x7f) { + found = true; + } + } + assertTrue(found, "no non-ASCII number generated"); + } + /** * Make sure boundary alpha characters are generated by randomAlphabetic This test will fail randomly with probability = 4 * (51/52)**1000 ~ 1.58E-8 */ @@ -732,74 +802,4 @@ public class RandomStringUtilsTest extends AbstractLangTest { assertNotEquals(r1, r3); assertNotEquals(r2, r3); } - - /** - * Test {@code RandomStringUtils.random} works appropriately when letters=true - * and the range does not only include ASCII letters. - * Fails with probability less than 2^-40 (in practice this never happens). - */ - @ParameterizedTest - @MethodSource("randomProvider") - void testNonASCIILetters(final RandomStringUtils rsu) { - // Check that the following create a string with 10 characters 0x4e00 (a non-ASCII letter) - String r1 = rsu.next(10, 0x4e00, 0x4e01, true, false); - assertEquals(10, r1.length(), "wrong length"); - for (int i = 0; i < r1.length(); i++) { - assertEquals(0x4e00, r1.charAt(i), "characters not all equal to 0x4e00"); - } - - // Same with both letters=true and numbers=true - r1 = rsu.next(10, 0x4e00, 0x4e01, true, true); - assertEquals(10, r1.length(), "wrong length"); - for (int i = 0; i < r1.length(); i++) { - assertEquals(0x4e00, r1.charAt(i), "characters not all equal to 0x4e00"); - } - - // Check that at least one letter is not ASCII - boolean found = false; - r1 = rsu.next(40, 'F', 0x3000, true, false); - assertEquals(40, r1.length(), "wrong length"); - for (int i = 0; i < r1.length(); i++) { - assertTrue(Character.isLetter(r1.charAt(i)), "characters not all letters"); - if (r1.charAt(i) > 0x7f) { - found = true; - } - } - assertTrue(found, "no non-ASCII letter generated"); - } - - /** - * Test {@code RandomStringUtils.random} works appropriately when numbers=true - * and the range does not only include ASCII numbers/digits. - * Fails with probability less than 2^-40 (in practice this never happens). - */ - @ParameterizedTest - @MethodSource("randomProvider") - void testNonASCIINumbers(final RandomStringUtils rsu) { - // Check that the following create a string with 10 characters 0x0660 (a non-ASCII digit) - String r1 = rsu.next(10, 0x0660, 0x0661, false, true); - assertEquals(10, r1.length(), "wrong length"); - for (int i = 0; i < r1.length(); i++) { - assertEquals(0x0660, r1.charAt(i), "characters not all equal to 0x0660"); - } - - // Same with both letters=true and numbers=true - r1 = rsu.next(10, 0x0660, 0x0661, true, true); - assertEquals(10, r1.length(), "wrong length"); - for (int i = 0; i < r1.length(); i++) { - assertEquals(0x0660, r1.charAt(i), "characters not all equal to 0x0660"); - } - - // Check that at least one letter is not ASCII - boolean found = false; - r1 = rsu.next(40, 'F', 0x3000, false, true); - assertEquals(40, r1.length(), "wrong length"); - for (int i = 0; i < r1.length(); i++) { - assertTrue(Character.isDigit(r1.charAt(i)), "characters not all numbers"); - if (r1.charAt(i) > 0x7f) { - found = true; - } - } - assertTrue(found, "no non-ASCII number generated"); - } }