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