pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestStressAdvance.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.lucene.util.*;
25 import org.apache.lucene.store.*;
26 import org.apache.lucene.document.*;
27
28 public class TestStressAdvance extends LuceneTestCase {
29
30   public void testStressAdvance() throws Exception {
31     for(int iter=0;iter<3;iter++) {
32       if (VERBOSE) {
33         System.out.println("\nTEST: iter=" + iter);
34       }
35       Directory dir = newDirectory();
36       RandomIndexWriter w = new RandomIndexWriter(random, dir);
37       final Set<Integer> aDocs = new HashSet<Integer>();
38       final Document doc = new Document();
39       final Field f = newField("field", "", Field.Index.NOT_ANALYZED_NO_NORMS);
40       doc.add(f);
41       final Field idField = newField("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
42       doc.add(idField);
43       int num = atLeast(4097);
44       for(int id=0;id<num;id++) {
45         if (random.nextInt(4) == 3) {
46           f.setValue("a");
47           aDocs.add(id);
48         } else {
49           f.setValue("b");
50         }
51         idField.setValue(""+id);
52         w.addDocument(doc);
53       }
54
55       w.optimize();
56
57       final List<Integer> aDocIDs = new ArrayList<Integer>();
58       final List<Integer> bDocIDs = new ArrayList<Integer>();
59
60       final IndexReader r = w.getReader();
61       final int[] idToDocID = new int[r.maxDoc()];
62       for(int docID=0;docID<idToDocID.length;docID++) {
63         int id = Integer.parseInt(r.document(docID).get("id"));
64         if (aDocs.contains(id)) {
65           aDocIDs.add(docID);
66         } else {
67           bDocIDs.add(docID);
68         }
69       }
70       final TermDocs de = r.termDocs();
71       
72       for(int iter2=0;iter2<10;iter2++) {
73         if (VERBOSE) {
74           System.out.println("\nTEST: iter=" + iter + " iter2=" + iter2);
75         }
76         de.seek(new Term("field", "a"));
77         testOne(de, aDocIDs);
78
79         de.seek(new Term("field", "b"));
80         testOne(de, bDocIDs);
81       }
82
83       w.close();
84       r.close();
85       dir.close();
86     }
87   }
88
89   private void testOne(TermDocs docs, List<Integer> expected) throws Exception {
90     if (VERBOSE) {
91       System.out.println("test");
92     }
93     int upto = -1;
94     while(upto < expected.size()) {
95       if (VERBOSE) {
96         System.out.println("  cycle upto=" + upto + " of " + expected.size());
97       }
98       final int docID;
99       if (random.nextInt(4) == 1 || upto == expected.size()-1) {
100         // test nextDoc()
101         if (VERBOSE) {
102           System.out.println("    do nextDoc");
103         }
104         upto++;
105         if (docs.next()) {
106           docID = docs.doc();
107         } else {
108           docID = -1;
109         }
110       } else {
111         // test advance()
112         final int inc = _TestUtil.nextInt(random, 1, expected.size()-1-upto);
113         if (VERBOSE) {
114           System.out.println("    do advance inc=" + inc);
115         }
116         upto += inc;
117         if (docs.skipTo(expected.get(upto))) {
118           docID = docs.doc();
119         } else {
120           docID = -1;
121         }
122       }
123       if (upto == expected.size()) {
124         if (VERBOSE) {
125           System.out.println("  expect docID=" + -1 + " actual=" + docID);
126         }
127         assertEquals(-1, docID);
128       } else {
129         if (VERBOSE) {
130           System.out.println("  expect docID=" + expected.get(upto) + " actual=" + docID);
131         }
132         assertTrue(docID != -1);
133         assertEquals(expected.get(upto).intValue(), docID);
134       }
135     }
136   }
137 }