Skip to content

Commit

Permalink
Merge pull request #37 from curious-odd-man/#35.EmptyAlternativesOnly…
Browse files Browse the repository at this point in the history
…FirstWorks

#35. Empty Alternatives Only First Works
  • Loading branch information
curious-odd-man authored Aug 13, 2020
2 parents 4068fb3 + 33ff8e0 commit 9253ba7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
6 changes: 3 additions & 3 deletions performance.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Benchmark Mode Cnt Score Error Units
PerformanceTests.generateTest avgt 5 88.374 ± 4.447 us/op
PerformanceTests.generateUniqueTest avgt 5 223167.433 ± 31382.610 us/op
Benchmark Mode Cnt Score Error Units
PerformanceTests.generateTest avgt 5 80,506 ± 11,109 us/op
PerformanceTests.generateUniqueTest avgt 5 169,864 ± 1,168 us/op
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ private GroupType processGroupType() {
}
}

private Node handleGroupEnd(StringBuilder sb, List<Node> nodes, boolean isChoice, List<Node> choices, Integer captureGroupIndex) {
sbToFinal(sb, nodes);
private static Node handleGroupEnd(StringBuilder sb, List<Node> nodes, boolean isChoice, List<Node> choices, Integer captureGroupIndex) {
if (sb.length() == 0 && nodes.isEmpty()) {
// Special case when '(a|)' is used - like empty
nodes.add(new FinalSymbol(""));
} else {
sbToFinal(sb, nodes);
}

if (isChoice) {
choices.add(sequenceOrNot(nodes, choices, false, null));
nodes.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void launchBenchmark() throws Exception {
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.MICROSECONDS)
// .warmupTime(TimeValue.seconds(1))
.warmupIterations(5)
.measurementIterations(5)
.warmupIterations(3)
.measurementIterations(3)
.threads(1)
.forks(1)
.shouldFailOnError(true)
Expand Down Expand Up @@ -66,7 +66,7 @@ public void generateTest() {
@Benchmark
public void generateUniqueTest() {
final StringIterator stringIterator = RGXGEN.iterateUnique();
for (int i = 0; i < 100000 && stringIterator.hasNext(); i++) {
for (int i = 0; i < 100 && stringIterator.hasNext(); i++) {
stringIterator.next();
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/github/curiousoddman/rgxgen/TestPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,19 @@ public enum TestPattern {
TOP_LEVEL_CHOICE_WITHOUT_PARENTHESIS("a|b",
new Choice(new FinalSymbol("a"), new FinalSymbol("b")),
BigInteger.valueOf(2),
Arrays.asList("a", "b"));
Arrays.asList("a", "b")),
EMPTY_CHOICE_AT_THE_START_OF_CHOICES("(|A)",
new Group(1, new Choice(new FinalSymbol(""), new FinalSymbol("A"))),
BigInteger.valueOf(2),
Arrays.asList("", "A")),
EMPTY_CHOICE_IN_THE_MIDDLE_OF_CHOICES("(B||A)",
new Group(1, new Choice(new FinalSymbol("B"), new FinalSymbol(""), new FinalSymbol("A"))),
BigInteger.valueOf(3),
Arrays.asList("B", "", "A")),
EMPTY_CHOICE_AT_THE_END_OF_CHOICES("(A|)",
new Group(1, new Choice(new FinalSymbol("A"), new FinalSymbol(""))),
BigInteger.valueOf(2),
Arrays.asList("A", ""));

final String aPattern;
final Node aResultNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void aLotOfValuesAvailableTest() {
StringIterator stringIterator = rgxGen.iterateUnique();
Set<String> set = new HashSet<>();

for (int i = 0; i < ITERATIONS * ITERATIONS; i++) {
for (int i = 0; i < ITERATIONS; i++) {
String next = stringIterator.next();
assertTrue(stringIterator.hasNext());
if (aUseFind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void verifyContextTest() {
String[] split = context.split("\n");
assertEquals("", split[0]);
int i = split[2].indexOf('^');
System.out.println(context);
// -1 because actual context is taken from previous symbol, not from actual at index, but previous.
assertEquals(String.format("%X", aOffset - 1), split[1].substring(i, i + 1));
}
Expand All @@ -61,7 +60,6 @@ public void verifyContextWithPosTest() {
String[] split = context.split("\n");
assertEquals("", split[0]);
int i = split[2].indexOf('^'); // -1 for the ' at the start of previous line
System.out.println(context);
assertEquals(String.format("%X", aOffset), split[1].substring(i, i + 1));
}

Expand Down

0 comments on commit 9253ba7

Please sign in to comment.