001: /********************************************************************
002: * COPYRIGHT:
003: * Copyright (c) 2002-2006, International Business Machines Corporation and
004: * others. All Rights Reserved.
005: ********************************************************************/
006:
007: /**
008: * UCAConformanceTest performs conformance tests defined in the data
009: * files. ICU ships with stub data files, as the whole test are too
010: * long. To do the whole test, download the test files.
011: */package com.ibm.icu.dev.test.collator;
012:
013: import java.io.BufferedReader;
014: import java.util.Locale;
015:
016: import com.ibm.icu.dev.test.TestFmwk;
017: import com.ibm.icu.dev.test.TestUtil;
018: import com.ibm.icu.text.CollationKey;
019: import com.ibm.icu.text.Collator;
020: import com.ibm.icu.text.RuleBasedCollator;
021: import com.ibm.icu.text.UTF16;
022:
023: public class UCAConformanceTest extends TestFmwk {
024:
025: /**
026: * @param args
027: */
028: public static void main(String[] args) {
029: new UCAConformanceTest().run(args);
030: }
031:
032: public UCAConformanceTest() {
033: }
034:
035: protected void init() throws Exception {
036: UCA = (RuleBasedCollator) Collator.getInstance(new Locale(
037: "root", "", ""));
038:
039: comparer = new UTF16.StringComparator(true, false,
040: UTF16.StringComparator.FOLD_CASE_DEFAULT);
041: }
042:
043: RuleBasedCollator UCA;
044: RuleBasedCollator rbUCA;
045: UTF16.StringComparator comparer;
046:
047: public void TestTableNonIgnorable() {
048: setCollNonIgnorable(UCA);
049: openTestFile("NON_IGNORABLE");
050: conformanceTest(UCA);
051: }
052:
053: public void TestTableShifted() {
054: setCollShifted(UCA);
055: openTestFile("SHIFTED");
056: conformanceTest(UCA);
057: }
058:
059: public void TestRulesNonIgnorable() {
060: initRbUCA();
061:
062: setCollNonIgnorable(rbUCA);
063: openTestFile("NON_IGNORABLE");
064: conformanceTest(rbUCA);
065: }
066:
067: public void TestRulesShifted() {
068: logln("This test is currently disabled, as it is impossible to "
069: + "wholly represent fractional UCA using tailoring rules.");
070: return;
071: /* initRbUCA();
072:
073: if(U_SUCCESS(status)) {
074: setCollShifted(rbUCA);
075: openTestFile("SHIFTED");
076: testConformance(rbUCA);
077: }
078: */
079: }
080:
081: BufferedReader in;
082:
083: private void openTestFile(String type) {
084: String collationTest = "CollationTest_";
085: String ext = ".txt";
086: try {
087: if (in != null) {
088: in.close();
089: }
090: } catch (Exception e) {
091: errln("Could not close the opened file!");
092: return;
093: }
094: try {
095: in = TestUtil.getDataReader(collationTest + type + ext);
096: } catch (Exception e) {
097: try {
098: in = TestUtil.getDataReader(collationTest + type
099: + "_SHORT" + ext);
100: } catch (Exception e1) {
101: try {
102: in = TestUtil.getDataReader(collationTest + type
103: + "_STUB" + ext);
104: logln("INFO: Working with the stub file.\n"
105: + "If you need the full conformance test, please\n"
106: + "download the appropriate data files from:\n"
107: + "http://dev.icu-project.org/cgi-bin/viewcvs.cgi/unicodetools/com/ibm/text/data/");
108: } catch (Exception e11) {
109: errln("ERROR: Could not find any of the test files");
110: }
111: }
112: }
113: }
114:
115: private void setCollNonIgnorable(RuleBasedCollator coll) {
116: if (coll != null) {
117: coll
118: .setDecomposition(RuleBasedCollator.CANONICAL_DECOMPOSITION);
119: coll.setLowerCaseFirst(false);
120: coll.setCaseLevel(false);
121: coll.setStrength(RuleBasedCollator.TERTIARY);
122: coll.setAlternateHandlingShifted(false);
123: }
124: }
125:
126: private void setCollShifted(RuleBasedCollator coll) {
127: if (coll != null) {
128: coll
129: .setDecomposition(RuleBasedCollator.CANONICAL_DECOMPOSITION);
130: coll.setLowerCaseFirst(false);
131: coll.setCaseLevel(false);
132: coll.setStrength(RuleBasedCollator.QUATERNARY);
133: coll.setAlternateHandlingShifted(true);
134: }
135: }
136:
137: private void initRbUCA() {
138: /* if(!rbUCA) {
139: UParseError parseError;
140: UChar *ucarules = buffer;
141: int32_t size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules,
142: BUFFER_SIZE_);
143: if (size > BUFFER_SIZE_) {
144: ucarules = (UChar *)malloc(size * sizeof(UChar));
145: size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules, size);
146: }
147: rbUCA = ucol_openRules(ucarules, size, UCOL_DEFAULT, UCOL_TERTIARY,
148: &parseError, &status);
149: if (U_FAILURE(status)) {
150: errln("Failure creating UCA rule-based collator: %s", u_errorName(status));
151: return;
152: }
153: }
154: */
155: }
156:
157: private String parseString(String line) {
158: int i = 0, value;
159: StringBuffer result = new StringBuffer(), buffer = new StringBuffer();
160:
161: for (;;) {
162: while (Character.isWhitespace(line.charAt(i))) {
163: i++;
164: }
165: if (line.charAt(i) == ';' || i == line.length()) {
166: return result.toString();
167: }
168: while (Character.isLetterOrDigit(line.charAt(i))) {
169: buffer.append(line.charAt(i));
170: i++;
171: }
172: /* read one code point */
173: value = Integer.parseInt(buffer.toString(), 16);
174: buffer.delete(0, buffer.length());
175:
176: UTF16.append(result, value);
177: }
178:
179: }
180:
181: private void conformanceTest(RuleBasedCollator coll) {
182: if (in == null || coll == null) {
183: return;
184: }
185:
186: int lineNo = 0;
187:
188: String line = null, oldLine = null, buffer = null, oldB = null;
189: CollationKey oldSk = null, newSk = null;
190:
191: int res = 0, cmpres = 0, cmpres2 = 0;
192:
193: try {
194: while ((line = in.readLine()) != null) {
195: lineNo++;
196: if (line.length() < 3 || line.charAt(0) == '#') {
197: continue;
198: }
199: buffer = parseString(line);
200:
201: newSk = coll.getCollationKey(buffer);
202: if (oldSk != null) {
203: res = oldSk.compareTo(newSk);
204: cmpres = coll.compare(oldB, buffer);
205: cmpres2 = coll.compare(buffer, oldB);
206:
207: if (cmpres != -cmpres2) {
208: errln("Compare result not symmetrical on line "
209: + lineNo);
210: }
211: if (((res & 0x80000000) != (cmpres & 0x80000000))
212: || (res == 0 && cmpres != 0)
213: || (res != 0 && cmpres == 0)) {
214: errln("Difference between ucol_strcoll and sortkey compare on line "
215: + lineNo);
216: logln(oldLine);
217: logln(line);
218: }
219:
220: if (res > 0) {
221: errln("Line "
222: + lineNo
223: + " is not greater or equal than previous line");
224: logln(oldLine);
225: logln(line);
226: cmpres = coll.compare(oldB, buffer);
227: } else if (res == 0) { // equal
228: res = comparer.compare(oldB, buffer);
229: if (res == 0) {
230: errln("Probable error in test file on line "
231: + lineNo
232: + " (comparing identical strings)");
233: logln(oldLine);
234: logln(line);
235: } else if (res > 0) {
236: errln("Sortkeys are identical, but code point comapare gives >0 on line "
237: + lineNo);
238: logln(oldLine);
239: logln(line);
240: }
241: }
242: }
243:
244: oldSk = newSk;
245: oldB = buffer;
246: oldLine = line;
247: }
248: } catch (Exception e) {
249: errln("Unexpected exception " + e);
250: }
251: }
252:
253: }
|