Skip to content

Commit

Permalink
GH-248: Cleaning up some confusion on RandomizedTest.randomAscii* and
Browse files Browse the repository at this point in the history
RandomStrings.randomAscii* methods. These methods generated
random strings based on letters from the Unicode basic latin block (C0).
All these methods are now deprecated and will be removed in favor of
more explicit methods that tell exactly (?) what kind of letters are
involved (Ryan Ernst, Dawid Weiss). For details, see:

RandomStrings.randomAsciiLettersOfLength(..)
RandomStrings.randomAsciiLettersOfLengthBetween(..)
RandomStrings.randomAsciiAlphanumOfLength(..)
RandomStrings.randomAsciiAlphanumOfLengthBetween(..)
  • Loading branch information
dweiss committed Apr 6, 2017
1 parent 565d363 commit d72ed11
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 32 deletions.
14 changes: 14 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ https://github.com/randomizedtesting/randomizedtesting/blob/master/CHANGES

======================= randomizedtesting 2.5.1-SNAPSHOT ====================

Changes in Backwards Compatibility

* GH-248: Cleaning up some confusion on RandomizedTest.randomAscii* and
RandomStrings.randomAscii* methods. These methods generated
random strings based on letters from the Unicode basic latin block (C0).
All these methods are now deprecated and will be removed in favor of
more explicit methods that tell exactly (?) what kind of letters are
involved (Ryan Ernst, Dawid Weiss). For details, see:

RandomStrings.randomAsciiLettersOfLength(..)
RandomStrings.randomAsciiLettersOfLengthBetween(..)
RandomStrings.randomAsciiAlphanumOfLength(..)
RandomStrings.randomAsciiAlphanumOfLengthBetween(..)

======================= randomizedtesting 2.5.0 ====================

Changes in Backwards Compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testArrayQueueReentrance() throws Exception {
// Mockups.
final List<String> foo = new ArrayList<>();
for (int i = randomIntBetween(2, 1000); --i > 0;) {
foo.add(randomAsciiOfLength(20));
foo.add(randomAsciiLettersOfLength(20));
}
final EventBus aggregatedBus = new EventBus("aggregated");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import com.carrotsearch.randomizedtesting.generators.StringGenerator;

/**
* Common scaffolding for subclassing randomized tests.
Expand Down Expand Up @@ -543,59 +542,107 @@ public static TimeZone randomTimeZone() {
// Characters and strings. Delegates to RandomStrings and that in turn to StringGenerators.
//

/** @see StringGenerator#ofCodeUnitsLength(Random, int, int) */
/**
* @deprecated Use {@link #randomAsciiLettersOfLengthBetween} instead.
*/
@Deprecated
public static String randomAsciiOfLengthBetween(int minCodeUnits, int maxCodeUnits) {
return RandomStrings.randomAsciiOfLengthBetween(getRandom(), minCodeUnits,
maxCodeUnits);
return randomAsciiLettersOfLengthBetween(minCodeUnits, maxCodeUnits);
}

/** @see StringGenerator#ofCodeUnitsLength(Random, int, int) */

/**
* @deprecated Use {@link #randomAsciiLettersOfLength} instead.
*/
@Deprecated
public static String randomAsciiOfLength(int codeUnits) {
return RandomStrings.randomAsciiOfLength(getRandom(), codeUnits);
return randomAsciiLettersOfLength(codeUnits);
}

/** @see StringGenerator#ofCodeUnitsLength(Random, int, int) */

/**
* @see RandomStrings#randomAsciiLettersOfLengthBetween
*/
public static String randomAsciiLettersOfLengthBetween(int minLetters, int maxLetters) {
return RandomStrings.randomAsciiLettersOfLengthBetween(getRandom(), minLetters, maxLetters);
}

/**
* @see RandomStrings#randomAsciiLettersOfLength
*/
public static String randomAsciiLettersOfLength(int codeUnits) {
return RandomStrings.randomAsciiLettersOfLength(getRandom(), codeUnits);
}

/**
* @see RandomStrings#randomAsciiAlphanumOfLengthBetween
*/
public static String randomAsciiAlphanumOfLengthBetween(int minCodeUnits, int maxCodeUnits) {
return RandomStrings.randomAsciiAlphanumOfLengthBetween(getRandom(), minCodeUnits, maxCodeUnits);
}

/**
* @see RandomStrings#randomAsciiAlphanumOfLength
*/
public static String randomAsciiAlphanumOfLength(int codeUnits) {
return RandomStrings.randomAsciiAlphanumOfLength(getRandom(), codeUnits);
}

/**
* @see RandomStrings#randomUnicodeOfLengthBetween
*/
public static String randomUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits) {
return RandomStrings.randomUnicodeOfLengthBetween(getRandom(),
minCodeUnits, maxCodeUnits);
}

/** @see StringGenerator#ofCodeUnitsLength(Random, int, int) */

/**
* @see RandomStrings#randomUnicodeOfLength
*/
public static String randomUnicodeOfLength(int codeUnits) {
return RandomStrings.randomUnicodeOfLength(getRandom(), codeUnits);
}

/** @see StringGenerator#ofCodePointsLength(Random, int, int) */
/**
* @see RandomStrings#randomUnicodeOfCodepointLengthBetween
*/
public static String randomUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints) {
return RandomStrings.randomUnicodeOfCodepointLengthBetween(getRandom(),
minCodePoints, maxCodePoints);
}

/** @see StringGenerator#ofCodePointsLength(Random, int, int) */
/**
* @see RandomStrings#randomUnicodeOfCodepointLength
*/
public static String randomUnicodeOfCodepointLength(int codePoints) {
return RandomStrings
.randomUnicodeOfCodepointLength(getRandom(), codePoints);
return RandomStrings.randomUnicodeOfCodepointLength(getRandom(), codePoints);
}

/** @see StringGenerator#ofCodeUnitsLength(Random, int, int) */
/**
* @see RandomStrings#randomRealisticUnicodeOfLengthBetween
*/
public static String randomRealisticUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits) {
return RandomStrings.randomRealisticUnicodeOfLengthBetween(getRandom(),
minCodeUnits, maxCodeUnits);
}

/** @see StringGenerator#ofCodeUnitsLength(Random, int, int) */
/**
* @see RandomStrings#randomRealisticUnicodeOfLength
*/
public static String randomRealisticUnicodeOfLength(int codeUnits) {
return RandomStrings.randomRealisticUnicodeOfLength(getRandom(), codeUnits);
}

/** @see StringGenerator#ofCodePointsLength(Random, int, int) */
/**
* @see RandomStrings#randomRealisticUnicodeOfCodepointLengthBetween
*/
public static String randomRealisticUnicodeOfCodepointLengthBetween(
int minCodePoints, int maxCodePoints) {
return RandomStrings.randomRealisticUnicodeOfCodepointLengthBetween(
getRandom(), minCodePoints, maxCodePoints);
}

/** @see StringGenerator#ofCodePointsLength(Random, int, int) */
/**
* @see RandomStrings#randomRealisticUnicodeOfCodepointLength
*/
public static String randomRealisticUnicodeOfCodepointLength(int codePoints) {
return RandomStrings.randomRealisticUnicodeOfCodepointLength(getRandom(),
codePoints);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
* abcdefghijklmnopqrstuvwxyz
* ABCDEFGHIJKLMNOPQRSTUVWXYZ
* </pre>
*
* @deprecated Use {@link AsciiLettersGenerator} instead.
*/
public class ASCIIGenerator extends CodepointSetGenerator {
private final static char [] ASCII_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

public ASCIIGenerator() {
super(ASCII_SET);
}
}
@Deprecated
public class ASCIIGenerator extends AsciiLettersGenerator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.carrotsearch.randomizedtesting.generators;

/**
* A generator emitting simple ASCII alphanumeric letters and numbers
* from the set (newlines not counted):
* <pre>
* abcdefghijklmnopqrstuvwxyz
* ABCDEFGHIJKLMNOPQRSTUVWXYZ
* 0123456789
* </pre>
*/
public class AsciiAlphanumGenerator extends CodepointSetGenerator {
private final static char [] CHARS =
("abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789").toCharArray();

public AsciiAlphanumGenerator() {
super(CHARS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.carrotsearch.randomizedtesting.generators;

/**
* A generator emitting simple ASCII characters from the set
* (newlines not counted):
* <pre>
* abcdefghijklmnopqrstuvwxyz
* ABCDEFGHIJKLMNOPQRSTUVWXYZ
* </pre>
*/
public class AsciiLettersGenerator extends CodepointSetGenerator {
private final static char [] CHARS =
("abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();

public AsciiLettersGenerator() {
super(CHARS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@
public final class RandomStrings {
public final static RealisticUnicodeGenerator realisticUnicodeGenerator = new RealisticUnicodeGenerator();
public final static UnicodeGenerator unicodeGenerator = new UnicodeGenerator();
public final static AsciiLettersGenerator asciiLettersGenerator = new AsciiLettersGenerator();
public final static AsciiAlphanumGenerator asciiAlphanumGenerator = new AsciiAlphanumGenerator();

/**
* @deprecated Use {@link RandomStrings#asciiLettersGenerator} instead.
*/
@Deprecated
public final static ASCIIGenerator asciiGenerator = new ASCIIGenerator();

// Ultra wide monitor required to read the source code :)

/** @deprecated Use {@link #randomAsciiLettersOfLengthBetween} instead. */
@Deprecated
public static String randomAsciiOfLengthBetween (Random r, int minCodeUnits, int maxCodeUnits) {return asciiGenerator.ofCodeUnitsLength(r, minCodeUnits, maxCodeUnits); }

/** @deprecated Use {@link #randomAsciiLettersOfLength} instead. */
@Deprecated
public static String randomAsciiOfLength (Random r, int codeUnits) {return asciiGenerator.ofCodeUnitsLength(r, codeUnits, codeUnits); }

public static String randomAsciiLettersOfLengthBetween (Random r, int minCodeUnits, int maxCodeUnits) {return asciiLettersGenerator.ofCodeUnitsLength(r, minCodeUnits, maxCodeUnits); }
public static String randomAsciiLettersOfLength (Random r, int codeUnits) {return asciiLettersGenerator.ofCodeUnitsLength(r, codeUnits, codeUnits); }

public static String randomAsciiAlphanumOfLengthBetween (Random r, int minCodeUnits, int maxCodeUnits) {return asciiAlphanumGenerator.ofCodeUnitsLength(r, minCodeUnits, maxCodeUnits); }
public static String randomAsciiAlphanumOfLength (Random r, int codeUnits) {return asciiAlphanumGenerator.ofCodeUnitsLength(r, codeUnits, codeUnits); }

public static String randomUnicodeOfLengthBetween (Random r, int minCodeUnits, int maxCodeUnits) {return unicodeGenerator.ofCodeUnitsLength(r, minCodeUnits, maxCodeUnits); }
public static String randomUnicodeOfLength (Random r, int codeUnits) {return unicodeGenerator.ofCodeUnitsLength(r, codeUnits, codeUnits); }
public static String randomUnicodeOfCodepointLengthBetween (Random r, int minCodePoints, int maxCodePoints) {return unicodeGenerator.ofCodePointsLength(r, minCodePoints, maxCodePoints); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,23 @@ public void testRandomTimeZone() {

@Test
public void testRandomAsciiOfLength() {
assertTrue(randomAsciiOfLength(0).isEmpty());
assertTrue(randomAsciiLettersOfLength(0).isEmpty());

for (int i = 0; i < 1000; i++) {
int maxLength = randomInt(20);
String str = randomAsciiOfLength(maxLength);
String str = randomAsciiLettersOfLength(maxLength);
assertTrue(str.matches("[a-zA-Z]*"));
assertTrue(str.length() <= maxLength);
}
}

@Test
public void testRandomAlphanumOfLength() {
assertTrue(randomAsciiAlphanumOfLength(0).isEmpty());

for (int i = 0; i < 1000; i++) {
int maxLength = randomInt(20);
String str = randomAsciiAlphanumOfLength(maxLength);
assertTrue(str.matches("[a-zA-Z0-9]*"));
assertTrue(str.length() <= maxLength);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class TestSeedDecorator extends WithNestedTestClass {
public static class Nested1 {
@BeforeClass
public static void generateSequence() {
strings.add(
RandomStrings.randomAsciiOfLength(RandomizedContext.current().getRandom(), 200));
strings.add(RandomStrings.randomAsciiLettersOfLength(RandomizedContext.current().getRandom(), 200));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class Nested1 extends RandomizedTest {
@Repeat(iterations = 2, useConstantSeed = false)
public void testNoTimeout() {
assumeRunningNested();
seeds.add(randomAsciiOfLength(20));
seeds.add(randomAsciiLettersOfLength(20));
}
}

Expand Down

0 comments on commit d72ed11

Please sign in to comment.