1 package org.apache.lucene.analysis.icu.segmentation;
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.CharacterIterator;
22 import org.apache.lucene.util.LuceneTestCase;
24 public class TestCharArrayIterator extends LuceneTestCase {
25 public void testBasicUsage() {
26 CharArrayIterator ci = new CharArrayIterator();
27 ci.setText("testing".toCharArray(), 0, "testing".length());
28 assertEquals(0, ci.getBeginIndex());
29 assertEquals(7, ci.getEndIndex());
30 assertEquals(0, ci.getIndex());
31 assertEquals('t', ci.current());
32 assertEquals('e', ci.next());
33 assertEquals('g', ci.last());
34 assertEquals('n', ci.previous());
35 assertEquals('t', ci.first());
36 assertEquals(CharacterIterator.DONE, ci.previous());
39 public void testFirst() {
40 CharArrayIterator ci = new CharArrayIterator();
41 ci.setText("testing".toCharArray(), 0, "testing".length());
43 // Sets the position to getBeginIndex() and returns the character at that position.
44 assertEquals('t', ci.first());
45 assertEquals(ci.getBeginIndex(), ci.getIndex());
46 // or DONE if the text is empty
47 ci.setText(new char[] {}, 0, 0);
48 assertEquals(CharacterIterator.DONE, ci.first());
51 public void testLast() {
52 CharArrayIterator ci = new CharArrayIterator();
53 ci.setText("testing".toCharArray(), 0, "testing".length());
54 // Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
55 // and returns the character at that position.
56 assertEquals('g', ci.last());
57 assertEquals(ci.getIndex(), ci.getEndIndex() - 1);
58 // or DONE if the text is empty
59 ci.setText(new char[] {}, 0, 0);
60 assertEquals(CharacterIterator.DONE, ci.last());
61 assertEquals(ci.getEndIndex(), ci.getIndex());
64 public void testCurrent() {
65 CharArrayIterator ci = new CharArrayIterator();
66 // Gets the character at the current position (as returned by getIndex()).
67 ci.setText("testing".toCharArray(), 0, "testing".length());
68 assertEquals('t', ci.current());
71 // or DONE if the current position is off the end of the text.
72 assertEquals(CharacterIterator.DONE, ci.current());
75 public void testNext() {
76 CharArrayIterator ci = new CharArrayIterator();
77 ci.setText("te".toCharArray(), 0, 2);
78 // Increments the iterator's index by one and returns the character at the new index.
79 assertEquals('e', ci.next());
80 assertEquals(1, ci.getIndex());
81 // or DONE if the new position is off the end of the text range.
82 assertEquals(CharacterIterator.DONE, ci.next());
83 assertEquals(ci.getEndIndex(), ci.getIndex());
86 public void testSetIndex() {
87 CharArrayIterator ci = new CharArrayIterator();
88 ci.setText("test".toCharArray(), 0, "test".length());
92 } catch (Exception e) {
93 assertTrue(e instanceof IllegalArgumentException);
97 public void testClone() {
98 char text[] = "testing".toCharArray();
99 CharArrayIterator ci = new CharArrayIterator();
100 ci.setText(text, 0, text.length);
102 CharArrayIterator ci2 = (CharArrayIterator) ci.clone();
103 assertEquals(ci.getIndex(), ci2.getIndex());
104 assertEquals(ci.next(), ci2.next());
105 assertEquals(ci.last(), ci2.last());