diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java index 2245b693f38..e27dba3472a 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java @@ -256,19 +256,26 @@ public class AutomatonTestUtil { return ArrayUtil.toIntArray(soFar); } } + + private static Automaton randomSingleAutomaton(Random random) { + while (true) { + try { + Automaton a1 = new RegExp(AutomatonTestUtil.randomRegexp(random), RegExp.NONE).toAutomaton(); + if (random.nextBoolean()) { + a1 = Operations.complement(a1, DEFAULT_MAX_DETERMINIZED_STATES); + } + return a1; + } catch (TooComplexToDeterminizeException tctde) { + // This can (rarely) happen if the random regexp is too hard; just try again... + } + } + } /** return a random NFA/DFA for testing */ public static Automaton randomAutomaton(Random random) { // get two random Automata from regexps - Automaton a1 = new RegExp(AutomatonTestUtil.randomRegexp(random), RegExp.NONE).toAutomaton(); - if (random.nextBoolean()) { - a1 = Operations.complement(a1, DEFAULT_MAX_DETERMINIZED_STATES); - } - - Automaton a2 = new RegExp(AutomatonTestUtil.randomRegexp(random), RegExp.NONE).toAutomaton(); - if (random.nextBoolean()) { - a2 = Operations.complement(a2, DEFAULT_MAX_DETERMINIZED_STATES); - } + Automaton a1 = randomSingleAutomaton(random); + Automaton a2 = randomSingleAutomaton(random); // combine them in random ways switch (random.nextInt(4)) {