]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/collate/src/com/ibm/icu/dev/test/collator/UCAConformanceTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / collate / src / com / ibm / icu / dev / test / collator / UCAConformanceTest.java
1 /********************************************************************
2  * COPYRIGHT: 
3  * Copyright (c) 2002-2010, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  ********************************************************************/
6
7 /**
8  * UCAConformanceTest performs conformance tests defined in the data
9  * files. ICU ships with stub data files, as the whole test are too 
10  * long. To do the whole test, download the test files.
11  */
12
13 package com.ibm.icu.dev.test.collator;
14
15 import java.io.BufferedReader;
16 import java.util.Locale;
17
18 import com.ibm.icu.dev.test.TestFmwk;
19 import com.ibm.icu.dev.test.TestUtil;
20 import com.ibm.icu.text.CollationKey;
21 import com.ibm.icu.text.Collator;
22 import com.ibm.icu.text.RuleBasedCollator;
23 import com.ibm.icu.text.UTF16;
24
25 public class UCAConformanceTest extends TestFmwk {
26
27     /**
28      * @param args
29      */
30     public static void main(String[] args) {
31         new UCAConformanceTest().run(args);
32     }
33
34     public UCAConformanceTest() {
35     }
36     protected void init()throws Exception{
37         UCA = (RuleBasedCollator)Collator.getInstance(
38                 new Locale("root", "", ""));
39
40         comparer = new UTF16.StringComparator(true, false, UTF16.StringComparator.FOLD_CASE_DEFAULT);
41     }
42     RuleBasedCollator UCA;
43     RuleBasedCollator rbUCA;
44     UTF16.StringComparator comparer;
45
46     public void TestTableNonIgnorable() {
47         setCollNonIgnorable(UCA);
48         openTestFile("NON_IGNORABLE");
49         conformanceTest(UCA);
50     }
51
52     public void TestTableShifted() {
53         setCollShifted(UCA);
54         openTestFile("SHIFTED");
55         conformanceTest(UCA);
56     }
57
58     public void TestRulesNonIgnorable() {
59         initRbUCA();
60
61         setCollNonIgnorable(rbUCA);
62         openTestFile("NON_IGNORABLE");
63         conformanceTest(rbUCA);
64     }
65
66     public void TestRulesShifted() {
67         logln("This test is currently disabled, as it is impossible to "+
68         "wholly represent fractional UCA using tailoring rules.");
69         return;
70         /*        initRbUCA();
71
72          if(U_SUCCESS(status)) {
73          setCollShifted(rbUCA);
74          openTestFile("SHIFTED");
75          testConformance(rbUCA);
76          }
77          */
78     }
79     BufferedReader in;
80     private void openTestFile(String type)
81     {
82         String collationTest = "CollationTest_";
83         String ext = ".txt";
84         try {
85             if(in != null) {
86                 in.close();
87             }
88         } catch (Exception e) {
89             errln("Could not close the opened file!");
90             return;
91         }
92         try {
93             in = TestUtil.getDataReader(collationTest+type+ext);
94         } catch (Exception e) {
95             try {
96                 in = TestUtil.getDataReader(collationTest+type+"_SHORT"+ext);
97             } catch (Exception e1) {
98                 try {
99                     in = TestUtil.getDataReader(collationTest+type+"_STUB"+ext);
100                     logln( "INFO: Working with the stub file.\n"+
101                             "If you need the full conformance test, please\n"+
102                             "download the appropriate data files from:\n"+
103                     "http://source.icu-project.org/repos/icu/tools/trunk/unicodetools/com/ibm/text/data/");
104                 } catch (Exception e11) {
105                     errln("ERROR: Could not find any of the test files");
106                 }
107             }
108         }
109     }          
110
111     private void setCollNonIgnorable(RuleBasedCollator coll) 
112     {
113         if(coll != null) {
114             coll.setDecomposition(RuleBasedCollator.CANONICAL_DECOMPOSITION);
115             coll.setLowerCaseFirst(false);
116             coll.setCaseLevel(false);
117             coll.setStrength(RuleBasedCollator.TERTIARY);
118             coll.setAlternateHandlingShifted(false);
119         }
120     }
121
122     private void setCollShifted(RuleBasedCollator coll) 
123     {
124         if(coll != null) {
125             coll.setDecomposition(RuleBasedCollator.CANONICAL_DECOMPOSITION);
126             coll.setLowerCaseFirst(false);
127             coll.setCaseLevel(false);
128             coll.setStrength(RuleBasedCollator.QUATERNARY);
129             coll.setAlternateHandlingShifted(true);
130         }
131     }
132
133
134
135     private void initRbUCA() 
136     {
137         /*      if(!rbUCA) {
138          UParseError parseError;
139          UChar      *ucarules = buffer;
140          int32_t size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules, 
141          BUFFER_SIZE_);
142          if (size > BUFFER_SIZE_) {
143          ucarules = (UChar *)malloc(size * sizeof(UChar));
144          size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules, size);
145          }
146          rbUCA = ucol_openRules(ucarules, size, UCOL_DEFAULT, UCOL_TERTIARY, 
147          &parseError, &status);
148          if (U_FAILURE(status)) {
149          errln("Failure creating UCA rule-based collator: %s", u_errorName(status));
150          return;
151          }
152          }
153          */
154     }
155
156     private String parseString(String line) {
157         int i = 0, value;
158         StringBuilder result = new StringBuilder(), buffer = new StringBuilder();
159
160         for(;;) {
161             while(i < line.length() && Character.isWhitespace(line.charAt(i))) {
162                 i++;
163             }
164             while(i < line.length() && Character.isLetterOrDigit(line.charAt(i))) {
165                 buffer.append(line.charAt(i));
166                 i++;
167             }
168             if(buffer.length() == 0) {
169                 // We hit something that was not whitespace/letter/digit.
170                 // Should be ';' or end of string.
171                 return result.toString();
172             }
173             /* read one code point */
174             value = Integer.parseInt(buffer.toString(), 16);
175             buffer.setLength(0);
176             result.appendCodePoint(value);
177         }
178
179     }
180     private void conformanceTest(RuleBasedCollator coll) {
181         if(in == null || coll == null) {
182             return;
183         }
184
185         int lineNo = 0;
186
187         String line = null, oldLine = null, buffer = null, oldB = null;
188         CollationKey oldSk = null, newSk = null;
189
190         int res = 0, cmpres = 0, cmpres2 = 0;
191
192         try {
193             while ((line = in.readLine()) != null) {
194                 lineNo++;
195                 if(line.length() < 3 || line.charAt(0) == '#') {
196                     continue;
197                 }
198                 buffer = parseString(line);
199
200                 newSk = coll.getCollationKey(buffer);
201                 if(oldSk != null) {
202                     res = oldSk.compareTo(newSk);
203                     cmpres = coll.compare(oldB, buffer);
204                     cmpres2 = coll.compare(buffer, oldB);
205
206                     if(cmpres != -cmpres2) {
207                         errln("Compare result not symmetrical on line "+lineNo);
208                     }
209                     if(((res&0x80000000) != (cmpres&0x80000000)) || (res == 0 && cmpres != 0) || (res != 0 && cmpres == 0)) {
210                         errln("Difference between ucol_strcoll and sortkey compare on line " + lineNo);
211                         logln(oldLine);
212                         logln(line);
213                     }
214
215                     if(res > 0) {
216                         errln("Line " + lineNo + " is not greater or equal than previous line");
217                         logln(oldLine);
218                         logln(line);
219                         cmpres = coll.compare(oldB, buffer);
220                     } else if(res == 0) {  // equal 
221                         res = comparer.compare(oldB, buffer);
222                         if (res == 0) {
223                             errln("Probable error in test file on line " + lineNo +" (comparing identical strings)");
224                             logln(oldLine);
225                             logln(line);
226                         } else if (res > 0) {
227                             errln("Sortkeys are identical, but code point comapare gives >0 on line " + lineNo);
228                             logln(oldLine);
229                             logln(line);
230                         }
231                     }
232                 }
233
234                 oldSk = newSk;
235                 oldB = buffer;
236                 oldLine = line;
237             }
238         } catch (Exception e) {
239             errln("Unexpected exception "+e);
240         }
241     }
242
243 }