LUCENE-2565: don't enter infinite random loop

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@979452 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2010-07-26 21:30:47 +00:00
parent 2b988eafaa
commit 2ba78a2ba0
3 changed files with 51 additions and 6 deletions

View File

@ -248,6 +248,9 @@ public abstract class LuceneTestCase extends TestCase {
throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test"); throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test");
} }
this.seed = Long.valueOf(seedRnd.nextLong()); this.seed = Long.valueOf(seedRnd.nextLong());
if (VERBOSE) {
System.out.println("NOTE: random seed of testcase '" + getName() + "' is: " + this.seed);
}
return new Random(seed); return new Random(seed);
} }

View File

@ -324,6 +324,9 @@ public class LuceneTestCaseJ4 {
throw new IllegalStateException("please call LuceneTestCaseJ4.newRandom only once per test"); throw new IllegalStateException("please call LuceneTestCaseJ4.newRandom only once per test");
} }
this.seed = Long.valueOf(seedRnd.nextLong()); this.seed = Long.valueOf(seedRnd.nextLong());
if (VERBOSE) {
System.out.println("NOTE: random seed of testcase '" + getName() + "' is: " + this.seed);
}
return new Random(seed); return new Random(seed);
} }

View File

@ -46,15 +46,44 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
private void testOne(Random r, ByteRunAutomaton a, int startCode, int endCode, int iters) { private void testOne(Random r, ByteRunAutomaton a, int startCode, int endCode, int iters) {
// Verify correct ints are accepted // Verify correct ints are accepted
final int nonSurrogateCount;
final boolean ovSurStart;
if (endCode < UnicodeUtil.UNI_SUR_HIGH_START ||
startCode > UnicodeUtil.UNI_SUR_LOW_END) {
// no overlap w/ surrogates
nonSurrogateCount = endCode - startCode + 1;
ovSurStart = false;
} else if (isSurrogate(startCode)) {
// start of range overlaps surrogates
nonSurrogateCount = endCode - startCode + 1 - (UnicodeUtil.UNI_SUR_LOW_END - startCode + 1);
ovSurStart = false;
} else if (isSurrogate(endCode)) {
// end of range overlaps surrogates
ovSurStart = true;
nonSurrogateCount = endCode - startCode + 1 - (endCode - UnicodeUtil.UNI_SUR_HIGH_START + 1);
} else {
// range completely subsumes surrogates
ovSurStart = true;
nonSurrogateCount = endCode - startCode + 1 - (UnicodeUtil.UNI_SUR_LOW_END - UnicodeUtil.UNI_SUR_HIGH_START + 1);
}
assert nonSurrogateCount > 0;
for(int iter=0;iter<iters;iter++) { for(int iter=0;iter<iters;iter++) {
// pick random code point in-range // pick random code point in-range
final int code = _TestUtil.nextInt(r, startCode, endCode); int code = startCode + r.nextInt(nonSurrogateCount);
if ((code >= UnicodeUtil.UNI_SUR_HIGH_START && code <= UnicodeUtil.UNI_SUR_HIGH_END) | if (isSurrogate(code)) {
(code >= UnicodeUtil.UNI_SUR_LOW_START && code <= UnicodeUtil.UNI_SUR_LOW_END)) { if (ovSurStart) {
iter--; code = UnicodeUtil.UNI_SUR_LOW_END + 1 + (code - UnicodeUtil.UNI_SUR_HIGH_START);
continue; } else {
code = UnicodeUtil.UNI_SUR_LOW_END + 1 + (code - startCode);
} }
}
assert code >= startCode && code <= endCode: "code=" + code + " start=" + startCode + " end=" + endCode;
assert !isSurrogate(code);
assertTrue("DFA for range " + startCode + "-" + endCode + " failed to match code=" + code, assertTrue("DFA for range " + startCode + "-" + endCode + " failed to match code=" + code,
matches(a, code)); matches(a, code));
} }
@ -97,6 +126,10 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
} }
} }
private static boolean isSurrogate(int code) {
return code >= UnicodeUtil.UNI_SUR_HIGH_START && code <= UnicodeUtil.UNI_SUR_LOW_END;
}
public void testRandomRanges() throws Exception { public void testRandomRanges() throws Exception {
final Random r = random; final Random r = random;
int ITERS = 10*_TestUtil.getRandomMultiplier(); int ITERS = 10*_TestUtil.getRandomMultiplier();
@ -114,6 +147,11 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
endCode = x1; endCode = x1;
} }
if (isSurrogate(startCode) && isSurrogate(endCode)) {
iter--;
continue;
}
final Automaton a = new Automaton(); final Automaton a = new Automaton();
final State end = new State(); final State end = new State();
end.setAccept(true); end.setAccept(true);
@ -166,9 +204,10 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
} }
public void testRandomRegexes() throws Exception { public void testRandomRegexes() throws Exception {
for (int i = 0; i < 250*_TestUtil.getRandomMultiplier(); i++) for (int i = 0; i < 250*_TestUtil.getRandomMultiplier(); i++) {
assertAutomaton(AutomatonTestUtil.randomRegexp(random).toAutomaton()); assertAutomaton(AutomatonTestUtil.randomRegexp(random).toAutomaton());
} }
}
private void assertAutomaton(Automaton automaton) throws Exception { private void assertAutomaton(Automaton automaton) throws Exception {
CharacterRunAutomaton cra = new CharacterRunAutomaton(automaton); CharacterRunAutomaton cra = new CharacterRunAutomaton(automaton);