2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.iterator;
\r
9 import java.text.CharacterIterator;
\r
10 import java.text.StringCharacterIterator;
\r
12 import com.ibm.icu.dev.test.TestFmwk;
\r
13 import com.ibm.icu.text.ReplaceableString;
\r
14 import com.ibm.icu.text.UCharacterIterator;
\r
15 import com.ibm.icu.text.UTF16;
\r
20 * To change this generated comment edit the template variable "typecomment":
\r
21 * Window>Preferences>Java>Templates.
\r
22 * To enable and disable the creation of type comments go to
\r
23 * Window>Preferences>Java>Code Generation.
\r
25 public class TestUCharacterIterator extends TestFmwk{
\r
27 // constructor -----------------------------------------------------
\r
32 public TestUCharacterIterator()
\r
36 // public methods --------------------------------------------------
\r
41 public void TestClone() throws CloneNotSupportedException
\r
43 UCharacterIterator iterator = UCharacterIterator.getInstance("testing");
\r
44 UCharacterIterator cloned = (UCharacterIterator)iterator.clone();
\r
46 while (completed != UCharacterIterator.DONE) {
\r
47 completed = iterator.next();
\r
48 if (completed != cloned.next()) {
\r
49 errln("Cloned operation failed");
\r
53 public void getText(UCharacterIterator iterator, String result){
\r
55 char[] buf= new char[1];
\r
58 iterator.getText(buf);
\r
60 }catch(IndexOutOfBoundsException e){
\r
61 buf = new char[iterator.getLength()];
\r
64 if(result.compareTo(new String(buf,0,iterator.getLength()))!=0){
\r
65 errln("getText failed for iterator");
\r
72 public void TestIteration()
\r
74 UCharacterIterator iterator = UCharacterIterator.getInstance(
\r
76 UCharacterIterator iterator2 = UCharacterIterator.getInstance(
\r
78 iterator.setToStart();
\r
79 if (iterator.current() != ITERATION_STRING_.charAt(0)) {
\r
80 errln("Iterator failed retrieving first character");
\r
82 iterator.setToLimit();
\r
83 if (iterator.previous() != ITERATION_STRING_.charAt(
\r
84 ITERATION_STRING_.length() - 1)) {
\r
85 errln("Iterator failed retrieving last character");
\r
87 if (iterator.getLength() != ITERATION_STRING_.length()) {
\r
88 errln("Iterator failed determining begin and end index");
\r
90 iterator2.setIndex(0);
\r
91 iterator.setIndex(0);
\r
93 while (ch != UCharacterIterator.DONE) {
\r
94 int index = iterator2.getIndex();
\r
95 ch = iterator2.nextCodePoint();
\r
96 if (index != ITERATION_SUPPLEMENTARY_INDEX) {
\r
97 if (ch != (int)iterator.next() &&
\r
98 ch != UCharacterIterator.DONE) {
\r
99 errln("Error mismatch in next() and nextCodePoint()");
\r
103 if (UTF16.getLeadSurrogate(ch) != iterator.next() ||
\r
104 UTF16.getTrailSurrogate(ch) != iterator.next()) {
\r
105 errln("Error mismatch in next and nextCodePoint for " +
\r
106 "supplementary characters");
\r
110 iterator.setIndex(ITERATION_STRING_.length());
\r
111 iterator2.setIndex(ITERATION_STRING_.length());
\r
112 while (ch != UCharacterIterator.DONE) {
\r
113 int index = iterator2.getIndex();
\r
114 ch = iterator2.previousCodePoint();
\r
115 if (index != ITERATION_SUPPLEMENTARY_INDEX) {
\r
116 if (ch != (int)iterator.previous() &&
\r
117 ch != UCharacterIterator.DONE) {
\r
118 errln("Error mismatch in previous() and " +
\r
119 "previousCodePoint()");
\r
123 if (UTF16.getLeadSurrogate(ch) != iterator.previous() ||
\r
124 UTF16.getTrailSurrogate(ch) != iterator.previous()) {
\r
125 errln("Error mismatch in previous and " +
\r
126 "previousCodePoint for supplementary characters");
\r
132 public static void main(String[] arg)
\r
136 TestUCharacterIterator test = new TestUCharacterIterator();
\r
139 catch (Exception e)
\r
141 e.printStackTrace();
\r
145 //Tests for new API for utf-16 support
\r
146 public void TestIterationUChar32() {
\r
147 String text="\u0061\u0062\ud841\udc02\u20ac\ud7ff\ud842\udc06\ud801\udc00\u0061";
\r
151 UCharacterIterator iter = UCharacterIterator.getInstance(text);
\r
153 String iterText = iter.getText();
\r
154 if (!iterText.equals(text))
\r
155 errln("iter.getText() failed");
\r
158 if (iter.currentCodePoint() != UTF16.charAt(text,1))
\r
159 errln("Iterator didn't start out in the right place.");
\r
162 c=iter.currentCodePoint();
\r
164 i=iter.moveCodePointIndex(1);
\r
165 c=iter.currentCodePoint();
\r
166 if(c != UTF16.charAt(text,1) || i!=1)
\r
167 errln("moveCodePointIndex(1) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,1)) + " i= " + i);
\r
169 i=iter.moveCodePointIndex(2);
\r
170 c=iter.currentCodePoint();
\r
171 if(c != UTF16.charAt(text,4) || i!=4)
\r
172 errln("moveCodePointIndex(2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,4)) + " i= " + i);
\r
174 i=iter.moveCodePointIndex(-2);
\r
175 c=iter.currentCodePoint();
\r
176 if(c != UTF16.charAt(text,1) || i!=1)
\r
177 errln("moveCodePointIndex(-2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,1)) + " i= " + i);
\r
180 i=iter.moveCodePointIndex(-2);
\r
181 c=iter.currentCodePoint();
\r
182 if(c != UTF16.charAt(text,(text.length()-3)) || i!=(text.length()-3))
\r
183 errln("moveCodePointIndex(-2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,(text.length()-3)) ) + " i= " + i);
\r
186 c = iter.currentCodePoint();
\r
189 //testing first32PostInc, nextCodePointPostInc, setTostart
\r
193 if(c != UTF16.charAt(text,i))
\r
194 errln("first32PostInc failed. Expected->"+hex(UTF16.charAt(text,i))+" Got-> "+hex(c));
\r
195 if(iter.getIndex() != UTF16.getCharCount(c) + i)
\r
196 errln("getIndex() after first32PostInc() failed");
\r
200 if (iter.getIndex() != 0)
\r
201 errln("setToStart failed");
\r
203 logln("Testing forward iteration...");
\r
205 if (c != UCharacterIterator.DONE)
\r
206 c = iter.nextCodePoint();
\r
208 if(c != UTF16.charAt(text,i))
\r
209 errln("Character mismatch at position "+i+", iterator has "+hex(c)+", string has "+hex(UTF16.charAt(text,i)));
\r
211 i+=UTF16.getCharCount(c);
\r
212 if(iter.getIndex() != i)
\r
213 errln("getIndex() aftr nextCodePointPostInc() isn't working right");
\r
214 c = iter.currentCodePoint();
\r
215 if( c!=UCharacterIterator.DONE && c != UTF16.charAt(text,i))
\r
216 errln("current() after nextCodePointPostInc() isn't working right");
\r
218 } while (c!=UCharacterIterator.DONE);
\r
219 c=iter.nextCodePoint();
\r
220 if(c!= UCharacterIterator.DONE)
\r
221 errln("nextCodePointPostInc() didn't return DONE at the beginning");
\r
227 class UCharIterator {
\r
229 public UCharIterator(int[] src, int len, int index){
\r
236 public int current() {
\r
244 public int next() {
\r
252 public int previous() {
\r
260 public int getIndex() {
\r
265 private int length, i;
\r
267 public void TestPreviousNext(){
\r
268 // src and expect strings
\r
270 UTF16.getLeadSurrogate(0x2f999), UTF16.getTrailSurrogate(0x2f999),
\r
271 UTF16.getLeadSurrogate(0x1d15f), UTF16.getTrailSurrogate(0x1d15f),
\r
276 UCharacterIterator iter1 = UCharacterIterator.getInstance(new ReplaceableString(new String(src)));
\r
277 UCharacterIterator iter2 = UCharacterIterator.getInstance(src/*char array*/);
\r
278 UCharacterIterator iter3 = UCharacterIterator.getInstance(new StringCharacterIterator(new String(src)));
\r
279 UCharacterIterator iter4 = UCharacterIterator.getInstance(new StringBuffer(new String(src)));
\r
280 previousNext(iter1);
\r
281 previousNext(iter2);
\r
282 previousNext(iter3);
\r
283 previousNext(iter4);
\r
284 getText(iter1,new String(src));
\r
285 getText(iter2,new String(src));
\r
286 getText(iter3,new String(src));
\r
287 /* getCharacterIterator */
\r
288 CharacterIterator citer1 = iter1.getCharacterIterator();
\r
289 CharacterIterator citer2 = iter2.getCharacterIterator();
\r
290 CharacterIterator citer3 = iter3.getCharacterIterator();
\r
291 if(citer1.first() !=iter1.current()){
\r
292 errln("getCharacterIterator for iter1 failed");
\r
294 if(citer2.first() !=iter2.current()){
\r
295 errln("getCharacterIterator for iter2 failed");
\r
297 if(citer3.first() !=iter3.current()){
\r
298 errln("getCharacterIterator for iter3 failed");
\r
300 /* Test clone() && moveIndex()*/
\r
302 UCharacterIterator clone1 = (UCharacterIterator)iter1.clone();
\r
303 UCharacterIterator clone2 = (UCharacterIterator)iter2.clone();
\r
304 UCharacterIterator clone3 = (UCharacterIterator)iter3.clone();
\r
305 if(clone1.moveIndex(3)!=iter1.moveIndex(3)){
\r
306 errln("moveIndex for iter1 failed");
\r
308 if(clone2.moveIndex(3)!=iter2.moveIndex(3)){
\r
309 errln("moveIndex for iter2 failed");
\r
311 if(clone3.moveIndex(3)!=iter3.moveIndex(3)){
\r
312 errln("moveIndex for iter1 failed");
\r
314 }catch (Exception e){
\r
315 errln("could not clone the iterator");
\r
318 public void previousNext(UCharacterIterator iter) {
\r
327 // expected src indexes corresponding to expect indexes
\r
328 int expectIndex[]={
\r
336 // initial indexes into the src and expect strings
\r
338 final int SRC_MIDDLE=4;
\r
339 final int EXPECT_MIDDLE=2;
\r
343 // - for previous(), 0 for current(), + for next()
\r
344 // not const so that we can terminate it below for the error message
\r
345 String moves="0+0+0--0-0-+++0--+++++++0--------";
\r
348 UCharIterator iter32 = new UCharIterator(expect, expect.length,
\r
354 // initially set the indexes into the middle of the strings
\r
355 iter.setIndex(SRC_MIDDLE);
\r
357 // move around and compare the iteration code points with
\r
358 // the expected ones
\r
360 while(movesIndex<moves.length()) {
\r
361 m=moves.charAt(movesIndex++);
\r
363 c1=iter.previousCodePoint();
\r
364 c2=iter32.previous();
\r
365 } else if(m=='0') {
\r
366 c1=iter.currentCodePoint();
\r
367 c2=iter32.current();
\r
369 c1=iter.nextCodePoint();
\r
375 // copy the moves until the current (m) move, and terminate
\r
376 String history = moves.substring(0,movesIndex);
\r
377 errln("error: mismatch in Normalizer iteration at "+history+": "
\r
378 +"got c1= " + hex(c1) +" != expected c2= "+ hex(c2));
\r
383 if(expectIndex[iter.getIndex()]!=iter32.getIndex()) {
\r
384 // copy the moves until the current (m) move, and terminate
\r
385 String history = moves.substring(0,movesIndex);
\r
386 errln("error: index mismatch in Normalizer iteration at "
\r
387 +history+ " : "+ "Normalizer index " +iter.getIndex()
\r
388 +" expected "+ expectIndex[iter32.getIndex()]);
\r
393 public void TestUCharacterIteratorWrapper(){
\r
394 String source ="asdfasdfjoiuyoiuy2341235679886765";
\r
395 UCharacterIterator it = UCharacterIterator.getInstance(source);
\r
396 CharacterIterator wrap_ci = it.getCharacterIterator();
\r
397 CharacterIterator ci = new StringCharacterIterator(source);
\r
398 wrap_ci.setIndex(10);
\r
400 String moves="0+0+0--0-0-+++0--+++++++0--------++++0000----0-";
\r
405 while(movesIndex<moves.length()) {
\r
406 m=moves.charAt(movesIndex++);
\r
408 c1=wrap_ci.previous();
\r
410 } else if(m=='0') {
\r
411 c1=wrap_ci.current();
\r
420 // copy the moves until the current (m) move, and terminate
\r
421 String history = moves.substring(0,movesIndex);
\r
422 errln("error: mismatch in Normalizer iteration at "+history+": "
\r
423 +"got c1= " + hex(c1) +" != expected c2= "+ hex(c2));
\r
428 if(wrap_ci.getIndex()!=ci.getIndex()) {
\r
429 // copy the moves until the current (m) move, and terminate
\r
430 String history = moves.substring(0,movesIndex);
\r
431 errln("error: index mismatch in Normalizer iteration at "
\r
432 +history+ " : "+ "Normalizer index " +wrap_ci.getIndex()
\r
433 +" expected "+ ci.getIndex());
\r
437 if(ci.first()!=wrap_ci.first()){
\r
438 errln("CharacterIteratorWrapper.first() failed. expected: " + ci.first() + " got: " +wrap_ci.first());
\r
440 if(ci.last()!=wrap_ci.last()){
\r
441 errln("CharacterIteratorWrapper.last() failed expected: " + ci.last() + " got: " +wrap_ci.last());
\r
443 if(ci.getBeginIndex()!=wrap_ci.getBeginIndex()){
\r
444 errln("CharacterIteratorWrapper.getBeginIndex() failed expected: " + ci.getBeginIndex() + " got: " +wrap_ci.getBeginIndex());
\r
446 if(ci.getEndIndex()!=wrap_ci.getEndIndex()){
\r
447 errln("CharacterIteratorWrapper.getEndIndex() failed expected: " + ci.getEndIndex() + " got: " +wrap_ci.getEndIndex());
\r
450 CharacterIterator cloneWCI = (CharacterIterator) wrap_ci.clone();
\r
451 if(wrap_ci.getIndex()!=cloneWCI.getIndex()){
\r
452 errln("CharacterIteratorWrapper.clone() failed expected: " +wrap_ci.getIndex() + " got: " + cloneWCI.getIndex());
\r
454 }catch(Exception e){
\r
455 errln("CharacterIterator.clone() failed");
\r
458 // private data members ---------------------------------------------
\r
460 private static final String ITERATION_STRING_ =
\r
461 "Testing 1 2 3 \ud800\udc00 456";
\r
462 private static final int ITERATION_SUPPLEMENTARY_INDEX = 14;
\r
464 public void TestJitterbug1952(){
\r
465 //test previous code point
\r
466 char[] src = new char[]{ '\uDC00','\uD800','\uDC01','\uD802','\uDC02','\uDC03'};
\r
467 UCharacterIterator iter = UCharacterIterator.getInstance(src);
\r
470 // this should never go into a infinite loop
\r
471 // if it does then we have a problem
\r
472 while((ch=iter.previousCodePoint())!=UCharacterIterator.DONE){
\r
474 errln("iter.previousCodePoint() failed");
\r
478 while((ch=iter.nextCodePoint()) !=UCharacterIterator.DONE){
\r
480 errln("iter.nextCodePoint() failed");
\r