From 2ba78a2ba03310e05072b3603bc5b3211e1f7f1c Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Mon, 26 Jul 2010 21:30:47 +0000 Subject: [PATCH] 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 --- .../apache/lucene/util/LuceneTestCase.java | 3 ++ .../apache/lucene/util/LuceneTestCaseJ4.java | 3 ++ .../util/automaton/TestUTF32ToUTF8.java | 51 ++++++++++++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java index e214f388663..ae7b846541e 100644 --- a/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java @@ -248,6 +248,9 @@ public abstract class LuceneTestCase extends TestCase { throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test"); } this.seed = Long.valueOf(seedRnd.nextLong()); + if (VERBOSE) { + System.out.println("NOTE: random seed of testcase '" + getName() + "' is: " + this.seed); + } return new Random(seed); } diff --git a/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java b/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java index 3fa12efd2cb..6a5db61da2c 100644 --- a/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java +++ b/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java @@ -324,6 +324,9 @@ public class LuceneTestCaseJ4 { throw new IllegalStateException("please call LuceneTestCaseJ4.newRandom only once per test"); } this.seed = Long.valueOf(seedRnd.nextLong()); + if (VERBOSE) { + System.out.println("NOTE: random seed of testcase '" + getName() + "' is: " + this.seed); + } return new Random(seed); } diff --git a/lucene/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java b/lucene/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java index 5ddedf71731..429da0b19f7 100644 --- a/lucene/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java +++ b/lucene/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java @@ -46,15 +46,44 @@ public class TestUTF32ToUTF8 extends LuceneTestCase { private void testOne(Random r, ByteRunAutomaton a, int startCode, int endCode, int iters) { // 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= UnicodeUtil.UNI_SUR_HIGH_START && code <= UnicodeUtil.UNI_SUR_HIGH_END) | - (code >= UnicodeUtil.UNI_SUR_LOW_START && code <= UnicodeUtil.UNI_SUR_LOW_END)) { - iter--; - continue; + int code = startCode + r.nextInt(nonSurrogateCount); + if (isSurrogate(code)) { + if (ovSurStart) { + code = UnicodeUtil.UNI_SUR_LOW_END + 1 + (code - UnicodeUtil.UNI_SUR_HIGH_START); + } 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, 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 { final Random r = random; int ITERS = 10*_TestUtil.getRandomMultiplier(); @@ -113,6 +146,11 @@ public class TestUTF32ToUTF8 extends LuceneTestCase { startCode = x2; endCode = x1; } + + if (isSurrogate(startCode) && isSurrogate(endCode)) { + iter--; + continue; + } final Automaton a = new Automaton(); final State end = new State(); @@ -166,8 +204,9 @@ public class TestUTF32ToUTF8 extends LuceneTestCase { } 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()); + } } private void assertAutomaton(Automaton automaton) throws Exception {