Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 2 methods to Generex: generateMaxLength + generateMinLength #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
######################
# Maven
######################
/target

######################
# Intellij
######################
.idea/
*.iml

######################
# Eclipse
######################
.project
.settings
.classpath
16 changes: 16 additions & 0 deletions src/main/java/com/mifmif/common/regex/Generex.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,20 @@ private static String requote(String regex) {
return sb.toString();
}

/**
* Get the length of the longest string that can be generated.
* @return
*/
public int generateMaxLength() {
return automaton.getNumberOfStates() - 1;
}

/**
* Get the length of the smallest string that can be generated.
* @return
*/
public int generateMinLength() {
return automaton.getShortestExample(true).length();
}

}
41 changes: 32 additions & 9 deletions src/test/java/com/mifmif/common/regex/GenerexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@ public class GenerexTest {

private String pattern;
private Generex generex;
private int expectedMatchedStringsSize;
private int expectedMatchedStringsSize;
private int expectedStringsMinLength;
private int expectedStringsMaxLength;

@Parameters(name = "Test get match: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { "Sample multicharacter expression", "[A-B]{5,9}", 992 }, { "Sample expression", "[0-3]([a-c]|[e-g]{1,2})", 60 },
{ "Number format", "\\d{3,4}", 11000 },
// {"Any non-number","\\D{3,4}"},
{ "Any word", "\\w{1,2}", 4032 }, { "Empty string", "", 1 },
// {"Any non-word","\\W{1,2}"}
});
return Arrays.asList(new Object[][] { //
{"Sample multicharacter expression", "[A-B]{5,9}", 992, 5, 9}, //
{"Sample expression", "[0-3]([a-c]|[e-g]{1,2})", 60, 2, 3}, //
{"Number format", "\\d{3,4}", 11000, 3, 4}, //
// {"Any non-number","\\D{3,4}", ???, 3, 4}, //
{"Any word", "\\w{1,2}", 4032, 1, 2}, //
{"Empty string", "", 1, 0, 0}, //
// {"Any non-word","\\W{1,2}", ???, 1, 2} //
});
}

public GenerexTest(String description, String patternValue, int numberOfStrings) {
public GenerexTest(String description, String patternValue, int numberOfStrings, int minLength, int maxLength) {
this.pattern = patternValue;
this.expectedMatchedStringsSize = numberOfStrings;
this.expectedMatchedStringsSize = numberOfStrings;
this.expectedStringsMinLength = minLength;
this.expectedStringsMaxLength = maxLength;
}

@Before
Expand All @@ -54,6 +61,22 @@ public void testMatchedStringsSizeShouldReturnExpectedValues() {
expectedMatchedStringsSize == size);
}

@Test
public void testMinLengthMatchedStringsShouldReturnExpectedValues() {
long size = generex.generateMinLength();
Assert.assertTrue(
String.format("The minimum lengh matched strings '%s' doesn't match the value '%s'", size, expectedStringsMinLength),
expectedStringsMinLength == size);
}

@Test
public void testMaxLengthMatchedStringsShouldReturnExpectedValues() {
long size = generex.generateMaxLength();
Assert.assertTrue(
String.format("The maximum lengh matched strings '%s' doesn't match the value '%s'", size, expectedStringsMaxLength),
expectedStringsMaxLength == size);
}

@Test
public void testGetMatchedFirstMatchShouldBeTheSameAsMatchWithZeroIndex() {
String firstMatch = generex.getFirstMatch();
Expand Down