Fix bug where NFARunAutomaton#getTransition does not set Transition correctly (#12909)

This commit is contained in:
Patrick Zhai 2023-12-27 22:49:35 -08:00 committed by GitHub
parent 02722eeb69
commit 948970be58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View File

@ -193,8 +193,12 @@ public class NFARunAutomaton implements ByteRunnable, TransitionAccessor {
// 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;
@ -222,12 +226,7 @@ public class NFARunAutomaton implements ByteRunnable, TransitionAccessor {
}
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 {

View File

@ -73,6 +73,40 @@ public class TestNFARunAutomaton extends LuceneTestCase {
}
}
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;