From 2de1e14b0c029629ffb7b01dbac95d5eb3bcbab8 Mon Sep 17 00:00:00 2001 From: mifmif Date: Thu, 16 Oct 2014 21:52:20 +0000 Subject: [PATCH] Bug with range values was fixed. Tests were added by mkolisnyk --- pom.xml | 5 +- .../java/com/mifmif/common/regex/Generex.java | 26 +++++- .../common/regex/GenerexIteratorTest.java | 69 ++++++++++++++++ .../common/regex/GenerexRandomTest.java | 82 +++++++++++++++++++ .../com/mifmif/common/regex/GenerexTest.java | 67 +++++++++++++++ 5 files changed, 244 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/mifmif/common/regex/GenerexIteratorTest.java create mode 100644 src/test/java/com/mifmif/common/regex/GenerexRandomTest.java create mode 100644 src/test/java/com/mifmif/common/regex/GenerexTest.java diff --git a/pom.xml b/pom.xml index d774921..24ba137 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.github.mifmif generex - 0.0.2-SNAPSHOT + 0.0.2 Generex https://github.com/mifmif/Generex/tree/master Generex A Java Library for regex to Strings generation @@ -383,14 +383,11 @@ automaton 1.11-8 - - junit junit 4.11 test - diff --git a/src/main/java/com/mifmif/common/regex/Generex.java b/src/main/java/com/mifmif/common/regex/Generex.java index af13b19..d071c56 100644 --- a/src/main/java/com/mifmif/common/regex/Generex.java +++ b/src/main/java/com/mifmif/common/regex/Generex.java @@ -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; @@ -41,7 +43,23 @@ */ public class Generex implements Iterable { + private Map predefinedCharacterClasses = new HashMap() { + 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(); } @@ -116,6 +134,7 @@ public String getFirstMatch() { result = result.concat("" + node.getMinChar()); node = node.getNextNodes().get(0); } + result = result.substring(1); return result; } @@ -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); } diff --git a/src/test/java/com/mifmif/common/regex/GenerexIteratorTest.java b/src/test/java/com/mifmif/common/regex/GenerexIteratorTest.java new file mode 100644 index 0000000..0a47da6 --- /dev/null +++ b/src/test/java/com/mifmif/common/regex/GenerexIteratorTest.java @@ -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 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++; } } + */ +} diff --git a/src/test/java/com/mifmif/common/regex/GenerexRandomTest.java b/src/test/java/com/mifmif/common/regex/GenerexRandomTest.java new file mode 100644 index 0000000..2158fa8 --- /dev/null +++ b/src/test/java/com/mifmif/common/regex/GenerexRandomTest.java @@ -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 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); + } +} diff --git a/src/test/java/com/mifmif/common/regex/GenerexTest.java b/src/test/java/com/mifmif/common/regex/GenerexTest.java new file mode 100644 index 0000000..76995d5 --- /dev/null +++ b/src/test/java/com/mifmif/common/regex/GenerexTest.java @@ -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 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)); + } + } +}