2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.normalizer;
\r
9 import com.ibm.icu.dev.test.TestFmwk;
\r
10 import com.ibm.icu.impl.Utility;
\r
11 import com.ibm.icu.lang.UCharacter;
\r
12 import com.ibm.icu.lang.UProperty;
\r
13 import com.ibm.icu.text.ComposedCharIter;
\r
14 import com.ibm.icu.text.Normalizer;
\r
15 import com.ibm.icu.text.StringCharacterIterator;
\r
17 public class TestDeprecatedNormalizerAPI extends TestFmwk
\r
20 public static void main(String[] args) throws Exception
\r
22 String[] tempArgs = new String[args.length];
\r
25 // Allow the test to be pointed at a specific version of the Unicode database
\r
26 //for (int i = 0; i < args.length; i++)
\r
28 // if (args[i].equals("-data")) {
\r
29 // tempInfo = new UInfo(args[++i], args[++i]);
\r
31 // tempArgs[count++] = args[i];
\r
35 args = new String[count];
\r
36 System.arraycopy(tempArgs, 0, args, 0, count);
\r
40 new TestDeprecatedNormalizerAPI().run(args);
\r
43 public TestDeprecatedNormalizerAPI() {
\r
46 public void TestNormalizerAPI(){
\r
47 // instantiate a Normalizer from a CharacterIterator
\r
48 String s=Utility.unescape("a\u0308\uac00\\U0002f800");
\r
49 // make s a bit longer and more interesting
\r
50 java.text.CharacterIterator iter = new StringCharacterIterator(s+s);
\r
51 //test deprecated constructors
\r
52 Normalizer norm = new Normalizer(iter, Normalizer.NFC,0);
\r
53 if(norm.next()!=0xe4) {
\r
54 errln("error in Normalizer(CharacterIterator).next()");
\r
56 Normalizer norm2 = new Normalizer(s,Normalizer.NFC,0);
\r
57 if(norm2.next()!=0xe4) {
\r
58 errln("error in Normalizer(CharacterIterator).next()");
\r
60 // test clone(), ==, and hashCode()
\r
61 Normalizer clone=(Normalizer)norm.clone();
\r
62 if(clone.getBeginIndex()!= norm.getBeginIndex()){
\r
63 errln("error in Normalizer.getBeginIndex()");
\r
66 if(clone.getEndIndex()!= norm.getEndIndex()){
\r
67 errln("error in Normalizer.getEndIndex()");
\r
69 // test setOption() and getOption()
\r
70 clone.setOption(0xaa0000, true);
\r
71 clone.setOption(0x20000, false);
\r
72 if(clone.getOption(0x880000) ==0|| clone.getOption(0x20000)==1) {
\r
73 errln("error in Normalizer::setOption() or Normalizer::getOption()");
\r
75 //test deprecated normalize method
\r
76 Normalizer.normalize(s,Normalizer.NFC,0);
\r
77 //test deprecated compose method
\r
78 Normalizer.compose(s,false,0);
\r
79 //test deprecated decompose method
\r
80 Normalizer.decompose(s,false,0);
\r
85 * Run through all of the characters returned by a composed-char iterator
\r
86 * and make sure that:
\r
88 * <li>a) They do indeed have decompositions.
\r
89 * <li>b) The decomposition according to the iterator is the same as
\r
90 * returned by Normalizer.decompose().
\r
91 * <li>c) All characters <em>not</em> returned by the iterator do not
\r
92 * have decompositions.
\r
95 public void TestComposedCharIter() {
\r
96 doTestComposedChars(false);
\r
99 public void doTestComposedChars(boolean compat) {
\r
100 int options = Normalizer.IGNORE_HANGUL;
\r
101 ComposedCharIter iter = new ComposedCharIter(compat, options);
\r
105 while (iter.hasNext()) {
\r
106 char ch = iter.next();
\r
108 // Test all characters between the last one and this one to make
\r
109 // sure that they don't have decompositions
\r
110 assertNoDecomp(lastChar, ch, compat, options);
\r
113 // Now make sure that the decompositions for this character
\r
115 String chString = new StringBuffer().append(ch).toString();
\r
116 String iterDecomp = iter.decomposition();
\r
117 String normDecomp = Normalizer.decompose(chString, compat);
\r
119 if (iterDecomp.equals(chString)) {
\r
120 errln("ERROR: " + hex(ch) + " has identical decomp");
\r
122 else if (!iterDecomp.equals(normDecomp)) {
\r
123 errln("ERROR: Normalizer decomp for " + hex(ch) + " (" + hex(normDecomp) + ")"
\r
124 + " != iter decomp (" + hex(iterDecomp) + ")" );
\r
127 assertNoDecomp(lastChar, '\uFFFF', compat, options);
\r
130 void assertNoDecomp(char start, char limit, boolean compat, int options)
\r
132 for (char x = ++start; x < limit; x++) {
\r
133 String xString = new StringBuffer().append(x).toString();
\r
134 String decomp = Normalizer.decompose(xString, compat);
\r
135 if (!decomp.equals(xString)) {
\r
136 errln("ERROR: " + hex(x) + " has decomposition (" + hex(decomp) + ")"
\r
137 + " but was not returned by iterator");
\r
143 public void TestRoundTrip() {
\r
144 int options = Normalizer.IGNORE_HANGUL;
\r
145 boolean compat = false;
\r
147 ComposedCharIter iter = new ComposedCharIter(false, options);
\r
148 while (iter.hasNext()) {
\r
149 final char ch = iter.next();
\r
151 String chStr = String.valueOf(ch);
\r
152 String decomp = iter.decomposition();
\r
153 String comp = Normalizer.compose(decomp, compat);
\r
155 if (UCharacter.hasBinaryProperty(ch, UProperty.FULL_COMPOSITION_EXCLUSION)) {
\r
156 logln("Skipped excluded char " + hex(ch) + " (" + UCharacter.getName(ch) + ")" );
\r
160 // Avoid disparaged characters
\r
161 if (decomp.length() == 4) continue;
\r
163 if (!comp.equals(chStr)) {
\r
164 errln("ERROR: Round trip invalid: " + hex(chStr) + " --> " + hex(decomp)
\r
165 + " --> " + hex(comp));
\r
167 errln(" char decomp is '" + decomp + "'");
\r