lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/search TestPositionIncrement.java
Date Sun, 21 Sep 2003 13:45:33 GMT
ehatcher    2003/09/21 06:45:33

  Modified:    src/test/org/apache/lucene/search TestPositionIncrement.java
  Log:
  add basic assertion of position increments test
  
  Revision  Changes    Path
  1.3       +55 -41    jakarta-lucene/src/test/org/apache/lucene/search/TestPositionIncrement.java
  
  Index: TestPositionIncrement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/search/TestPositionIncrement.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestPositionIncrement.java	19 Oct 2002 17:02:43 -0000	1.2
  +++ TestPositionIncrement.java	21 Sep 2003 13:45:33 -0000	1.3
  @@ -63,44 +63,43 @@
   import org.apache.lucene.analysis.Analyzer;
   import org.apache.lucene.analysis.Token;
   import org.apache.lucene.analysis.TokenStream;
  +import org.apache.lucene.analysis.WhitespaceAnalyzer;
   import org.apache.lucene.document.Document;
   import org.apache.lucene.document.Field;
   
   import java.io.Reader;
   import java.io.IOException;
  +import java.io.StringReader;
   
   import junit.framework.TestCase;
   
  - /**
  -  * Term position unit test.
  -  *
  -  * @author Doug Cutting
  -  * @version $Revision$
  -  */
  +/**
  + * Term position unit test.
  + *
  + * @author Doug Cutting
  + * @version $Revision$
  + */
   public class TestPositionIncrement extends TestCase {
  -  public TestPositionIncrement(String name) {
  -    super(name);
  -  }
  -  
   
  -  public static void test() throws Exception {
  +  public void testSetPosition() throws Exception {
       Analyzer analyzer = new Analyzer() {
  -        public TokenStream tokenStream(String fieldName, Reader reader) {
  -          return new TokenStream() {
  -              private final String[] TOKENS = {"1", "2", "3", "4", "5"};
  -              private final int[] INCREMENTS = {1, 2,  1,    0,   1};
  -              private int i = 0;
  -              public Token next() throws IOException {
  -                if (i == TOKENS.length)
  -                  return null;
  -                Token t = new Token(TOKENS[i], i, i);
  -                t.setPositionIncrement(INCREMENTS[i]);
  -                i++;
  -                return t;
  -              }
  -            };
  -        }
  -      };
  +      public TokenStream tokenStream(String fieldName, Reader reader) {
  +        return new TokenStream() {
  +          private final String[] TOKENS = {"1", "2", "3", "4", "5"};
  +          private final int[] INCREMENTS = {1, 2, 1, 0, 1};
  +          private int i = 0;
  +
  +          public Token next() throws IOException {
  +            if (i == TOKENS.length)
  +              return null;
  +            Token t = new Token(TOKENS[i], i, i);
  +            t.setPositionIncrement(INCREMENTS[i]);
  +            i++;
  +            return t;
  +          }
  +        };
  +      }
  +    };
       RAMDirectory store = new RAMDirectory();
       IndexWriter writer = new IndexWriter(store, analyzer, true);
       Document d = new Document();
  @@ -114,46 +113,61 @@
       Hits hits;
   
       q = new PhraseQuery();
  -    q.add(new Term("field","1"));
  -    q.add(new Term("field","2"));
  +    q.add(new Term("field", "1"));
  +    q.add(new Term("field", "2"));
       hits = searcher.search(q);
       assertEquals(0, hits.length());
   
       q = new PhraseQuery();
  -    q.add(new Term("field","2"));
  -    q.add(new Term("field","3"));
  +    q.add(new Term("field", "2"));
  +    q.add(new Term("field", "3"));
       hits = searcher.search(q);
       assertEquals(1, hits.length());
   
       q = new PhraseQuery();
  -    q.add(new Term("field","3"));
  -    q.add(new Term("field","4"));
  +    q.add(new Term("field", "3"));
  +    q.add(new Term("field", "4"));
       hits = searcher.search(q);
       assertEquals(0, hits.length());
   
       q = new PhraseQuery();
  -    q.add(new Term("field","2"));
  -    q.add(new Term("field","4"));
  +    q.add(new Term("field", "2"));
  +    q.add(new Term("field", "4"));
       hits = searcher.search(q);
       assertEquals(1, hits.length());
   
       q = new PhraseQuery();
  -    q.add(new Term("field","3"));
  -    q.add(new Term("field","5"));
  +    q.add(new Term("field", "3"));
  +    q.add(new Term("field", "5"));
       hits = searcher.search(q);
       assertEquals(1, hits.length());
   
       q = new PhraseQuery();
  -    q.add(new Term("field","4"));
  -    q.add(new Term("field","5"));
  +    q.add(new Term("field", "4"));
  +    q.add(new Term("field", "5"));
       hits = searcher.search(q);
       assertEquals(1, hits.length());
   
       q = new PhraseQuery();
  -    q.add(new Term("field","2"));
  -    q.add(new Term("field","5"));
  +    q.add(new Term("field", "2"));
  +    q.add(new Term("field", "5"));
       hits = searcher.search(q);
       assertEquals(0, hits.length());
  +  }
   
  +  /**
  +   * Basic analyzer behavior should be to keep sequential terms in one
  +   * increment from one another.
  +   */
  +  public void testIncrementingPositions() throws Exception {
  +    Analyzer analyzer = new WhitespaceAnalyzer();
  +    TokenStream ts = analyzer.tokenStream("field",
  +                                new StringReader("one two three four five"));
  +
  +    while (true) {
  +      Token token = ts.next();
  +      if (token == null) break;
  +      assertEquals(token.termText(), 1, token.getPositionIncrement());
  +    }
     }
   }
  
  
  

Mime
View raw message