Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / i18n / _XCharacterClassification.java
blobc11b2360b8830b4c86e8e71fa1bcafb9d27f7a6d
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.i18n;
21 import lib.MultiMethodTest;
23 import com.sun.star.i18n.KParseTokens;
24 import com.sun.star.i18n.KParseType;
25 import com.sun.star.i18n.ParseResult;
26 import com.sun.star.i18n.XCharacterClassification;
27 import com.sun.star.lang.Locale;
29 /**
30 * Testing <code>com.sun.star.i18n.XCharacterClassification</code>
31 * interface methods:
32 * <ul>
33 * <li><code> toUpper() </code></li>
34 * <li><code> toLower() </code></li>
35 * <li><code> toTitle() </code></li>
36 * <li><code> getType() </code></li>
37 * <li><code> getCharacterType() </code></li>
38 * <li><code> getStringType() </code></li>
39 * <li><code> getCharacterDirection() </code></li>
40 * <li><code> getScript() </code></li>
41 * <li><code> parseAnyToken() </code></li>
42 * <li><code> parsePredefinedToken() </code></li>
43 * </ul><p>
44 * Test is <b> NOT </b> multithread compliant. <p>
45 * @see com.sun.star.i18n.XCharacterClassification
47 public class _XCharacterClassification extends MultiMethodTest {
48 public XCharacterClassification oObj = null;
49 public String[] languages = new String[]{"de","en","es","fr","ja","ko","zh"};
50 public String[] countries = new String[]{"DE","US","ES","FR","JP","KR","CN"};
52 public String[] charstyles_java = new String[] {"UNASSIGNED","UPPERCASE_LETTER",
53 "LOWERCASE_LETTER","TITLECASE_LETTER","MODIFIER_LETTER","OTHER_LETTER",
54 "NON_SPACING_MARK","ENCLOSING_MARK","COMBINING_SPACING_MARK",
55 "DECIMAL_DIGIT_NUMBER","LETTER_NUMBER","OTHER_NUMBER","SPACE_SEPARATOR",
56 "LINE_SEPARATOR","PARAGRAPH_SEPARATOR","CONTROL","FORMAT","none17",
57 "PRIVATE_USE","none19","DASH_PUNCTUATION","START_PUNCTUATION","END_PUNCTUATION",
58 "CONNECTOR_PUNCTUATION","OTHER_PUNCTUATION","MATH_SYMBOL","CURRENCY_SYMBOL",
59 "MODIFIER_SYMBOL","OTHER_SYMBOL"};
61 public String[] charstyles_office = new String[] {"UNASSIGNED","UPPERCASE_LETTER",
62 "LOWERCASE_LETTER","TITLECASE_LETTER","MODIFIER_LETTER","OTHER_LETTER",
63 "NON_SPACING_MARK","ENCLOSING_MARK","COMBINING_SPACING_MARK",
64 "DECIMAL_DIGIT_NUMBER","LETTER_NUMBER","OTHER_NUMBER","SPACE_SEPARATOR",
65 "LINE_SEPARATOR","PARAGRAPH_SEPARATOR","CONTROL","FORMAT","PRIVATE_USE",
66 "OTHER_PUNCTUATION","DASH_PUNCTUATION","START_PUNCTUATION","END_PUNCTUATION",
67 "CONNECTOR_PUNCTUATION",
68 "OTHER_PUNCTUATION","MATH_SYMBOL","CURRENCY_SYMBOL","MODIFIER_SYMBOL",
69 "OTHER_SYMBOL","INITIAL_PUNCTUATION","FINAL_PUNCTUATION","GENERAL_TYPES_COUNT"};
71 public String[] unicode_script = new String[] {"U_BASIC_LATIN","U_LATIN_1_SUPPLEMENT",
72 "U_LATIN_EXTENDED_A","U_LATIN_EXTENDED_B","U_IPA_EXTENSIONS","U_SPACING_MODIFIER_LETTERS",
73 "U_COMBINING_DIACRITICAL_MARKS","U_GREEK","U_CYRILLIC","U_ARMENIAN","U_HEBREW",
74 "U_ARABIC","U_SYRIAC","U_THAANA","U_DEVANAGARI","U_BENGALI","U_GURMUKHI",
75 "U_GUJARATI","U_ORIYA","U_TAMIL","U_TELUGU","U_KANNADA","U_MALAYALAM",
76 "U_SINHALA","U_THAI","U_LAO","U_TIBETAN","U_MYANMAR","U_GEORGIAN",
77 "U_HANGUL_JAMO","U_ETHIOPIC","U_CHEROKEE","U_UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS",
78 "U_OGHAM","U_RUNIC","U_KHMER","U_MONGOLIAN","U_LATIN_EXTENDED_ADDITIONAL",
79 "U_GREEK_EXTENDED","U_GENERAL_PUNCTUATION","U_SUPERSCRIPTS_AND_SUBSCRIPTS",
80 "U_CURRENCY_SYMBOLS","U_COMBINING_MARKS_FOR_SYMBOLS","U_LETTERLIKE_SYMBOLS",
81 "U_NUMBER_FORMS","U_ARROWS","U_MATHEMATICAL_OPERATORS","U_MISCELLANEOUS_TECHNICAL",
82 "U_CONTROL_PICTURES","U_OPTICAL_CHARACTER_RECOGNITION","U_ENCLOSED_ALPHANUMERICS",
83 "U_BOX_DRAWING","U_BLOCK_ELEMENTS","U_GEOMETRIC_SHAPES","U_MISCELLANEOUS_SYMBOLS",
84 "U_DINGBATS","U_BRAILLE_PATTERNS","U_CJK_RADICALS_SUPPLEMENT","U_KANGXI_RADICALS",
85 "U_IDEOGRAPHIC_DESCRIPTION_CHARACTERS","U_CJK_SYMBOLS_AND_PUNCTUATION",
86 "U_HIRAGANA","U_KATAKANA","U_BOPOMOFO","U_HANGUL_COMPATIBILITY_JAMO","U_KANBUN",
87 "U_BOPOMOFO_EXTENDED","U_ENCLOSED_CJK_LETTERS_AND_MONTHS","U_CJK_COMPATIBILITY",
88 "U_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A","U_CJK_UNIFIED_IDEOGRAPHS","U_YI_SYLLABLES",
89 "U_YI_RADICALS","U_HANGUL_SYLLABLES","U_HIGH_SURROGATES","U_HIGH_PRIVATE_USE_SURROGATES",
90 "U_LOW_SURROGATES","U_PRIVATE_USE_AREA","U_CJK_COMPATIBILITY_IDEOGRAPHS",
91 "U_ALPHABETIC_PRESENTATION_FORMS","U_ARABIC_PRESENTATION_FORMS_A","U_COMBINING_HALF_MARKS",
92 "U_CJK_COMPATIBILITY_FORMS","U_SMALL_FORM_VARIANTS","U_ARABIC_PRESENTATION_FORMS_B",
93 "U_SPECIALS","U_HALFWIDTH_AND_FULLWIDTH_FORMS","U_CHAR_SCRIPT_COUNT","U_NO_SCRIPT"};
95 /**
96 * Test calls the method for different locales. Then each result is compared
97 * with a string, converted to a upper case using
98 * <code>String</code> method <code>toUpperCase()</code>.<p>
99 * Has <b> OK </b> status if string, returned by the method is equal to
100 * a string that is returned by String.toUpperCase() for all locales.
102 public void _toUpper() {
103 boolean res = true;
104 char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404};
105 String toCheck = new String(characters);
106 String get = "";
107 String exp = "";
109 for (int i=0;i<7;i++) {
110 get = oObj.toUpper(toCheck, 0, toCheck.length(), getLocale(i));
111 exp = toCheck.toUpperCase(
112 new java.util.Locale(languages[i], countries[i]));
113 res &= get.equals(exp);
114 if (!res) {
115 log.println("FAILED for: language=" + languages[i] +
116 " ; country=" + countries[i]);
117 log.println("Expected: " + exp);
118 log.println("Gained : " + get);
121 tRes.tested("toUpper()", res);
125 * Test calls the method for different locales. Then each result is compared
126 * with a string, converted to a lower case using
127 * <code>String</code> method <code>toLowerCase()</code>.<p>
128 * Has <b> OK </b> status if string, returned by the method is equal to
129 * a string that is returned by String.toLowerCase() for all locales.
131 public void _toLower() {
132 boolean res = true;
133 char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404};
134 String toCheck = new String(characters);
135 String get = "";
136 String exp = "";
138 for (int i=0;i<7;i++) {
139 get = oObj.toLower(toCheck,0,toCheck.length(),getLocale(i));
140 exp = toCheck.toLowerCase(
141 new java.util.Locale(languages[i],countries[i]));
142 res &= get.equals(exp);
143 if (!res) {
144 log.println("FAILED for: language=" + languages[i]
145 + " ; country=" + countries[i]);
146 log.println("Expected: " + exp);
147 log.println("Gained : " + get);
150 tRes.tested("toLower()", res);
154 * Test calls the method for different locales. Then each result is compared
155 * with a string, converted to a title case using
156 * <code>java.lang.Character</code> method <code>toTitleCase()</code>.<p>
157 * Has <b> OK </b> status if string, returned by the method is equal to
158 * a string that was converted using Character.toTitleCase() for all locales.
160 public void _toTitle() {
161 boolean res = true;
162 String toCheck = new String(new char[]{8112});
163 String get = "";
164 String exp = "";
166 for (int i=0;i<7;i++) {
167 get = oObj.toTitle(toCheck, 0, 1, getLocale(i));
168 exp = new String(
169 new char[]{Character.toTitleCase(toCheck.toCharArray()[0])});
170 res &= get.equals(exp);
171 if (!res) {
172 log.println("FAILED for: language=" + languages[i]
173 + " ; country=" + countries[i]);
174 log.println("Expected: " + exp);
175 log.println("Gained : " + get);
178 tRes.tested("toTitle()", res);
182 * At first we define <code>int[]</code> and <code>char[]</code> arrays of
183 * unicode symbol numbers, arranged as sequences, where symbols are sorted
184 * by type, so the character of <code>i<sup><small>th</small></sup></code>
185 * type is located on <code>i<sup><small>th</small></sup></code> position.<p>
186 * Has <b> OK </b> status if for all 30 types the method returns value, that
187 * is equal to an element number.<p>
188 * @see com.sun.star.i18n.CharType
190 public void _getType() {
191 boolean res = true;
192 char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404,
193 48,8544,179,32,8232,8233,144,8204,57344,56320,173,40,41,95,3852,247,
194 3647,901,3896,171,187};
195 int[] charsInt = new int[]{586,65,97,498,721,4588,772,8413,3404,48,
196 8544,179,32,8232,8233,144,8204,57344,56320,173,40,41,95,3852,247,
197 3647,901,3896,171,187};
198 String toCheck = new String(characters);
200 for (int i=0;i<characters.length;i++) {
201 int get = oObj.getType(toCheck, i);
202 res &= charstyles_office[get].equals(charstyles_office[i]);
203 if (!res) {
204 log.println("Code :" + Integer.toHexString(charsInt[i]));
205 log.println("Gained: " + charstyles_office[get]);
206 log.println("Expected : " + charstyles_office[i]);
209 tRes.tested("getType()", res);
213 * After defining string to be checked and array of expected types, test
214 * calls the method for each character of a string and for all locales.<p>
215 * Has <b> OK </b> status if the method returns type, expected for a given
216 * character and locale.
218 public void _getCharacterType() {
219 boolean res = true;
220 String toCheck = "Ab0)";
221 int[] expected = new int[]{226,228,97,32};
223 for (int i=0;i<toCheck.length();i++) {
224 for (int j=1;j<7;j++) {
225 int get = oObj.getCharacterType(toCheck, i, getLocale(j));
226 res &= (get == expected[i]);
227 if (!res) {
228 log.println("FAILED for: language=" + languages[j] +
229 " ; country=" + countries[j]);
230 log.println("Sysmbol :" + toCheck.toCharArray()[i]);
231 log.println("Gained: " + get);
232 log.println("Expected : " + expected[i]);
236 tRes.tested("getCharacterType()", res);
240 * After defining array of strings to be checked and array of expected types,
241 * test calls the method for each string of an array and for all locales.<p>
242 * Has <b> OK </b> status if the method returns type, expected for a given
243 * string and locale.
245 public void _getStringType() {
246 boolean res = true;
247 String[] toCheck = new String[]{"01234","AAAAA","bbbbb","AA()bb"};
248 int[] exp = new int[]{97,226,228,230};
250 for (int j=0;j<toCheck.length;j++) {
251 for (int i=0;i<7;i++) {
252 int get = oObj.getStringType(toCheck[j], 0,
253 toCheck[j].length(), getLocale(i));
254 res &= (get == exp[j]);
255 if (!res) {
256 log.println("FAILED for: language=" + languages[i] +
257 " ; country=" + countries[i]);
258 log.println("Expected: " + exp[j]);
259 log.println("Gained : " + get);
263 tRes.tested("getStringType()", res);
267 * After string to be checked is initialized (all symbols are sorted
268 * by direction, so the character of <code>i<sup><small>th</small></sup></code>
269 * direction is located on <code>i<sup><small>th</small></sup></code>
270 * position), test calls the method for every character of that string. <p>
271 * Has <b> OK </b> status if the method returns direction, that's equal to
272 * a symbol position in the string.
274 public void _getCharacterDirection() {
275 boolean res = true;
276 String toCheck = new String(new char[]{65,1470,48,47,35,1632,44,10,
277 9,12,33,8234,8237,1563,8235,8238,8236,768,1});
278 for (short i=0;i<19;i++) {
279 short get = oObj.getCharacterDirection(toCheck, i);
280 res &= (get == i);
281 if (!res) {
282 log.println("Code :" + toCheck.toCharArray()[i]);
283 log.println("Gained: " + get);
284 log.println("Expected: " + i);
287 tRes.tested("getCharacterDirection()", res);
291 * At first we define <code>int[]</code> and <code>char[]</code> arrays of
292 * unicode symbol numbers, arranged as sequences, where symbols are sorted
293 * by type, so the character of <code>i<sup><small>th</small></sup></code>
294 * type is located on <code>i<sup><small>th</small></sup></code> position.<p>
295 * Has <b> OK </b> status if for each character method returns value, that
296 * is equal to a number where element is located in array. Also method has
297 * <b> OK </b> status for symbol with code 55296, because it doesn't work
298 * since it hasn't the right neighborhood.<p>
299 * @see "http://ppewww.ph.gla.ac.uk/~flavell/unicode/unidata.html"
301 public void _getScript() {
302 boolean res = true;
303 char[] characters = new char[]{65,128,256,384,592,750,773,924,1030,1331,1448,
304 1569,1792,1936,2313,2465,2570,2707,2822,2972,3079,3240,3337,3464,3590,
305 3745,3906,4097,4274,4357,4621,5040,5200,5776,5806,6030,6155,7683,7943,
306 8202,8319,8352,8413,8452,8545,8616,8715,8965,9217,9281,9336,9474,9608,9719,
307 9734,9999,10247,11911,12034,12274,12294,12358,12456,12552,12605,12688,12727,
308 12806,13065,13312,19968,40964,42152,44032,55296,56192,56320,57344,63744,
309 64257,64370,65056,65073,65131,65146,65532,65288};
310 int[] charsInt = new int[]{65,128,256,384,592,750,773,924,1030,1331,1448,
311 1569,1792,1936,2313,2465,2570,2707,2822,2972,3079,3240,3337,3464,3590,
312 3745,3906,4097,4274,4357,4621,5040,5200,5776,5806,6030,6155,7683,7943,
313 8202,8319,8352,8413,8452,8545,8616,8715,8965,9217,9281,9336,9474,9608,9719,
314 9734,9999,10247,11911,12034,12274,12294,12358,12456,12552,12605,12688,12727,
315 12806,13065,13312,19968,40964,42152,44032,55296,56192,56320,57344,63744,
316 64257,64370,65056,65073,65131,65146,65532,65288};
317 String toCheck = new String(characters);
319 for (int i=0;i<characters.length;i++) {
320 int get = oObj.getScript(toCheck, i);
321 res &= (get == i);
322 //The HIGH_SURROGATE 55296 doesn't work since it hasn't the right
323 //neighborhood
324 if (toCheck.substring(i, i + 1).hashCode() == 55296) res = true;
325 if (!res) {
326 log.println("-- " + toCheck.substring(i, i + 1).hashCode());
327 log.println("Code: " + Integer.toHexString(charsInt[i]));
328 log.println("Gained: " + unicode_script[get]);
329 log.println("Expected: " + unicode_script[i]);
332 tRes.tested("getScript()", res);
336 * After defining a string to be parsed and parse conditions (flags), test
337 * calls the method for different locales three times with different parameters,
338 * checking result after every call. <p>
339 * Has <b> OK </b> status if the method returns right results all three
340 * times.
342 public void _parseAnyToken() {
343 int nStartFlags = KParseTokens.ANY_ALPHA | KParseTokens.ASC_UNDERSCORE;
344 int nContFlags = KParseTokens.ANY_ALNUM | KParseTokens.ASC_UNDERSCORE
345 | KParseTokens.ASC_DOT;
346 String toCheck = " 18 i18n ^";
347 ParseResult pRes = null;
348 boolean res = true;
350 for (int i=0;i<7;i++) {
351 pRes = oObj.parseAnyToken(toCheck, 1, getLocale(i),
352 nStartFlags, "", nContFlags, "");
353 res = ( (pRes.CharLen==2)
354 && (pRes.TokenType==32)
355 && (pRes.Value==18.0) );
356 pRes = oObj.parseAnyToken(toCheck, 4, getLocale(i),
357 nStartFlags, "", nContFlags, "");
358 res &= ( (pRes.CharLen==4)
359 && (pRes.TokenType==4)
360 && (pRes.Value==0.0) );
361 pRes = oObj.parseAnyToken(toCheck, 9, getLocale(i),
362 nStartFlags, "", nContFlags, "");
363 res &= ( (pRes.CharLen==1)
364 && (pRes.TokenType==1)
365 && (pRes.Value==0.0) );
367 tRes.tested("parseAnyToken()", res);
371 * After defining a string to be parsed and parse conditions (flags), test
372 * calls the method for different locales two times with different parameters,
373 * checking result after every call. <p>
374 * Has <b> OK </b> status if the method returns right results.
376 public void _parsePredefinedToken() {
377 int nStartFlags = KParseTokens.ANY_ALPHA | KParseTokens.ASC_UNDERSCORE;
378 int nContFlags = nStartFlags;
379 String toCheck = " 18 int";
380 ParseResult pRes = null;
381 boolean res = true;
383 for (int i=0;i<7;i++) {
384 pRes = oObj.parsePredefinedToken(KParseType.IDENTNAME, toCheck,
385 1, getLocale(i), nStartFlags, "", nContFlags, "");
386 res = (pRes.CharLen==0);
387 pRes = oObj.parsePredefinedToken(KParseType.IDENTNAME, toCheck,
388 4, getLocale(i), nStartFlags, "", nContFlags, "");
389 res &= ( (pRes.CharLen==3)
390 && (pRes.TokenType==4)
391 && (pRes.Value==0.0) );
393 tRes.tested("parsePredefinedToken()", res);
398 * Method returns locale for a given language and country.
399 * @param k index of needed locale.
401 private Locale getLocale(int k) {
402 return new Locale(languages[k],countries[k],"");
406 } // end XCharacterClassification