001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.normalizer;
008:
009: import com.ibm.icu.dev.test.TestFmwk;
010: import com.ibm.icu.impl.NormalizerImpl;
011: import com.ibm.icu.impl.Utility;
012: import com.ibm.icu.lang.UCharacter;
013: import com.ibm.icu.text.ComposedCharIter;
014: import com.ibm.icu.text.Normalizer;
015: import com.ibm.icu.text.StringCharacterIterator;
016:
017: public class TestDeprecatedNormalizerAPI extends TestFmwk {
018:
019: public static void main(String[] args) throws Exception {
020: String[] tempArgs = new String[args.length];
021: int count = 0;
022:
023: // Allow the test to be pointed at a specific version of the Unicode database
024: //for (int i = 0; i < args.length; i++)
025: //{
026: // if (args[i].equals("-data")) {
027: // tempInfo = new UInfo(args[++i], args[++i]);
028: // } else {
029: // tempArgs[count++] = args[i];
030: // }
031: //}
032:
033: args = new String[count];
034: System.arraycopy(tempArgs, 0, args, 0, count);
035:
036: new TestDeprecatedNormalizerAPI().run(args);
037: }
038:
039: public TestDeprecatedNormalizerAPI() {
040: }
041:
042: public void TestNormalizerAPI() {
043: // instantiate a Normalizer from a CharacterIterator
044: String s = Utility.unescape("a\u0308\uac00\\U0002f800");
045: // make s a bit longer and more interesting
046: java.text.CharacterIterator iter = new StringCharacterIterator(
047: s + s);
048: //test deprecated constructors
049: Normalizer norm = new Normalizer(iter, Normalizer.NFC, 0);
050: Normalizer norm2 = new Normalizer(s, Normalizer.NFC, 0);
051: if (norm.next() != 0xe4) {
052: errln("error in Normalizer(CharacterIterator).next()");
053: }
054: // test clone(), ==, and hashCode()
055: Normalizer clone = (Normalizer) norm.clone();
056: if (clone.getBeginIndex() != norm.getBeginIndex()) {
057: errln("error in Normalizer.getBeginIndex()");
058: }
059:
060: if (clone.getEndIndex() != norm.getEndIndex()) {
061: errln("error in Normalizer.getEndIndex()");
062: }
063: // test setOption() and getOption()
064: clone.setOption(0xaa0000, true);
065: clone.setOption(0x20000, false);
066: if (clone.getOption(0x880000) == 0
067: || clone.getOption(0x20000) == 1) {
068: errln("error in Normalizer::setOption() or Normalizer::getOption()");
069: }
070: //test deprecated normalize method
071: Normalizer.normalize(s, Normalizer.NFC, 0);
072: //test deprecated compose method
073: Normalizer.compose(s, false, 0);
074: //test deprecated decompose method
075: Normalizer.decompose(s, false, 0);
076:
077: }
078:
079: /**
080: * Run through all of the characters returned by a composed-char iterator
081: * and make sure that:
082: * <ul>
083: * <li>a) They do indeed have decompositions.
084: * <li>b) The decomposition according to the iterator is the same as
085: * returned by Normalizer.decompose().
086: * <li>c) All characters <em>not</em> returned by the iterator do not
087: * have decompositions.
088: * </ul>
089: */
090: public void TestComposedCharIter() {
091: doTestComposedChars(false);
092: }
093:
094: public void doTestComposedChars(boolean compat) {
095: int options = Normalizer.IGNORE_HANGUL;
096: ComposedCharIter iter = new ComposedCharIter(compat, options);
097:
098: char lastChar = 0;
099:
100: while (iter.hasNext()) {
101: char ch = iter.next();
102:
103: // Test all characters between the last one and this one to make
104: // sure that they don't have decompositions
105: assertNoDecomp(lastChar, ch, compat, options);
106: lastChar = ch;
107:
108: // Now make sure that the decompositions for this character
109: // make sense
110: String chString = new StringBuffer().append(ch).toString();
111: String iterDecomp = iter.decomposition();
112: String normDecomp = Normalizer.decompose(chString, compat);
113:
114: if (iterDecomp.equals(chString)) {
115: errln("ERROR: " + hex(ch) + " has identical decomp");
116: } else if (!iterDecomp.equals(normDecomp)) {
117: errln("ERROR: Normalizer decomp for " + hex(ch) + " ("
118: + hex(normDecomp) + ")" + " != iter decomp ("
119: + hex(iterDecomp) + ")");
120: }
121: }
122: assertNoDecomp(lastChar, '\uFFFF', compat, options);
123: }
124:
125: void assertNoDecomp(char start, char limit, boolean compat,
126: int options) {
127: for (char x = ++start; x < limit; x++) {
128: String xString = new StringBuffer().append(x).toString();
129: String decomp = Normalizer.decompose(xString, compat);
130: if (!decomp.equals(xString)) {
131: errln("ERROR: " + hex(x) + " has decomposition ("
132: + hex(decomp) + ")"
133: + " but was not returned by iterator");
134: }
135: }
136: }
137:
138: public void TestRoundTrip() {
139: int options = Normalizer.IGNORE_HANGUL;
140: boolean compat = false;
141:
142: ComposedCharIter iter = new ComposedCharIter(false, options);
143: while (iter.hasNext()) {
144: char ch = iter.next();
145:
146: String chStr = new StringBuffer().append(ch).toString();
147: String decomp = Normalizer.decompose(chStr, compat);
148: String comp = Normalizer.compose(decomp, compat);
149:
150: int cClass = UCharacter.getCombiningClass(decomp.charAt(0));
151: cClass = 0;
152:
153: if (NormalizerImpl.isFullCompositionExclusion(ch)) {
154: logln("Skipped excluded char " + hex(ch) + " ("
155: + UCharacter.getName(ch) + ")");
156: continue;
157: }
158:
159: // Avoid disparaged characters
160: if (getDecomposition(ch, compat).length() == 4)
161: continue;
162:
163: if (!comp.equals(chStr)) {
164: errln("ERROR: Round trip invalid: " + hex(chStr)
165: + " --> " + hex(decomp) + " --> " + hex(comp));
166:
167: errln(" char decomp is '"
168: + getDecomposition(ch, compat) + "'");
169: }
170: }
171: }
172:
173: private String getDecomposition(char ch, boolean compat) {
174: char[] dest = new char[10];
175: int length = NormalizerImpl.getDecomposition(ch, compat, dest,
176: 0, dest.length);
177: return new String(dest, 0, length);
178: }
179: }
|