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