From 337aaade171388279e88270f9b305e3665624675 Mon Sep 17 00:00:00 2001 From: Ram Viswanadha Date: Tue, 16 Jul 2002 00:31:01 +0000 Subject: [PATCH] ICU-1763 add tests for UCharacaterITerator X-SVN-Rev: 9186 --- .../test/iterator/TestUCharacterIterator.java | 334 ++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 icu4j/src/com/ibm/icu/dev/test/iterator/TestUCharacterIterator.java diff --git a/icu4j/src/com/ibm/icu/dev/test/iterator/TestUCharacterIterator.java b/icu4j/src/com/ibm/icu/dev/test/iterator/TestUCharacterIterator.java new file mode 100644 index 0000000000..0bc3798dc6 --- /dev/null +++ b/icu4j/src/com/ibm/icu/dev/test/iterator/TestUCharacterIterator.java @@ -0,0 +1,334 @@ +package com.ibm.icu.dev.test.iterator; + +import com.ibm.icu.dev.test.TestFmwk; +import com.ibm.icu.impl.UCharacterIterator; +import com.ibm.icu.text.UTF16; +import com.ibm.icu.impl.Utility; + +/** + * @author ram + * + * To change this generated comment edit the template variable "typecomment": + * Window>Preferences>Java>Templates. + * To enable and disable the creation of type comments go to + * Window>Preferences>Java>Code Generation. + */ +public class TestUCharacterIterator extends TestFmwk{ + + // constructor ----------------------------------------------------- + + /** + * Constructor + */ + public TestUCharacterIterator() + { + } + + // public methods -------------------------------------------------- + + /** + * Testing cloning + */ + public void TestClone() throws CloneNotSupportedException + { + UCharacterIterator iterator = UCharacterIterator.getInstance("testing"); + UCharacterIterator cloned = (UCharacterIterator)iterator.clone(); + int completed = 0; + while (completed != UCharacterIterator.DONE) { + completed = iterator.next(); + if (completed != cloned.next()) { + errln("Cloned operation failed"); + } + } + } + + /** + * Testing iteration + */ + public void TestIteration() + { + UCharacterIterator iterator = UCharacterIterator.getInstance( + ITERATION_STRING_); + UCharacterIterator iterator2 = UCharacterIterator.getInstance( + ITERATION_STRING_); + iterator.setToStart(); + if (iterator.current() != ITERATION_STRING_.charAt(0)) { + errln("Iterator failed retrieving first character"); + } + iterator.setToLimit(); + if (iterator.previous() != ITERATION_STRING_.charAt( + ITERATION_STRING_.length() - 1)) { + errln("Iterator failed retrieving last character"); + } + if (iterator.getLength() != ITERATION_STRING_.length()) { + errln("Iterator failed determining begin and end index"); + } + iterator2.setIndex(0); + iterator.setIndex(0); + int ch = 0; + while (ch != UCharacterIterator.DONE) { + int index = iterator2.getIndex(); + ch = iterator2.nextCodePoint(); + if (index != ITERATION_SUPPLEMENTARY_INDEX) { + if (ch != (int)iterator.next() && + ch != UCharacterIterator.DONE) { + errln("Error mismatch in next() and nextCodePoint()"); + } + } + else { + if (UTF16.getLeadSurrogate(ch) != iterator.next() || + UTF16.getTrailSurrogate(ch) != iterator.next()) { + errln("Error mismatch in next and nextCodePoint for " + + "supplementary characters"); + } + } + } + iterator.setIndex(ITERATION_STRING_.length()); + iterator2.setIndex(ITERATION_STRING_.length()); + while (ch != UCharacterIterator.DONE) { + int index = iterator2.getIndex(); + ch = iterator2.previousCodePoint(); + if (index != ITERATION_SUPPLEMENTARY_INDEX) { + if (ch != (int)iterator.previous() && + ch != UCharacterIterator.DONE) { + errln("Error mismatch in previous() and " + + "previousCodePoint()"); + } + } + else { + if (UTF16.getLeadSurrogate(ch) != iterator.previous() || + UTF16.getTrailSurrogate(ch) != iterator.previous()) { + errln("Error mismatch in previous and " + + "previousCodePoint for supplementary characters"); + } + } + } + } + + public static void main(String[] arg) + { + try + { + TestUCharacterIterator test = new TestUCharacterIterator(); + test.run(arg); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + //Tests for new API for utf-16 support + public void TestIterationUChar32() { + String text="\u0061\u0062\ud841\udc02\u20ac\ud7ff\ud842\udc06\ud801\udc00\u0061"; + int c; + int i; + { + UCharacterIterator iter = UCharacterIterator.getInstance(text); + + String iterText = iter.getText(); + if (!iterText.equals(text)) + errln("iter.getText() failed"); + + iter.setIndex(1); + if (iter.currentCodePoint() != UTF16.charAt(text,1)) + errln("Iterator didn't start out in the right place."); + + iter.setToStart(); + c=iter.currentCodePoint(); + i=0; + i=iter.moveCodePointIndex(1); + c=iter.currentCodePoint(); + if(c != UTF16.charAt(text,1) || i!=1) + errln("moveCodePointIndex(1) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,1)) + " i= " + i); + + i=iter.moveCodePointIndex(2); + c=iter.currentCodePoint(); + if(c != UTF16.charAt(text,4) || i!=4) + errln("moveCodePointIndex(2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,4)) + " i= " + i); + + i=iter.moveCodePointIndex(-2); + c=iter.currentCodePoint(); + if(c != UTF16.charAt(text,1) || i!=1) + errln("moveCodePointIndex(-2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,1)) + " i= " + i); + + iter.setToLimit(); + i=iter.moveCodePointIndex(-2); + c=iter.currentCodePoint(); + if(c != UTF16.charAt(text,(text.length()-3)) || i!=(text.length()-3)) + errln("moveCodePointIndex(-2) didn't work correctly expected "+ hex(c) +" got "+hex(UTF16.charAt(text,(text.length()-3)) ) + " i= " + i); + + iter.setToStart(); + c = iter.currentCodePoint(); + i = 0; + + //testing first32PostInc, nextCodePointPostInc, setTostart + i = 0; + iter.setToStart(); + c =iter.next(); + if(c != UTF16.charAt(text,i)) + errln("first32PostInc failed. Expected->"+hex(UTF16.charAt(text,i))+" Got-> "+hex(c)); + if(iter.getIndex() != UTF16.getCharCount(c) + i) + errln("getIndex() after first32PostInc() failed"); + + iter.setToStart(); + i=0; + if (iter.getIndex() != 0) + errln("setToStart failed"); + + logln("Testing forward iteration..."); + do { + if (c != UCharacterIterator.DONE) + c = iter.nextCodePoint(); + + if(c != UTF16.charAt(text,i)) + errln("Character mismatch at position "+i+", iterator has "+hex(c)+", string has "+hex(UTF16.charAt(text,i))); + + i+=UTF16.getCharCount(c); + if(iter.getIndex() != i) + errln("getIndex() aftr nextCodePointPostInc() isn't working right"); + c = iter.currentCodePoint(); + if( c!=UCharacterIterator.DONE && c != UTF16.charAt(text,i)) + errln("current() after nextCodePointPostInc() isn't working right"); + + } while (c!=UCharacterIterator.DONE); + c=iter.nextCodePoint(); + if(c!= UCharacterIterator.DONE) + errln("nextCodePointPostInc() didn't return DONE at the beginning"); + + + } + } + + class UCharIterator { + + public UCharIterator(int[] src, int len, int index){ + + s=src; + length=len; + i=index; + } + + public int current() { + if(i0) { + return s[--i]; + } else { + return -1; + } + } + + public int getIndex() { + return i; + } + + private int[] s; + private int length, i; + }; + /* + public void TestPreviousNext() { + // src and expect strings + char src[]={ + UTF16.getLeadSurrogate(0x2f999), UTF16.getTrailSurrogate(0x2f999), + UTF16.getLeadSurrogate(0x1d15f), UTF16.getTrailSurrogate(0x1d15f), + 0xc4, + 0x1ed0 + }; + int expect[]={ + 0x2f999, + 0x1d15f, + 0xc4, + 0x1ed0 + }; + + // expected src indexes corresponding to expect indexes + int expectIndex[]={ + 0,0, + 1,1, + 2, + 3, + 3 + }; + + // initial indexes into the src and expect strings + + final int SRC_MIDDLE=4; + final int EXPECT_MIDDLE=2; + + + // movement vector + // - for previous(), 0 for current(), + for next() + // not const so that we can terminate it below for the error message + String moves="0+0+0--0-0-+++0--+++++++0--------"; + + // iterators + UCharacterIterator iter = UCharacterIterator.getInstance(new String(src)); + UCharIterator iter32 = new UCharIterator(expect, expect.length, + EXPECT_MIDDLE); + + int c1, c2; + char m; + + // initially set the indexes into the middle of the strings + iter.setIndex(SRC_MIDDLE); + + // move around and compare the iteration code points with + // the expected ones + int movesIndex =0; + while(movesIndex