1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5 *******************************************************************************
6 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
7 * others. All Rights Reserved.                                                *
8 *******************************************************************************
9 */
10package android.icu.dev.test.normalizer;
11
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.junit.runners.JUnit4;
15
16import android.icu.dev.test.TestFmwk;
17import android.icu.impl.Utility;
18import android.icu.lang.UCharacter;
19import android.icu.lang.UProperty;
20import android.icu.text.ComposedCharIter;
21import android.icu.text.Normalizer;
22import android.icu.text.StringCharacterIterator;
23import android.icu.testsharding.MainTestShard;
24
25@MainTestShard
26@RunWith(JUnit4.class)
27public class TestDeprecatedNormalizerAPI extends TestFmwk
28{
29    public TestDeprecatedNormalizerAPI() {
30    }
31
32    @Test
33    public void TestNormalizerAPI(){
34         // instantiate a Normalizer from a CharacterIterator
35        String s=Utility.unescape("a\u0308\uac00\\U0002f800");
36        // make s a bit longer and more interesting
37        java.text.CharacterIterator iter = new StringCharacterIterator(s+s);
38        //test deprecated constructors
39        Normalizer norm = new Normalizer(iter, Normalizer.NFC,0);
40        if(norm.next()!=0xe4) {
41            errln("error in Normalizer(CharacterIterator).next()");
42        }
43        Normalizer norm2 = new Normalizer(s,Normalizer.NFC,0);
44        if(norm2.next()!=0xe4) {
45            errln("error in Normalizer(CharacterIterator).next()");
46        }
47        // test clone(), ==, and hashCode()
48        Normalizer clone=(Normalizer)norm.clone();
49        if(clone.getBeginIndex()!= norm.getBeginIndex()){
50           errln("error in Normalizer.getBeginIndex()");
51        }
52
53        if(clone.getEndIndex()!= norm.getEndIndex()){
54           errln("error in Normalizer.getEndIndex()");
55        }
56        // test setOption() and getOption()
57        clone.setOption(0xaa0000, true);
58        clone.setOption(0x20000, false);
59        if(clone.getOption(0x880000) ==0|| clone.getOption(0x20000)==1) {
60           errln("error in Normalizer::setOption() or Normalizer::getOption()");
61        }
62        //test deprecated normalize method
63        Normalizer.normalize(s,Normalizer.NFC,0);
64        //test deprecated compose method
65        Normalizer.compose(s,false,0);
66        //test deprecated decompose method
67        Normalizer.decompose(s,false,0);
68
69    }
70
71    /**
72     * Run through all of the characters returned by a composed-char iterator
73     * and make sure that:
74     * <ul>
75     * <li>a) They do indeed have decompositions.
76     * <li>b) The decomposition according to the iterator is the same as
77     *          returned by Normalizer.decompose().
78     * <li>c) All characters <em>not</em> returned by the iterator do not
79     *          have decompositions.
80     * </ul>
81     */
82    @Test
83    public void TestComposedCharIter() {
84        doTestComposedChars(false);
85    }
86
87    private void doTestComposedChars(boolean compat) {
88        int options = Normalizer.IGNORE_HANGUL;
89        ComposedCharIter iter = new ComposedCharIter(compat, options);
90
91        char lastChar = 0;
92
93        while (iter.hasNext()) {
94            char ch = iter.next();
95
96            // Test all characters between the last one and this one to make
97            // sure that they don't have decompositions
98            assertNoDecomp(lastChar, ch, compat, options);
99            lastChar = ch;
100
101            // Now make sure that the decompositions for this character
102            // make sense
103            String chString   = new StringBuffer().append(ch).toString();
104            String iterDecomp = iter.decomposition();
105            String normDecomp = Normalizer.decompose(chString, compat);
106
107            if (iterDecomp.equals(chString)) {
108                errln("ERROR: " + hex(ch) + " has identical decomp");
109            }
110            else if (!iterDecomp.equals(normDecomp)) {
111                errln("ERROR: Normalizer decomp for " + hex(ch) + " (" + hex(normDecomp) + ")"
112                    + " != iter decomp (" + hex(iterDecomp) + ")" );
113            }
114        }
115        assertNoDecomp(lastChar, '\uFFFF', compat, options);
116    }
117
118    void assertNoDecomp(char start, char limit, boolean compat, int options)
119    {
120        for (char x = ++start; x < limit; x++) {
121            String xString   = new StringBuffer().append(x).toString();
122            String decomp = Normalizer.decompose(xString, compat);
123            if (!decomp.equals(xString)) {
124                errln("ERROR: " + hex(x) + " has decomposition (" + hex(decomp) + ")"
125                    + " but was not returned by iterator");
126            }
127        }
128    }
129
130
131    @Test
132    public void TestRoundTrip() {
133        int options = Normalizer.IGNORE_HANGUL;
134        boolean compat = false;
135
136        ComposedCharIter iter = new ComposedCharIter(false, options);
137        while (iter.hasNext()) {
138            final char ch = iter.next();
139
140            String chStr = String.valueOf(ch);
141            String decomp = iter.decomposition();
142            String comp = Normalizer.compose(decomp, compat);
143
144            if (UCharacter.hasBinaryProperty(ch, UProperty.FULL_COMPOSITION_EXCLUSION)) {
145                logln("Skipped excluded char " + hex(ch) + " (" + UCharacter.getName(ch) + ")" );
146                continue;
147            }
148
149            // Avoid disparaged characters
150            if (decomp.length() == 4) continue;
151
152            if (!comp.equals(chStr)) {
153                errln("ERROR: Round trip invalid: " + hex(chStr) + " --> " + hex(decomp)
154                    + " --> " + hex(comp));
155
156                errln("  char decomp is '" + decomp + "'");
157            }
158        }
159    }
160}
161