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