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