TestDeprecatedNormalizerAPI.java revision 7935b1839a081ed19ae0d33029ad3c09632a2caa
1/*
2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.dev.test.normalizer;
8
9import com.ibm.icu.dev.test.TestFmwk;
10import com.ibm.icu.impl.Utility;
11import com.ibm.icu.lang.UCharacter;
12import com.ibm.icu.lang.UProperty;
13import com.ibm.icu.text.ComposedCharIter;
14import com.ibm.icu.text.Normalizer;
15import com.ibm.icu.text.StringCharacterIterator;
16
17public class TestDeprecatedNormalizerAPI extends TestFmwk
18{
19
20    public static void main(String[] args) throws Exception
21    {
22        String[] tempArgs = new String[args.length];
23        int count = 0;
24
25        // Allow the test to be pointed at a specific version of the Unicode database
26        //for (int i = 0; i < args.length; i++)
27        //{
28        //    if (args[i].equals("-data")) {
29        //        tempInfo = new UInfo(args[++i], args[++i]);
30        //    } else {
31        //        tempArgs[count++] = args[i];
32        //    }
33        //}
34
35        args = new String[count];
36        System.arraycopy(tempArgs, 0, args, 0, count);
37
38
39
40        new TestDeprecatedNormalizerAPI().run(args);
41    }
42
43    public TestDeprecatedNormalizerAPI() {
44    }
45
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()");
55        }
56        Normalizer norm2 = new Normalizer(s,Normalizer.NFC,0);
57        if(norm2.next()!=0xe4) {
58            errln("error in Normalizer(CharacterIterator).next()");
59        }
60        // test clone(), ==, and hashCode()
61        Normalizer clone=(Normalizer)norm.clone();
62        if(clone.getBeginIndex()!= norm.getBeginIndex()){
63           errln("error in Normalizer.getBeginIndex()");
64        }
65
66        if(clone.getEndIndex()!= norm.getEndIndex()){
67           errln("error in Normalizer.getEndIndex()");
68        }
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()");
74        }
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);
81
82    }
83
84    /**
85     * Run through all of the characters returned by a composed-char iterator
86     * and make sure that:
87     * <ul>
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.
93     * </ul>
94     */
95    public void TestComposedCharIter() {
96        doTestComposedChars(false);
97    }
98
99    public void doTestComposedChars(boolean compat) {
100        int options = Normalizer.IGNORE_HANGUL;
101        ComposedCharIter iter = new ComposedCharIter(compat, options);
102
103        char lastChar = 0;
104
105        while (iter.hasNext()) {
106            char ch = iter.next();
107
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);
111            lastChar = ch;
112
113            // Now make sure that the decompositions for this character
114            // make sense
115            String chString   = new StringBuffer().append(ch).toString();
116            String iterDecomp = iter.decomposition();
117            String normDecomp = Normalizer.decompose(chString, compat);
118
119            if (iterDecomp.equals(chString)) {
120                errln("ERROR: " + hex(ch) + " has identical decomp");
121            }
122            else if (!iterDecomp.equals(normDecomp)) {
123                errln("ERROR: Normalizer decomp for " + hex(ch) + " (" + hex(normDecomp) + ")"
124                    + " != iter decomp (" + hex(iterDecomp) + ")" );
125            }
126        }
127        assertNoDecomp(lastChar, '\uFFFF', compat, options);
128    }
129
130    void assertNoDecomp(char start, char limit, boolean compat, int options)
131    {
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");
138            }
139        }
140    }
141
142
143    public void TestRoundTrip() {
144        int options = Normalizer.IGNORE_HANGUL;
145        boolean compat = false;
146
147        ComposedCharIter iter = new ComposedCharIter(false, options);
148        while (iter.hasNext()) {
149            final char ch = iter.next();
150
151            String chStr = String.valueOf(ch);
152            String decomp = iter.decomposition();
153            String comp = Normalizer.compose(decomp, compat);
154
155            if (UCharacter.hasBinaryProperty(ch, UProperty.FULL_COMPOSITION_EXCLUSION)) {
156                logln("Skipped excluded char " + hex(ch) + " (" + UCharacter.getName(ch) + ")" );
157                continue;
158            }
159
160            // Avoid disparaged characters
161            if (decomp.length() == 4) continue;
162
163            if (!comp.equals(chStr)) {
164                errln("ERROR: Round trip invalid: " + hex(chStr) + " --> " + hex(decomp)
165                    + " --> " + hex(comp));
166
167                errln("  char decomp is '" + decomp + "'");
168            }
169        }
170    }
171}
172