1 /********************************************************************
\r
3 * Copyright (c) 2002-2009, International Business Machines Corporation and
\r
4 * others. All Rights Reserved.
\r
5 ********************************************************************/
\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
13 package com.ibm.icu.dev.test.collator;
\r
15 import java.io.BufferedReader;
\r
16 import java.util.Locale;
\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
25 public class UCAConformanceTest extends TestFmwk {
\r
30 public static void main(String[] args) {
\r
31 new UCAConformanceTest().run(args);
\r
34 public UCAConformanceTest() {
\r
36 protected void init()throws Exception{
\r
37 UCA = (RuleBasedCollator)Collator.getInstance(
\r
38 new Locale("root", "", ""));
\r
40 comparer = new UTF16.StringComparator(true, false, UTF16.StringComparator.FOLD_CASE_DEFAULT);
\r
42 RuleBasedCollator UCA;
\r
43 RuleBasedCollator rbUCA;
\r
44 UTF16.StringComparator comparer;
\r
46 public void TestTableNonIgnorable() {
\r
47 setCollNonIgnorable(UCA);
\r
48 openTestFile("NON_IGNORABLE");
\r
49 conformanceTest(UCA);
\r
52 public void TestTableShifted() {
\r
53 setCollShifted(UCA);
\r
54 openTestFile("SHIFTED");
\r
55 conformanceTest(UCA);
\r
58 public void TestRulesNonIgnorable() {
\r
61 setCollNonIgnorable(rbUCA);
\r
62 openTestFile("NON_IGNORABLE");
\r
63 conformanceTest(rbUCA);
\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
72 if(U_SUCCESS(status)) {
\r
73 setCollShifted(rbUCA);
\r
74 openTestFile("SHIFTED");
\r
75 testConformance(rbUCA);
\r
80 private void openTestFile(String type)
\r
82 String collationTest = "CollationTest_";
\r
83 String ext = ".txt";
\r
88 } catch (Exception e) {
\r
89 errln("Could not close the opened file!");
\r
93 in = TestUtil.getDataReader(collationTest+type+ext);
\r
94 } catch (Exception e) {
\r
96 in = TestUtil.getDataReader(collationTest+type+"_SHORT"+ext);
\r
97 } catch (Exception e1) {
\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
111 private void setCollNonIgnorable(RuleBasedCollator coll)
\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
122 private void setCollShifted(RuleBasedCollator coll)
\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
135 private void initRbUCA()
\r
138 UParseError parseError;
\r
139 UChar *ucarules = buffer;
\r
140 int32_t size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules,
\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
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
156 private String parseString(String line) {
\r
158 StringBuilder result = new StringBuilder(), buffer = new StringBuilder();
\r
161 while(i < line.length() && Character.isWhitespace(line.charAt(i))) {
\r
164 while(i < line.length() && Character.isLetterOrDigit(line.charAt(i))) {
\r
165 buffer.append(line.charAt(i));
\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
173 /* read one code point */
\r
174 value = Integer.parseInt(buffer.toString(), 16);
\r
175 buffer.setLength(0);
\r
176 result.appendCodePoint(value);
\r
180 private void conformanceTest(RuleBasedCollator coll) {
\r
181 if(in == null || coll == null) {
\r
187 String line = null, oldLine = null, buffer = null, oldB = null;
\r
188 CollationKey oldSk = null, newSk = null;
\r
190 int res = 0, cmpres = 0, cmpres2 = 0;
\r
193 while ((line = in.readLine()) != null) {
\r
195 if(line.length() < 3 || line.charAt(0) == '#') {
\r
198 buffer = parseString(line);
\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
206 if(cmpres != -cmpres2) {
\r
207 errln("Compare result not symmetrical on line "+lineNo);
\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
216 errln("Line " + lineNo + " is not greater or equal than previous line");
\r
219 cmpres = coll.compare(oldB, buffer);
\r
220 } else if(res == 0) { // equal
\r
221 res = comparer.compare(oldB, buffer);
\r
223 errln("Probable error in test file on line " + lineNo +" (comparing identical strings)");
\r
226 } else if (res > 0) {
\r
227 errln("Sortkeys are identical, but code point comapare gives >0 on line " + lineNo);
\r
238 } catch (Exception e) {
\r
239 errln("Unexpected exception "+e);
\r