001: /**
002: *******************************************************************************
003: * Copyright (C) 1996-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.test.lang;
007:
008: import com.ibm.icu.lang.UScript;
009: import com.ibm.icu.util.ULocale;
010: import com.ibm.icu.dev.test.TestFmwk;
011:
012: import java.util.Locale;
013:
014: public class TestUScript extends TestFmwk {
015:
016: /**
017: * Constructor
018: */
019: public TestUScript() {
020: }
021:
022: public static void main(String[] args) throws Exception {
023: new TestUScript().run(args);
024: }
025:
026: public void TestLocaleGetCode() {
027: final ULocale[] testNames = {
028: /* test locale */
029: new ULocale("en"), new ULocale("en_US"), new ULocale("sr"),
030: new ULocale("ta"), new ULocale("te_IN"),
031: new ULocale("hi"), new ULocale("he"),
032: new ULocale("ar"), new ULocale("abcde"),
033: new ULocale("abcde_cdef"), new ULocale("iw") };
034: final int[] expected = {
035: /* locales should return */
036: UScript.LATIN, UScript.LATIN, UScript.CYRILLIC, UScript.TAMIL,
037: UScript.TELUGU, UScript.DEVANAGARI, UScript.HEBREW,
038: UScript.ARABIC, UScript.INVALID_CODE,
039: UScript.INVALID_CODE, UScript.HEBREW };
040: int i = 0;
041: int numErrors = 0;
042:
043: for (; i < testNames.length; i++) {
044: int[] code = UScript.getCode(testNames[i]);
045:
046: if (code == null) {
047: if (expected[i] != UScript.INVALID_CODE) {
048: logln("Error getting script code Got: null"
049: + " Expected: " + expected[i]
050: + " for name " + testNames[i]);
051: numErrors++;
052: }
053: // getCode returns null if the code could not be found
054: continue;
055: }
056: if ((code[0] != expected[i])) {
057: logln("Error getting script code Got: " + code[0]
058: + " Expected: " + expected[i] + " for name "
059: + testNames[i]);
060: numErrors++;
061: }
062: }
063: reportDataErrors(numErrors);
064:
065: //
066: ULocale defaultLoc = ULocale.getDefault();
067: ULocale esparanto = new ULocale("eo_DE");
068: ULocale.setDefault(esparanto);
069: int[] code = UScript.getCode(esparanto);
070: if (code != null) {
071: if (code[0] != UScript.LATIN) {
072: errln("Did not get the expected script code for Esparanto");
073: }
074: } else {
075: warnln("Could not load the locale data.");
076: }
077: ULocale.setDefault(defaultLoc);
078: }
079:
080: private void reportDataErrors(int numErrors) {
081: if (numErrors > 0) {
082: // assume missing locale data, so not an error, just a warning
083: if (isModularBuild() || noData()) {
084: // if nodata is set don't even warn
085: warnln("Could not find locale data");
086: } else {
087: errln("encountered " + numErrors + " errors.");
088: }
089: }
090: }
091:
092: public void TestMultipleCode() {
093: final String[] testNames = { "ja", "ko_KR", "zh", "zh_TW" };
094: final int[][] expected = {
095: { UScript.KATAKANA, UScript.HIRAGANA, UScript.HAN },
096: { UScript.HANGUL, UScript.HAN }, { UScript.HAN },
097: { UScript.HAN, UScript.BOPOMOFO } };
098:
099: int numErrors = 0;
100: for (int i = 0; i < testNames.length; i++) {
101: int[] code = UScript.getCode(testNames[i]);
102: int[] expt = (int[]) expected[i];
103: if (code != null) {
104: for (int j = 0; j < code.length; j++) {
105: if (code[j] != expt[j]) {
106: numErrors++;
107: logln("Error getting script code Got: "
108: + code[j] + " Expected: " + expt[j]
109: + " for name " + testNames[i]);
110: }
111: }
112: } else {
113: numErrors++;
114: logln("Error getting script code for name "
115: + testNames[i]);
116: }
117: }
118: reportDataErrors(numErrors);
119:
120: //cover UScript.getCode(Locale)
121: Locale[] testLocales = new Locale[] { Locale.JAPANESE,
122: Locale.KOREA, Locale.CHINESE, Locale.TAIWAN };
123: logln("Testing UScript.getCode(Locale) ...");
124: numErrors = 0;
125: for (int i = 0; i < testNames.length; i++) {
126: logln(" Testing locale: "
127: + testLocales[i].getDisplayName());
128: int[] code = UScript.getCode(testLocales[i]);
129: int[] expt = (int[]) expected[i];
130: if (code != null) {
131: for (int j = 0; j < code.length; j++) {
132: if (code[j] != expt[j]) {
133: numErrors++;
134: logln(" Error getting script code Got: "
135: + code[j] + " Expected: " + expt[j]
136: + " for name " + testNames[i]);
137: }
138: }
139: } else {
140: numErrors++;
141: logln(" Error getting script code for name "
142: + testNames[i]);
143: }
144: }
145: reportDataErrors(numErrors);
146: }
147:
148: public void TestGetCode() {
149:
150: final String[] testNames = {
151: /* test locale */
152: "en", "en_US", "sr", "ta", "gu", "te_IN", "hi", "he", "ar",
153: /* test abbr */
154: "Hani", "Hang", "Hebr", "Hira", "Knda", "Kana", "Khmr", "Lao",
155: "Latn",/*"Latf","Latg",*/
156: "Mlym", "Mong",
157:
158: /* test names */
159: "CYRILLIC", "DESERET", "DEVANAGARI", "ETHIOPIC",
160: "GEORGIAN", "GOTHIC", "GREEK", "GUJARATI", "COMMON",
161: "INHERITED",
162: /* test lower case names */
163: "malayalam", "mongolian", "myanmar", "ogham",
164: "old-italic", "oriya", "runic", "sinhala", "syriac",
165: "tamil", "telugu", "thaana", "thai", "tibetan",
166: /* test the bounds*/
167: "Cans", "arabic", "Yi", "Zyyy" };
168: final int[] expected = {
169: /* locales should return */
170: UScript.LATIN, UScript.LATIN, UScript.CYRILLIC, UScript.TAMIL,
171: UScript.GUJARATI, UScript.TELUGU, UScript.DEVANAGARI,
172: UScript.HEBREW, UScript.ARABIC,
173: /* abbr should return */
174: UScript.HAN, UScript.HANGUL, UScript.HEBREW,
175: UScript.HIRAGANA, UScript.KANNADA, UScript.KATAKANA,
176: UScript.KHMER, UScript.LAO, UScript.LATIN,/* UScript.LATIN, UScript.LATIN,*/
177: UScript.MALAYALAM, UScript.MONGOLIAN,
178: /* names should return */
179: UScript.CYRILLIC, UScript.DESERET, UScript.DEVANAGARI,
180: UScript.ETHIOPIC, UScript.GEORGIAN, UScript.GOTHIC,
181: UScript.GREEK, UScript.GUJARATI, UScript.COMMON,
182: UScript.INHERITED,
183: /* lower case names should return */
184: UScript.MALAYALAM, UScript.MONGOLIAN, UScript.MYANMAR,
185: UScript.OGHAM, UScript.OLD_ITALIC, UScript.ORIYA,
186: UScript.RUNIC, UScript.SINHALA, UScript.SYRIAC,
187: UScript.TAMIL, UScript.TELUGU, UScript.THAANA,
188: UScript.THAI, UScript.TIBETAN,
189: /* bounds */
190: UScript.CANADIAN_ABORIGINAL, UScript.ARABIC,
191: UScript.YI, UScript.COMMON };
192: int i = 0;
193: int numErrors = 0;
194:
195: for (; i < testNames.length; i++) {
196: int[] code = UScript.getCode(testNames[i]);
197: if (code == null) {
198: if (expected[i] == UScript.INVALID_CODE) {
199: // getCode returns null if the code could not be found
200: continue;
201: }
202: // currently commented out until jitterbug#2678 is fixed
203: logln("Error getting script code Got: null"
204: + " Expected: " + expected[i] + " for name "
205: + testNames[i]);
206: numErrors++;
207: continue;
208: }
209: if ((code[0] != expected[i])) {
210: logln("Error getting script code Got: " + code[0]
211: + " Expected: " + expected[i] + " for name "
212: + testNames[i]);
213: numErrors++;
214: }
215: }
216: reportDataErrors(numErrors);
217: }
218:
219: public void TestGetName() {
220:
221: final int[] testCodes = {
222: /* names should return */
223: UScript.CYRILLIC, UScript.DESERET, UScript.DEVANAGARI,
224: UScript.ETHIOPIC, UScript.GEORGIAN, UScript.GOTHIC,
225: UScript.GREEK, UScript.GUJARATI, };
226:
227: final String[] expectedNames = {
228:
229: /* test names */
230: "Cyrillic", "Deseret", "Devanagari", "Ethiopic", "Georgian",
231: "Gothic", "Greek", "Gujarati", };
232: int i = 0;
233: int numErrors = 0;
234: while (i < testCodes.length) {
235: String scriptName = UScript.getName(testCodes[i]);
236: if (!expectedNames[i].equals(scriptName)) {
237: logln("Error getting abbreviations Got: " + scriptName
238: + " Expected: " + expectedNames[i]);
239: numErrors++;
240: }
241: i++;
242: }
243: if (numErrors > 0) {
244: warnln("encountered " + numErrors
245: + " errors in UScript.getName()");
246: }
247:
248: }
249:
250: public void TestGetShortName() {
251: final int[] testCodes = {
252: /* abbr should return */
253: UScript.HAN, UScript.HANGUL, UScript.HEBREW, UScript.HIRAGANA,
254: UScript.KANNADA, UScript.KATAKANA, UScript.KHMER,
255: UScript.LAO, UScript.LATIN, UScript.MALAYALAM,
256: UScript.MONGOLIAN, };
257:
258: final String[] expectedAbbr = {
259: /* test abbr */
260: "Hani", "Hang", "Hebr", "Hira", "Knda", "Kana", "Khmr", "Laoo",
261: "Latn", "Mlym", "Mong", };
262: int i = 0;
263: int numErrors = 0;
264: while (i < testCodes.length) {
265: String shortName = UScript.getShortName(testCodes[i]);
266: if (!expectedAbbr[i].equals(shortName)) {
267: logln("Error getting abbreviations Got: " + shortName
268: + " Expected: " + expectedAbbr[i]);
269: numErrors++;
270: }
271: i++;
272: }
273: if (numErrors > 0) {
274: warnln("encountered " + numErrors
275: + " errors in UScript.getShortName()");
276: }
277: }
278:
279: public void TestGetScript() {
280: int codepoints[][] = new int[][] {
281: { 0x0000FF9D, UScript.KATAKANA },
282: { 0x0000FFBE, UScript.HANGUL },
283: { 0x0000FFC7, UScript.HANGUL },
284: { 0x0000FFCF, UScript.HANGUL },
285: { 0x0000FFD7, UScript.HANGUL },
286: { 0x0000FFDC, UScript.HANGUL },
287: { 0x00010300, UScript.OLD_ITALIC },
288: { 0x00010330, UScript.GOTHIC },
289: { 0x0001034A, UScript.GOTHIC },
290: { 0x00010400, UScript.DESERET },
291: { 0x00010428, UScript.DESERET },
292: { 0x0001D167, UScript.INHERITED },
293: { 0x0001D17B, UScript.INHERITED },
294: { 0x0001D185, UScript.INHERITED },
295: { 0x0001D1AA, UScript.INHERITED },
296: { 0x00020000, UScript.HAN },
297: { 0x00000D02, UScript.MALAYALAM },
298: { 0x00000D00, UScript.UNKNOWN },
299: { 0x00000000, UScript.COMMON },
300: { 0x0001D169, UScript.INHERITED },
301: { 0x0001D182, UScript.INHERITED },
302: { 0x0001D18B, UScript.INHERITED },
303: { 0x0001D1AD, UScript.INHERITED }, };
304:
305: int i = 0;
306: int code = UScript.INVALID_CODE;
307: boolean passed = true;
308:
309: while (i < codepoints.length) {
310: code = UScript.getScript(codepoints[i][0]);
311:
312: if (code != codepoints[i][1]) {
313: logln("UScript.getScript for codepoint 0x"
314: + hex(codepoints[i][0]) + " failed");
315: passed = false;
316: }
317:
318: i++;
319: }
320: if (!passed) {
321: errln("UScript.getScript failed.");
322: }
323: }
324:
325: public void TestScriptNames() {
326: for (int i = 0; i < UScript.CODE_LIMIT; i++) {
327: String name = UScript.getName(i);
328: if (name.equals("")) {
329: errln("FAILED: getName for code : " + i);
330: }
331: String shortName = UScript.getShortName(i);
332: if (shortName.equals("")) {
333: errln("FAILED: getName for code : " + i);
334: }
335: }
336: }
337:
338: public void TestAllCodepoints() {
339: int code;
340: //String oldId="";
341: //String oldAbbrId="";
342: for (int i = 0; i <= 0x10ffff; i++) {
343: code = UScript.INVALID_CODE;
344: code = UScript.getScript(i);
345: if (code == UScript.INVALID_CODE) {
346: errln("UScript.getScript for codepoint 0x" + hex(i)
347: + " failed");
348: }
349: String id = UScript.getName(code);
350: if (id.indexOf("INVALID") >= 0) {
351: errln("UScript.getScript for codepoint 0x" + hex(i)
352: + " failed");
353: }
354: String abbr = UScript.getShortName(code);
355: if (abbr.indexOf("INV") >= 0) {
356: errln("UScript.getScript for codepoint 0x" + hex(i)
357: + " failed");
358: }
359: }
360: }
361:
362: public void TestNewCode() {
363: /*
364: * These script codes were originally added to ICU pre-3.6, so that ICU would
365: * have all ISO 15924 script codes. ICU was then based on Unicode 4.1.
366: * These script codes were added with only short names because we don't
367: * want to invent long names ourselves.
368: * Unicode 5 and later encode some of these scripts and give them long names.
369: * Whenever this happens, the long script names here need to be updated.
370: */
371: String[] expectedLong = new String[] { "Balinese", "Batk",
372: "Blis", "Brah", "Cham", "Cirt", "Cyrs", "Egyd", "Egyh",
373: "Egyp", "Geok", "Hans", "Hant", "Hmng", "Hung", "Inds",
374: "Java", "Kali", "Latf", "Latg", "Lepc", "Lina", "Mand",
375: "Maya", "Mero", "Nko", "Orkh", "Perm", "Phags_Pa",
376: "Phoenician", "Plrd", "Roro", "Sara", "Syre", "Syrj",
377: "Syrn", "Teng", "Vaii", "Visp", "Cuneiform", "Zxxx",
378: "Unknown", };
379: String[] expectedShort = new String[] { "Bali", "Batk", "Blis",
380: "Brah", "Cham", "Cirt", "Cyrs", "Egyd", "Egyh", "Egyp",
381: "Geok", "Hans", "Hant", "Hmng", "Hung", "Inds", "Java",
382: "Kali", "Latf", "Latg", "Lepc", "Lina", "Mand", "Maya",
383: "Mero", "Nkoo", "Orkh", "Perm", "Phag", "Phnx", "Plrd",
384: "Roro", "Sara", "Syre", "Syrj", "Syrn", "Teng", "Vaii",
385: "Visp", "Xsux", "Zxxx", "Zzzz", };
386: int j = 0;
387: int i = 0;
388: for (i = UScript.BALINESE; i < UScript.CODE_LIMIT; i++, j++) {
389: String name = UScript.getName(i);
390: if (name == null || !name.equals(expectedLong[j])) {
391: errln("UScript.getName failed for code" + i + name
392: + "!=" + expectedLong[j]);
393: }
394: name = UScript.getShortName(i);
395: if (name == null || !name.equals(expectedShort[j])) {
396: errln("UScript.getShortName failed for code" + i + name
397: + "!=" + expectedShort[j]);
398: }
399: }
400: for (i = 0; i < expectedLong.length; i++) {
401: int[] ret = UScript.getCode(expectedShort[i]);
402: if (ret.length > 1) {
403: errln("UScript.getCode did not return expected number of codes for script"
404: + expectedShort[i]
405: + ". EXPECTED: 1 GOT: "
406: + ret.length);
407: }
408: if (ret[0] != (UScript.BALINESE + i)) {
409: errln("UScript.getCode did not return expected code for script"
410: + expectedShort[i]
411: + ". EXPECTED: "
412: + (UScript.BALINESE + i)
413: + " GOT: %i\n"
414: + ret[0]);
415: }
416: }
417: }
418: }
|