Skip to content

Commit

Permalink
Bug with range values was fixed. Tests were added by mkolisnyk
Browse files Browse the repository at this point in the history
  • Loading branch information
mifmif committed Oct 16, 2014
1 parent 9ec1e38 commit 2de1e14
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 5 deletions.
5 changes: 1 addition & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.mifmif</groupId>
<artifactId>generex</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.2</version>
<name>Generex</name>
<url>https://github.com/mifmif/Generex/tree/master</url>
<description>Generex A Java Library for regex to Strings generation</description>
Expand Down Expand Up @@ -383,14 +383,11 @@
<artifactId>automaton</artifactId>
<version>1.11-8</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
26 changes: 25 additions & 1 deletion src/main/java/com/mifmif/common/regex/Generex.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package com.mifmif.common.regex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.mifmif.common.regex.util.Iterable;
Expand All @@ -41,7 +43,23 @@
*/
public class Generex implements Iterable {

private Map<String, String> predefinedCharacterClasses = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;

{
put("\\\\d","[0-9]");
put("\\\\D","[^0-9]");
put("\\\\s","[ \t\n\f\r]");
put("\\\\S","[^ \t\n\f\r]");
put("\\\\w","[a-zA-Z_0-9]");
put("\\\\W","[^a-zA-Z_0-9]");
}
};

public Generex(String regex) {
for (String key : predefinedCharacterClasses.keySet()) {
regex = regex.replaceAll(key, predefinedCharacterClasses.get(key));
}
regExp = new RegExp(regex);
automaton = regExp.toAutomaton();
}
Expand Down Expand Up @@ -116,6 +134,7 @@ public String getFirstMatch() {
result = result.concat("" + node.getMinChar());
node = node.getNextNodes().get(0);
}
result = result.substring(1);
return result;
}

Expand Down Expand Up @@ -275,7 +294,12 @@ private String prepareRandom(String strMatch, State state, int minLength, int ma
}
Random random = new Random();
Transition randomTransition = transitions.get(random.nextInt(transitions.size()));
char randomChar = (char) (random.nextInt(randomTransition.getMax() - randomTransition.getMin()) + randomTransition.getMin());
int diff = randomTransition.getMax() - randomTransition.getMin();
int randomOffset = diff;
if( diff > 0 ) {
randomOffset = (int) (random.nextInt(diff));
}
char randomChar = (char) (randomOffset + randomTransition.getMin());
return prepareRandom(strMatch + randomChar, randomTransition.getDest(), minLength, maxLength);

}
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/com/mifmif/common/regex/GenerexIteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.mifmif.common.regex;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.mifmif.common.regex.util.Iterator;

/**
* @author Myk Kolisnyk
*
*/
@RunWith(Parameterized.class)
public class GenerexIteratorTest {

private String pattern;
private Generex generex;

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

public GenerexIteratorTest(String description, String patternValue) {
this.pattern = patternValue;
}

@Before
public void setUp() throws Exception {
generex = new Generex(pattern);
}

@After
public void tearDown() throws Exception {
}

@Test
public void testIterateThroughAllGeneratedStrings() {
Iterator iterator = generex.iterator();
while (iterator.hasNext()) {
String result = iterator.next();
Assert.assertTrue(String.format("The string '%s' doesn't match the '%s' pattern", result, pattern), result.matches(pattern));

}
}

/*
* @Test public void testIterateShouldReturnTheSameAsGetMatchedStrings() {
* int count = 1; Iterator iterator = generex.iterator(); while
* (iterator.hasNext()) { String matchedResult =
* generex.getMatchedString(count); String result = iterator.next();
* Assert.assertEquals(String.format("Iteration %d mismatch", count),
* result, matchedResult); count++; } }
*/
}
82 changes: 82 additions & 0 deletions src/test/java/com/mifmif/common/regex/GenerexRandomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
*
*/
package com.mifmif.common.regex;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
* @author Myk Kolisnyk
*
*/
@RunWith(Parameterized.class)
public class GenerexRandomTest {

private String pattern;
private int minLength;
private int maxLength;

public GenerexRandomTest(String description,
String patternValue,
int minLengthValue,
int maxLengthValue) {
this.pattern = patternValue;
this.minLength = minLengthValue;
this.maxLength = maxLengthValue;
}

@Parameters(name = "Test random: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{"Sample multicharacter expression","[A-Z]{5,9}", 4 , 8},
{"Sample expression","[0-3]([a-c]|[e-g]{1,2})", 1 , 3},
{"E-mail format","([a-z0-9]+)[@]([a-z0-9]+)[.]([a-z0-9]+)", 8 , 24},
{"Any number","(\\d+)", 4 , 8},
{"Any non-number","(\\D+)", 4 , 8},
{"Any word","(\\w+)", 4 , 8},
{"Any non-word","(\\W+)", 4 , 8},
{"Any text","(.*)", 4 , 8}
});
}

@Test
public void testSimpleRandom() {
Generex generex = new Generex(pattern);
String result = generex.random();
Assert.assertTrue(
String.format("The string '%s' doesn't match the '%s' pattern", result, pattern),
result.matches(pattern));
}
@Test
public void testRandomWithMinLength() {
Generex generex = new Generex(pattern);
String result = generex.random(minLength);
Assert.assertTrue(
String.format("The string '%s' doesn't match the '%s' pattern", result, pattern),
result.matches(pattern));
Assert.assertTrue(
String.format("The string '%s' size doesn't fit the minimal size of %d", result, minLength),
result.length() >= minLength);
}
@Test
public void testRandomWithMaxLength() {
Generex generex = new Generex(pattern);
String result = generex.random(minLength, maxLength);
Assert.assertTrue(
String.format("The string '%s' doesn't match the '%s' pattern", result, pattern),
result.matches(pattern));
Assert.assertTrue(
String.format("The string '%s' size doesn't fit the minimal size of %d", result, minLength),
result.length() >= minLength);
Assert.assertTrue(
String.format("The string '%s' size doesn't fit the maximal size of %d", result, maxLength),
result.length() <= maxLength);
}
}
67 changes: 67 additions & 0 deletions src/test/java/com/mifmif/common/regex/GenerexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.mifmif.common.regex;

import java.util.Arrays;
import java.util.Collection;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
* @author Myk Kolisnyk
*
*/
@RunWith(Parameterized.class)
public class GenerexTest {

private String pattern;
private Generex generex;

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

public GenerexTest(String description, String patternValue) {
this.pattern = patternValue;
}

@Before
public void setUp() throws Exception {
generex = new Generex(pattern);
}

@After
public void tearDown() throws Exception {
}

@Test
public void testGetMatchedFirstMatchShouldBeTheSameAsMatchWithZeroIndex() {
String firstMatch = generex.getFirstMatch();
String matchStringZeroIndex = generex.getMatchedString(0);
Assert.assertTrue(String.format("The generated string '%s' doesn't match the pattern '%s'", firstMatch, pattern), firstMatch.matches(pattern));
Assert.assertTrue(String.format("The generated string '%s' doesn't match the pattern '%s'", matchStringZeroIndex, pattern),
matchStringZeroIndex.matches(pattern));
Assert.assertEquals(firstMatch, matchStringZeroIndex);
}

@Test
public void testIterateThroughAllMatchesShouldReturnConsistentResults() {
generex.getFirstMatch();
long total = generex.matchedStringsSize();
for (int count = 1; count < total; count++) {
String matchStringZeroIndex = generex.getMatchedString(count);
Assert.assertTrue(String.format("The generated string '%s' doesn't match the pattern '%s' at iteration #%d", matchStringZeroIndex, pattern, count),
matchStringZeroIndex.matches(pattern));
}
}
}

0 comments on commit 2de1e14

Please sign in to comment.