X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/index/TestTermsEnum.java diff --git a/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/index/TestTermsEnum.java b/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/index/TestTermsEnum.java new file mode 100644 index 0000000..6e2e569 --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/index/TestTermsEnum.java @@ -0,0 +1,470 @@ +package org.apache.lucene.index; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.lucene.analysis.MockAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.LineFileDocs; +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; + +public class TestTermsEnum extends LuceneTestCase { + + public void test() throws Exception { + final LineFileDocs docs = new LineFileDocs(random); + final Directory d = newDirectory(); + final RandomIndexWriter w = new RandomIndexWriter(random, d); + final int numDocs = atLeast(10); + for(int docCount=0;docCount terms = new ArrayList(); + TermEnum termEnum = r.terms(new Term("body")); + do { + Term term = termEnum.term(); + if (term == null || !"body".equals(term.field())) { + break; + } + terms.add(term); + } while (termEnum.next()); + + if (VERBOSE) { + System.out.println("TEST: " + terms.size() + " terms"); + } + + int upto = -1; + final int iters = atLeast(200); + for(int iter=0;iter seen = new HashSet(); + + final boolean allowEmptyString = random.nextBoolean(); + + if (random.nextInt(10) == 7 && terms.length > 2) { + // Sometimes add a bunch of terms sharing a longish common prefix: + final int numTermsSamePrefix = random.nextInt(terms.length/2); + if (numTermsSamePrefix > 0) { + String prefix; + while(true) { + prefix = getRandomString(); + if (prefix.length() < 5) { + continue; + } else { + break; + } + } + while(seen.size() < numTermsSamePrefix) { + final String t = prefix + getRandomString(); + if (!seen.contains(t)) { + terms[seen.size()] = t; + seen.add(t); + } + } + } + } + + while(seen.size() < terms.length) { + final String t = getRandomString(); + if (!seen.contains(t) && (allowEmptyString || t.length() != 0)) { + terms[seen.size()] = t; + seen.add(t); + } + } + r = makeIndex(terms); + testRandomSeeks(r, terms); + close(); + } + + private BytesRef getNonExistTerm(BytesRef[] terms) { + BytesRef t = null; + while(true) { + final String ts = getRandomString(); + t = new BytesRef(ts); + if (Arrays.binarySearch(terms, t) < 0) { + return t; + } + } + } + + private void testRandomSeeks(IndexReader r, String... validTermStrings) throws IOException { + final BytesRef[] validTerms = new BytesRef[validTermStrings.length]; + for(int termIDX=0;termIDX= 0) { + // assertEquals(TermsEnum.SeekStatus.FOUND, result); + } else if (loc == END_LOC) { + assertTrue(actualTerm == null || !FIELD.equals(actualTerm.field())); + } else { + assert loc >= -validTerms.length; + assertTrue(actualTerm != null && FIELD.equals(actualTerm.field())); + //assertEquals(TermsEnum.SeekStatus.NOT_FOUND, result); + } + + if (loc >= 0) { + assertEquals(targetTerm, actualTerm); + } else if (loc == END_LOC) { + continue; + } else { + loc = -loc-1; + assertEquals(new Term(FIELD, validTerms[loc].utf8ToString()), actualTerm); + } + + // Do a bunch of next's after the seek + final int numNext = random.nextInt(validTerms.length); + + if (VERBOSE) { + System.out.println("\nTEST: numNext=" + numNext); + } + + for(int nextCount=0;nextCount