1 package org.apache.lucene.analysis.util;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.text.BreakIterator;
21 import java.text.CharacterIterator;
22 import java.util.Locale;
24 import org.apache.lucene.util.LuceneTestCase;
25 import org.apache.lucene.util.UnicodeUtil;
26 import org.apache.lucene.util._TestUtil;
28 public class TestCharArrayIterator extends LuceneTestCase {
30 public void testWordInstance() {
31 doTests(CharArrayIterator.newWordInstance());
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);
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();
52 ci.count = text.length;
58 public void testSentenceInstance() {
59 doTests(CharArrayIterator.newSentenceInstance());
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);
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();
80 ci.count = text.length;
86 private void doTests(CharArrayIterator ci) {
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());
100 ci.setText("testing".toCharArray(), 0, "testing".length());
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());
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());
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());
126 // or DONE if the current position is off the end of the text.
127 assertEquals(CharacterIterator.DONE, ci.current());
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());
139 ci.setText("test".toCharArray(), 0, "test".length());
143 } catch (Exception e) {
144 assertTrue(e instanceof IllegalArgumentException);
148 char text[] = "testing".toCharArray();
149 ci.setText(text, 0, text.length);
151 CharArrayIterator ci2 = (CharArrayIterator) ci.clone();
152 assertEquals(ci.getIndex(), ci2.getIndex());
153 assertEquals(ci.next(), ci2.next());
154 assertEquals(ci.last(), ci2.last());
157 private void consume(BreakIterator bi, CharacterIterator ci) {
159 while (bi.next() != BreakIterator.DONE)