Skip to content

Commit

Permalink
Fix bug where NFARunAutomaton#getTransition does not set Transition c…
Browse files Browse the repository at this point in the history
…orrectly (#12909)
  • Loading branch information
zhaih authored Dec 28, 2023
1 parent 02722ee commit 948970b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ public void getNextTransition(Transition t) {
// numTransitions times
}
assert dStates[t.source].transitions[t.transitionUpto] != NOT_COMPUTED;
t.dest = dStates[t.source].transitions[t.transitionUpto];

setTransitionAccordingly(t);
}

private void setTransitionAccordingly(Transition t) {
t.dest = dStates[t.source].transitions[t.transitionUpto];
t.min = points[t.transitionUpto];
if (t.transitionUpto == points.length - 1) {
t.max = alphabetSize - 1;
Expand Down Expand Up @@ -222,12 +226,7 @@ public void getTransition(int state, int index, Transition t) {
}
assert outgoingTransitions == index;

t.min = points[t.transitionUpto];
if (t.transitionUpto == points.length - 1) {
t.max = alphabetSize - 1;
} else {
t.max = points[t.transitionUpto + 1] - 1;
}
setTransitionAccordingly(t);
}

private class DState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@ public void testWithRandomRegex() {
}
}

public void testRandomAccessTransition() {
Automaton nfa = new RegExp(AutomatonTestUtil.randomRegexp(random()), RegExp.NONE).toAutomaton();
while (nfa.isDeterministic()) {
nfa = new RegExp(AutomatonTestUtil.randomRegexp(random()), RegExp.NONE).toAutomaton();
}
NFARunAutomaton runAutomaton1, runAutomaton2;
runAutomaton1 = new NFARunAutomaton(nfa);
runAutomaton2 = new NFARunAutomaton(nfa);
assertRandomAccessTransition(runAutomaton1, runAutomaton2, 0, new HashSet<>());
}

private void assertRandomAccessTransition(
NFARunAutomaton automaton1, NFARunAutomaton automaton2, int state, Set<Integer> visited) {
if (visited.contains(state)) {
return;
}
visited.add(state);

Transition t1 = new Transition();
Transition t2 = new Transition();
automaton1.initTransition(state, t1);
if (random().nextBoolean()) {
// init is not really necessary for t2
automaton2.initTransition(state, t2);
}
int numStates = automaton2.getNumTransitions(state);
for (int i = 0; i < numStates; i++) {
automaton1.getNextTransition(t1);
automaton2.getTransition(state, i, t2);
assertEquals(t1.toString(), t2.toString());
assertRandomAccessTransition(automaton1, automaton2, t1.dest, visited);
}
}

public void testRandomAutomatonQuery() throws IOException {
final int docNum = 50;
final int automatonNum = 50;
Expand Down

0 comments on commit 948970b

Please sign in to comment.