2 *******************************************************************************
\r
3 * Copyright (C) 2001-2007, 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 com.ibm.icu.dev.test.TestFmwk;
\r
10 import com.ibm.icu.text.UCharacterIterator;
\r
11 import com.ibm.icu.text.UTF16;
\r
12 import com.ibm.icu.text.ReplaceableString;
\r
13 import java.text.CharacterIterator;
\r
14 import java.text.StringCharacterIterator;
\r
19 * To change this generated comment edit the template variable "typecomment":
\r
20 * Window>Preferences>Java>Templates.
\r
21 * To enable and disable the creation of type comments go to
\r
22 * Window>Preferences>Java>Code Generation.
\r
24 public class TestUCharacterIterator extends TestFmwk{
\r
26 // constructor -----------------------------------------------------
\r
31 public TestUCharacterIterator()
\r
35 // public methods --------------------------------------------------
\r
40 public void TestClone() throws CloneNotSupportedException
\r
42 UCharacterIterator iterator = UCharacterIterator.getInstance("testing");
\r
43 UCharacterIterator cloned = (UCharacterIterator)iterator.clone();
\r
45 while (completed != UCharacterIterator.DONE) {
\r
46 completed = iterator.next();
\r
47 if (completed != cloned.next()) {
\r
48 errln("Cloned operation failed");
\r
52 public void getText(UCharacterIterator iterator, String result){
\r
54 char[] buf= new char[1];
\r
57 iterator.getText(buf);
\r
59 }catch(IndexOutOfBoundsException e){
\r
60 buf = new char[iterator.getLength()];
\r
63 if(result.compareTo(new String(buf,0,iterator.getLength()))!=0){
\r
64 errln("getText failed for iterator");
\r
71 public void TestIteration()
\r
73 UCharacterIterator iterator = UCharacterIterator.getInstance(
\r
75 UCharacterIterator iterator2 = UCharacterIterator.getInstance(
\r
77 iterator.setToStart();
\r
78 if (iterator.current() != ITERATION_STRING_.charAt(0)) {
\r
79 errln("Iterator failed retrieving first character");
\r
81 iterator.setToLimit();
\r
82 if (iterator.previous() != ITERATION_STRING_.charAt(
\r
83 ITERATION_STRING_.length() - 1)) {
\r
84 errln("Iterator failed retrieving last character");
\r
86 if (iterator.getLength() != ITERATION_STRING_.length()) {
\r
87 errln("Iterator failed determining begin and end index");
\r
89 iterator2.setIndex(0);
\r
90 iterator.setIndex(0);
\r
92 while (ch != UCharacterIterator.DONE) {
\r
93 int index = iterator2.getIndex();
\r
94 ch = iterator2.nextCodePoint();
\r
95 if (index != ITERATION_SUPPLEMENTARY_INDEX) {
\r
96 if (ch != (int)iterator.next() &&
\r
97 ch != UCharacterIterator.DONE) {
\r
98 errln("Error mismatch in next() and nextCodePoint()");
\r
102 if (UTF16.getLeadSurrogate(ch) != iterator.next() ||
\r
103 UTF16.getTrailSurrogate(ch) != iterator.next()) {
\r
104 errln("Error mismatch in next and nextCodePoint for " +
\r
105 "supplementary characters");
\r
109 iterator.setIndex(ITERATION_STRING_.length());
\r
110 iterator2.setIndex(ITERATION_STRING_.length());
\r
111 while (ch != UCharacterIterator.DONE) {
\r
112 int index = iterator2.getIndex();
\r
113 ch = iterator2.previousCodePoint();
\r
114 if (index != ITERATION_SUPPLEMENTARY_INDEX) {
\r
115 if (ch != (int)iterator.previous() &&
\r
116 ch != UCharacterIterator.DONE) {
\r
117 errln("Error mismatch in previous() and " +
\r
118 "previousCodePoint()");
\r
122 if (UTF16.getLeadSurrogate(ch) != iterator.previous() ||
\r
123 UTF16.getTrailSurrogate(ch) != iterator.previous()) {
\r
124 errln("Error mismatch in previous and " +
\r
125 "previousCodePoint for supplementary characters");
\r
131 public static void main(String[] arg)
\r
135 TestUCharacterIterator test = new TestUCharacterIterator();
\r
138 catch (Exception e)
\r
140 e.printStackTrace();
\r
144 //Tests for new API for utf-16 support
\r
145 public void TestIterationUChar32() {
\r
146 String text="\u0061\u0062\ud841\udc02\u20ac\ud7ff\ud842\udc06\ud801\udc00\u0061";
\r
150 UCharacterIterator iter = UCharacterIterator.getInstance(text);
\r
152 String iterText = iter.getText();
\r
153 if (!iterText.equals(text))
\r
154 errln("iter.getText() failed");
\r
157 if (iter.currentCodePoint() != UTF16.charAt(text,1))
\r
158 errln("Iterator didn't start out in the right place.");
\r
161 c=iter.currentCodePoint();
\r
163 i=iter.moveCodePointIndex(1);
\r
164 c=iter.currentCodePoint();
\r
165 if(c != UTF16.charAt(text,1) || i!=1)
\r
166 errln("moveCodePointIndex(1) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,1)) + " i= " + i);
\r
168 i=iter.moveCodePointIndex(2);
\r
169 c=iter.currentCodePoint();
\r
170 if(c != UTF16.charAt(text,4) || i!=4)
\r
171 errln("moveCodePointIndex(2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,4)) + " i= " + i);
\r
173 i=iter.moveCodePointIndex(-2);
\r
174 c=iter.currentCodePoint();
\r
175 if(c != UTF16.charAt(text,1) || i!=1)
\r
176 errln("moveCodePointIndex(-2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,1)) + " i= " + i);
\r
179 i=iter.moveCodePointIndex(-2);
\r
180 c=iter.currentCodePoint();
\r
181 if(c != UTF16.charAt(text,(text.length()-3)) || i!=(text.length()-3))
\r
182 errln("moveCodePointIndex(-2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,(text.length()-3)) ) + " i= " + i);
\r
185 c = iter.currentCodePoint();
\r
188 //testing first32PostInc, nextCodePointPostInc, setTostart
\r
192 if(c != UTF16.charAt(text,i))
\r
193 errln("first32PostInc failed. Expected->"+hex(UTF16.charAt(text,i))+" Got-> "+hex(c));
\r
194 if(iter.getIndex() != UTF16.getCharCount(c) + i)
\r
195 errln("getIndex() after first32PostInc() failed");
\r
199 if (iter.getIndex() != 0)
\r
200 errln("setToStart failed");
\r
202 logln("Testing forward iteration...");
\r
204 if (c != UCharacterIterator.DONE)
\r
205 c = iter.nextCodePoint();
\r
207 if(c != UTF16.charAt(text,i))
\r
208 errln("Character mismatch at position "+i+", iterator has "+hex(c)+", string has "+hex(UTF16.charAt(text,i)));
\r
210 i+=UTF16.getCharCount(c);
\r
211 if(iter.getIndex() != i)
\r
212 errln("getIndex() aftr nextCodePointPostInc() isn't working right");
\r
213 c = iter.currentCodePoint();
\r
214 if( c!=UCharacterIterator.DONE && c != UTF16.charAt(text,i))
\r
215 errln("current() after nextCodePointPostInc() isn't working right");
\r
217 } while (c!=UCharacterIterator.DONE);
\r
218 c=iter.nextCodePoint();
\r
219 if(c!= UCharacterIterator.DONE)
\r
220 errln("nextCodePointPostInc() didn't return DONE at the beginning");
\r
226 class UCharIterator {
\r
228 public UCharIterator(int[] src, int len, int index){
\r
235 public int current() {
\r
243 public int next() {
\r
251 public int previous() {
\r
259 public int getIndex() {
\r
264 private int length, i;
\r
266 public void TestPreviousNext(){
\r
267 // src and expect strings
\r
269 UTF16.getLeadSurrogate(0x2f999), UTF16.getTrailSurrogate(0x2f999),
\r
270 UTF16.getLeadSurrogate(0x1d15f), UTF16.getTrailSurrogate(0x1d15f),
\r
275 UCharacterIterator iter1 = UCharacterIterator.getInstance(new ReplaceableString(new String(src)));
\r
276 UCharacterIterator iter2 = UCharacterIterator.getInstance(src/*char array*/);
\r
277 UCharacterIterator iter3 = UCharacterIterator.getInstance(new StringCharacterIterator(new String(src)));
\r
278 UCharacterIterator iter4 = UCharacterIterator.getInstance(new StringBuffer(new String(src)));
\r
279 previousNext(iter1);
\r
280 previousNext(iter2);
\r
281 previousNext(iter3);
\r
282 previousNext(iter4);
\r
283 getText(iter1,new String(src));
\r
284 getText(iter2,new String(src));
\r
285 getText(iter3,new String(src));
\r
286 /* getCharacterIterator */
\r
287 CharacterIterator citer1 = iter1.getCharacterIterator();
\r
288 CharacterIterator citer2 = iter2.getCharacterIterator();
\r
289 CharacterIterator citer3 = iter3.getCharacterIterator();
\r
290 if(citer1.first() !=iter1.current()){
\r
291 errln("getCharacterIterator for iter1 failed");
\r
293 if(citer2.first() !=iter2.current()){
\r
294 errln("getCharacterIterator for iter2 failed");
\r
296 if(citer3.first() !=iter3.current()){
\r
297 errln("getCharacterIterator for iter3 failed");
\r
299 /* Test clone() && moveIndex()*/
\r
301 UCharacterIterator clone1 = (UCharacterIterator)iter1.clone();
\r
302 UCharacterIterator clone2 = (UCharacterIterator)iter2.clone();
\r
303 UCharacterIterator clone3 = (UCharacterIterator)iter3.clone();
\r
304 if(clone1.moveIndex(3)!=iter1.moveIndex(3)){
\r
305 errln("moveIndex for iter1 failed");
\r
307 if(clone2.moveIndex(3)!=iter2.moveIndex(3)){
\r
308 errln("moveIndex for iter2 failed");
\r
310 if(clone3.moveIndex(3)!=iter3.moveIndex(3)){
\r
311 errln("moveIndex for iter1 failed");
\r
313 }catch (Exception e){
\r
314 errln("could not clone the iterator");
\r
317 public void previousNext(UCharacterIterator iter) {
\r
326 // expected src indexes corresponding to expect indexes
\r
327 int expectIndex[]={
\r
335 // initial indexes into the src and expect strings
\r
337 final int SRC_MIDDLE=4;
\r
338 final int EXPECT_MIDDLE=2;
\r
342 // - for previous(), 0 for current(), + for next()
\r
343 // not const so that we can terminate it below for the error message
\r
344 String moves="0+0+0--0-0-+++0--+++++++0--------";
\r
347 UCharIterator iter32 = new UCharIterator(expect, expect.length,
\r
353 // initially set the indexes into the middle of the strings
\r
354 iter.setIndex(SRC_MIDDLE);
\r
356 // move around and compare the iteration code points with
\r
357 // the expected ones
\r
359 while(movesIndex<moves.length()) {
\r
360 m=moves.charAt(movesIndex++);
\r
362 c1=iter.previousCodePoint();
\r
363 c2=iter32.previous();
\r
364 } else if(m=='0') {
\r
365 c1=iter.currentCodePoint();
\r
366 c2=iter32.current();
\r
368 c1=iter.nextCodePoint();
\r
374 // copy the moves until the current (m) move, and terminate
\r
375 String history = moves.substring(0,movesIndex);
\r
376 errln("error: mismatch in Normalizer iteration at "+history+": "
\r
377 +"got c1= " + hex(c1) +" != expected c2= "+ hex(c2));
\r
382 if(expectIndex[iter.getIndex()]!=iter32.getIndex()) {
\r
383 // copy the moves until the current (m) move, and terminate
\r
384 String history = moves.substring(0,movesIndex);
\r
385 errln("error: index mismatch in Normalizer iteration at "
\r
386 +history+ " : "+ "Normalizer index " +iter.getIndex()
\r
387 +" expected "+ expectIndex[iter32.getIndex()]);
\r
392 public void TestUCharacterIteratorWrapper(){
\r
393 String source ="asdfasdfjoiuyoiuy2341235679886765";
\r
394 UCharacterIterator it = UCharacterIterator.getInstance(source);
\r
395 CharacterIterator wrap_ci = it.getCharacterIterator();
\r
396 CharacterIterator ci = new StringCharacterIterator(source);
\r
397 wrap_ci.setIndex(10);
\r
399 String moves="0+0+0--0-0-+++0--+++++++0--------++++0000----0-";
\r
404 while(movesIndex<moves.length()) {
\r
405 m=moves.charAt(movesIndex++);
\r
407 c1=wrap_ci.previous();
\r
409 } else if(m=='0') {
\r
410 c1=wrap_ci.current();
\r
419 // copy the moves until the current (m) move, and terminate
\r
420 String history = moves.substring(0,movesIndex);
\r
421 errln("error: mismatch in Normalizer iteration at "+history+": "
\r
422 +"got c1= " + hex(c1) +" != expected c2= "+ hex(c2));
\r
427 if(wrap_ci.getIndex()!=ci.getIndex()) {
\r
428 // copy the moves until the current (m) move, and terminate
\r
429 String history = moves.substring(0,movesIndex);
\r
430 errln("error: index mismatch in Normalizer iteration at "
\r
431 +history+ " : "+ "Normalizer index " +wrap_ci.getIndex()
\r
432 +" expected "+ ci.getIndex());
\r
436 if(ci.first()!=wrap_ci.first()){
\r
437 errln("CharacterIteratorWrapper.first() failed. expected: " + ci.first() + " got: " +wrap_ci.first());
\r
439 if(ci.last()!=wrap_ci.last()){
\r
440 errln("CharacterIteratorWrapper.last() failed expected: " + ci.last() + " got: " +wrap_ci.last());
\r
442 if(ci.getBeginIndex()!=wrap_ci.getBeginIndex()){
\r
443 errln("CharacterIteratorWrapper.getBeginIndex() failed expected: " + ci.getBeginIndex() + " got: " +wrap_ci.getBeginIndex());
\r
445 if(ci.getEndIndex()!=wrap_ci.getEndIndex()){
\r
446 errln("CharacterIteratorWrapper.getEndIndex() failed expected: " + ci.getEndIndex() + " got: " +wrap_ci.getEndIndex());
\r
449 CharacterIterator cloneWCI = (CharacterIterator) wrap_ci.clone();
\r
450 if(wrap_ci.getIndex()!=cloneWCI.getIndex()){
\r
451 errln("CharacterIteratorWrapper.clone() failed expected: " +wrap_ci.getIndex() + " got: " + cloneWCI.getIndex());
\r
453 }catch(Exception e){
\r
454 errln("CharacterIterator.clone() failed");
\r
457 // private data members ---------------------------------------------
\r
459 private static final String ITERATION_STRING_ =
\r
460 "Testing 1 2 3 \ud800\udc00 456";
\r
461 private static final int ITERATION_SUPPLEMENTARY_INDEX = 14;
\r
463 public void TestJitterbug1952(){
\r
464 //test previous code point
\r
465 char[] src = new char[]{ '\uDC00','\uD800','\uDC01','\uD802','\uDC02','\uDC03'};
\r
466 UCharacterIterator iter = UCharacterIterator.getInstance(src);
\r
469 // this should never go into a infinite loop
\r
470 // if it does then we have a problem
\r
471 while((ch=iter.previousCodePoint())!=UCharacterIterator.DONE){
\r
473 errln("iter.previousCodePoint() failed");
\r
477 while((ch=iter.nextCodePoint()) !=UCharacterIterator.DONE){
\r
479 errln("iter.nextCodePoint() failed");
\r