pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / util / TestCharArrayIterator.java
1 package org.apache.lucene.analysis.util;
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
20 import java.text.BreakIterator;
21 import java.text.CharacterIterator;
22 import java.util.Locale;
23
24 import org.apache.lucene.util.LuceneTestCase;
25 import org.apache.lucene.util.UnicodeUtil;
26 import org.apache.lucene.util._TestUtil;
27
28 public class TestCharArrayIterator extends LuceneTestCase {
29   
30   public void testWordInstance() {
31     doTests(CharArrayIterator.newWordInstance());
32   }
33   
34   public void testConsumeWordInstance() {
35     BreakIterator bi = BreakIterator.getWordInstance();
36     CharArrayIterator ci = CharArrayIterator.newWordInstance();
37     for (int i = 0; i < 10000; i++) {
38       char text[] = _TestUtil.randomUnicodeString(random).toCharArray();
39       ci.setText(text, 0, text.length);
40       consume(bi, ci);
41     }
42   }
43   
44   /* run this to test if your JRE is buggy
45   public void testWordInstanceJREBUG() {
46     BreakIterator bi = BreakIterator.getWordInstance();
47     Segment ci = new Segment();
48     for (int i = 0; i < 10000; i++) {
49       char text[] = _TestUtil.randomUnicodeString(random).toCharArray();
50       ci.array = text;
51       ci.offset = 0;
52       ci.count = text.length;
53       consume(bi, ci);
54     }
55   }
56   */
57   
58   public void testSentenceInstance() {
59     doTests(CharArrayIterator.newSentenceInstance());
60   }
61   
62   public void testConsumeSentenceInstance() {
63     BreakIterator bi = BreakIterator.getSentenceInstance();
64     CharArrayIterator ci = CharArrayIterator.newSentenceInstance();
65     for (int i = 0; i < 10000; i++) {
66       char text[] = _TestUtil.randomUnicodeString(random).toCharArray();
67       ci.setText(text, 0, text.length);
68       consume(bi, ci);
69     }
70   }
71   
72   /* run this to test if your JRE is buggy
73   public void testSentenceInstanceJREBUG() {
74     BreakIterator bi = BreakIterator.getSentenceInstance();
75     Segment ci = new Segment();
76     for (int i = 0; i < 10000; i++) {
77       char text[] = _TestUtil.randomUnicodeString(random).toCharArray();
78       ci.array = text;
79       ci.offset = 0;
80       ci.count = text.length;
81       consume(bi, ci);
82     }
83   }
84   */
85   
86   private void doTests(CharArrayIterator ci) {
87     // basics
88     ci.setText("testing".toCharArray(), 0, "testing".length());
89     assertEquals(0, ci.getBeginIndex());
90     assertEquals(7, ci.getEndIndex());
91     assertEquals(0, ci.getIndex());
92     assertEquals('t', ci.current());
93     assertEquals('e', ci.next());
94     assertEquals('g', ci.last());
95     assertEquals('n', ci.previous());
96     assertEquals('t', ci.first());
97     assertEquals(CharacterIterator.DONE, ci.previous());
98     
99     // first()
100     ci.setText("testing".toCharArray(), 0, "testing".length());
101     ci.next();
102     // Sets the position to getBeginIndex() and returns the character at that position. 
103     assertEquals('t', ci.first());
104     assertEquals(ci.getBeginIndex(), ci.getIndex());
105     // or DONE if the text is empty
106     ci.setText(new char[] {}, 0, 0);
107     assertEquals(CharacterIterator.DONE, ci.first());
108     
109     // last()
110     ci.setText("testing".toCharArray(), 0, "testing".length());
111     // Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) 
112     // and returns the character at that position. 
113     assertEquals('g', ci.last());
114     assertEquals(ci.getIndex(), ci.getEndIndex() - 1);
115     // or DONE if the text is empty
116     ci.setText(new char[] {}, 0, 0);
117     assertEquals(CharacterIterator.DONE, ci.last());
118     assertEquals(ci.getEndIndex(), ci.getIndex());
119     
120     // current()
121     // Gets the character at the current position (as returned by getIndex()). 
122     ci.setText("testing".toCharArray(), 0, "testing".length());
123     assertEquals('t', ci.current());
124     ci.last();
125     ci.next();
126     // or DONE if the current position is off the end of the text.
127     assertEquals(CharacterIterator.DONE, ci.current());
128     
129     // next()
130     ci.setText("te".toCharArray(), 0, 2);
131     // Increments the iterator's index by one and returns the character at the new index.
132     assertEquals('e', ci.next());
133     assertEquals(1, ci.getIndex());
134     // or DONE if the new position is off the end of the text range.
135     assertEquals(CharacterIterator.DONE, ci.next());
136     assertEquals(ci.getEndIndex(), ci.getIndex());
137     
138     // setIndex()
139     ci.setText("test".toCharArray(), 0, "test".length());
140     try {
141       ci.setIndex(5);
142       fail();
143     } catch (Exception e) {
144       assertTrue(e instanceof IllegalArgumentException);
145     }
146     
147     // clone()
148     char text[] = "testing".toCharArray();
149     ci.setText(text, 0, text.length);
150     ci.next();
151     CharArrayIterator ci2 = (CharArrayIterator) ci.clone();
152     assertEquals(ci.getIndex(), ci2.getIndex());
153     assertEquals(ci.next(), ci2.next());
154     assertEquals(ci.last(), ci2.last());
155   }
156   
157   private void consume(BreakIterator bi, CharacterIterator ci) {
158     bi.setText(ci);
159     while (bi.next() != BreakIterator.DONE)
160       ;
161   }
162 }